Skip to content

Commit 8ff8d2b

Browse files
author
ledouxm
committed
feat: optimize stats API queries for improved performance and readability
1 parent e649812 commit 8ff8d2b

File tree

1 file changed

+38
-52
lines changed

1 file changed

+38
-52
lines changed

packages/backend/src/routes/statsRoutes.ts

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -31,55 +31,45 @@ export const statsPlugin: FastifyPluginAsyncTypebox = async (fastify) => {
3131
const periodFrom = request.query.from ?? defaultFrom.toISOString().slice(0, 10);
3232
const periodTo = request.query.to ?? now.toISOString().slice(0, 10);
3333

34-
const [
35-
totalConstatsResult,
36-
totalReportsResult,
37-
totalUsersResult,
38-
usersWithNoDocumentsResult,
39-
activeUsersResult,
40-
] = await Promise.all([
41-
db
42-
.selectFrom("state_report")
43-
.where("disabled", "is not", true)
44-
.select(db.fn.countAll<number>().as("count"))
45-
.executeTakeFirst(),
34+
const [totalConstatsResult, totalReportsResult, totalUsersResult, usersWithNoDocumentsResult, activeUsersResult] =
35+
await Promise.all([
36+
db
37+
.selectFrom("state_report")
38+
.where("disabled", "is not", true)
39+
.select(db.fn.countAll<number>().as("count"))
40+
.executeTakeFirst(),
4641

47-
db
48-
.selectFrom("report")
49-
.where("disabled", "is not", true)
50-
.select(db.fn.countAll<number>().as("count"))
51-
.executeTakeFirst(),
42+
db
43+
.selectFrom("report")
44+
.where("disabled", "is not", true)
45+
.select(db.fn.countAll<number>().as("count"))
46+
.executeTakeFirst(),
5247

53-
db
54-
.selectFrom("user")
55-
.select(db.fn.countAll<number>().as("count"))
56-
.executeTakeFirst(),
48+
db.selectFrom("user").select(db.fn.countAll<number>().as("count")).executeTakeFirst(),
5749

58-
db
59-
.selectFrom("user")
60-
.where((eb) =>
61-
eb.and([
62-
eb.not(
63-
eb.exists(
64-
eb.selectFrom("report").whereRef("report.createdBy", "=", "user.id").select("report.id"),
50+
db
51+
.selectFrom("user")
52+
.where((eb) =>
53+
eb.and([
54+
eb.not(
55+
eb.exists(eb.selectFrom("report").whereRef("report.createdBy", "=", "user.id").select("report.id")),
6556
),
66-
),
67-
eb.not(
68-
eb.exists(
69-
eb
70-
.selectFrom("state_report")
71-
.whereRef("state_report.created_by", "=", "user.id")
72-
.select("state_report.id"),
57+
eb.not(
58+
eb.exists(
59+
eb
60+
.selectFrom("state_report")
61+
.whereRef("state_report.created_by", "=", "user.id")
62+
.select("state_report.id"),
63+
),
7364
),
74-
),
75-
]),
76-
)
77-
.select(db.fn.countAll<number>().as("count"))
78-
.executeTakeFirst(),
65+
]),
66+
)
67+
.select(db.fn.countAll<number>().as("count"))
68+
.executeTakeFirst(),
7969

80-
db
81-
.selectFrom(
82-
sql<{ user_id: string }>`(
70+
db
71+
.selectFrom(
72+
sql<{ user_id: string }>`(
8373
SELECT r."createdBy" AS user_id
8474
FROM sent_email se
8575
JOIN report r ON r.id = se.report_id
@@ -90,10 +80,10 @@ export const statsPlugin: FastifyPluginAsyncTypebox = async (fastify) => {
9080
JOIN state_report sr ON sr.id = srse.state_report_id
9181
WHERE srse.sent_at::date >= ${periodFrom}::date AND srse.sent_at::date <= ${periodTo}::date
9282
)`.as("active_users"),
93-
)
94-
.select(db.fn.countAll<number>().as("count"))
95-
.executeTakeFirst(),
96-
]);
83+
)
84+
.select(db.fn.countAll<number>().as("count"))
85+
.executeTakeFirst(),
86+
]);
9787

9888
return {
9989
totalConstats: Number(totalConstatsResult?.count ?? 0),
@@ -131,10 +121,7 @@ export const statsPlugin: FastifyPluginAsyncTypebox = async (fastify) => {
131121
db
132122
.selectFrom("service")
133123
.leftJoin("state_report", (join) =>
134-
join
135-
.onRef("state_report.service_id", "=", "service.id")
136-
.on("state_report.alerts_sent", "=", true)
137-
.on("state_report.disabled", "is not", true),
124+
join.onRef("state_report.service_id", "=", "service.id").on("state_report.disabled", "is not", true),
138125
)
139126
.groupBy(["service.id", "service.name"])
140127
.orderBy(sql`COUNT(DISTINCT state_report.id)`, "desc")
@@ -147,7 +134,6 @@ export const statsPlugin: FastifyPluginAsyncTypebox = async (fastify) => {
147134

148135
db
149136
.selectFrom("state_report")
150-
.where("alerts_sent", "=", false)
151137
.where("disabled", "is not", true)
152138
.where("created_at", "<", sql<string>`NOW() - INTERVAL '21 days'`)
153139
.select(db.fn.countAll<number>().as("count"))

0 commit comments

Comments
 (0)