Collect match timelines during analysis

This commit is contained in:
dahyeon committed 2026-07-02 22:38:24 -07:00
1 parent 304f157b91
commit 43662a86d8
2 files changed
+161 -7

No files matched your search

@@ -71,6 +71,19 @@ function createMatchPayload(matchId = 'KR_1') {
}; };
} }
function createTimelinePayload(matchId = 'KR_1') {
return {
metadata: {
matchId,
participants: ['target-puuid', 'ally-puuid'],
},
info: {
frameInterval: 60_000,
frames: [],
},
};
}
interface ExecQuery<T> { interface ExecQuery<T> {
exec: jest.Mock<Promise<T>, []>; exec: jest.Mock<Promise<T>, []>;
} }
@@ -88,6 +101,14 @@ function asRecord(value: unknown): Record<string, unknown> {
return {}; return {};
} }
function findTimelineSet(updateRiotMatch: UpdateOneMock) {
const timelineUpdate = updateRiotMatch.mock.calls.find(([, update]) =>
Boolean(asRecord(asRecord(update).$set).timeline),
);
expect(timelineUpdate).toBeDefined();
return asRecord(asRecord(timelineUpdate?.[1]).$set);
}
function execResolved<T>(value: T): ExecQuery<T> { function execResolved<T>(value: T): ExecQuery<T> {
return { exec: jest.fn<Promise<T>, []>().mockResolvedValue(value) }; return { exec: jest.fn<Promise<T>, []>().mockResolvedValue(value) };
} }
@@ -124,7 +145,12 @@ function suppressLoggerError() {
interface PipelineHarnessOptions { interface PipelineHarnessOptions {
accountError?: Error; accountError?: Error;
existingMatchRaw?: Record<string, unknown> | null; existingMatchRaw?: Record<string, unknown> | null;
existingTimeline?: Record<string, unknown>;
getMatch?: jest.Mock<Promise<Record<string, unknown>>, [string, string]>; getMatch?: jest.Mock<Promise<Record<string, unknown>>, [string, string]>;
getMatchTimeline?: jest.Mock<
Promise<Record<string, unknown>>,
[string, string]
>;
matchIds?: string[]; matchIds?: string[];
storedAccount?: Account; storedAccount?: Account;
} }
@@ -141,11 +167,14 @@ function createPipelineHarness(options: PipelineHarnessOptions = {}) {
const updateAccount: UpdateOneMock = jest const updateAccount: UpdateOneMock = jest
.fn<ExecQuery<AcknowledgedUpdate>, Parameters<UpdateOneMock>>() .fn<ExecQuery<AcknowledgedUpdate>, Parameters<UpdateOneMock>>()
.mockReturnValue(execResolved({ acknowledged: true })); .mockReturnValue(execResolved({ acknowledged: true }));
const findOneRiotMatch = jest const findOneRiotMatch = jest.fn().mockReturnValue(
.fn()
.mockReturnValue(
leanExecResolved( leanExecResolved(
options.existingMatchRaw ? { raw: options.existingMatchRaw } : null, options.existingMatchRaw
? {
raw: options.existingMatchRaw,
timeline: options.existingTimeline,
}
: null,
), ),
); );
const updateRiotMatch: UpdateOneMock = jest const updateRiotMatch: UpdateOneMock = jest
@@ -194,6 +223,11 @@ function createPipelineHarness(options: PipelineHarnessOptions = {}) {
jest jest
.fn<Promise<Record<string, unknown>>, [string, string]>() .fn<Promise<Record<string, unknown>>, [string, string]>()
.mockResolvedValue(createMatchPayload()); .mockResolvedValue(createMatchPayload());
const getMatchTimeline =
options.getMatchTimeline ??
jest
.fn<Promise<Record<string, unknown>>, [string, string]>()
.mockResolvedValue(createTimelinePayload());
const riotApiClient = { const riotApiClient = {
getAccountByRiotId, getAccountByRiotId,
getSummonerByPuuid: jest.fn().mockResolvedValue({ getSummonerByPuuid: jest.fn().mockResolvedValue({
@@ -204,11 +238,13 @@ function createPipelineHarness(options: PipelineHarnessOptions = {}) {
getLeagueEntriesByPuuid: jest.fn().mockResolvedValue([]), getLeagueEntriesByPuuid: jest.fn().mockResolvedValue([]),
getMatchIds: jest.fn().mockResolvedValue(options.matchIds ?? ['KR_1']), getMatchIds: jest.fn().mockResolvedValue(options.matchIds ?? ['KR_1']),
getMatch, getMatch,
getMatchTimeline,
} as unknown as RiotApiClient; } as unknown as RiotApiClient;
return { return {
findOneRiotMatch, findOneRiotMatch,
getMatch, getMatch,
getMatchTimeline,
service: new AnalysisPipelineService( service: new AnalysisPipelineService(
connectionService, connectionService,
riotApiClient, riotApiClient,
@@ -227,6 +263,7 @@ describe('AnalysisPipelineService', () => {
const { const {
findOneRiotMatch, findOneRiotMatch,
getMatch, getMatch,
getMatchTimeline,
service, service,
updateAnalysisJob, updateAnalysisJob,
updatePlayerMatchFeature, updatePlayerMatchFeature,
@@ -238,6 +275,7 @@ describe('AnalysisPipelineService', () => {
expect(findOneRiotMatch).toHaveBeenCalledWith({ matchId: 'KR_1' }); expect(findOneRiotMatch).toHaveBeenCalledWith({ matchId: 'KR_1' });
expect(getMatch).toHaveBeenCalledWith('KR_1', 'asia'); expect(getMatch).toHaveBeenCalledWith('KR_1', 'asia');
expect(getMatchTimeline).toHaveBeenCalledWith('KR_1', 'asia');
const riotMatchUpdate = updateRiotMatch.mock.calls[0][1]; const riotMatchUpdate = updateRiotMatch.mock.calls[0][1];
expect(riotMatchUpdate).toMatchObject({ expect(riotMatchUpdate).toMatchObject({
@@ -246,6 +284,9 @@ describe('AnalysisPipelineService', () => {
queueId: 420, queueId: 420,
}, },
}); });
const timelineSet = findTimelineSet(updateRiotMatch);
expect(timelineSet.timeline).toEqual(createTimelinePayload());
expect(timelineSet.timelineFetchedAt).toBeInstanceOf(Date);
const playerMatchFeatureUpdate = updatePlayerMatchFeature.mock.calls[0][1]; const playerMatchFeatureUpdate = updatePlayerMatchFeature.mock.calls[0][1];
expect(playerMatchFeatureUpdate).toMatchObject({ expect(playerMatchFeatureUpdate).toMatchObject({
@@ -290,7 +331,31 @@ describe('AnalysisPipelineService', () => {
}); });
it('uses cached match details without recalling Riot Match-V5', async () => { it('uses cached match details without recalling Riot Match-V5', async () => {
const { getMatch, service, updatePlayerMatchFeature } = const {
getMatch,
getMatchTimeline,
service,
updatePlayerMatchFeature,
updateRiotMatch,
} = createPipelineHarness({
existingMatchRaw: createMatchPayload(),
existingTimeline: createTimelinePayload(),
});
await service.processUserAnalysisJob(createJobData(), 'analysis-job-1');
expect(getMatch).not.toHaveBeenCalled();
expect(getMatchTimeline).not.toHaveBeenCalled();
expect(
updateRiotMatch.mock.calls.find(([, update]) =>
Boolean(asRecord(asRecord(update).$set).timeline),
),
).toBeUndefined();
expect(updatePlayerMatchFeature).toHaveBeenCalledTimes(1);
});
it('fetches a missing timeline for cached match details', async () => {
const { getMatch, getMatchTimeline, service, updateRiotMatch } =
createPipelineHarness({ createPipelineHarness({
existingMatchRaw: createMatchPayload(), existingMatchRaw: createMatchPayload(),
}); });
@@ -298,7 +363,30 @@ describe('AnalysisPipelineService', () => {
await service.processUserAnalysisJob(createJobData(), 'analysis-job-1'); await service.processUserAnalysisJob(createJobData(), 'analysis-job-1');
expect(getMatch).not.toHaveBeenCalled(); expect(getMatch).not.toHaveBeenCalled();
expect(updatePlayerMatchFeature).toHaveBeenCalledTimes(1); expect(getMatchTimeline).toHaveBeenCalledWith('KR_1', 'asia');
const timelineSet = findTimelineSet(updateRiotMatch);
expect(timelineSet.timeline).toEqual(createTimelinePayload());
expect(timelineSet.timelineFetchedAt).toBeInstanceOf(Date);
});
it('continues raw match analysis when timeline fetch fails', async () => {
const loggerWarnSpy = jest
.spyOn(Logger.prototype, 'warn')
.mockImplementation(() => undefined);
const getMatchTimeline = jest
.fn<Promise<Record<string, unknown>>, [string, string]>()
.mockRejectedValue(new Error('timeline upstream failure'));
const { service, updatePlayerProfile } = createPipelineHarness({
getMatchTimeline,
});
await service.processUserAnalysisJob(createJobData(), 'analysis-job-1');
expect(updatePlayerProfile).toHaveBeenCalled();
expect(loggerWarnSpy).toHaveBeenCalledWith(
expect.stringContaining('Skipping timeline fetch'),
);
loggerWarnSpy.mockRestore();
}); });
it('skips individual match fetch failures and still builds from successful matches', async () => { it('skips individual match fetch failures and still builds from successful matches', async () => {
@@ -306,7 +394,12 @@ describe('AnalysisPipelineService', () => {
.fn<Promise<Record<string, unknown>>, [string, string]>() .fn<Promise<Record<string, unknown>>, [string, string]>()
.mockRejectedValueOnce(new Error('temporary upstream failure')) .mockRejectedValueOnce(new Error('temporary upstream failure'))
.mockResolvedValueOnce(createMatchPayload('KR_2')); .mockResolvedValueOnce(createMatchPayload('KR_2'));
const { service, updatePlayerProfile } = createPipelineHarness({ const {
getMatchTimeline,
service,
updateAnalysisJob,
updatePlayerProfile,
} = createPipelineHarness({
getMatch, getMatch,
matchIds: ['KR_1', 'KR_2'], matchIds: ['KR_1', 'KR_2'],
}); });
@@ -314,12 +407,18 @@ describe('AnalysisPipelineService', () => {
await service.processUserAnalysisJob(createJobData(), 'analysis-job-1'); await service.processUserAnalysisJob(createJobData(), 'analysis-job-1');
expect(getMatch).toHaveBeenCalledTimes(2); expect(getMatch).toHaveBeenCalledTimes(2);
expect(getMatchTimeline).toHaveBeenCalledTimes(1);
expect(getMatchTimeline).toHaveBeenCalledWith('KR_2', 'asia');
const playerProfileUpdate = updatePlayerProfile.mock.calls[0][1]; const playerProfileUpdate = updatePlayerProfile.mock.calls[0][1];
expect(playerProfileUpdate).toMatchObject({ expect(playerProfileUpdate).toMatchObject({
$set: { $set: {
sampleGameCount: 1, sampleGameCount: 1,
}, },
}); });
const completedJobUpdate = updateAnalysisJob.mock.calls.find(
([, update]) => asRecord(asRecord(update).$set).status === 'completed',
);
expect(completedJobUpdate).toBeDefined();
}); });
it('does not mark the job failed before BullMQ exhausts retries', async () => { it('does not mark the job failed before BullMQ exhausts retries', async () => {
@@ -42,6 +42,10 @@ function asRecord(value: unknown): Record<string, unknown> {
return {}; return {};
} }
function isRecordValue(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
function asNumber(value: unknown, fallback = 0): number { function asNumber(value: unknown, fallback = 0): number {
return typeof value === 'number' && Number.isFinite(value) ? value : fallback; return typeof value === 'number' && Number.isFinite(value) ? value : fallback;
} }
@@ -225,6 +229,11 @@ export class AnalysisPipelineService {
if (existingMatch) { if (existingMatch) {
if (this.isEligibleMatch(existingMatch.raw, data.queueId)) { if (this.isEligibleMatch(existingMatch.raw, data.queueId)) {
await this.fetchAndStoreMissingTimeline(
matchId,
existingMatch.timeline,
data.regionalRoute,
);
matchRecords.push({ matchId, raw: existingMatch.raw }); matchRecords.push({ matchId, raw: existingMatch.raw });
} }
} else { } else {
@@ -234,6 +243,11 @@ export class AnalysisPipelineService {
); );
await this.storeRiotMatch(matchId, rawMatch); await this.storeRiotMatch(matchId, rawMatch);
if (this.isEligibleMatch(rawMatch, data.queueId)) { if (this.isEligibleMatch(rawMatch, data.queueId)) {
await this.fetchAndStoreMissingTimeline(
matchId,
undefined,
data.regionalRoute,
);
matchRecords.push({ matchId, raw: rawMatch }); matchRecords.push({ matchId, raw: rawMatch });
} }
} }
@@ -281,6 +295,47 @@ export class AnalysisPipelineService {
.exec(); .exec();
} }
private async fetchAndStoreMissingTimeline(
matchId: string,
existingTimeline: unknown,
regionalRoute: UserAnalysisJobData['regionalRoute'],
): Promise<void> {
if (isRecordValue(existingTimeline)) {
return;
}
try {
const timeline = await this.riotApiClient.getMatchTimeline(
matchId,
regionalRoute,
);
await this.storeRiotMatchTimeline(matchId, timeline);
} catch (error) {
this.logger.warn(
`Skipping timeline fetch for matchId="${this.safeLogText(matchId)}": ${this.safeLogText(errorMessage(error))}`,
);
}
}
private async storeRiotMatchTimeline(
matchId: string,
timeline: Record<string, unknown>,
): Promise<void> {
const riotMatchModel = await this.connectionService.getModel('riotMatch');
await riotMatchModel
.updateOne(
{ matchId },
{
$set: {
timeline,
timelineFetchedAt: new Date(),
},
},
)
.exec();
}
private async extractAndStoreFeatures( private async extractAndStoreFeatures(
matchRecords: MatchRecord[], matchRecords: MatchRecord[],
puuid: string, puuid: string,