feat: more optimal fetching

This commit is contained in:
TrèsAbhi committed 2024-03-28 10:31:36 -05:00
1 parent c7021c5689
commit f083c18dd0
2 files changed
+50 -52

No files matched your search

+2 -11
View File
@@ -30,14 +30,5 @@ jobs:
- name: Install dependencies - name: Install dependencies
run: yarn run: yarn
- name: Generate NA leaderboard - name: Generate all leaderboards
run: GH_TOKEN=${{ secrets.GH_TOKEN }} DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }} DISCORD_BLITZ_DB=${{ secrets.DISCORD_BLITZ_DB }} yarn publish:ratingleaderboard --region=na --target=latest --production run: GH_TOKEN=${{ secrets.GH_TOKEN }} DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }} DISCORD_BLITZ_DB=${{ secrets.DISCORD_BLITZ_DB }} yarn publish:ratingleaderboard --region=na,asia,eu --target=latest --production
if: always()
- name: Generate EU leaderboard
run: GH_TOKEN=${{ secrets.GH_TOKEN }} DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }} DISCORD_BLITZ_DB=${{ secrets.DISCORD_BLITZ_DB }} yarn publish:ratingleaderboard --region=eu --target=latest --production
if: always()
- name: Generate Asia leaderboard
run: GH_TOKEN=${{ secrets.GH_TOKEN }} DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }} DISCORD_BLITZ_DB=${{ secrets.DISCORD_BLITZ_DB }} yarn publish:ratingleaderboard --region=asia --target=latest --production
if: always()
+48 -41
View File
@@ -30,29 +30,32 @@ const production = argv.includes('--production');
const target = argv const target = argv
.find((arg) => arg.startsWith('--target=')) .find((arg) => arg.startsWith('--target='))
?.split('=')[1] as 'midnight' | 'latest' | undefined; ?.split('=')[1] as 'midnight' | 'latest' | undefined;
const regionSubdomain = argv const regionSubdomains = argv
.find((arg) => arg.startsWith('--region=')) .find((arg) => arg.startsWith('--region='))
?.split('=')[1] as RegionSubdomain | undefined; ?.split('=')[1];
if (!target) throw new Error('Target parameter not specified'); if (!target) throw new Error('Target parameter not specified');
if (!regionSubdomain) throw new Error('Region parameter not specified'); if (!regionSubdomains) throw new Error('Region parameter not specified');
const region = regionSubdomainToRegion(regionSubdomain); regionSubdomains.split(',').forEach(async (regionSubdomain) => {
const region = regionSubdomainToRegion(regionSubdomain as RegionSubdomain);
const NEIGHBORS = 2 ** 8; console.log(`Building ${target} rating leaderboard for ${region}`);
const PLAYERS = await patientFetchJSON<RatingInfo>(
const NEIGHBORS = 2 ** 8;
const PLAYERS = await patientFetchJSON<RatingInfo>(
`https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/season/`, `https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/season/`,
).then((data) => (data.detail === undefined ? data.count : undefined)); ).then((data) => (data.detail === undefined ? data.count : undefined));
if (PLAYERS === undefined) { if (PLAYERS === undefined) {
throw new Error('No current rating season running'); throw new Error('No current rating season running');
} }
const players: Record<number, RatingPlayer> = {}; const players: Record<number, RatingPlayer> = {};
const leaderboard: BkrlBaseEntry[] = []; const leaderboard: BkrlBaseEntry[] = [];
let registered = 0; let registered = 0;
async function branchFromPlayer(id: number) { async function branchFromPlayer(id: number) {
const { neighbors } = await patientFetchJSON<RatingNeighbors>( const { neighbors } = await patientFetchJSON<RatingNeighbors>(
`https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/user/${id}/?neighbors=${NEIGHBORS}`, `https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/user/${id}/?neighbors=${NEIGHBORS}`,
); );
@@ -81,9 +84,9 @@ async function branchFromPlayer(id: number) {
); );
await Promise.all(branches); await Promise.all(branches);
} }
async function branchFromLeague(id: number) { async function branchFromLeague(id: number) {
const { result } = await patientFetchJSON<{ result: RatingPlayer[] }>( const { result } = await patientFetchJSON<{ result: RatingPlayer[] }>(
`https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/league/${id}/top/`, `https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/league/${id}/top/`,
); );
@@ -95,19 +98,19 @@ async function branchFromLeague(id: number) {
branchFromPlayer(firstPlayer.spa_id), branchFromPlayer(firstPlayer.spa_id),
branchFromPlayer(lastPlayer.spa_id), branchFromPlayer(lastPlayer.spa_id),
]); ]);
} }
console.log('Branching from leagues...'); console.log('Branching from leagues...');
await Promise.all([ await Promise.all([
branchFromLeague(0), branchFromLeague(0),
branchFromLeague(1), branchFromLeague(1),
branchFromLeague(2), branchFromLeague(2),
branchFromLeague(3), branchFromLeague(3),
branchFromLeague(4), branchFromLeague(4),
]); ]);
console.log('Converting to an array...'); console.log('Converting to an array...');
for (let index = 0; index < PLAYERS; index++) { for (let index = 0; index < PLAYERS; index++) {
const listing = players[index + 1]; const listing = players[index + 1];
if (listing) { if (listing) {
leaderboard[index] = { id: listing.spa_id, score: listing.score }; leaderboard[index] = { id: listing.spa_id, score: listing.score };
@@ -115,12 +118,12 @@ for (let index = 0; index < PLAYERS; index++) {
console.warn(`Position ${index + 1} had no player`); console.warn(`Position ${index + 1} had no player`);
leaderboard[index] = { id: 0, score: 0 }; leaderboard[index] = { id: 0, score: 0 };
} }
} }
console.log('Requesting further stats...'); console.log('Requesting further stats...');
const comprehensiveLeaderboard: BkrlSuperset1Entry[] = []; const comprehensiveLeaderboard: BkrlSuperset1Entry[] = [];
let chunkIndex = 0; let chunkIndex = 0;
for (const leaderboardChunk of chunk(leaderboard, 100)) { for (const leaderboardChunk of chunk(leaderboard, 100)) {
console.log( console.log(
`Chunk ${chunkIndex + 1} of ${Math.ceil(leaderboard.length / 100)}`, `Chunk ${chunkIndex + 1} of ${Math.ceil(leaderboard.length / 100)}`,
); );
@@ -134,7 +137,8 @@ for (const leaderboardChunk of chunk(leaderboard, 100)) {
stats.forEach((stat, index) => { stats.forEach((stat, index) => {
const trueIndex = chunkIndex * 100 + index; const trueIndex = chunkIndex * 100 + index;
comprehensiveLeaderboard[trueIndex] = comprehensiveLeaderboard[trueIndex] =
leaderboardChunk[index].id === 0 || stat?.statistics.rating === undefined leaderboardChunk[index].id === 0 ||
stat?.statistics.rating === undefined
? { ? {
battles: 0, battles: 0,
damage: 0, damage: 0,
@@ -156,23 +160,23 @@ for (const leaderboardChunk of chunk(leaderboard, 100)) {
}); });
chunkIndex++; chunkIndex++;
} }
const info = await patientFetchJSON<RatingInfo & { detail: undefined }>( const info = await patientFetchJSON<RatingInfo & { detail: undefined }>(
`https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/season/`, `https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/season/`,
); );
const normalizedServer = regionSubdomain === 'na' ? 'com' : regionSubdomain; const normalizedServer = regionSubdomain === 'na' ? 'com' : regionSubdomain;
const infoContent = new CdonWriteStream().cdon( const infoContent = new CdonWriteStream().cdon(
info as unknown as CdonValue, info as unknown as CdonValue,
).uint8Array; ).uint8Array;
const leaderboardWriteStream = new BkrlWriteStream(); const leaderboardWriteStream = new BkrlWriteStream();
leaderboardWriteStream.bkrl({ leaderboardWriteStream.bkrl({
format: BkrlFormat.Superset1, format: BkrlFormat.Superset1,
entries: comprehensiveLeaderboard, entries: comprehensiveLeaderboard,
}); });
const changes: FileChange[] = [ const changes: FileChange[] = [
{ {
path: `regions/${normalizedServer}/rating/${info.current_season}/info.cdon.lz4`, path: `regions/${normalizedServer}/rating/${info.current_season}/info.cdon.lz4`,
content: superCompress(infoContent), content: superCompress(infoContent),
@@ -180,13 +184,16 @@ const changes: FileChange[] = [
}, },
{ {
path: `regions/${normalizedServer}/rating/${info.current_season}/${target}.bkrl`, path: `regions/${normalizedServer}/rating/${info.current_season}/${target}.bkrl`,
content: Buffer.from(leaderboardWriteStream.uint8Array).toString('base64'), content: Buffer.from(leaderboardWriteStream.uint8Array).toString(
'base64',
),
encoding: 'base64', encoding: 'base64',
}, },
]; ];
commitAssets( commitAssets(
`rating leaderboard ${regionSubdomain} ${target}`, `rating leaderboard ${regionSubdomain} ${target}`,
changes, changes,
production, production,
); );
});