Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More integration improvements #43

Merged
merged 8 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions astro.config.mjs
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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
}
});
117 changes: 104 additions & 13 deletions db/seed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
db,
count,
Car,
CarClass,
PastSeason,
Expand Down Expand Up @@ -40,6 +41,16 @@ async function paginatedQuery(
);
return results.flat();
}
function batchInserts(array: Array<any>): Array<Array<any>> {
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() {
Expand All @@ -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 } =
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<any> = [];
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<any> = [];
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<any> = [];
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!");
}
18 changes: 0 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
"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"
},
"dependencies": {
"@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",
Expand Down
7 changes: 7 additions & 0 deletions seed-remote-database.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -a
source .env
set +a

npx astro db execute ./db/seed.ts --remote
1 change: 1 addition & 0 deletions src/lib/components/subsession/header.astro
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { series_logo, season_name, race_week_num, max_weeks } = subsession;
width={300}
height={100}
format="avif"
loading="eager"
/>
</div>
<div class="text-center py-4">
Expand Down
20 changes: 7 additions & 13 deletions src/lib/layouts/default.astro
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,15 @@ const seoDescription =
<div
class="block lg:text-3xl md:text-lg sm:text-md break-words text-center py-2"
>
<a class="underline" href="/Stat-N-Track/" rel="prefetch">Home</a>
<a
class="underline pl-2"
href="/Stat-N-Track/user/300752/"
rel="prefetch">Jacob Collins</a
<a class="underline" href="/Stat-N-Track/">Home</a>
<a class="underline pl-2" href="/Stat-N-Track/user/300752/"
>Jacob Collins</a
>
<a
class="underline pl-2"
href="/Stat-N-Track/user/815162/"
rel="prefetch">Jack Glenzinski</a
<a class="underline pl-2" href="/Stat-N-Track/user/815162/"
>Jack Glenzinski</a
>
<a
class="underline pl-2"
href="/Stat-N-Track/shared-subsessions/"
rel="prefetch">Shared Subsessions</a
<a class="underline pl-2" href="/Stat-N-Track/shared-subsessions/"
>Shared Subsessions</a
>
</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion src/lib/layouts/standings.astro
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ const generateLinkForKey = ({
}) ? (
<a
class="underline"
rel="prefetch"
href={generateLinkForKey({
key,
resultAtKey: result[key],
Expand Down
1 change: 0 additions & 1 deletion src/lib/layouts/subsessions.astro
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ const { keysArray, handledResults } = handleResults({ keysToDisplay, results });
{key === "0" ? (
<a
class="underline"
rel="prefetch"
href={`/Stat-N-Track/subsession/${result[key]}`}
>
{result[key]}
Expand Down
11 changes: 7 additions & 4 deletions src/lib/mongodb.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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),
};
}
);
Expand Down
1 change: 0 additions & 1 deletion src/pages/shared-subsessions.astro
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ const description = "Shared subsessions for all Stat 'n' Track users";
>
<a
class="underline"
rel="prefetch"
href={`/Stat-N-Track/subsession/${result.subsessionInfo.subsession_id}`}
>
<div class="text-center py-4 border-2">
Expand Down
Loading
Loading