Add feature recompute jobs

This commit is contained in:
dahyeon committed 2026-07-03 00:45:04 -07:00
1 parent 1a7a1cca68
commit 8668234065
8 files changed
+1176 -36

No files matched your search

@@ -1,9 +1,17 @@
import { Logger } from '@nestjs/common';
import { FEATURE_SCHEMA_VERSION, VECTOR_METRICS } from '@persona-lenz/shared';
import type { ConnectionService } from '../global/database/connection.service';
import type { Account } from '../global/database/schemas';
import type {
Account,
PlayerMatchFeature,
RiotMatch,
} from '../global/database/schemas';
import { RiotNotFoundError, type RiotApiClient } from '../riot/riot-api.client';
import type { UserAnalysisJobData } from './analysis.types';
import type {
FeatureRecomputeJobData,
UserAnalysisJobData,
} from './analysis.types';
import { FEATURE_RECOMPUTE_JOB_NAME } from './analysis.constants';
import { FeatureExtractorService } from './feature-extractor.service';
import { AnalysisPipelineService } from './analysis-pipeline.service';
import { ProfileBuilderService } from './profile-builder.service';
@@ -159,6 +167,20 @@ function leanExecResolved<T>(value: T) {
};
}
function findManyChain<T>(value: T) {
const exec = jest.fn<Promise<T>, []>().mockResolvedValue(value);
const lean = jest.fn(() => ({ exec }));
const limit = jest.fn(() => ({ lean }));
const sort = jest.fn(() => ({ limit }));
return {
chain: { sort },
exec,
lean,
limit,
sort,
};
}
function createStoredAccount(overrides: Partial<Account> = {}): Account {
return {
createdAt: new Date('2026-07-01T00:00:00.000Z'),
@@ -176,6 +198,43 @@ function createStoredAccount(overrides: Partial<Account> = {}): Account {
};
}
function createStoredFeature(
overrides: Partial<PlayerMatchFeature> = {},
): PlayerMatchFeature {
return {
createdAt: new Date('2026-07-01T00:00:00.000Z'),
updatedAt: new Date('2026-07-01T00:00:00.000Z'),
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: 7 },
normalized: { kda: 1 },
vector: [],
featureSchemaVersion: 1,
normalizationMethod: 'registry-baseline',
confidence: 1,
timelineProcessed: false,
...overrides,
};
}
function createFeatureRecomputeJobData(
overrides: Partial<FeatureRecomputeJobData> = {},
): FeatureRecomputeJobData {
return {
platformRoute: 'kr',
queueId: 420,
batchSize: 100,
...overrides,
};
}
function suppressLoggerError() {
return jest
.spyOn(Logger.prototype, 'error')
@@ -300,6 +359,113 @@ function createPipelineHarness(options: PipelineHarnessOptions = {}) {
};
}
interface FeatureRecomputeHarnessOptions {
account?: Account | null;
profileFeatures?: PlayerMatchFeature[];
remainingTargets?: PlayerMatchFeature[];
scanError?: Error;
storedMatch?: Pick<RiotMatch, 'raw' | 'timeline'> | null;
targetFeatures?: PlayerMatchFeature[];
}
function createFeatureRecomputeHarness(
options: FeatureRecomputeHarnessOptions = {},
) {
const scanChain = findManyChain(
options.targetFeatures ?? [createStoredFeature()],
);
if (options.scanError) {
scanChain.exec.mockRejectedValueOnce(options.scanError);
}
const remainingTargetsChain = findManyChain(options.remainingTargets ?? []);
const profileFeaturesChain = findManyChain(
options.profileFeatures ?? [
createStoredFeature({
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
timelineProcessed: true,
}),
],
);
const findPlayerMatchFeatures = jest
.fn()
.mockReturnValueOnce(scanChain.chain)
.mockReturnValueOnce(remainingTargetsChain.chain)
.mockReturnValueOnce(profileFeaturesChain.chain);
const updatePlayerMatchFeature: UpdateOneMock = jest
.fn<ExecQuery<AcknowledgedUpdate>, Parameters<UpdateOneMock>>()
.mockReturnValue(execResolved({ acknowledged: true }));
const updatePlayerProfile: UpdateOneMock = jest
.fn<ExecQuery<AcknowledgedUpdate>, Parameters<UpdateOneMock>>()
.mockReturnValue(execResolved({ acknowledged: true }));
const updateAnalysisJob: UpdateOneMock = jest
.fn<ExecQuery<AcknowledgedUpdate>, Parameters<UpdateOneMock>>()
.mockReturnValue(execResolved({ acknowledged: true }));
const updateAccount: UpdateOneMock = jest
.fn<ExecQuery<AcknowledgedUpdate>, Parameters<UpdateOneMock>>()
.mockReturnValue(execResolved({ acknowledged: true }));
const findOneAccount = jest
.fn()
.mockReturnValue(
leanExecResolved(
options.account === undefined ? createStoredAccount() : options.account,
),
);
const storedMatch =
options.storedMatch === undefined
? {
raw: createMatchPayload(),
timeline: createTimelinePayload(),
}
: options.storedMatch;
const findOneRiotMatch = jest
.fn()
.mockReturnValue(leanExecResolved(storedMatch));
const models = {
account: {
findOne: findOneAccount,
updateOne: updateAccount,
},
analysisJob: {
updateOne: updateAnalysisJob,
},
playerMatchFeature: {
find: findPlayerMatchFeatures,
updateOne: updatePlayerMatchFeature,
},
playerProfile: {
updateOne: updatePlayerProfile,
},
riotMatch: {
findOne: findOneRiotMatch,
},
};
const connectionService = {
getModel: jest.fn((modelKey: keyof typeof models) =>
Promise.resolve(models[modelKey]),
),
} as unknown as ConnectionService;
return {
findOneAccount,
findOneRiotMatch,
findPlayerMatchFeatures,
profileFeaturesChain,
remainingTargetsChain,
scanChain,
service: new AnalysisPipelineService(
connectionService,
{} as RiotApiClient,
new FeatureExtractorService(),
new ProfileBuilderService(),
new TimelineDerivedService(),
new TimelineMetricService(),
),
updateAnalysisJob,
updatePlayerMatchFeature,
updatePlayerProfile,
};
}
describe('AnalysisPipelineService', () => {
it('fetches missing match details, stores features, builds a profile, and completes the job', async () => {
const {
@@ -645,6 +811,276 @@ describe('AnalysisPipelineService', () => {
expect(completedJobUpdate).toBeDefined();
});
it('recomputes stale stored features and rebuilds affected profiles', async () => {
const staleFeature = createStoredFeature({
featureSchemaVersion: 1,
timelineProcessed: false,
});
const currentProfileFeature = createStoredFeature({
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
normalized: { kda: 1 },
vector: VECTOR_METRICS.map((metric) => (metric.id === 'kda' ? 1 : 0)),
timelineProcessed: true,
});
const scanChain = findManyChain([staleFeature]);
const remainingTargetsChain = findManyChain([]);
const profileFeaturesChain = findManyChain([currentProfileFeature]);
const findPlayerMatchFeatures = jest
.fn()
.mockReturnValueOnce(scanChain.chain)
.mockReturnValueOnce(remainingTargetsChain.chain)
.mockReturnValueOnce(profileFeaturesChain.chain);
const updatePlayerMatchFeature: UpdateOneMock = jest
.fn<ExecQuery<AcknowledgedUpdate>, Parameters<UpdateOneMock>>()
.mockReturnValue(execResolved({ acknowledged: true }));
const updatePlayerProfile: UpdateOneMock = jest
.fn<ExecQuery<AcknowledgedUpdate>, Parameters<UpdateOneMock>>()
.mockReturnValue(execResolved({ acknowledged: true }));
const updateAnalysisJob: UpdateOneMock = jest
.fn<ExecQuery<AcknowledgedUpdate>, Parameters<UpdateOneMock>>()
.mockReturnValue(execResolved({ acknowledged: true }));
const updateAccount: UpdateOneMock = jest
.fn<ExecQuery<AcknowledgedUpdate>, Parameters<UpdateOneMock>>()
.mockReturnValue(execResolved({ acknowledged: true }));
const findOneAccount = jest
.fn()
.mockReturnValue(leanExecResolved(createStoredAccount()));
const findOneRiotMatch = jest.fn().mockReturnValue(
leanExecResolved({
matchId: 'KR_1',
raw: createMatchPayload(),
timeline: createTimelinePayload(),
}),
);
const models = {
account: {
findOne: findOneAccount,
updateOne: updateAccount,
},
analysisJob: {
updateOne: updateAnalysisJob,
},
playerMatchFeature: {
find: findPlayerMatchFeatures,
updateOne: updatePlayerMatchFeature,
},
playerProfile: {
updateOne: updatePlayerProfile,
},
riotMatch: {
findOne: findOneRiotMatch,
},
};
const connectionService = {
getModel: jest.fn((modelKey: keyof typeof models) =>
Promise.resolve(models[modelKey]),
),
} as unknown as ConnectionService;
const service = new AnalysisPipelineService(
connectionService,
{} as RiotApiClient,
new FeatureExtractorService(),
new ProfileBuilderService(),
new TimelineDerivedService(),
new TimelineMetricService(),
);
await service.processFeatureRecomputeJob(
createFeatureRecomputeJobData(),
'recompute-job-1',
);
expect(findPlayerMatchFeatures).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
queueId: 420,
$or: [
{ featureSchemaVersion: { $lt: FEATURE_SCHEMA_VERSION } },
{
timelineProcessed: false,
'derived.timelineUnavailableForFeatureSchemaVersion': {
$ne: FEATURE_SCHEMA_VERSION,
},
},
],
}),
);
expect(scanChain.sort).toHaveBeenCalledWith({ gameCreation: -1 });
expect(scanChain.limit).toHaveBeenCalledWith(100);
expect(findOneRiotMatch).toHaveBeenCalledWith({ matchId: 'KR_1' });
const recomputedFeatureSet = asRecord(
asRecord(updatePlayerMatchFeature.mock.calls[0][1]).$set,
);
expect(recomputedFeatureSet).toMatchObject({
matchId: 'KR_1',
puuid: 'target-puuid',
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
timelineProcessed: true,
});
expect(findPlayerMatchFeatures).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
puuid: 'target-puuid',
queueId: 420,
$or: [
{ featureSchemaVersion: { $lt: FEATURE_SCHEMA_VERSION } },
{
timelineProcessed: false,
'derived.timelineUnavailableForFeatureSchemaVersion': {
$ne: FEATURE_SCHEMA_VERSION,
},
},
],
}),
);
expect(remainingTargetsChain.limit).toHaveBeenCalledWith(1);
expect(findPlayerMatchFeatures).toHaveBeenNthCalledWith(3, {
puuid: 'target-puuid',
queueId: 420,
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
});
expect(profileFeaturesChain.limit).toHaveBeenCalledWith(50);
const playerProfileSet = asRecord(
asRecord(updatePlayerProfile.mock.calls[0][1]).$set,
);
expect(playerProfileSet).toMatchObject({
puuid: 'target-puuid',
queueId: 420,
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
sampleGameCount: 1,
});
const completedJobUpdate = updateAnalysisJob.mock.calls.find(
([, update]) => asRecord(asRecord(update).$set).status === 'completed',
);
expect(asRecord(asRecord(completedJobUpdate?.[1]).$set)).toMatchObject({
jobType: FEATURE_RECOMPUTE_JOB_NAME,
status: 'completed',
stage: 'completed',
});
});
it('marks timeline-less recomputed features as current but missing timeline coverage', async () => {
const targetFeature = createStoredFeature({
featureSchemaVersion: 1,
timelineProcessed: false,
});
const currentProfileFeature = createStoredFeature({
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
normalized: { kda: 1 },
vector: VECTOR_METRICS.map((metric) => (metric.id === 'kda' ? 1 : 0)),
timelineProcessed: false,
});
const { service, updatePlayerMatchFeature, updatePlayerProfile } =
createFeatureRecomputeHarness({
profileFeatures: [currentProfileFeature],
storedMatch: {
raw: createMatchPayload(),
},
targetFeatures: [targetFeature],
});
await service.processFeatureRecomputeJob(
createFeatureRecomputeJobData(),
'recompute-job-1',
);
const recomputedFeatureSet = asRecord(
asRecord(updatePlayerMatchFeature.mock.calls[0][1]).$set,
);
expect(recomputedFeatureSet).toMatchObject({
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
timelineProcessed: false,
derived: {
timelineUnavailableForFeatureSchemaVersion: FEATURE_SCHEMA_VERSION,
},
});
expect(asRecord(recomputedFeatureSet.normalized).objectivePreWardRate).toBe(
undefined,
);
const playerProfileSet = asRecord(
asRecord(updatePlayerProfile.mock.calls[0][1]).$set,
);
expect(playerProfileSet.sampleGameCount).toBe(1);
expect(playerProfileSet.confidence).toBeCloseTo(0.14);
});
it('does not publish a partial profile while stale features remain for that player', async () => {
const { findOneAccount, service, updatePlayerProfile } =
createFeatureRecomputeHarness({
remainingTargets: [
createStoredFeature({
matchId: 'KR_2',
featureSchemaVersion: 1,
timelineProcessed: false,
}),
],
targetFeatures: [
createStoredFeature({
matchId: 'KR_1',
featureSchemaVersion: 1,
timelineProcessed: false,
}),
],
});
await service.processFeatureRecomputeJob(
createFeatureRecomputeJobData(),
'recompute-job-1',
);
expect(updatePlayerProfile).not.toHaveBeenCalled();
expect(findOneAccount).not.toHaveBeenCalled();
});
it('does not mark feature recompute jobs failed before BullMQ exhausts retries', async () => {
const { service, updateAnalysisJob } = createFeatureRecomputeHarness({
scanError: new Error('database unavailable'),
});
await expect(
service.processFeatureRecomputeJob(
createFeatureRecomputeJobData(),
'recompute-job-1',
{ isFinalAttempt: false },
),
).rejects.toThrow('database unavailable');
const failedJobUpdate = updateAnalysisJob.mock.calls.find(
([, update]) => asRecord(asRecord(update).$set).status === 'failed',
);
expect(failedJobUpdate).toBeUndefined();
});
it('marks feature recompute jobs failed with a public error on the final attempt', async () => {
const loggerErrorSpy = suppressLoggerError();
const { service, updateAnalysisJob } = createFeatureRecomputeHarness({
scanError: new Error('database unavailable'),
});
await expect(
service.processFeatureRecomputeJob(
createFeatureRecomputeJobData({ puuid: 'target-puuid' }),
'recompute-job-1',
{ isFinalAttempt: true },
),
).rejects.toThrow('database unavailable');
const failedJobUpdate = updateAnalysisJob.mock.calls.find(
([, update]) => asRecord(asRecord(update).$set).status === 'failed',
);
expect(asRecord(asRecord(failedJobUpdate?.[1]).$set)).toMatchObject({
jobType: FEATURE_RECOMPUTE_JOB_NAME,
status: 'failed',
error: 'Analysis failed during processing.',
});
expect(loggerErrorSpy).toHaveBeenCalledWith(
expect.stringContaining('Feature recompute job failed'),
expect.any(String),
);
expect(loggerErrorSpy.mock.calls[0][0]).toContain('puuid="target-puuid"');
loggerErrorSpy.mockRestore();
});
it('does not mark the job failed before BullMQ exhausts retries', async () => {
const { service, updateAnalysisJob } = createPipelineHarness({
accountError: new Error('upstream unavailable'),