feat: connect tournament views to APIs and recover archived stats
Remove development previews and complete hall of fame, match history, official results, and multi-kill API integration. Fix active match pagination and team-ready controls, preserve basic scoreboards, and recover statistics when Riot archives arrive. Use bounded in-process cache invalidation retries and add the Riot match lookup index migration with regression coverage.
This commit is contained in:
1 parent
20e1e6ec04
commit
0d7617e2d2
70 files changed
+3739
-1793
No files matched your search
@@ -0,0 +1,4 @@
|
||||
export const MATCH_STATS_RIOT_MATCH_INDEX = {
|
||||
key: { 'gameDetails.riotMatchId': 1 as const },
|
||||
options: { name: 'gameDetails.riotMatchId_1' },
|
||||
};
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
import type { mongo } from 'mongoose';
|
||||
import { migrateMatchStatsRawIndex } from './20261111-match-stats-raw-index';
|
||||
import { MATCH_STATS_RIOT_MATCH_INDEX } from '../match-stats-raw-index';
|
||||
import { parseMatchStatsIndexOptions } from './run-match-stats-raw-index';
|
||||
|
||||
it('defaults to a read-only plan and validates the target environment', async () => {
|
||||
expect(parseMatchStatsIndexOptions(['--env', 'prod']).dryRun).toBe(true);
|
||||
expect(() => parseMatchStatsIndexOptions(['--env', 'unknown'])).toThrow();
|
||||
const createIndex = jest.fn();
|
||||
await migrateMatchStatsRawIndex(database([], createIndex));
|
||||
expect(createIndex).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('adds only the lookup index and accepts an identical existing index', async () => {
|
||||
const { key, options } = MATCH_STATS_RIOT_MATCH_INDEX;
|
||||
const createIndex = jest.fn();
|
||||
const result = await migrateMatchStatsRawIndex(
|
||||
database([{ key, ...options }], createIndex),
|
||||
{ dryRun: false },
|
||||
);
|
||||
expect(result.alreadyPresent).toBe(true);
|
||||
expect(createIndex).toHaveBeenCalledWith(key, options);
|
||||
});
|
||||
|
||||
it('rejects an incompatible existing index without dropping or recreating it', async () => {
|
||||
const { key, options } = MATCH_STATS_RIOT_MATCH_INDEX;
|
||||
const createIndex = jest.fn();
|
||||
await expect(
|
||||
migrateMatchStatsRawIndex(
|
||||
database([{ key, ...options, unique: true }], createIndex),
|
||||
{ dryRun: false },
|
||||
),
|
||||
).rejects.toThrow('different definition');
|
||||
expect(createIndex).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
function database(indexes: unknown[], createIndex: jest.Mock): mongo.Db {
|
||||
return {
|
||||
collection: (name: string) => {
|
||||
expect(name).toBe('match_stats');
|
||||
return {
|
||||
listIndexes: () => ({ toArray: async () => indexes }),
|
||||
createIndex,
|
||||
};
|
||||
},
|
||||
} as unknown as mongo.Db;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import type { mongo } from 'mongoose';
|
||||
import { MATCH_STATS_RIOT_MATCH_INDEX } from '../match-stats-raw-index';
|
||||
|
||||
/** Adds the lookup index without dropping indexes or modifying game records. */
|
||||
export async function migrateMatchStatsRawIndex(
|
||||
db: mongo.Db,
|
||||
{ dryRun = true }: { dryRun?: boolean } = {},
|
||||
) {
|
||||
const { key, options } = MATCH_STATS_RIOT_MATCH_INDEX;
|
||||
const collection = db.collection('match_stats');
|
||||
const indexes = await collection
|
||||
.listIndexes()
|
||||
.toArray()
|
||||
.catch((error: unknown) => {
|
||||
if (error instanceof Error && 'code' in error && error.code === 26)
|
||||
return [];
|
||||
throw error;
|
||||
});
|
||||
const existing = indexes.find((index) => index.name === options.name);
|
||||
if (
|
||||
existing &&
|
||||
(JSON.stringify(existing.key) !== JSON.stringify(key) ||
|
||||
existing.unique === true ||
|
||||
existing.sparse === true ||
|
||||
existing.hidden === true ||
|
||||
existing.partialFilterExpression !== undefined)
|
||||
) {
|
||||
throw new Error(
|
||||
`Index ${options.name} already exists with a different definition.`,
|
||||
);
|
||||
}
|
||||
if (!dryRun) await collection.createIndex(key, options);
|
||||
return {
|
||||
dryRun,
|
||||
alreadyPresent: Boolean(existing),
|
||||
index: { key, ...options },
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
import 'reflect-metadata';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { resolve } from 'node:path';
|
||||
import { ConnectionService, resolveMongoDbName } from '../connection.service';
|
||||
import { SecretModule } from '../../secret/secret.module';
|
||||
import { migrateMatchStatsRawIndex } from './20261111-match-stats-raw-index';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
envFilePath: [
|
||||
resolve(__dirname, '../../../../../../project.env'),
|
||||
resolve(process.cwd(), '../project.env'),
|
||||
resolve(process.cwd(), 'apps/project.env'),
|
||||
resolve(process.cwd(), 'project.env'),
|
||||
],
|
||||
}),
|
||||
SecretModule,
|
||||
],
|
||||
providers: [ConnectionService],
|
||||
})
|
||||
class MigrationModule {}
|
||||
|
||||
export function parseMatchStatsIndexOptions(argv: string[]) {
|
||||
let dryRun = true;
|
||||
let help = false;
|
||||
let nodeEnv = process.env.NODE_ENV ?? 'dev';
|
||||
for (let index = 0; index < argv.length; index++) {
|
||||
const argument = argv[index];
|
||||
if (argument === '--') continue;
|
||||
if (argument === '--apply') dryRun = false;
|
||||
else if (argument === '--dry-run') dryRun = true;
|
||||
else if (argument === '--help' || argument === '-h') help = true;
|
||||
else if (argument === '--env') nodeEnv = argv[++index] ?? '';
|
||||
else if (argument.startsWith('--env=')) nodeEnv = argument.slice(6);
|
||||
else throw new Error(`Unknown argument: ${argument}`);
|
||||
}
|
||||
if (!help && !['dev', 'staging', 'prod'].includes(nodeEnv)) {
|
||||
throw new Error('--env must be dev, staging, or prod.');
|
||||
}
|
||||
return { dryRun, help, nodeEnv };
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const options = parseMatchStatsIndexOptions(process.argv.slice(2));
|
||||
if (options.help) {
|
||||
console.log(`Usage:
|
||||
pnpm --filter api stats:indexes -- --env staging --dry-run
|
||||
pnpm --filter api stats:indexes -- --env prod --apply
|
||||
|
||||
Defaults to dry-run. --env accepts dev, staging, or prod (NODE_ENV or dev by default).
|
||||
Creates the internal stats Riot match ID index; does not modify documents.`);
|
||||
return;
|
||||
}
|
||||
// Use the same optional machine environment as the existing migration runners.
|
||||
const machineHome = process.env.HOME ?? process.env.USERPROFILE;
|
||||
if (machineHome) {
|
||||
const contents = await readFile(
|
||||
resolve(machineHome, 'source/global-env-vars.txt'),
|
||||
'utf8',
|
||||
).catch(() => '');
|
||||
for (const line of contents.split(/\r?\n/)) {
|
||||
const match = line.trim().match(/^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/);
|
||||
if (match) process.env[match[1]] = match[2].replace(/^["']|["']$/g, '');
|
||||
}
|
||||
}
|
||||
process.env.NODE_ENV = options.nodeEnv;
|
||||
// Even a dry-run with auto-index enabled in the environment must not write.
|
||||
process.env.MONGODB_AUTO_INDEX = 'false';
|
||||
const app = await NestFactory.createApplicationContext(MigrationModule, {
|
||||
logger: ['warn', 'error'],
|
||||
});
|
||||
try {
|
||||
const connection = await app.get(ConnectionService).getConnection();
|
||||
if (!connection.db) throw new Error('MongoDB connection has no db handle.');
|
||||
const result = await migrateMatchStatsRawIndex(connection.db, options);
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
env: options.nodeEnv,
|
||||
db: resolveMongoDbName(options.nodeEnv),
|
||||
...result,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
} finally {
|
||||
await app.close();
|
||||
}
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
void main().catch((error: unknown) => {
|
||||
console.error(error instanceof Error ? error.message : error);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
} from '../../competition/competition.schema';
|
||||
import { ModelDefinition } from './types';
|
||||
import { AUCTION_CANCELLATION_CLEANUP_INDEX } from './auction-cancellation-index';
|
||||
import { MATCH_STATS_RIOT_MATCH_INDEX } from './match-stats-raw-index';
|
||||
|
||||
export type Position = 'TOP' | 'JGL' | 'MID' | 'ADC' | 'SUP';
|
||||
export const POSITIONS: Position[] = ['TOP', 'JGL', 'MID', 'ADC', 'SUP'];
|
||||
@@ -1019,6 +1020,14 @@ export interface MatchStatsRecord {
|
||||
championLevel: number;
|
||||
teamPosition: string;
|
||||
kills: number;
|
||||
multiKills?: {
|
||||
doubleKills: number;
|
||||
tripleKills: number;
|
||||
quadraKills: number;
|
||||
pentaKills: number;
|
||||
largestMultiKill: number;
|
||||
};
|
||||
tier?: TierInfo;
|
||||
deaths: number;
|
||||
assists: number;
|
||||
win: boolean;
|
||||
@@ -1051,6 +1060,7 @@ export interface MatchStatsGameDetail {
|
||||
gameCreation: number;
|
||||
gameDuration: number;
|
||||
gameVersion?: string;
|
||||
rawAvailable?: boolean;
|
||||
winnerSide: TeamSide;
|
||||
timeline: MatchStatsTimelinePoint[];
|
||||
timelineLoaded?: boolean;
|
||||
@@ -2535,7 +2545,7 @@ export const MatchStatsSchema: mongoose.Schema<MatchStats> = new Schema(
|
||||
{ timestamps: true, collection: 'match_stats' },
|
||||
) as mongoose.Schema<MatchStats>;
|
||||
|
||||
MatchStatsSchema.index({ communityId: 1, scheduledAt: -1 });
|
||||
MatchStatsSchema.index({ communityId: 1, scheduledAt: -1, matchId: -1 });
|
||||
MatchStatsSchema.index({
|
||||
communityId: 1,
|
||||
gameCreationMin: 1,
|
||||
@@ -2544,6 +2554,7 @@ MatchStatsSchema.index({
|
||||
MatchStatsSchema.index({ communityId: 1, userIds: 1 });
|
||||
MatchStatsSchema.index({ communityId: 1, userIds: 1, scheduledAt: -1 });
|
||||
MatchStatsSchema.index({ communityId: 1, championIds: 1 });
|
||||
MatchStatsSchema.index(MATCH_STATS_RIOT_MATCH_INDEX.key, MATCH_STATS_RIOT_MATCH_INDEX.options);
|
||||
|
||||
export const tournamentModelDefinitions: Array<
|
||||
ModelDefinition & { name: keyof TournamentModelTypes }
|
||||
|
||||
Reference in new issue
Block a user