Skip to content

Commit

Permalink
chore: limit db request when setting up db
Browse files Browse the repository at this point in the history
  • Loading branch information
sebald committed Oct 11, 2023
1 parent be1c7a3 commit 2fa660a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"kysely": "^0.26.3",
"kysely-planetscale": "^1.4.0",
"next": "13.5.3",
"p-limit": "^4.0.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-is": "18.2.0",
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

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

50 changes: 28 additions & 22 deletions scripts/setup-database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env tsx
import 'zx/globals';
import dotenv from 'dotenv';
import pLimit from 'p-limit';

import { pointsUpdateDate } from '@/lib/config';
import { getAllTournaments, getSquads } from '@/lib/vendor/listfortress';
Expand All @@ -12,6 +13,7 @@ import { normalize, toCompositionId } from '@/lib/xws';
// ---------------
$.verbose = false;
dotenv.config({ path: '.env.local' });
const limit = pLimit(100);

// Script
// ---------------
Expand Down Expand Up @@ -53,28 +55,32 @@ void (async () => {
squadCount += squads.length;

return squads.map(squad =>
addSquads([
{
listfortress_ref: tournament.listfortress_ref,
composition: squad.xws ? toCompositionId(squad.xws) : undefined,
faction: squad.xws?.faction || 'unknown',
player: squad.player,
date: tournament.date,
xws: squad.xws
? JSON.stringify(normalize(squad.xws)) || undefined
: undefined,
wins: squad.record.wins,
ties: squad.record.ties,
losses: squad.record.losses,
record: JSON.stringify(squad.record),
swiss: squad.rank.swiss,
cut: squad.rank.elimination,
percentile: percentile(
squad.rank.elimination ?? squad.rank.swiss,
squads.length
).toString(),
},
])
limit(() =>
addSquads([
{
listfortress_ref: tournament.listfortress_ref,
composition: squad.xws
? toCompositionId(squad.xws)
: undefined,
faction: squad.xws?.faction || 'unknown',
player: squad.player,
date: tournament.date,
xws: squad.xws
? JSON.stringify(normalize(squad.xws)) || undefined
: undefined,
wins: squad.record.wins,
ties: squad.record.ties,
losses: squad.record.losses,
record: JSON.stringify(squad.record),
swiss: squad.rank.swiss,
cut: squad.rank.elimination,
percentile: percentile(
squad.rank.elimination ?? squad.rank.swiss,
squads.length
).toString(),
},
])
)
);
})
.flat()
Expand Down

1 comment on commit 2fa660a

@vercel
Copy link

@vercel vercel bot commented on 2fa660a Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.