From 1e2f1695ce94ed9024225e47ef92667b3d7604b9 Mon Sep 17 00:00:00 2001 From: dahyeon Date: Thu, 2 Jul 2026 23:13:27 -0700 Subject: [PATCH] Store timeline derived data --- .../analysis-pipeline.service.spec.ts | 65 ++++++++++++++++++- .../analysis/analysis-pipeline.service.ts | 33 ++++++++-- .../src/modules/analysis/analysis.types.ts | 3 +- 3 files changed, 92 insertions(+), 9 deletions(-) diff --git a/apps/api/src/modules/analysis/analysis-pipeline.service.spec.ts b/apps/api/src/modules/analysis/analysis-pipeline.service.spec.ts index a4a89a0..bb5de04 100644 --- a/apps/api/src/modules/analysis/analysis-pipeline.service.spec.ts +++ b/apps/api/src/modules/analysis/analysis-pipeline.service.spec.ts @@ -7,6 +7,7 @@ import type { UserAnalysisJobData } from './analysis.types'; import { FeatureExtractorService } from './feature-extractor.service'; import { AnalysisPipelineService } from './analysis-pipeline.service'; import { ProfileBuilderService } from './profile-builder.service'; +import { TimelineDerivedService } from './timeline-derived.service'; function createJobData(): UserAnalysisJobData { return { @@ -71,7 +72,10 @@ function createMatchPayload(matchId = 'KR_1') { }; } -function createTimelinePayload(matchId = 'KR_1') { +function createTimelinePayload( + matchId = 'KR_1', + events: Record[] = [], +) { return { metadata: { matchId, @@ -79,7 +83,7 @@ function createTimelinePayload(matchId = 'KR_1') { }, info: { frameInterval: 60_000, - frames: [], + frames: events.length > 0 ? [{ timestamp: 0, events }] : [], }, }; } @@ -250,6 +254,7 @@ function createPipelineHarness(options: PipelineHarnessOptions = {}) { riotApiClient, new FeatureExtractorService(), new ProfileBuilderService(), + new TimelineDerivedService(), ), updateAnalysisJob, updatePlayerMatchFeature, @@ -369,6 +374,62 @@ describe('AnalysisPipelineService', () => { expect(timelineSet.timelineFetchedAt).toBeInstanceOf(Date); }); + it('stores derived timeline data when a timeline is available', async () => { + const timeline = createTimelinePayload('KR_1', [ + { + type: 'CHAMPION_KILL', + timestamp: 10_000, + killerId: 1, + victimId: 6, + assistingParticipantIds: [2], + position: { x: 1_000, y: 1_000 }, + }, + { + type: 'ELITE_MONSTER_KILL', + timestamp: 900_000, + killerId: 4, + killerTeamId: 100, + monsterType: 'DRAGON', + }, + ]); + const getMatchTimeline = jest + .fn>, [string, string]>() + .mockResolvedValue(timeline); + const { service, updatePlayerMatchFeature } = createPipelineHarness({ + getMatchTimeline, + }); + + await service.processUserAnalysisJob(createJobData(), 'analysis-job-1'); + + const playerMatchFeatureSet = asRecord( + asRecord(updatePlayerMatchFeature.mock.calls[0][1]).$set, + ); + expect(playerMatchFeatureSet.timelineProcessed).toBe(true); + expect(playerMatchFeatureSet.derived).toEqual({ + fightClusters: [ + { + startTimeMs: 10_000, + endTimeMs: 10_000, + killCount: 1, + participantIds: [1, 2, 6], + openerParticipantIds: [1, 2], + cleanupParticipantIds: [1, 2, 6], + isTeamfight: false, + }, + ], + objectiveWindows: [ + { + objectiveTimeMs: 900_000, + startTimeMs: 780_000, + endTimeMs: 930_000, + killerParticipantId: 4, + killerTeamId: 100, + monsterType: 'DRAGON', + }, + ], + }); + }); + it('continues raw match analysis when timeline fetch fails', async () => { const loggerWarnSpy = jest .spyOn(Logger.prototype, 'warn') diff --git a/apps/api/src/modules/analysis/analysis-pipeline.service.ts b/apps/api/src/modules/analysis/analysis-pipeline.service.ts index 9e290d9..1e63844 100644 --- a/apps/api/src/modules/analysis/analysis-pipeline.service.ts +++ b/apps/api/src/modules/analysis/analysis-pipeline.service.ts @@ -21,10 +21,12 @@ import type { } from './analysis.types'; import { FeatureExtractorService } from './feature-extractor.service'; import { ProfileBuilderService } from './profile-builder.service'; +import { TimelineDerivedService } from './timeline-derived.service'; interface MatchRecord { matchId: string; raw: Record; + timeline?: Record; } interface ParsedMatchSummary { @@ -72,6 +74,7 @@ export class AnalysisPipelineService { private readonly riotApiClient: RiotApiClient, private readonly featureExtractorService: FeatureExtractorService, private readonly profileBuilderService: ProfileBuilderService, + private readonly timelineDerivedService: TimelineDerivedService, ) {} public async processUserAnalysisJob( @@ -229,12 +232,16 @@ export class AnalysisPipelineService { if (existingMatch) { if (this.isEligibleMatch(existingMatch.raw, data.queueId)) { - await this.fetchAndStoreMissingTimeline( + const timeline = await this.fetchAndStoreMissingTimeline( matchId, existingMatch.timeline, data.regionalRoute, ); - matchRecords.push({ matchId, raw: existingMatch.raw }); + matchRecords.push({ + matchId, + raw: existingMatch.raw, + ...(timeline ? { timeline } : {}), + }); } } else { const rawMatch = await this.riotApiClient.getMatch( @@ -243,12 +250,16 @@ export class AnalysisPipelineService { ); await this.storeRiotMatch(matchId, rawMatch); if (this.isEligibleMatch(rawMatch, data.queueId)) { - await this.fetchAndStoreMissingTimeline( + const timeline = await this.fetchAndStoreMissingTimeline( matchId, undefined, data.regionalRoute, ); - matchRecords.push({ matchId, raw: rawMatch }); + matchRecords.push({ + matchId, + raw: rawMatch, + ...(timeline ? { timeline } : {}), + }); } } } catch (error) { @@ -299,9 +310,9 @@ export class AnalysisPipelineService { matchId: string, existingTimeline: unknown, regionalRoute: UserAnalysisJobData['regionalRoute'], - ): Promise { + ): Promise | undefined> { if (isRecordValue(existingTimeline)) { - return; + return existingTimeline; } try { @@ -310,10 +321,12 @@ export class AnalysisPipelineService { regionalRoute, ); await this.storeRiotMatchTimeline(matchId, timeline); + return timeline; } catch (error) { this.logger.warn( `Skipping timeline fetch for matchId="${this.safeLogText(matchId)}": ${this.safeLogText(errorMessage(error))}`, ); + return undefined; } } @@ -352,6 +365,14 @@ export class AnalysisPipelineService { puuid, ); if (feature.queueId !== queueId) continue; + if (matchRecord.timeline) { + feature.derived = { + ...this.timelineDerivedService.buildDerivedData( + matchRecord.timeline, + ), + }; + feature.timelineProcessed = true; + } await playerMatchFeatureModel .updateOne( diff --git a/apps/api/src/modules/analysis/analysis.types.ts b/apps/api/src/modules/analysis/analysis.types.ts index b4c96c1..7ae8c40 100644 --- a/apps/api/src/modules/analysis/analysis.types.ts +++ b/apps/api/src/modules/analysis/analysis.types.ts @@ -30,8 +30,9 @@ export interface ExtractedPlayerMatchFeature { featureSchemaVersion: number; normalizationMethod: 'registry-baseline'; confidence: number; - timelineProcessed: false; + timelineProcessed: boolean; laneOpponent?: LaneOpponentRef; + derived?: Record; } export interface BuiltPlayerProfile {