2024-11-21 08:20:29 +00:00
|
|
|
import type { Report } from '../../packages/libraries/core/src/client/usage.js';
|
2022-12-28 09:37:23 +00:00
|
|
|
import { getServiceHost } from './utils';
|
2022-05-18 07:26:57 +00:00
|
|
|
|
|
|
|
|
export interface CollectedOperation {
|
|
|
|
|
timestamp?: number;
|
|
|
|
|
operation: string;
|
|
|
|
|
operationName?: string;
|
|
|
|
|
fields: string[];
|
|
|
|
|
execution: {
|
|
|
|
|
ok: boolean;
|
|
|
|
|
duration: number;
|
|
|
|
|
errorsTotal: number;
|
|
|
|
|
};
|
|
|
|
|
metadata?: {
|
|
|
|
|
client?: {
|
|
|
|
|
name?: string;
|
|
|
|
|
version?: string;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 08:20:29 +00:00
|
|
|
export async function collect(params: { report: Report; accessToken: string }) {
|
|
|
|
|
const usageAddress = await getServiceHost('usage', 8081);
|
|
|
|
|
const res = await fetch(`http://${usageAddress}`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(params.report),
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
Authorization: `Bearer ${params.accessToken}`,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
status: res.status,
|
|
|
|
|
body:
|
|
|
|
|
res.status === 200
|
|
|
|
|
? ((await res.json()) as {
|
|
|
|
|
operations: {
|
|
|
|
|
accepted: number;
|
|
|
|
|
rejected: number;
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
: await res.text(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-25 12:08:58 +00:00
|
|
|
export async function legacyCollect(params: {
|
2022-07-01 09:43:27 +00:00
|
|
|
operations: CollectedOperation[];
|
|
|
|
|
token: string;
|
|
|
|
|
authorizationHeader?: 'x-api-token' | 'authorization';
|
|
|
|
|
}) {
|
2022-12-28 09:37:23 +00:00
|
|
|
const usageAddress = await getServiceHost('usage', 8081);
|
2022-10-06 11:48:01 +00:00
|
|
|
const res = await fetch(`http://${usageAddress}`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(params.operations),
|
2022-05-18 07:26:57 +00:00
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
2022-10-17 12:49:30 +00:00
|
|
|
Accept: 'application/json',
|
2022-07-01 09:43:27 +00:00
|
|
|
...(params.authorizationHeader === 'x-api-token'
|
|
|
|
|
? {
|
|
|
|
|
'X-API-Token': params.token,
|
|
|
|
|
}
|
|
|
|
|
: {
|
|
|
|
|
Authorization: `Bearer ${params.token}`,
|
|
|
|
|
}),
|
2022-05-18 07:26:57 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
status: res.status,
|
2022-10-17 12:49:30 +00:00
|
|
|
body:
|
|
|
|
|
res.status === 200
|
|
|
|
|
? ((await res.json()) as {
|
|
|
|
|
operations: {
|
|
|
|
|
accepted: number;
|
|
|
|
|
rejected: number;
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
: await res.text(),
|
2022-05-18 07:26:57 +00:00
|
|
|
};
|
|
|
|
|
}
|