From 15196cd80c83802f94b64cc12c5c4e53d67b4f83 Mon Sep 17 00:00:00 2001 From: Shinsina Date: Fri, 19 Apr 2024 13:18:14 -0500 Subject: [PATCH 1/8] Remove @astrojs/prefetch, update scripts with --remote flag --- package-lock.json | 18 ------------------ package.json | 7 +++---- 2 files changed, 3 insertions(+), 22 deletions(-) diff --git a/package-lock.json b/package-lock.json index cb2d12b..84aa48f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,6 @@ "@alpinejs/collapse": "^3.13.8", "@astrojs/alpinejs": "^0.4.0", "@astrojs/db": "^0.10.4", - "@astrojs/prefetch": "^0.4.1", "@astrojs/sitemap": "^3.1.3", "@astrojs/tailwind": "^5.1.0", "@types/alpinejs": "^3.13.10", @@ -130,15 +129,6 @@ "vfile": "^6.0.1" } }, - "node_modules/@astrojs/prefetch": { - "version": "0.4.1", - "resolved": "https://registry.npmjs.org/@astrojs/prefetch/-/prefetch-0.4.1.tgz", - "integrity": "sha512-bpC875BqeIuWVgqhi4X814ftzzbKocaLkiZczaj8k5J2SRpueIGkww3XmD+yY/Ekkm9j30aS3neVO6wSm4IJNA==", - "deprecated": "@astrojs/prefetch is deprecated in favor of the builtin prefetch option. Please see the migration guide for more information: https://docs.astro.build/en/guides/prefetch/#migrating-from-astrojsprefetch", - "dependencies": { - "throttles": "^1.0.1" - } - }, "node_modules/@astrojs/prism": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/@astrojs/prism/-/prism-3.1.0.tgz", @@ -6555,14 +6545,6 @@ "node": ">=0.8" } }, - "node_modules/throttles": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/throttles/-/throttles-1.0.1.tgz", - "integrity": "sha512-fab7Xg+zELr9KOv4fkaBoe/b3L0GMGLd0IBSCn16GoE/Qx6/OfCr1eGNyEcDU2pUA79qQfZ8kPQWlRuok4YwTw==", - "engines": { - "node": ">=6" - } - }, "node_modules/to-fast-properties": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-2.0.0.tgz", diff --git a/package.json b/package.json index c0d9be1..a1f42b0 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,9 @@ "type": "module", "version": "0.0.1", "scripts": { - "dev": "astro dev", - "start": "astro dev", - "build": "astro build", + "dev": "astro dev --remote", + "start": "astro dev --remote", + "build": "astro build --remote", "preview": "astro preview", "astro": "astro" }, @@ -13,7 +13,6 @@ "@alpinejs/collapse": "^3.13.8", "@astrojs/alpinejs": "^0.4.0", "@astrojs/db": "^0.10.4", - "@astrojs/prefetch": "^0.4.1", "@astrojs/sitemap": "^3.1.3", "@astrojs/tailwind": "^5.1.0", "@types/alpinejs": "^3.13.10", From 6bb4c039902d23f22ebfb1136230ea1f313c5c5e Mon Sep 17 00:00:00 2001 From: Shinsina Date: Fri, 19 Apr 2024 13:18:56 -0500 Subject: [PATCH 2/8] Properly configure new prefetch configuration option --- astro.config.mjs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/astro.config.mjs b/astro.config.mjs index af1fd0b..f125b03 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,6 +1,5 @@ import { defineConfig } from "astro/config"; import tailwind from "@astrojs/tailwind"; -import prefetch from "@astrojs/prefetch"; import alpinejs from "@astrojs/alpinejs"; import sitemap from "@astrojs/sitemap"; import db from "@astrojs/db"; @@ -11,9 +10,12 @@ export default defineConfig({ base: "/Stat-N-Track", integrations: [ tailwind(), - prefetch(), alpinejs({ entrypoint: "/src/lib/alpine" }), sitemap(), db(), ], + prefetch: { + defaultStrategy: 'viewport', + prefetchAll: true + } }); From 0bdbac61af3b2d8f25711410cb9349f9bf538e0e Mon Sep 17 00:00:00 2001 From: Shinsina Date: Fri, 19 Apr 2024 13:20:30 -0500 Subject: [PATCH 3/8] Remove rel="prefetch" from all tags --- src/lib/layouts/default.astro | 20 +++------ src/lib/layouts/standings.astro | 1 - src/lib/layouts/subsessions.astro | 1 - src/pages/shared-subsessions.astro | 1 - src/pages/user/[id]/index.astro | 43 ++++++------------- .../user/[id]/scheduling/by-week/index.astro | 2 +- .../car-class/[carClassId]/index.astro | 2 - .../[carClassId]/season-summary.astro | 1 - .../[id]/standings/by-car-class/index.astro | 2 +- .../user/[id]/standings/by-year/index.astro | 2 +- .../[id]/subsessions/by-car-class/index.astro | 2 +- .../[id]/subsessions/by-track/index.astro | 2 +- .../user/[id]/subsessions/by-year/index.astro | 2 +- 13 files changed, 25 insertions(+), 56 deletions(-) diff --git a/src/lib/layouts/default.astro b/src/lib/layouts/default.astro index 42fa9c8..849f568 100644 --- a/src/lib/layouts/default.astro +++ b/src/lib/layouts/default.astro @@ -25,21 +25,15 @@ const seoDescription =
- Home - Jacob CollinsHome + Jacob Collins - Jack GlenzinskiJack Glenzinski - Shared SubsessionsShared Subsessions
diff --git a/src/lib/layouts/standings.astro b/src/lib/layouts/standings.astro index b67cb59..2907496 100644 --- a/src/lib/layouts/standings.astro +++ b/src/lib/layouts/standings.astro @@ -111,7 +111,6 @@ const generateLinkForKey = ({ }) ? ( {result[key]} diff --git a/src/pages/shared-subsessions.astro b/src/pages/shared-subsessions.astro index ab65b00..1376bcf 100644 --- a/src/pages/shared-subsessions.astro +++ b/src/pages/shared-subsessions.astro @@ -194,7 +194,6 @@ const description = "Shared subsessions for all Stat 'n' Track users"; >
diff --git a/src/pages/user/[id]/index.astro b/src/pages/user/[id]/index.astro index 810a526..f8626cc 100644 --- a/src/pages/user/[id]/index.astro +++ b/src/pages/user/[id]/index.astro @@ -12,76 +12,57 @@ const description = `User page for user ID: ${id}`;
- SchedulingScheduling
- Scheduling By WeekScheduling By Week
- All SessionsAll Sessions
- Sessions By YearSessions By Year
- All Season StandingsAll Season Standings
Season Standings By Car Class diff --git a/src/pages/user/[id]/scheduling/by-week/index.astro b/src/pages/user/[id]/scheduling/by-week/index.astro index f5acf2c..a94bf3f 100644 --- a/src/pages/user/[id]/scheduling/by-week/index.astro +++ b/src/pages/user/[id]/scheduling/by-week/index.astro @@ -16,6 +16,6 @@ const description = `Scheduling by week page for user ID: ${id}`; diff --git a/src/pages/user/[id]/season/[seasonId]/car-class/[carClassId]/index.astro b/src/pages/user/[id]/season/[seasonId]/car-class/[carClassId]/index.astro index 4cc15bf..208cfde 100644 --- a/src/pages/user/[id]/season/[seasonId]/car-class/[carClassId]/index.astro +++ b/src/pages/user/[id]/season/[seasonId]/car-class/[carClassId]/index.astro @@ -122,7 +122,6 @@ const description = title;
@@ -156,7 +155,6 @@ const description = title; {key === "0" ? ( {result[key]} 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 69d7a8d..2575913 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 @@ -244,7 +244,6 @@ const overviewItems = [ {key === "0" && result[key] ? ( {result[key]} diff --git a/src/pages/user/[id]/standings/by-car-class/index.astro b/src/pages/user/[id]/standings/by-car-class/index.astro index c83797f..f7e5763 100644 --- a/src/pages/user/[id]/standings/by-car-class/index.astro +++ b/src/pages/user/[id]/standings/by-car-class/index.astro @@ -27,6 +27,6 @@
- {Array.from(carClassNames).sort().map((carClassName) => ())} + {Array.from(carClassNames).sort().map((carClassName) => ())}
diff --git a/src/pages/user/[id]/standings/by-year/index.astro b/src/pages/user/[id]/standings/by-year/index.astro index 10ea167..0d75f93 100644 --- a/src/pages/user/[id]/standings/by-year/index.astro +++ b/src/pages/user/[id]/standings/by-year/index.astro @@ -29,6 +29,6 @@ import {
- {years.map((year) => ())} + {years.map((year) => ())}
diff --git a/src/pages/user/[id]/subsessions/by-car-class/index.astro b/src/pages/user/[id]/subsessions/by-car-class/index.astro index c22d341..5336e6a 100644 --- a/src/pages/user/[id]/subsessions/by-car-class/index.astro +++ b/src/pages/user/[id]/subsessions/by-car-class/index.astro @@ -27,6 +27,6 @@
- {Array.from(carClassNames).sort().map((carClassName) => ())} + {Array.from(carClassNames).sort().map((carClassName) => ())}
diff --git a/src/pages/user/[id]/subsessions/by-track/index.astro b/src/pages/user/[id]/subsessions/by-track/index.astro index 9301df2..fe55b5c 100644 --- a/src/pages/user/[id]/subsessions/by-track/index.astro +++ b/src/pages/user/[id]/subsessions/by-track/index.astro @@ -26,6 +26,6 @@ const description = `Subsessions by track page for user ID: ${id}`;
- {Array.from(trackMap).sort().map(([key, track]) => ())} + {Array.from(trackMap).sort().map(([key, track]) => ())}
diff --git a/src/pages/user/[id]/subsessions/by-year/index.astro b/src/pages/user/[id]/subsessions/by-year/index.astro index a12f919..da76b90 100644 --- a/src/pages/user/[id]/subsessions/by-year/index.astro +++ b/src/pages/user/[id]/subsessions/by-year/index.astro @@ -29,6 +29,6 @@ import {
- {years.map((year) => ())} + {years.map((year) => ())}
From ab568bae5f977fbf7abe374c74213ea04e5e7664 Mon Sep 17 00:00:00 2001 From: Shinsina Date: Fri, 19 Apr 2024 13:20:51 -0500 Subject: [PATCH 4/8] Add loading="eager" to series logos --- src/lib/components/subsession/header.astro | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/components/subsession/header.astro b/src/lib/components/subsession/header.astro index 4eb4920..4399915 100644 --- a/src/lib/components/subsession/header.astro +++ b/src/lib/components/subsession/header.astro @@ -13,6 +13,7 @@ const { series_logo, season_name, race_week_num, max_weeks } = subsession; width={300} height={100} format="avif" + loading="eager" />
From b3bf6bb76097453425ef41fb4a8d6275a11826e1 Mon Sep 17 00:00:00 2001 From: Shinsina Date: Fri, 19 Apr 2024 13:21:30 -0500 Subject: [PATCH 5/8] Support connection of MongoDB client via non Astro environment --- src/lib/mongodb.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lib/mongodb.ts b/src/lib/mongodb.ts index 428f529..55585a4 100644 --- a/src/lib/mongodb.ts +++ b/src/lib/mongodb.ts @@ -1,17 +1,20 @@ import { MongoClient } from "mongodb"; -if (!import.meta.env.MONGODB_URI) { +if (!process.env.MONGODB_URI && !import.meta.env.MONGODB_URI) { throw new Error( "Please define the MONGODB_URI environment variable inside a root .env file" ); } -if (!import.meta.env.MONGODB_DB) { +if (!process.env.MONGODB_DB && !import.meta.env.MONGODB_DB) { throw new Error( "Please define the MONGODB_DB environment variable inside a root .env file" ); } +const mongodbURI = process.env.MONGODB_URI || import.meta.env.MONGODB_URI; +const mongodbDb = process.env.MONGODB_DB || import.meta.env.MONGODB_DB + /** * Global is used here to maintain a cached connection across hot reloads * in development. This prevents connections growing exponentially @@ -29,11 +32,11 @@ export async function connectToDatabase() { } if (!cached.promise) { - cached.promise = MongoClient.connect(import.meta.env.MONGODB_URI).then( + cached.promise = MongoClient.connect(mongodbURI).then( (client) => { return { client, - db: client.db(import.meta.env.MONGODB_DB), + db: client.db(mongodbDb), }; } ); From 9c9da0649395717203c0d67f0192540ee8af872e Mon Sep 17 00:00:00 2001 From: Shinsina Date: Fri, 19 Apr 2024 13:22:05 -0500 Subject: [PATCH 6/8] Update seed function to support remote DB seeding --- db/seed.ts | 117 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 104 insertions(+), 13 deletions(-) diff --git a/db/seed.ts b/db/seed.ts index 3a8bcc5..413c97b 100644 --- a/db/seed.ts +++ b/db/seed.ts @@ -1,5 +1,6 @@ import { db, + count, Car, CarClass, PastSeason, @@ -40,6 +41,16 @@ async function paginatedQuery( ); return results.flat(); } +function batchInserts(array: Array): Array> { + const size = 2000; + const count = array.length; + const batches = Math.ceil(count / size); + return Array(batches) + .fill({}) + .map((_, index) => + array.slice(index * size, (index + 1) * size).filter((v) => v) + ); +} // https://astro.build/db/seed export default async function seed() { @@ -50,6 +61,7 @@ export default async function seed() { const userCollection = mongoDb.collection("users"); const users = await userCollection.find({}).toArray(); console.log("Seeding Users..."); + await db.delete(User); await db.insert(User).values( users.map((user: any) => { const { _id, last_login, read_comp_rules, read_pp, read_tc, ...rest } = @@ -67,6 +79,7 @@ export default async function seed() { const carCollection = mongoDb.collection("cars"); const cars = await carCollection.find({}).toArray(); console.log("Seeding Cars..."); + await db.delete(Car); await db.insert(Car).values( cars.map((car: any) => { const { _id, created, first_sale, ...rest } = car; @@ -81,6 +94,7 @@ export default async function seed() { const carClassCollection = mongoDb.collection("carclasses"); const carClasses = await paginatedQuery(carClassCollection); console.log("Seeding Car Classes..."); + await db.delete(CarClass); await db.insert(CarClass).values( carClasses.map((carClass: any) => { const { _id, ...rest } = carClass; @@ -91,6 +105,7 @@ export default async function seed() { const seasonsCollection = mongoDb.collection("seasons"); const seasons = await paginatedQuery(seasonsCollection); console.log("Seeding Seasons..."); + await db.delete(Season); await db.insert(Season).values( seasons.map((season: any) => { const { _id, start_date, ...rest } = season; @@ -104,6 +119,7 @@ export default async function seed() { const standingsCollection = mongoDb.collection("standings"); const standings = await paginatedQuery(standingsCollection); console.log("Seeding Standings..."); + await db.delete(Standing); await db.insert(Standing).values( standings.map((standing: any) => { const { _id: id, season_driver_data, ...rest } = standing; @@ -118,6 +134,7 @@ export default async function seed() { const pastSeasonsCollection = mongoDb.collection("pastseasons"); const pastSeasons = await paginatedQuery(pastSeasonsCollection); console.log("Seeding Past Seasons..."); + await db.delete(PastSeason); await db.insert(PastSeason).values( pastSeasons.map((pastSeason: any) => { const { _id, ...rest } = pastSeason; @@ -172,27 +189,101 @@ export default async function seed() { allSubsessions: [], } ); - await db.insert(Subsession).values(allSubsessions); + const currentSubsessions = await db + .select({ count: count(Subsession.subsession_id) }) + .from(Subsession); + const { count: subsessionCount } = currentSubsessions[0]; + console.log( + `Currently have ${subsessionCount} subsessions, new pending count ${allSubsessions.length}` + ); + if (subsessionCount !== allSubsessions.length) { + await db.delete(Subsession); + await db.insert(Subsession).values(allSubsessions); + } console.log("Seeding Subsession Practice Results..."); - await Promise.all( - allPracticeResults.map((result) => - db.insert(SubsessionPracticeResults).values(result) - ) + const currentPracticeResults = await db + .select({ count: count(SubsessionPracticeResults.cust_id) }) + .from(SubsessionPracticeResults); + const { count: practiceResultCount } = currentPracticeResults[0]; + console.log( + `Currently have ${practiceResultCount} practice results, new pending count ${allPracticeResults.length}` ); + if (practiceResultCount !== allPracticeResults.length) { + await db.delete(SubsessionPracticeResults); + const practiceResultInserts: Array = []; + allPracticeResults.forEach((result) => { + practiceResultInserts.push( + db.insert(SubsessionPracticeResults).values(result) + ); + }); + await Promise.all( + batchInserts(practiceResultInserts).map((batch, index) => { + console.log( + `Processing batch ${index + 1} of ${Math.ceil( + practiceResultInserts.length / 2000 + )} for practiceResults` + ); + // @ts-expect-error + return db.batch(batch); + }) + ); + } console.log("Subsession Practice Results Seeded!"); console.log("Seeding Subsession Qualifying Results..."); - await Promise.all( - allQualifyingResults.map((result) => - db.insert(SubsessionQualifyingResults).values(result) - ) + const currentQualifyingResults = await db + .select({ count: count(SubsessionQualifyingResults.cust_id) }) + .from(SubsessionQualifyingResults); + const { count: qualifyingResultCount } = currentQualifyingResults[0]; + console.log( + `Currently have ${qualifyingResultCount} qualifying results, new pending count ${allQualifyingResults.length}` ); + if (qualifyingResultCount !== allQualifyingResults.length) { + await db.delete(SubsessionQualifyingResults); + const qualifyingResultInserts: Array = []; + allQualifyingResults.forEach((result) => { + qualifyingResultInserts.push( + db.insert(SubsessionQualifyingResults).values(result) + ); + }); + await Promise.all( + batchInserts(qualifyingResultInserts).map((batch, index) => { + console.log( + `Processing batch ${index + 1} of ${Math.ceil( + qualifyingResultInserts.length / 2000 + )} for qualifyingResults` + ); + // @ts-expect-error + return db.batch(batch); + }) + ); + } console.log("Subsession Qualifying Results Seeded!"); console.log("Seeding Subsession Race Results..."); - await Promise.all( - allRaceResults.map((result) => - db.insert(SubsessionRaceResults).values(result) - ) + const currentRaceResults = await db + .select({ count: count(SubsessionRaceResults.cust_id) }) + .from(SubsessionRaceResults); + const { count: raceResultCount } = currentRaceResults[0]; + console.log( + `Currently have ${raceResultCount} race results, new pending count ${allRaceResults.length}` ); + if (raceResultCount !== allRaceResults.length) { + await db.delete(SubsessionRaceResults); + const raceResultInserts: Array = []; + allRaceResults.forEach((result) => { + raceResultInserts.push(db.insert(SubsessionRaceResults).values(result)); + }); + await Promise.all( + batchInserts(raceResultInserts).map((batch, index) => { + console.log( + `Processing batch ${index + 1} of ${Math.ceil( + raceResultInserts.length / 2000 + )} for raceResults` + ); + // @ts-expect-error + return db.batch(batch); + }) + ); + } console.log("Subsession Race Results Seeded!"); console.log("Subsessions Seeded!"); } From b2cdf3b21d4fff8108069ae1958cbd9209711634 Mon Sep 17 00:00:00 2001 From: Shinsina Date: Fri, 19 Apr 2024 13:22:29 -0500 Subject: [PATCH 7/8] Add shell script to update remote database using env vars --- seed-remote-database.sh | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100755 seed-remote-database.sh diff --git a/seed-remote-database.sh b/seed-remote-database.sh new file mode 100755 index 0000000..b45ac8f --- /dev/null +++ b/seed-remote-database.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +set -a +source .env +set +a + +npx astro db execute ./db/seed.ts --remote From 9a5fa4d88b03f7cb89ddee7817712f25dfc569ad Mon Sep 17 00:00:00 2001 From: Shinsina Date: Fri, 19 Apr 2024 13:27:22 -0500 Subject: [PATCH 8/8] Add ASTRO_STUDIO_APP_TOKEN to deployment workflow --- .github/workflows/deploy.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 36e425e..7173edc 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -22,6 +22,7 @@ jobs: uses: actions/checkout@v3 - name: Install, build, and upload your site env: + ASTRO_STUDIO_APP_TOKEN: ${{secrets.ASTRO_STUDIO_APP_TOKEN }} MONGODB_URI: ${{ secrets.MONGODB_URI }} MONGODB_DB: ${{ secrets.MONGODB_DB }} uses: withastro/action@v0