From 37e3b5de1f3459aeb0ccc358102ea94c5fa9f198 Mon Sep 17 00:00:00 2001 From: Shinsina Date: Thu, 8 Aug 2024 14:32:51 -0500 Subject: [PATCH] Batch SQL queries where possible --- src/pages/shared-subsessions.astro | 26 ++++---- src/pages/subsession/[id].astro | 43 ++++++------- .../[id]/scheduling/by-week/[timestamp].astro | 27 +++++---- src/pages/user/[id]/scheduling/index.astro | 27 +++++---- .../[carClassId]/season-summary.astro | 60 +++++++++++-------- 5 files changed, 103 insertions(+), 80 deletions(-) diff --git a/src/pages/shared-subsessions.astro b/src/pages/shared-subsessions.astro index df09daa..9203e30 100644 --- a/src/pages/shared-subsessions.astro +++ b/src/pages/shared-subsessions.astro @@ -26,17 +26,19 @@ const sharedRaceSessions = await db ) .having(({ count }) => gt(count, 1)); const sharedSubsessionIds = sharedRaceSessions.map((v) => v.subsession_id); -const sharedSubsessions = await db - .select() - .from(Subsession) - .where(sql`${Subsession.subsession_id} IN ${sharedSubsessionIds}`) - .orderBy(desc(Subsession.subsession_id)); -const sharedSubsessionRaceResults = await db - .select() - .from(SubsessionRaceResults) - .where( - sql`${SubsessionRaceResults.cust_id} IN ${userIds} AND ${SubsessionRaceResults.simsession_number} = 0 AND ${SubsessionRaceResults.subsession_id} IN ${sharedSubsessionIds}` - ); +const [sharedSubsessions, sharedSubsessionRaceResults] = await db.batch([ + db + .select() + .from(Subsession) + .where(sql`${Subsession.subsession_id} IN ${sharedSubsessionIds}`) + .orderBy(desc(Subsession.subsession_id)), + db + .select() + .from(SubsessionRaceResults) + .where( + sql`${SubsessionRaceResults.cust_id} IN ${userIds} AND ${SubsessionRaceResults.simsession_number} = 0 AND ${SubsessionRaceResults.subsession_id} IN ${sharedSubsessionIds}` + ), +]); const subsessionIdToSharedRaceResultsMap = sharedSubsessionRaceResults.reduce( (map: Map>, value) => { const { subsession_id } = value; @@ -93,7 +95,7 @@ const results = sharedSubsessions.map((subsession) => { headToHeadWins[display_name] = [subsession_id]; } const { group_name: License } = Array.isArray(allowed_licenses) - ? allowed_licenses.slice(0, 2).pop() as LicenseType + ? (allowed_licenses.slice(0, 2).pop() as LicenseType) : { group_name: "" }; const assertTrackAsTrackType = () => track as Track; const trackInfo = assertTrackAsTrackType(); diff --git a/src/pages/subsession/[id].astro b/src/pages/subsession/[id].astro index 34e24c5..4f6bd85 100644 --- a/src/pages/subsession/[id].astro +++ b/src/pages/subsession/[id].astro @@ -22,25 +22,28 @@ export async function getStaticPaths() { return subsessions.map(({ subsession_id: id }) => ({ params: { id } })); } const { id } = Astro.params; -const [subsession] = await db - .select() - .from(Subsession) - .where(eq(Subsession.subsession_id, Number(id))); -const practiceResults = await db - .select() - .from(SubsessionPracticeResults) - .where(eq(SubsessionPracticeResults.subsession_id, Number(id))) - .orderBy(asc(SubsessionPracticeResults.finish_position)); -const qualifyingResults = await db - .select() - .from(SubsessionQualifyingResults) - .where(eq(SubsessionQualifyingResults.subsession_id, Number(id))) - .orderBy(asc(SubsessionQualifyingResults.finish_position)); -const raceResults = await db - .select() - .from(SubsessionRaceResults) - .where(eq(SubsessionRaceResults.subsession_id, Number(id))) - .orderBy(asc(SubsessionRaceResults.finish_position)); +const [subsessions, practiceResults, qualifyingResults, raceResults] = await db.batch([ + db + .select() + .from(Subsession) + .where(eq(Subsession.subsession_id, Number(id))), + db + .select() + .from(SubsessionPracticeResults) + .where(eq(SubsessionPracticeResults.subsession_id, Number(id))) + .orderBy(asc(SubsessionPracticeResults.finish_position)), + db + .select() + .from(SubsessionQualifyingResults) + .where(eq(SubsessionQualifyingResults.subsession_id, Number(id))) + .orderBy(asc(SubsessionQualifyingResults.finish_position)), + db + .select() + .from(SubsessionRaceResults) + .where(eq(SubsessionRaceResults.subsession_id, Number(id))) + .orderBy(asc(SubsessionRaceResults.finish_position)), +]); +const subsession = subsessions.pop(); const [practiceResultOne] = practiceResults || [{}]; const [qualifyingResultOne] = qualifyingResults || [{}]; const splitRaceResults = raceResults.find((result) => @@ -108,7 +111,7 @@ const { const title = `Subsession - ${id}`; const description = `Subsession information for subsession - ${id}`; const notMultiClass = - Array.isArray(subsession.car_classes) && subsession.car_classes.length === 1; + Array.isArray(subsession?.car_classes) && subsession?.car_classes.length === 1; --- diff --git a/src/pages/user/[id]/scheduling/by-week/[timestamp].astro b/src/pages/user/[id]/scheduling/by-week/[timestamp].astro index ada02b6..ad53631 100644 --- a/src/pages/user/[id]/scheduling/by-week/[timestamp].astro +++ b/src/pages/user/[id]/scheduling/by-week/[timestamp].astro @@ -19,13 +19,25 @@ export async function getStaticPaths() { .flat(); } const { id, timestamp } = Astro.params; -const [user] = await db +const [users, cars, carClassesResults, carClasses, seasons] = await db.batch([ +db .select({ carPackages: User.car_packages, trackPackages: User.track_packages, }) .from(User) - .where(eq(User.cust_id, id)); + .where(eq(User.cust_id, id)), + db.select().from(Car), + db + .select({ + carClassId: CarClass.car_class_id, + carsInClass: CarClass.cars_in_class, + }) + .from(CarClass), + db.select().from(CarClass), + db.select().from(Season) +]); +const user = users.pop() || { carPackages: [], trackPackages: [] }; const { carPackages, trackPackages } = user; const carIds = Array.isArray(carPackages) ? carPackages.map((carPackage) => carPackage.content_ids).flat() @@ -33,26 +45,18 @@ const carIds = Array.isArray(carPackages) const trackIds = Array.isArray(trackPackages) ? trackPackages.map((trackPackage) => trackPackage.content_ids).flat() : []; -const cars = await db.select().from(Car); const carsMap = new Map(); cars.forEach((car) => { const { car_id } = car; carsMap.set(car_id, car); }); -// @todo Improve this query to not have to do this -const carClassesResults = await db - .select({ - carClassId: CarClass.car_class_id, - carsInClass: CarClass.cars_in_class, - }) - .from(CarClass); +// @todo Improve CarClass query to not have to do this const carClassIdsForCarIds = carClassesResults .filter((result: any) => result.carsInClass.some((v: any) => carIds.includes(v.car_id)) ) .map((result) => result.carClassId); // End comment of previous todo -const carClasses = await db.select().from(CarClass); const carClassesMap = new Map(); carClasses.forEach((carClass) => { const { car_class_id, cars_in_class } = carClass; @@ -70,7 +74,6 @@ const licenseGroupToNameMap: Record = { "4": "Class B", "5": "Class A", }; -const seasons = await db.select().from(Season); const schedulesForWeek = seasons.reduce( (array: Array>, season) => { const { schedules, ...rest } = season; diff --git a/src/pages/user/[id]/scheduling/index.astro b/src/pages/user/[id]/scheduling/index.astro index dee638c..af89ce7 100644 --- a/src/pages/user/[id]/scheduling/index.astro +++ b/src/pages/user/[id]/scheduling/index.astro @@ -7,13 +7,25 @@ export async function getStaticPaths() { return userIds.map(({ id }) => ({ params: { id } })); } const { id } = Astro.params; -const [user] = await db +const [users, cars, carClassesResults, carClasses, seasons] = await db.batch([ +db .select({ carPackages: User.car_packages, trackPackages: User.track_packages, }) .from(User) - .where(eq(User.cust_id, id)); + .where(eq(User.cust_id, id)), + db.select().from(Car), + db + .select({ + carClassId: CarClass.car_class_id, + carsInClass: CarClass.cars_in_class, + }) + .from(CarClass), + db.select().from(CarClass), + db.select().from(Season) +]); +const user = users.pop() || { carPackages: [], trackPackages: [] }; const { carPackages, trackPackages } = user; const carIds = Array.isArray(carPackages) ? carPackages.map((carPackage) => carPackage.content_ids).flat() @@ -21,26 +33,18 @@ const carIds = Array.isArray(carPackages) const trackIds = Array.isArray(trackPackages) ? trackPackages.map((trackPackage) => trackPackage.content_ids).flat() : []; -const cars = await db.select().from(Car); const carsMap = new Map(); cars.forEach((car) => { const { car_id } = car; carsMap.set(car_id, car); }); -// @todo Improve this query to not have to do this -const carClassesResults = await db - .select({ - carClassId: CarClass.car_class_id, - carsInClass: CarClass.cars_in_class, - }) - .from(CarClass); +// @todo Improve CarClass query to not have to do this const carClassIdsForCarIds = carClassesResults .filter((result: any) => result.carsInClass.some((v: any) => carIds.includes(v.car_id)) ) .map((result) => result.carClassId); // End comment of previous todo -const carClasses = await db.select().from(CarClass); const carClassesMap = new Map(); carClasses.forEach((carClass) => { const { car_class_id, cars_in_class } = carClass; @@ -49,7 +53,6 @@ carClasses.forEach((carClass) => { : []; carClassesMap.set(car_class_id, { ...carClass, cars_in_class: carsInClass }); }); -const seasons = await db.select().from(Season); const carClassIdSet = new Set(carClassIdsForCarIds); const trackIdSet = new Set(trackIds); const licenseGroupToNameMap: Record = { diff --git a/src/pages/user/[id]/season/[seasonId]/car-class/[carClassId]/season-summary.astro b/src/pages/user/[id]/season/[seasonId]/car-class/[carClassId]/season-summary.astro index b4d0b33..86f8fc4 100644 --- a/src/pages/user/[id]/season/[seasonId]/car-class/[carClassId]/season-summary.astro +++ b/src/pages/user/[id]/season/[seasonId]/car-class/[carClassId]/season-summary.astro @@ -35,31 +35,43 @@ export async function getStaticPaths() { })); } const { id, seasonId, carClassId } = Astro.params; -const allSeasonSubsessions = await db - .select() - .from(Subsession) - .innerJoin( - SubsessionRaceResults, - eq(Subsession.subsession_id, SubsessionRaceResults.subsession_id) - ) - .where( - and( - eq(Subsession.season_id, Number(seasonId)), - eq(SubsessionRaceResults.cust_id, Number(id)), - eq(SubsessionRaceResults.car_class_id, Number(carClassId)), - sql`${SubsessionRaceResults.simsession_name} IN ${["RACE", "FEATURE", "N/A"]}` +const [subsessions, seasons, pastSeasonInformations] = await db.batch([ + db + .select() + .from(Subsession) + .innerJoin( + SubsessionRaceResults, + eq(Subsession.subsession_id, SubsessionRaceResults.subsession_id) ) - ); -const subsessions = allSeasonSubsessions; -const [season] = await db - .select() - .from(Standing) - .where(eq(Standing.id, `${seasonId}_${carClassId}_${id}`)); -const [pastSeasonInformation] = await db - .select() - .from(PastSeason) - .where(eq(PastSeason.season_id, Number(seasonId))); -const trackSchedule = Array.isArray(pastSeasonInformation.race_weeks) + .where( + and( + eq(Subsession.season_id, Number(seasonId)), + eq(SubsessionRaceResults.cust_id, Number(id)), + eq(SubsessionRaceResults.car_class_id, Number(carClassId)), + sql`${SubsessionRaceResults.simsession_name} IN ${["RACE", "FEATURE", "N/A"]}` + ) + ), + db + .select() + .from(Standing) + .where(eq(Standing.id, `${seasonId}_${carClassId}_${id}`)), + db + .select() + .from(PastSeason) + .where(eq(PastSeason.season_id, Number(seasonId))), +]); +const season = seasons.pop() || { + division: 0, + division_rank: 0, + overall_rank: 0, + season_name: "", + display_name: "", + points: 0, +}; +const pastSeasonInformation = pastSeasonInformations.pop() || { + race_weeks: [], +}; +const trackSchedule = Array.isArray(pastSeasonInformation?.race_weeks) ? pastSeasonInformation.race_weeks.map((raceWeek: Record) => ({ ...raceWeek.track, race_week_num: raceWeek.race_week_num,