feat(api): add match stats fragments
This commit is contained in:
1 parent
b3b59d37fc
commit
956e45ee59
6 files changed
+777
No files matched your search
@@ -0,0 +1,104 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import mongoose from 'mongoose';
|
||||
import { ConnectionService } from '../global/database/connection.service';
|
||||
import type { MatchDocument } from '../global/database/schema';
|
||||
import type { RiotMatchDto } from '../riot/riot.types';
|
||||
import { MatchStatsFragmentBuilder } from './match-stats-fragment.builder';
|
||||
import { MatchStatsRepository } from './match-stats.repository';
|
||||
import { MATCH_STATS_BUILDER_VERSION } from './match-stats.types';
|
||||
|
||||
@Injectable()
|
||||
export class MatchStatsService {
|
||||
constructor(
|
||||
private readonly connectionService: ConnectionService,
|
||||
private readonly repository: MatchStatsRepository,
|
||||
private readonly builder: MatchStatsFragmentBuilder,
|
||||
) {}
|
||||
|
||||
public async buildAndUpsert(matchId: mongoose.Types.ObjectId): Promise<void> {
|
||||
const MatchModel = await this.connectionService.getModel('match');
|
||||
const RiotMatchModel = await this.connectionService.getModel('riotMatch');
|
||||
const match = await MatchModel.findById(matchId).exec();
|
||||
if (!match) return;
|
||||
if (!this.isEligible(match)) {
|
||||
await this.repository.deleteByMatch(matchId);
|
||||
return;
|
||||
}
|
||||
|
||||
const riotIds = [
|
||||
...new Set(
|
||||
(match.result?.games ?? [])
|
||||
.map((game) => game.riotMatchId)
|
||||
.filter((riotMatchId): riotMatchId is string => Boolean(riotMatchId)),
|
||||
),
|
||||
];
|
||||
const riotDocs =
|
||||
riotIds.length > 0
|
||||
? await RiotMatchModel.find({ matchId: { $in: riotIds } })
|
||||
.lean()
|
||||
.exec()
|
||||
: [];
|
||||
const rawByRiotMatchId = new Map(
|
||||
riotDocs.map((doc) => [doc.matchId, doc.raw as unknown as RiotMatchDto]),
|
||||
);
|
||||
const fragment = this.builder.buildFragment({ match, rawByRiotMatchId });
|
||||
|
||||
await this.repository.upsert({
|
||||
matchId: match._id,
|
||||
communityId: match.communityId,
|
||||
scheduledAt: match.scheduledAt,
|
||||
gameCreationMin: fragment.gameCreationMin,
|
||||
gameCreationMax: fragment.gameCreationMax,
|
||||
userIds: fragment.userIds,
|
||||
championIds: fragment.championIds,
|
||||
matchCard: fragment.matchCard as unknown as Record<string, unknown>,
|
||||
records: fragment.records,
|
||||
gameDetails: fragment.gameDetails,
|
||||
builtAt: new Date(),
|
||||
builderVersion: MATCH_STATS_BUILDER_VERSION,
|
||||
});
|
||||
}
|
||||
|
||||
public async removeByMatch(matchId: mongoose.Types.ObjectId): Promise<void> {
|
||||
await this.repository.deleteByMatch(matchId);
|
||||
}
|
||||
|
||||
public async removeByMatches(
|
||||
matchIds: mongoose.Types.ObjectId[],
|
||||
): Promise<void> {
|
||||
await this.repository.deleteByMatches(matchIds);
|
||||
}
|
||||
|
||||
public async backfillCommunity(
|
||||
communityId: mongoose.Types.ObjectId,
|
||||
): Promise<{ processed: number; failed: number }> {
|
||||
const MatchModel = await this.connectionService.getModel('match');
|
||||
const matches = await MatchModel.find({
|
||||
communityId,
|
||||
status: { $in: ['finished', 'in-progress'] },
|
||||
'result.games.0': { $exists: true },
|
||||
})
|
||||
.sort({ scheduledAt: -1 })
|
||||
.select({ _id: 1 })
|
||||
.exec();
|
||||
|
||||
let processed = 0;
|
||||
let failed = 0;
|
||||
for (const match of matches) {
|
||||
try {
|
||||
await this.buildAndUpsert(match._id);
|
||||
processed += 1;
|
||||
} catch {
|
||||
failed += 1;
|
||||
}
|
||||
}
|
||||
return { processed, failed };
|
||||
}
|
||||
|
||||
private isEligible(match: MatchDocument): boolean {
|
||||
return (
|
||||
(match.status === 'finished' || match.status === 'in-progress') &&
|
||||
Boolean(match.result?.games?.length)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user