Backfill usage statistics

This commit is contained in:
David Corbitt
2023-08-08 15:26:14 -07:00
parent 2861c64428
commit 65a0f9065f
2 changed files with 24 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ import { sql } from "kysely";
import { z } from "zod";
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
import { kysely, prisma } from "~/server/db";
import dayjs from "~/utils/dayjs";
export const dashboardRouter = createTRPCRouter({
stats: publicProcedure
@@ -30,6 +31,26 @@ export const dashboardRouter = createTRPCRouter({
.orderBy("period")
.execute();
let originalDataIndex = periods.length - 1;
let dayToMatch = dayjs(input.startDate).startOf("day");
const backfilledPeriods: typeof periods = [];
// Backfill from now to 14 days ago or the date of the first logged call, whichever is earlier
while (backfilledPeriods.length < 14 || originalDataIndex >= 0) {
const nextOriginalPeriod = periods[originalDataIndex];
if (nextOriginalPeriod && dayjs(nextOriginalPeriod?.period).isSame(dayToMatch, "day")) {
backfilledPeriods.unshift(nextOriginalPeriod);
originalDataIndex--;
} else {
backfilledPeriods.unshift({
period: dayjs(dayToMatch).toDate(),
numQueries: 0,
totalCost: 0,
});
}
dayToMatch = dayToMatch.subtract(1, "day");
}
const totals = await kysely
.selectFrom("LoggedCall")
.leftJoin(
@@ -68,7 +89,7 @@ export const dashboardRouter = createTRPCRouter({
}
});
return { periods, totals, errors: namedErrors };
return { periods: backfilledPeriods, totals, errors: namedErrors };
}),
// TODO useInfiniteQuery