Wire baseline computation jobs

This commit is contained in:
dahyeon committed 2026-07-03 01:19:45 -07:00
1 parent 3ef86b0b66
commit 2935a526c7
8 files changed
+418 -4

No files matched your search

@@ -7,11 +7,16 @@ import type {
RiotMatch,
} from '../global/database/schemas';
import { RiotNotFoundError, type RiotApiClient } from '../riot/riot-api.client';
import type { MetricBaselineService } from './metric-baseline.service';
import type {
BaselineComputationJobData,
FeatureRecomputeJobData,
UserAnalysisJobData,
} from './analysis.types';
import { FEATURE_RECOMPUTE_JOB_NAME } from './analysis.constants';
import {
BASELINE_COMPUTATION_JOB_NAME,
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';
@@ -235,6 +240,18 @@ function createFeatureRecomputeJobData(
};
}
function createBaselineComputationJobData(
overrides: Partial<BaselineComputationJobData> = {},
): BaselineComputationJobData {
return {
platformRoute: 'kr',
queueId: 420,
cohort: 'MASTER_PLUS',
patch: 'ALL',
...overrides,
};
}
function suppressLoggerError() {
return jest
.spyOn(Logger.prototype, 'error')
@@ -243,6 +260,7 @@ function suppressLoggerError() {
interface PipelineHarnessOptions {
accountError?: Error;
computeMetricBaselines?: jest.Mock;
existingMatchRaw?: Record<string, unknown> | null;
existingTimeline?: Record<string, unknown>;
findOneAccountResult?: Account | null;
@@ -341,6 +359,15 @@ function createPipelineHarness(options: PipelineHarnessOptions = {}) {
const getMatchIds = jest
.fn<Promise<string[]>, [string, Record<string, unknown>]>()
.mockResolvedValue(options.matchIds ?? ['KR_1']);
const computeMetricBaselines =
options.computeMetricBaselines ??
jest.fn().mockResolvedValue({
baselinesWritten: 3,
cohort: 'MASTER_PLUS',
computedAt: new Date('2026-07-03T12:00:00.000Z'),
patch: 'ALL',
samplesRead: 30,
});
const riotApiClient = {
getAccountByRiotId,
getSummonerByPuuid: jest.fn().mockResolvedValue({
@@ -357,6 +384,7 @@ function createPipelineHarness(options: PipelineHarnessOptions = {}) {
return {
findOneAccount,
findOneRiotMatch,
computeMetricBaselines,
getMatchIds,
getMatch,
getMatchTimeline,
@@ -367,6 +395,9 @@ function createPipelineHarness(options: PipelineHarnessOptions = {}) {
new ProfileBuilderService(),
new TimelineDerivedService(),
new TimelineMetricService(),
{
computeMetricBaselines,
} as unknown as MetricBaselineService,
),
updateAnalysisJob,
updatePlayerMatchFeature,
@@ -630,6 +661,42 @@ describe('AnalysisPipelineService', () => {
expect(updatePlayerProfile).not.toHaveBeenCalled();
});
it('computes metric baselines and completes the baseline job', async () => {
const { computeMetricBaselines, service, updateAnalysisJob } =
createPipelineHarness();
await service.processBaselineComputationJob(
createBaselineComputationJobData(),
'baseline-job-1',
);
expect(computeMetricBaselines).toHaveBeenCalledWith({
cohort: 'MASTER_PLUS',
patch: 'ALL',
});
const processingUpdate = updateAnalysisJob.mock.calls.find(
([, update]) =>
asRecord(asRecord(update).$set).jobType ===
BASELINE_COMPUTATION_JOB_NAME &&
asRecord(asRecord(update).$set).status === 'processing',
);
expect(processingUpdate).toBeDefined();
const completedUpdate = updateAnalysisJob.mock.calls.find(
([, update]) =>
asRecord(asRecord(update).$set).jobType ===
BASELINE_COMPUTATION_JOB_NAME &&
asRecord(asRecord(update).$set).status === 'completed',
);
expect(completedUpdate).toBeDefined();
expect(asRecord(asRecord(completedUpdate?.[1]).$set)).toMatchObject({
progress: {
fetchedMatches: 30,
totalMatches: 3,
},
stage: 'completed',
});
});
it('completes without building a profile when no ranked matches are returned', async () => {
const { service, updateAnalysisJob, updatePlayerProfile } =
createPipelineHarness({