MCPcopy Create free account
hub / github.com/Writesonic/GPTRouter / getUsageDataByModel

Function getUsageDataByModel

src/controllers/modelUsage.controller.ts:15–67  ·  view source on GitHub ↗
(
  request: FastifyRequestTypebox<typeof GetAllUsageByModelSchema>,
  reply: FastifyReplyTypebox<typeof GetAllUsageByModelSchema>,
)

Source from the content-addressed store, hash-verified

13 * @returns {Promise<void>} - returns a promise with the usage data as response
14 */
15export const getUsageDataByModel = async (
16 request: FastifyRequestTypebox<typeof GetAllUsageByModelSchema>,
17 reply: FastifyReplyTypebox<typeof GetAllUsageByModelSchema>,
18) => {
19 try {
20 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
21 // @ts-ignore
22 const { startDate, endDate } = request.query;
23 const endDatePlus1 = new Date(endDate);
24 const newStartDate = new Date(startDate);
25 endDatePlus1.setDate(endDatePlus1.getDate() + 1);
26
27 const data = await request.server.orm.getRepository(ModelUsage).find({
28 where: {
29 createdAt: Between(newStartDate, endDatePlus1),
30 },
31 order: {
32 createdAt: "ASC",
33 },
34 });
35
36 if (!data) {
37 return reply.code(404).send({
38 message: ERROR_MESSAGES.MODEL_USAGE_DATA_NOT_FOUND,
39 });
40 }
41
42 const res: any = {};
43 for (const usage of data) {
44 const { modelId, promptTokens, completionTokens, totalTokens, createdAt, cost } = usage;
45 if (!res[modelId]) {
46 res[modelId] = [];
47 }
48 res[modelId].push({
49 promptTokens: promptTokens,
50 completionTokens: completionTokens,
51 totalTokens: totalTokens,
52 createdAt: createdAt?.toISOString(),
53 cost: cost,
54 });
55 }
56
57 const output = Object.keys(res).map(modelId => ({
58 modelId,
59 data: res[modelId],
60 }));
61
62 reply.code(200).send(output);
63 } catch (e: any) {
64 Sentry.captureException(e);
65 reply.code(500).send(e);
66 }
67};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected