Combine queries into batches
All checks were successful
Test, Build, Deploy / Test, Build, and Deploy (push) Successful in 45s
Test, Build, Deploy / Create Sentry Release (push) Successful in 6s

This commit is contained in:
2026-03-14 05:18:50 -04:00
parent 994a7a7a58
commit 47df3dc55f
2 changed files with 34 additions and 45 deletions

View File

@@ -35,29 +35,24 @@ export async function loader({ context }: { context: RequestContext }) {
month = 12;
}
const eventMemberQuery = await context.env.D1.prepare(
"SELECT id, name, roblox_id FROM et_members;",
).all();
const inactivityQuery: D1Result<Record<string, any>> =
await context.env.D1.prepare(
"SELECT decisions, json_extract(user, '$.id') AS uid FROM inactivity_notices WHERE (end BETWEEN DATE('now', 'start of month', '-1 month') AND DATE('now', 'start of month', '-1 day')) OR (start BETWEEN DATE('now', 'start of month', '-1 month') AND DATE('now', 'start of month', '-1 day'));",
).all();
const batchStatements: D1Result<Record<string, any>>[] =
await context.env.D1.batch([
context.env.D1.prepare("SELECT id, name, roblox_id FROM et_members;"),
context.env.D1.prepare(
"SELECT decisions, json_extract(user, '$.id') AS uid FROM inactivity_notices WHERE (end BETWEEN DATE('now', 'start of month', '-1 month') AND DATE('now', 'start of month', '-1 day')) OR (start BETWEEN DATE('now', 'start of month', '-1 month') AND DATE('now', 'start of month', '-1 day'));",
),
context.env.D1.prepare(
"SELECT approved, answered_at, created_by, performed_at, reached_minimum_player_count, type FROM events WHERE month = ? AND year = ?;",
).bind(month, year),
]);
const eventsQuery = await context.env.D1.prepare(
"SELECT approved, answered_at, created_by, performed_at, reached_minimum_player_count, type FROM events WHERE month = ? AND year = ?;",
)
.bind(month, year)
.all();
const memberMap = Object.fromEntries(
eventMemberQuery.results.map((entry) => {
return [
entry.id,
{ name: entry.name, points: 0, roblox_id: entry.roblox_id },
];
batchStatements[0].results.map((e) => {
return [e.id, { name: e.name, points: 0, roblox_id: e.roblox_id }];
}),
);
for (const event of eventsQuery.results as {
for (const event of batchStatements[2].results as {
approved: number;
answered_at: number;
created_by: string;
@@ -77,10 +72,10 @@ export async function loader({ context }: { context: RequestContext }) {
for (const member of Object.keys(memberMap))
if (
(memberMap[member].points < 50 ||
eventsQuery.results.filter(
batchStatements[2].results.filter(
(e) => e.type === "gamenight" && e.created_by === member,
).length === 0) &&
!inactivityQuery.results.find(
!batchStatements[1].results.find(
(i) => i.uid === member && JSON.parse(i.decisions).et,
)
)