perf: optimize user internal history reads

This commit is contained in:
dahyeon committed 2026-06-26 05:56:04 -07:00
1 parent e80313e459
commit d6e0bbf0e8
4 files changed
+293 -44

No files matched your search

@@ -24,7 +24,11 @@ import type {
TeamInsightsResponse,
UserInternalHistoryResponse,
} from './community-stats.types';
import { MatchStatsRepository } from './match-stats.repository';
import {
MatchStatsDetailFragment,
MatchStatsRepository,
MatchStatsUserRecordFragment,
} from './match-stats.repository';
import { MatchStatsService } from './match-stats.service';
import {
InternalGameDetail,
@@ -55,6 +59,10 @@ interface BaseDataset {
@Injectable()
export class CommunityStatsService {
private readonly logger = new Logger(CommunityStatsService.name);
private readonly userInternalHistoryInFlight = new Map<
string,
Promise<UserInternalHistoryResponse>
>();
constructor(
private readonly connectionService: ConnectionService,
@@ -293,7 +301,7 @@ export class CommunityStatsService {
filters: CommunityStatsFilters;
}): Promise<UserInternalHistoryResponse> {
const key = this.cacheKey(
`userInternal:v2:${params.userId.toString()}`,
`userInternal:v3:${params.userId.toString()}`,
params.communityId,
params.filters,
);
@@ -301,38 +309,20 @@ export class CommunityStatsService {
await this.cacheService.get<UserInternalHistoryResponse>(key);
if (cached) return cached;
const dataset = await this.buildBaseDataset(
params.communityId,
params.filters,
);
const allUserRecords = dataset.records
.filter((record) => record.userId === params.userId.toString())
.sort((left, right) => right.gameCreation - left.gameCreation);
const records = this.applyChampionFilter(allUserRecords, params.filters);
const limit = this.clampLimit(params.filters.limit, 30, 50);
const cursor = params.filters.cursor;
const page = records
.filter((record) => !cursor || record.gameCreation < cursor)
.slice(0, limit + 1);
const visible = page.slice(0, limit);
const response: UserInternalHistoryResponse = {
season: dataset.season,
championStats: this.buildUserChampionStats(
allUserRecords,
dataset.season,
),
summary: this.buildUserSummary(records, visible.length || limit),
matches: visible.map((record) => this.toRecentMatchView(record, dataset)),
nextCursor:
page.length > limit
? (visible[visible.length - 1]?.gameCreation ?? null)
: null,
lastFetchedAt: records[0]
? new Date(records[0].gameCreation).toISOString()
: null,
};
await this.cacheService.set(key, response);
return response;
// Auction viewers can all request the same next player's internal history
// at once. Coalesce local cache misses so one player change does one DB read.
const inFlight = this.userInternalHistoryInFlight.get(key);
if (inFlight) return inFlight;
const promise = this.buildUserInternalHistoryResponse(params, key);
this.userInternalHistoryInFlight.set(key, promise);
try {
return await promise;
} finally {
if (this.userInternalHistoryInFlight.get(key) === promise) {
this.userInternalHistoryInFlight.delete(key);
}
}
}
public invalidateCommunity(
@@ -397,6 +387,120 @@ export class CommunityStatsService {
};
}
private async buildUserInternalHistoryResponse(
params: {
communityId: mongoose.Types.ObjectId;
userId: mongoose.Types.ObjectId;
filters: CommunityStatsFilters;
},
cacheKey: string,
): Promise<UserInternalHistoryResponse> {
const context = await this.resolveContext(
params.communityId,
params.filters,
);
const userId = params.userId.toString();
const fragments = await this.matchStatsRepository.findUserRecordFragments({
communityId: params.communityId,
userId: params.userId,
fromGameCreation: context.from?.getTime(),
toGameCreation: context.to?.getTime(),
limit: MAX_MATCH_SCAN,
});
const allUserRecords = this.extractUserRecords(
fragments,
userId,
context,
);
const records = this.applyChampionFilter(allUserRecords, params.filters);
const limit = this.clampLimit(params.filters.limit, 30, 50);
const cursor = params.filters.cursor;
const page = records
.filter((record) => !cursor || record.gameCreation < cursor)
.slice(0, limit + 1);
const visible = page.slice(0, limit);
const detailsByMatchId = await this.loadVisibleDetails(
params.communityId,
visible,
context,
);
const dataset: BaseDataset = {
season: context.season,
matches: [],
records: allUserRecords,
detailsByMatchId,
};
const response: UserInternalHistoryResponse = {
season: context.season,
championStats: this.buildUserChampionStats(
allUserRecords,
context.season,
),
summary: this.buildUserSummary(records, visible.length || limit),
matches: visible.map((record) => this.toRecentMatchView(record, dataset)),
nextCursor:
page.length > limit
? (visible[visible.length - 1]?.gameCreation ?? null)
: null,
lastFetchedAt: records[0]
? new Date(records[0].gameCreation).toISOString()
: null,
};
await this.cacheService.set(cacheKey, response);
return response;
}
private extractUserRecords(
fragments: MatchStatsUserRecordFragment[],
userId: string,
context: CommunityStatsContext,
): InternalRecord[] {
return fragments
.flatMap((fragment) =>
(fragment.records as InternalRecord[]).filter(
(record) =>
record.userId === userId &&
this.isGameInRange(record.gameCreation, context),
),
)
.sort((left, right) => right.gameCreation - left.gameCreation);
}
private async loadVisibleDetails(
communityId: mongoose.Types.ObjectId,
visible: InternalRecord[],
context: CommunityStatsContext,
): Promise<Map<string, InternalGameDetail[]>> {
const matchIds = [
...new Set(
visible
.map((record) => record.matchId)
.filter((matchId) => mongoose.isValidObjectId(matchId)),
),
].map((matchId) => new mongoose.Types.ObjectId(matchId));
const fragments = await this.matchStatsRepository.findDetailsByMatches({
communityId,
matchIds,
});
return this.toDetailsByMatchId(fragments, context);
}
private toDetailsByMatchId(
fragments: MatchStatsDetailFragment[],
context: CommunityStatsContext,
): Map<string, InternalGameDetail[]> {
const detailsByMatchId = new Map<string, InternalGameDetail[]>();
for (const fragment of fragments) {
detailsByMatchId.set(
fragment.matchId.toString(),
(fragment.gameDetails as InternalGameDetail[]).filter((game) =>
this.isGameInRange(game.gameCreation, context),
),
);
}
return detailsByMatchId;
}
private async rebuildMissingOrStaleFragments(
communityId: mongoose.Types.ObjectId,
): Promise<number> {