diff --git a/apps/api/src/modules/community-stats/community-stats.service.spec.ts b/apps/api/src/modules/community-stats/community-stats.service.spec.ts index 4c340dd4..5de46644 100644 --- a/apps/api/src/modules/community-stats/community-stats.service.spec.ts +++ b/apps/api/src/modules/community-stats/community-stats.service.spec.ts @@ -205,24 +205,36 @@ describe('CommunityStatsService', () => { get: jest.fn().mockResolvedValue(null), set: jest.fn().mockResolvedValue(undefined), } as unknown as jest.Mocked; + const matchStatsRepository = { + findUserRecordFragments: jest + .fn() + .mockResolvedValue([{ matchId, records: [participant] }]), + findDetailsByMatches: jest + .fn() + .mockResolvedValue([{ matchId, gameDetails: [] }]), + } as unknown as jest.Mocked; const service = new CommunityStatsService( {} as ConnectionService, cacheService, - {} as MatchStatsRepository, + matchStatsRepository, {} as MatchStatsService, ); - (service as unknown as { buildBaseDataset: jest.Mock }).buildBaseDataset = + (service as unknown as { resolveContext: jest.Mock }).resolveContext = jest.fn().mockResolvedValue({ + community: {}, season: { id: 'all', label: '전체', - startTime: null, - endTime: null, + startedAt: null, + endedAt: null, }, - matches: [], - records: [participant], - detailsByMatchId: new Map(), + from: null, + to: null, }); + const buildBaseDataset = jest.spyOn( + service as unknown as { buildBaseDataset: jest.Mock }, + 'buildBaseDataset', + ); const response = await service.getUserInternalHistory({ communityId, @@ -236,9 +248,85 @@ describe('CommunityStatsService', () => { gameNumber: 2, queueLabel: '내전', }); + expect(matchStatsRepository.findUserRecordFragments).toHaveBeenCalledWith({ + communityId, + userId, + fromGameCreation: undefined, + toGameCreation: undefined, + limit: 300, + }); + expect( + matchStatsRepository.findDetailsByMatches.mock.calls[0][0].matchIds.map( + (id) => id.toString(), + ), + ).toEqual([matchId.toString()]); + expect(buildBaseDataset).not.toHaveBeenCalled(); expect(cacheService.set).toHaveBeenCalledWith( - expect.stringContaining(`userInternal:v2:${userId.toString()}`), + expect.stringContaining(`userInternal:v3:${userId.toString()}`), response, ); }); + + it('coalesces concurrent user internal history cache misses', async () => { + const communityId = new mongoose.Types.ObjectId(); + const matchId = new mongoose.Types.ObjectId(); + const userId = new mongoose.Types.ObjectId(); + const participant = record(matchId); + participant.userId = userId.toString(); + + let resolveFragments: + | ((fragments: Array<{ matchId: mongoose.Types.ObjectId; records: InternalRecord[] }>) => void) + | undefined; + const fragmentsPromise = new Promise< + Array<{ matchId: mongoose.Types.ObjectId; records: InternalRecord[] }> + >((resolve) => { + resolveFragments = resolve; + }); + const cacheService = { + get: jest.fn().mockResolvedValue(null), + set: jest.fn().mockResolvedValue(undefined), + } as unknown as jest.Mocked; + const matchStatsRepository = { + findUserRecordFragments: jest.fn().mockReturnValue(fragmentsPromise), + findDetailsByMatches: jest.fn().mockResolvedValue([]), + } as unknown as jest.Mocked; + const service = new CommunityStatsService( + {} as ConnectionService, + cacheService, + matchStatsRepository, + {} as MatchStatsService, + ); + (service as unknown as { resolveContext: jest.Mock }).resolveContext = + jest.fn().mockResolvedValue({ + community: {}, + season: { + id: 'all', + label: '전체', + startedAt: null, + endedAt: null, + }, + from: null, + to: null, + }); + + const first = service.getUserInternalHistory({ + communityId, + userId, + filters: { limit: 5 }, + }); + const second = service.getUserInternalHistory({ + communityId, + userId, + filters: { limit: 5 }, + }); + await Promise.resolve(); + resolveFragments?.([{ matchId, records: [participant] }]); + const [firstResponse, secondResponse] = await Promise.all([first, second]); + + expect(firstResponse).toBe(secondResponse); + expect(matchStatsRepository.findUserRecordFragments).toHaveBeenCalledTimes( + 1, + ); + expect(cacheService.set).toHaveBeenCalledTimes(1); + }); }); diff --git a/apps/api/src/modules/community-stats/community-stats.service.ts b/apps/api/src/modules/community-stats/community-stats.service.ts index 9244dee8..aa843f7e 100644 --- a/apps/api/src/modules/community-stats/community-stats.service.ts +++ b/apps/api/src/modules/community-stats/community-stats.service.ts @@ -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 + >(); constructor( private readonly connectionService: ConnectionService, @@ -293,7 +301,7 @@ export class CommunityStatsService { filters: CommunityStatsFilters; }): Promise { 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(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 { + 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> { + 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 { + const detailsByMatchId = new Map(); + 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 { diff --git a/apps/api/src/modules/community-stats/match-stats.repository.ts b/apps/api/src/modules/community-stats/match-stats.repository.ts index 21b35acf..e625bc73 100644 --- a/apps/api/src/modules/community-stats/match-stats.repository.ts +++ b/apps/api/src/modules/community-stats/match-stats.repository.ts @@ -3,6 +3,16 @@ import mongoose from 'mongoose'; import { ConnectionService } from '../global/database/connection.service'; import type { MatchStats, MatchStatsDocument } from '../global/database/schema'; +export type MatchStatsUserRecordFragment = Pick< + MatchStats, + 'matchId' | 'records' +>; + +export type MatchStatsDetailFragment = Pick< + MatchStats, + 'matchId' | 'gameDetails' +>; + @Injectable() export class MatchStatsRepository { constructor(private readonly connectionService: ConnectionService) {} @@ -68,6 +78,53 @@ export class MatchStatsRepository { toGameCreation?: number; }): Promise { const MatchStatsModel = await this.connectionService.getModel('matchStats'); + const filter = this.buildUserFilter(params); + + return MatchStatsModel.find(filter).sort({ scheduledAt: -1 }).exec(); + } + + public async findUserRecordFragments(params: { + communityId: mongoose.Types.ObjectId; + userId: mongoose.Types.ObjectId; + fromGameCreation?: number; + toGameCreation?: number; + limit?: number; + }): Promise { + const MatchStatsModel = await this.connectionService.getModel('matchStats'); + const filter = this.buildUserFilter(params); + + // User history only needs records for summaries/page selection; gameDetails + // are much larger and are fetched separately for the visible page. + return MatchStatsModel.find(filter) + .sort({ scheduledAt: -1 }) + .limit(params.limit ?? 300) + .select({ _id: 0, matchId: 1, records: 1 }) + .lean() + .exec(); + } + + public async findDetailsByMatches(params: { + communityId: mongoose.Types.ObjectId; + matchIds: mongoose.Types.ObjectId[]; + }): Promise { + if (params.matchIds.length === 0) return []; + + const MatchStatsModel = await this.connectionService.getModel('matchStats'); + return MatchStatsModel.find({ + communityId: params.communityId, + matchId: { $in: params.matchIds }, + }) + .select({ _id: 0, matchId: 1, gameDetails: 1 }) + .lean() + .exec(); + } + + private buildUserFilter(params: { + communityId: mongoose.Types.ObjectId; + userId: mongoose.Types.ObjectId; + fromGameCreation?: number; + toGameCreation?: number; + }): Record { const filter: Record = { communityId: params.communityId, userIds: params.userId, @@ -78,7 +135,6 @@ export class MatchStatsRepository { if (params.toGameCreation !== undefined) { filter.gameCreationMin = { $lte: params.toGameCreation }; } - - return MatchStatsModel.find(filter).sort({ scheduledAt: -1 }).exec(); + return filter; } } diff --git a/apps/api/src/modules/global/database/schema.ts b/apps/api/src/modules/global/database/schema.ts index 30dc1160..ead05193 100644 --- a/apps/api/src/modules/global/database/schema.ts +++ b/apps/api/src/modules/global/database/schema.ts @@ -2371,6 +2371,7 @@ MatchStatsSchema.index({ gameCreationMax: 1, }); MatchStatsSchema.index({ communityId: 1, userIds: 1 }); +MatchStatsSchema.index({ communityId: 1, userIds: 1, scheduledAt: -1 }); MatchStatsSchema.index({ communityId: 1, championIds: 1 }); export const tournamentModelDefinitions: ModelDefinition[] = [