Backfill usage statistics
This commit is contained in:
@@ -105,14 +105,14 @@ export default function LoggedCalls() {
|
|||||||
stroke="#8884d8"
|
stroke="#8884d8"
|
||||||
yAxisId="left"
|
yAxisId="left"
|
||||||
dot={false}
|
dot={false}
|
||||||
strokeWidth={3}
|
strokeWidth={2}
|
||||||
/>
|
/>
|
||||||
<Line
|
<Line
|
||||||
dataKey="Total Spent (USD)"
|
dataKey="Total Spent (USD)"
|
||||||
stroke="#82ca9d"
|
stroke="#82ca9d"
|
||||||
yAxisId="right"
|
yAxisId="right"
|
||||||
dot={false}
|
dot={false}
|
||||||
strokeWidth={3}
|
strokeWidth={2}
|
||||||
/>
|
/>
|
||||||
</LineChart>
|
</LineChart>
|
||||||
</ResponsiveContainer>
|
</ResponsiveContainer>
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { sql } from "kysely";
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
|
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
|
||||||
import { kysely, prisma } from "~/server/db";
|
import { kysely, prisma } from "~/server/db";
|
||||||
|
import dayjs from "~/utils/dayjs";
|
||||||
|
|
||||||
export const dashboardRouter = createTRPCRouter({
|
export const dashboardRouter = createTRPCRouter({
|
||||||
stats: publicProcedure
|
stats: publicProcedure
|
||||||
@@ -30,6 +31,26 @@ export const dashboardRouter = createTRPCRouter({
|
|||||||
.orderBy("period")
|
.orderBy("period")
|
||||||
.execute();
|
.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
|
const totals = await kysely
|
||||||
.selectFrom("LoggedCall")
|
.selectFrom("LoggedCall")
|
||||||
.leftJoin(
|
.leftJoin(
|
||||||
@@ -68,7 +89,7 @@ export const dashboardRouter = createTRPCRouter({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return { periods, totals, errors: namedErrors };
|
return { periods: backfilledPeriods, totals, errors: namedErrors };
|
||||||
}),
|
}),
|
||||||
|
|
||||||
// TODO useInfiniteQuery
|
// TODO useInfiniteQuery
|
||||||
|
|||||||
Reference in New Issue
Block a user