Fix dashboard stats

This commit is contained in:
David Corbitt
2023-08-08 11:49:08 -07:00
parent ea91d692d3
commit 6188f55569

View File

@@ -12,7 +12,6 @@ export const dashboardRouter = createTRPCRouter({
}), }),
) )
.query(async ({ input }) => { .query(async ({ input }) => {
console.log("made it 1");
// Return the stats group by hour // Return the stats group by hour
const periods = await kysely const periods = await kysely
.selectFrom("LoggedCall") .selectFrom("LoggedCall")
@@ -25,30 +24,26 @@ export const dashboardRouter = createTRPCRouter({
.select(({ fn }) => [ .select(({ fn }) => [
sql<Date>`date_trunc('day', "LoggedCallModelResponse"."startTime")`.as("period"), sql<Date>`date_trunc('day', "LoggedCallModelResponse"."startTime")`.as("period"),
sql<number>`count("LoggedCall"."id")::int`.as("numQueries"), sql<number>`count("LoggedCall"."id")::int`.as("numQueries"),
fn.sum(fn.coalesce('LoggedCallModelResponse.totalCost', sql<number>`0`)).as("totalCost"), fn.sum(fn.coalesce("LoggedCallModelResponse.totalCost", sql<number>`0`)).as("totalCost"),
]) ])
.groupBy("period") .groupBy("period")
.orderBy("period") .orderBy("period")
.execute(); .execute();
console.log("made it 2");
const totals = await kysely const totals = await kysely
.selectFrom("LoggedCall") .selectFrom("LoggedCall")
.where("organizationId", "=", input.organizationId)
.leftJoin( .leftJoin(
"LoggedCallModelResponse", "LoggedCallModelResponse",
"LoggedCall.id", "LoggedCall.id",
"LoggedCallModelResponse.originalLoggedCallId", "LoggedCallModelResponse.originalLoggedCallId",
) )
.where("organizationId", "=", input.organizationId)
.select(({ fn }) => [ .select(({ fn }) => [
fn.sum(fn.coalesce('LoggedCallModelResponse.totalCost', sql<number>`0`)).as("totalCost"), fn.sum(fn.coalesce("LoggedCallModelResponse.totalCost", sql<number>`0`)).as("totalCost"),
fn.count("id").as("numQueries"), fn.count("LoggedCall.id").as("numQueries"),
]) ])
.executeTakeFirst(); .executeTakeFirst();
console.log("made it 3");
const errors = await kysely const errors = await kysely
.selectFrom("LoggedCall") .selectFrom("LoggedCall")
.where("organizationId", "=", input.organizationId) .where("organizationId", "=", input.organizationId)
@@ -57,14 +52,12 @@ export const dashboardRouter = createTRPCRouter({
"LoggedCall.id", "LoggedCall.id",
"LoggedCallModelResponse.originalLoggedCallId", "LoggedCallModelResponse.originalLoggedCallId",
) )
.select(({ fn }) => [fn.count("id").as("count"), "respStatus as code"]) .select(({ fn }) => [fn.count("LoggedCall.id").as("count"), "respStatus as code"])
.where("respStatus", ">", 200) .where("respStatus", ">", 200)
.groupBy("code") .groupBy("code")
.orderBy("count", "desc") .orderBy("count", "desc")
.execute(); .execute();
console.log("made it 4");
const namedErrors = errors.map((e) => { const namedErrors = errors.map((e) => {
if (e.code === 429) { if (e.code === 429) {
return { ...e, name: "Rate limited" }; return { ...e, name: "Rate limited" };
@@ -75,10 +68,7 @@ export const dashboardRouter = createTRPCRouter({
} }
}); });
console.log("data is", { periods, totals, errors: namedErrors });
return { periods, totals, errors: namedErrors }; return { periods, totals, errors: namedErrors };
// const resp = await kysely.selectFrom("LoggedCall").selectAll().execute();
}), }),
// TODO useInfiniteQuery // TODO useInfiniteQuery