Store timeline derived data

This commit is contained in:
dahyeon committed 2026-07-02 23:13:27 -07:00
1 parent 897e2092a3
commit 1e2f1695ce
3 files changed
+92 -9

No files matched your search

@@ -7,6 +7,7 @@ import type { UserAnalysisJobData } from './analysis.types';
import { FeatureExtractorService } from './feature-extractor.service'; import { FeatureExtractorService } from './feature-extractor.service';
import { AnalysisPipelineService } from './analysis-pipeline.service'; import { AnalysisPipelineService } from './analysis-pipeline.service';
import { ProfileBuilderService } from './profile-builder.service'; import { ProfileBuilderService } from './profile-builder.service';
import { TimelineDerivedService } from './timeline-derived.service';
function createJobData(): UserAnalysisJobData { function createJobData(): UserAnalysisJobData {
return { return {
@@ -71,7 +72,10 @@ function createMatchPayload(matchId = 'KR_1') {
}; };
} }
function createTimelinePayload(matchId = 'KR_1') { function createTimelinePayload(
matchId = 'KR_1',
events: Record<string, unknown>[] = [],
) {
return { return {
metadata: { metadata: {
matchId, matchId,
@@ -79,7 +83,7 @@ function createTimelinePayload(matchId = 'KR_1') {
}, },
info: { info: {
frameInterval: 60_000, frameInterval: 60_000,
frames: [], frames: events.length > 0 ? [{ timestamp: 0, events }] : [],
}, },
}; };
} }
@@ -250,6 +254,7 @@ function createPipelineHarness(options: PipelineHarnessOptions = {}) {
riotApiClient, riotApiClient,
new FeatureExtractorService(), new FeatureExtractorService(),
new ProfileBuilderService(), new ProfileBuilderService(),
new TimelineDerivedService(),
), ),
updateAnalysisJob, updateAnalysisJob,
updatePlayerMatchFeature, updatePlayerMatchFeature,
@@ -369,6 +374,62 @@ describe('AnalysisPipelineService', () => {
expect(timelineSet.timelineFetchedAt).toBeInstanceOf(Date); 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<Promise<Record<string, unknown>>, [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 () => { it('continues raw match analysis when timeline fetch fails', async () => {
const loggerWarnSpy = jest const loggerWarnSpy = jest
.spyOn(Logger.prototype, 'warn') .spyOn(Logger.prototype, 'warn')
@@ -21,10 +21,12 @@ import type {
} from './analysis.types'; } from './analysis.types';
import { FeatureExtractorService } from './feature-extractor.service'; import { FeatureExtractorService } from './feature-extractor.service';
import { ProfileBuilderService } from './profile-builder.service'; import { ProfileBuilderService } from './profile-builder.service';
import { TimelineDerivedService } from './timeline-derived.service';
interface MatchRecord { interface MatchRecord {
matchId: string; matchId: string;
raw: Record<string, unknown>; raw: Record<string, unknown>;
timeline?: Record<string, unknown>;
} }
interface ParsedMatchSummary { interface ParsedMatchSummary {
@@ -72,6 +74,7 @@ export class AnalysisPipelineService {
private readonly riotApiClient: RiotApiClient, private readonly riotApiClient: RiotApiClient,
private readonly featureExtractorService: FeatureExtractorService, private readonly featureExtractorService: FeatureExtractorService,
private readonly profileBuilderService: ProfileBuilderService, private readonly profileBuilderService: ProfileBuilderService,
private readonly timelineDerivedService: TimelineDerivedService,
) {} ) {}
public async processUserAnalysisJob( public async processUserAnalysisJob(
@@ -229,12 +232,16 @@ export class AnalysisPipelineService {
if (existingMatch) { if (existingMatch) {
if (this.isEligibleMatch(existingMatch.raw, data.queueId)) { if (this.isEligibleMatch(existingMatch.raw, data.queueId)) {
await this.fetchAndStoreMissingTimeline( const timeline = await this.fetchAndStoreMissingTimeline(
matchId, matchId,
existingMatch.timeline, existingMatch.timeline,
data.regionalRoute, data.regionalRoute,
); );
matchRecords.push({ matchId, raw: existingMatch.raw }); matchRecords.push({
matchId,
raw: existingMatch.raw,
...(timeline ? { timeline } : {}),
});
} }
} else { } else {
const rawMatch = await this.riotApiClient.getMatch( const rawMatch = await this.riotApiClient.getMatch(
@@ -243,12 +250,16 @@ 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( const timeline = await this.fetchAndStoreMissingTimeline(
matchId, matchId,
undefined, undefined,
data.regionalRoute, data.regionalRoute,
); );
matchRecords.push({ matchId, raw: rawMatch }); matchRecords.push({
matchId,
raw: rawMatch,
...(timeline ? { timeline } : {}),
});
} }
} }
} catch (error) { } catch (error) {
@@ -299,9 +310,9 @@ export class AnalysisPipelineService {
matchId: string, matchId: string,
existingTimeline: unknown, existingTimeline: unknown,
regionalRoute: UserAnalysisJobData['regionalRoute'], regionalRoute: UserAnalysisJobData['regionalRoute'],
): Promise<void> { ): Promise<Record<string, unknown> | undefined> {
if (isRecordValue(existingTimeline)) { if (isRecordValue(existingTimeline)) {
return; return existingTimeline;
} }
try { try {
@@ -310,10 +321,12 @@ export class AnalysisPipelineService {
regionalRoute, regionalRoute,
); );
await this.storeRiotMatchTimeline(matchId, timeline); await this.storeRiotMatchTimeline(matchId, timeline);
return timeline;
} catch (error) { } catch (error) {
this.logger.warn( this.logger.warn(
`Skipping timeline fetch for matchId="${this.safeLogText(matchId)}": ${this.safeLogText(errorMessage(error))}`, `Skipping timeline fetch for matchId="${this.safeLogText(matchId)}": ${this.safeLogText(errorMessage(error))}`,
); );
return undefined;
} }
} }
@@ -352,6 +365,14 @@ export class AnalysisPipelineService {
puuid, puuid,
); );
if (feature.queueId !== queueId) continue; if (feature.queueId !== queueId) continue;
if (matchRecord.timeline) {
feature.derived = {
...this.timelineDerivedService.buildDerivedData(
matchRecord.timeline,
),
};
feature.timelineProcessed = true;
}
await playerMatchFeatureModel await playerMatchFeatureModel
.updateOne( .updateOne(
@@ -30,8 +30,9 @@ export interface ExtractedPlayerMatchFeature {
featureSchemaVersion: number; featureSchemaVersion: number;
normalizationMethod: 'registry-baseline'; normalizationMethod: 'registry-baseline';
confidence: number; confidence: number;
timelineProcessed: false; timelineProcessed: boolean;
laneOpponent?: LaneOpponentRef; laneOpponent?: LaneOpponentRef;
derived?: Record<string, unknown>;
} }
export interface BuiltPlayerProfile { export interface BuiltPlayerProfile {