perf: aggregate champion stats in MongoDB
This commit is contained in:
1 parent
88de2148ae
commit
2ee8c3807b
2 files changed
+261
-16
No files matched your search
@@ -69,6 +69,10 @@ interface AggregatedChampion {
|
||||
pentaKills: number;
|
||||
}
|
||||
|
||||
interface ChampionAggregateRow extends AggregatedChampion {
|
||||
lastFetchedAt?: Date;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class MatchHistoryService {
|
||||
constructor(private readonly connectionService: ConnectionService) {}
|
||||
@@ -129,29 +133,17 @@ export class MatchHistoryService {
|
||||
const { queueGroup, rank, queueIds } = this.resolveQueueFilters(
|
||||
params.filters,
|
||||
);
|
||||
const docs = await this.findStoredMatches({
|
||||
const aggregates = await this.aggregateChampionStats({
|
||||
context,
|
||||
queueIds,
|
||||
limit: MAX_AGGREGATE_MATCHES,
|
||||
});
|
||||
const aggregates = new Map<number, AggregatedChampion>();
|
||||
|
||||
for (const doc of docs) {
|
||||
const dto = doc.raw as unknown as RiotMatchDto;
|
||||
const participant = this.findParticipant(dto, context.account.puuid);
|
||||
if (!participant) continue;
|
||||
const aggregate =
|
||||
aggregates.get(participant.championId) ??
|
||||
this.createChampionAggregate(participant.championId);
|
||||
this.addParticipantToChampionAggregate(aggregate, participant, dto);
|
||||
aggregates.set(participant.championId, aggregate);
|
||||
}
|
||||
|
||||
const champions = [...aggregates.values()]
|
||||
const champions = aggregates
|
||||
.map((aggregate) => this.toChampionStatsRow(aggregate))
|
||||
.sort((a, b) => b.games - a.games || b.winRate - a.winRate);
|
||||
const totals = this.toChampionStatsRow(
|
||||
[...aggregates.values()].reduce(
|
||||
aggregates.reduce(
|
||||
(acc, aggregate) => this.mergeChampionAggregate(acc, aggregate),
|
||||
this.createChampionAggregate(0),
|
||||
),
|
||||
@@ -162,7 +154,7 @@ export class MatchHistoryService {
|
||||
rank,
|
||||
queueIds,
|
||||
season: context.season,
|
||||
lastFetchedAt: this.resolveLastFetchedAt(docs, context),
|
||||
lastFetchedAt: this.resolveAggregateLastFetchedAt(aggregates, context),
|
||||
champions,
|
||||
totals,
|
||||
};
|
||||
@@ -363,6 +355,101 @@ export class MatchHistoryService {
|
||||
.slice(0, params.limit);
|
||||
}
|
||||
|
||||
private async aggregateChampionStats(params: {
|
||||
context: HistoryContext;
|
||||
queueIds: number[];
|
||||
limit: number;
|
||||
}): Promise<ChampionAggregateRow[]> {
|
||||
if (params.queueIds.length === 0) return [];
|
||||
const RiotMatchModel = await this.connectionService.getModel('riotMatch');
|
||||
const gameCreation: Record<string, number> = {};
|
||||
if (params.context.seasonStartedAt) {
|
||||
gameCreation.$gte = params.context.seasonStartedAt.getTime();
|
||||
}
|
||||
|
||||
const match: Record<string, unknown> = {
|
||||
participantPuuids: params.context.account.puuid,
|
||||
queueId: { $in: params.queueIds },
|
||||
};
|
||||
if (Object.keys(gameCreation).length > 0) match.gameCreation = gameCreation;
|
||||
|
||||
return RiotMatchModel.aggregate<ChampionAggregateRow>([
|
||||
{ $match: match },
|
||||
{ $sort: { gameCreation: -1 } },
|
||||
{ $limit: params.limit },
|
||||
{
|
||||
$project: {
|
||||
fetchedAt: 1,
|
||||
gameDuration: '$raw.info.gameDuration',
|
||||
participant: {
|
||||
$first: {
|
||||
$filter: {
|
||||
input: '$raw.info.participants',
|
||||
as: 'participant',
|
||||
cond: {
|
||||
$eq: ['$$participant.puuid', params.context.account.puuid],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{ $match: { participant: { $ne: null } } },
|
||||
{
|
||||
$group: {
|
||||
_id: '$participant.championId',
|
||||
championId: { $first: '$participant.championId' },
|
||||
games: { $sum: 1 },
|
||||
wins: {
|
||||
$sum: { $cond: ['$participant.win', 1, 0] },
|
||||
},
|
||||
totalKills: { $sum: { $ifNull: ['$participant.kills', 0] } },
|
||||
totalDeaths: { $sum: { $ifNull: ['$participant.deaths', 0] } },
|
||||
totalAssists: { $sum: { $ifNull: ['$participant.assists', 0] } },
|
||||
totalCs: {
|
||||
$sum: {
|
||||
$add: [
|
||||
{ $ifNull: ['$participant.totalMinionsKilled', 0] },
|
||||
{ $ifNull: ['$participant.neutralMinionsKilled', 0] },
|
||||
],
|
||||
},
|
||||
},
|
||||
totalGameMinutes: {
|
||||
$sum: {
|
||||
$max: [
|
||||
{
|
||||
$divide: [
|
||||
{
|
||||
$ifNull: ['$participant.timePlayed', '$gameDuration'],
|
||||
},
|
||||
60,
|
||||
],
|
||||
},
|
||||
1,
|
||||
],
|
||||
},
|
||||
},
|
||||
doubleKills: {
|
||||
$sum: { $ifNull: ['$participant.doubleKills', 0] },
|
||||
},
|
||||
tripleKills: {
|
||||
$sum: { $ifNull: ['$participant.tripleKills', 0] },
|
||||
},
|
||||
quadraKills: {
|
||||
$sum: { $ifNull: ['$participant.quadraKills', 0] },
|
||||
},
|
||||
pentaKills: {
|
||||
$sum: { $ifNull: ['$participant.pentaKills', 0] },
|
||||
},
|
||||
lastFetchedAt: { $min: '$fetchedAt' },
|
||||
},
|
||||
},
|
||||
{ $sort: { games: -1, wins: -1, championId: 1 } },
|
||||
])
|
||||
.read('secondaryPreferred')
|
||||
.exec();
|
||||
}
|
||||
|
||||
private findParticipant(
|
||||
match: RiotMatchDto,
|
||||
puuid: string,
|
||||
@@ -760,6 +847,24 @@ export class MatchHistoryService {
|
||||
return oldest.toISOString();
|
||||
}
|
||||
|
||||
private resolveAggregateLastFetchedAt(
|
||||
rows: ChampionAggregateRow[],
|
||||
context: HistoryContext,
|
||||
): string | null {
|
||||
if (rows.length === 0) {
|
||||
return context.account.recentStatsFetchedAt?.toISOString() ?? null;
|
||||
}
|
||||
const oldest = rows
|
||||
.map((row) => row.lastFetchedAt)
|
||||
.filter((value): value is Date => value instanceof Date)
|
||||
.reduce<Date | null>(
|
||||
(acc, value) =>
|
||||
!acc || value.getTime() < acc.getTime() ? value : acc,
|
||||
null,
|
||||
);
|
||||
return oldest?.toISOString() ?? null;
|
||||
}
|
||||
|
||||
private clampLimit(value: number | undefined, fallback: number, max: number): number {
|
||||
if (!value || !Number.isFinite(value)) return fallback;
|
||||
return Math.max(1, Math.min(Math.floor(value), max));
|
||||
|
||||
Reference in new issue
Block a user