Add timeline vector metrics

This commit is contained in:
dahyeon committed 2026-07-03 00:12:13 -07:00
1 parent 77ba93b94a
commit a69e86f2bc
13 files changed
+1543 -37

No files matched your search

@@ -0,0 +1,342 @@
import { VECTOR_METRICS } from '@persona-lenz/shared';
import type { ExtractedPlayerMatchFeature } from './analysis.types';
import { TimelineMetricService } from './timeline-metric.service';
function createBaseFeature(): ExtractedPlayerMatchFeature {
return {
matchId: 'KR_1',
puuid: 'target-puuid',
queueId: 420,
role: 'MIDDLE',
championId: 103,
gameCreation: 1_700_000_000_000,
gameDurationSec: 1_800,
win: true,
patch: '26.13',
features: {
kda: 3,
},
normalized: {
kda: 0.5,
},
vector: [],
featureSchemaVersion: 2,
normalizationMethod: 'registry-baseline',
confidence: 1,
timelineProcessed: false,
laneOpponent: {
puuid: 'enemy-mid-puuid',
championId: 134,
},
};
}
function createRawMatch() {
return {
metadata: {
matchId: 'KR_1',
},
info: {
participants: [
{
participantId: 1,
puuid: 'target-puuid',
teamId: 100,
teamPosition: 'MIDDLE',
},
{
participantId: 2,
puuid: 'ally-jungle-puuid',
teamId: 100,
teamPosition: 'JUNGLE',
},
{
participantId: 3,
puuid: 'ally-top-puuid',
teamId: 100,
teamPosition: 'TOP',
},
{
participantId: 6,
puuid: 'enemy-mid-puuid',
teamId: 200,
teamPosition: 'MIDDLE',
},
{
participantId: 7,
puuid: 'enemy-jungle-puuid',
teamId: 200,
teamPosition: 'JUNGLE',
},
],
},
};
}
function participantFrame(overrides: Record<string, unknown>) {
return {
jungleMinionsKilled: 0,
minionsKilled: 0,
position: { x: 0, y: 0 },
timeEnemySpentControlled: 0,
totalGold: 0,
xp: 0,
...overrides,
};
}
function createTimeline() {
return {
info: {
frames: [
{
timestamp: 8 * 60_000,
events: [],
participantFrames: {
'1': participantFrame({ position: { x: 4_000, y: 4_000 } }),
'2': participantFrame({ position: { x: 5_000, y: 4_000 } }),
'3': participantFrame({ position: { x: 10_000, y: 4_000 } }),
},
},
{
timestamp: 10 * 60_000,
events: [
{
type: 'CHAMPION_KILL',
timestamp: 100_000,
killerId: 1,
victimId: 6,
assistingParticipantIds: [2],
},
{
type: 'CHAMPION_KILL',
timestamp: 200_000,
killerId: 2,
victimId: 7,
assistingParticipantIds: [1],
},
{
type: 'CHAMPION_KILL',
timestamp: 300_000,
killerId: 2,
victimId: 7,
assistingParticipantIds: null,
victimDamageReceived: [{ participantId: 1 }],
},
{
type: 'CHAMPION_KILL',
timestamp: 400_000,
killerId: 6,
victimId: 1,
assistingParticipantIds: [],
},
{
type: 'WARD_PLACED',
timestamp: 500_000,
creatorId: 1,
},
],
participantFrames: {
'1': participantFrame({
jungleMinionsKilled: 5,
minionsKilled: 75,
position: { x: 4_000, y: 4_000 },
timeEnemySpentControlled: 10_000,
totalGold: 4_000,
xp: 5_000,
}),
'2': participantFrame({ position: { x: 4_800, y: 4_000 } }),
'3': participantFrame({ position: { x: 9_000, y: 4_000 } }),
'6': participantFrame({
jungleMinionsKilled: 0,
minionsKilled: 70,
totalGold: 3_500,
xp: 4_500,
}),
},
},
{
timestamp: 14 * 60_000,
events: [
{
type: 'WARD_PLACED',
timestamp: 830_000,
creatorId: 1,
},
],
participantFrames: {
'1': participantFrame({
position: { x: 4_000, y: 4_000 },
timeEnemySpentControlled: 30_000,
totalGold: 6_000,
}),
'2': participantFrame({ position: { x: 5_500, y: 4_000 } }),
'3': participantFrame({ position: { x: 11_000, y: 4_000 } }),
'6': participantFrame({
totalGold: 6_200,
}),
},
},
{
timestamp: 15 * 60_000,
events: [
{
type: 'WARD_PLACED',
timestamp: 850_000,
creatorId: 1,
},
{
type: 'ELITE_MONSTER_KILL',
timestamp: 900_000,
killerId: 2,
killerTeamId: 100,
monsterType: 'DRAGON',
assistingParticipantIds: [1],
},
],
participantFrames: {},
},
{
timestamp: 20 * 60_000,
events: [],
participantFrames: {
'1': participantFrame({
position: { x: 4_000, y: 4_000 },
timeEnemySpentControlled: 60_000,
}),
'2': participantFrame({ position: { x: 7_000, y: 4_000 } }),
'3': participantFrame({ position: { x: 12_000, y: 4_000 } }),
},
},
{
timestamp: 30 * 60_000,
events: [],
participantFrames: {
'1': participantFrame({
timeEnemySpentControlled: 90_000,
}),
},
},
],
},
};
}
describe('TimelineMetricService', () => {
const service = new TimelineMetricService();
it('extracts timeline vector metrics and ratio counts', () => {
const feature = createBaseFeature();
const result = service.extractTimelineMetrics(
feature,
createRawMatch(),
createTimeline(),
{
advantageEvents: [],
deathEvents: [
{
t: 400_000,
victimParticipantId: 1,
x: 4_000,
y: 4_000,
zone: 'middleLane',
shutdownBountyGiven: 300,
nearestAllyDistanceApprox: 1_500,
},
{
t: 1_000_000,
victimParticipantId: 1,
x: 4_000,
y: 4_000,
zone: 'middleLane',
shutdownBountyGiven: 0,
nearestAllyDistanceApprox: 1_500,
},
],
fightClusters: [
{
startTimeMs: 100_000,
endTimeMs: 130_000,
killCount: 3,
participantIds: [1, 2, 6, 7],
openerParticipantIds: [1],
cleanupParticipantIds: [2],
isTeamfight: true,
},
{
startTimeMs: 600_000,
endTimeMs: 640_000,
killCount: 3,
participantIds: [1, 2, 6, 7],
openerParticipantIds: [2],
cleanupParticipantIds: [1],
isTeamfight: true,
},
],
objectiveWindows: [
{
objectiveTimeMs: 900_000,
startTimeMs: 780_000,
endTimeMs: 930_000,
killerParticipantId: 2,
killerTeamId: 100,
monsterType: 'DRAGON',
},
],
roamFrames: [],
},
);
expect(result.features).toMatchObject({
goldDiffAt10: 500,
goldDiffAt14: -200,
csDiffAt10: 10,
xpDiffAt10: 500,
deathsBefore14: 1,
killParticipationBefore14: 1,
earlyWardsPerMin: 2 / 14,
objectivePreWardRateNumerator: 1,
objectivePreWardRateDenominator: 1,
eliteObjectiveParticipationRateNumerator: 1,
eliteObjectiveParticipationRateDenominator: 1,
teamfightFirstEngagementParticipationRateNumerator: 1,
teamfightFirstEngagementParticipationRateDenominator: 2,
teamfightCleanupKillParticipationRateNumerator: 1,
teamfightCleanupKillParticipationRateDenominator: 2,
deathWithShutdownBountyGivenRateNumerator: 1,
deathWithShutdownBountyGivenRateDenominator: 2,
ccTimePerMinute: 3,
});
expect(result.features.isolationTendency).toBeCloseTo(
(1_000 + 800 + 1_500 + 3_000) / 4,
);
expect(result.normalized.objectivePreWardRate).toBe(1);
expect(result.normalized.eliteObjectiveParticipationRate).toBe(1);
expect(result.normalized.teamfightFirstEngagementParticipationRate).toBe(
0.5,
);
expect(result.vector).toHaveLength(VECTOR_METRICS.length);
expect(
result.vector[
VECTOR_METRICS.findIndex((metric) => metric.id === 'goldDiffAt10')
],
).toBeCloseTo(result.normalized.goldDiffAt10);
expect(
result.vector[
VECTOR_METRICS.findIndex(
(metric) => metric.id === 'objectivePreWardRate',
)
],
).toBe(1);
expect(
result.vector[
VECTOR_METRICS.findIndex(
(metric) => metric.id === 'deathWithShutdownBountyGivenRate',
)
],
).toBe(0.5);
expect(result.vector[VECTOR_METRICS.length - 1]).toBe(
result.normalized.ccTimePerMinute,
);
});
});