Add feature recompute jobs
This commit is contained in:
1 parent
1a7a1cca68
commit
8668234065
8 files changed
+1171
-31
No files matched your search
@@ -1,9 +1,17 @@
|
|||||||
import { Logger } from '@nestjs/common';
|
import { Logger } from '@nestjs/common';
|
||||||
import { FEATURE_SCHEMA_VERSION, VECTOR_METRICS } from '@persona-lenz/shared';
|
import { FEATURE_SCHEMA_VERSION, VECTOR_METRICS } from '@persona-lenz/shared';
|
||||||
import type { ConnectionService } from '../global/database/connection.service';
|
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 { 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 { 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';
|
||||||
@@ -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 {
|
function createStoredAccount(overrides: Partial<Account> = {}): Account {
|
||||||
return {
|
return {
|
||||||
createdAt: new Date('2026-07-01T00:00:00.000Z'),
|
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() {
|
function suppressLoggerError() {
|
||||||
return jest
|
return jest
|
||||||
.spyOn(Logger.prototype, 'error')
|
.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', () => {
|
describe('AnalysisPipelineService', () => {
|
||||||
it('fetches missing match details, stores features, builds a profile, and completes the job', async () => {
|
it('fetches missing match details, stores features, builds a profile, and completes the job', async () => {
|
||||||
const {
|
const {
|
||||||
@@ -645,6 +811,276 @@ describe('AnalysisPipelineService', () => {
|
|||||||
expect(completedJobUpdate).toBeDefined();
|
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 () => {
|
it('does not mark the job failed before BullMQ exhausts retries', async () => {
|
||||||
const { service, updateAnalysisJob } = createPipelineHarness({
|
const { service, updateAnalysisJob } = createPipelineHarness({
|
||||||
accountError: new Error('upstream unavailable'),
|
accountError: new Error('upstream unavailable'),
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
|
import { FEATURE_SCHEMA_VERSION } from '@persona-lenz/shared';
|
||||||
import { ConnectionService } from '../global/database/connection.service';
|
import { ConnectionService } from '../global/database/connection.service';
|
||||||
import type {
|
import type {
|
||||||
Account,
|
Account,
|
||||||
AnalysisJobStatus,
|
AnalysisJobStatus,
|
||||||
|
PlayerMatchFeature,
|
||||||
RiotMatch,
|
RiotMatch,
|
||||||
TierInfo,
|
TierInfo,
|
||||||
} from '../global/database/schemas';
|
} from '../global/database/schemas';
|
||||||
@@ -12,11 +14,15 @@ import {
|
|||||||
type RiotLeagueEntryDto,
|
type RiotLeagueEntryDto,
|
||||||
} from '../riot/riot-api.client';
|
} from '../riot/riot-api.client';
|
||||||
import {
|
import {
|
||||||
|
FEATURE_RECOMPUTE_JOB_NAME,
|
||||||
|
MAX_ANALYSIS_MATCH_COUNT,
|
||||||
|
normalizeFeatureRecomputeBatchSize,
|
||||||
normalizeRequestedMatchCount,
|
normalizeRequestedMatchCount,
|
||||||
USER_ANALYSIS_JOB_NAME,
|
USER_ANALYSIS_JOB_NAME,
|
||||||
} from './analysis.constants';
|
} from './analysis.constants';
|
||||||
import type {
|
import type {
|
||||||
ExtractedPlayerMatchFeature,
|
ExtractedPlayerMatchFeature,
|
||||||
|
FeatureRecomputeJobData,
|
||||||
UserAnalysisJobData,
|
UserAnalysisJobData,
|
||||||
} from './analysis.types';
|
} from './analysis.types';
|
||||||
import { FeatureExtractorService } from './feature-extractor.service';
|
import { FeatureExtractorService } from './feature-extractor.service';
|
||||||
@@ -38,6 +44,10 @@ interface ParsedMatchSummary {
|
|||||||
participantPuuids: string[];
|
participantPuuids: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const TIMELINE_UNAVAILABLE_FEATURE_SCHEMA_VERSION_FIELD =
|
||||||
|
'timelineUnavailableForFeatureSchemaVersion';
|
||||||
|
const TIMELINE_UNAVAILABLE_FEATURE_SCHEMA_VERSION_PATH = `derived.${TIMELINE_UNAVAILABLE_FEATURE_SCHEMA_VERSION_FIELD}`;
|
||||||
|
|
||||||
function asRecord(value: unknown): Record<string, unknown> {
|
function asRecord(value: unknown): Record<string, unknown> {
|
||||||
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
||||||
return value as Record<string, unknown>;
|
return value as Record<string, unknown>;
|
||||||
@@ -173,6 +183,97 @@ export class AnalysisPipelineService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async processFeatureRecomputeJob(
|
||||||
|
data: FeatureRecomputeJobData,
|
||||||
|
jobId: string,
|
||||||
|
options: { isFinalAttempt?: boolean } = {},
|
||||||
|
): Promise<void> {
|
||||||
|
const normalizedData = {
|
||||||
|
...data,
|
||||||
|
batchSize: normalizeFeatureRecomputeBatchSize(data.batchSize),
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.updateJob(
|
||||||
|
jobId,
|
||||||
|
{
|
||||||
|
status: 'processing',
|
||||||
|
stage: 'scan-features',
|
||||||
|
progress: {
|
||||||
|
fetchedMatches: 0,
|
||||||
|
totalMatches: 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
FEATURE_RECOMPUTE_JOB_NAME,
|
||||||
|
);
|
||||||
|
|
||||||
|
const targetFeatures =
|
||||||
|
await this.findFeatureRecomputeTargets(normalizedData);
|
||||||
|
await this.updateJob(
|
||||||
|
jobId,
|
||||||
|
{
|
||||||
|
stage: 'recompute-features',
|
||||||
|
progress: {
|
||||||
|
fetchedMatches: 0,
|
||||||
|
totalMatches: targetFeatures.length,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
FEATURE_RECOMPUTE_JOB_NAME,
|
||||||
|
);
|
||||||
|
|
||||||
|
const affectedPuuids = new Set<string>();
|
||||||
|
for (const [index, targetFeature] of targetFeatures.entries()) {
|
||||||
|
const recomputedFeature =
|
||||||
|
await this.recomputeStoredFeature(targetFeature);
|
||||||
|
if (recomputedFeature) {
|
||||||
|
affectedPuuids.add(recomputedFeature.puuid);
|
||||||
|
}
|
||||||
|
await this.updateJob(
|
||||||
|
jobId,
|
||||||
|
{
|
||||||
|
progress: {
|
||||||
|
fetchedMatches: index + 1,
|
||||||
|
totalMatches: targetFeatures.length,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
FEATURE_RECOMPUTE_JOB_NAME,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.updateJob(
|
||||||
|
jobId,
|
||||||
|
{
|
||||||
|
stage: 'build-profile',
|
||||||
|
},
|
||||||
|
FEATURE_RECOMPUTE_JOB_NAME,
|
||||||
|
);
|
||||||
|
for (const puuid of affectedPuuids) {
|
||||||
|
await this.rebuildStoredProfile(puuid, normalizedData.queueId);
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.completeJob(jobId, FEATURE_RECOMPUTE_JOB_NAME);
|
||||||
|
} catch (error) {
|
||||||
|
if (options.isFinalAttempt === true) {
|
||||||
|
const publicError = this.publicErrorMessage(error);
|
||||||
|
this.logFeatureRecomputeFinalFailure(
|
||||||
|
normalizedData,
|
||||||
|
jobId,
|
||||||
|
publicError,
|
||||||
|
error,
|
||||||
|
);
|
||||||
|
await this.updateJob(
|
||||||
|
jobId,
|
||||||
|
{
|
||||||
|
status: 'failed',
|
||||||
|
error: publicError,
|
||||||
|
},
|
||||||
|
FEATURE_RECOMPUTE_JOB_NAME,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async resolveAndStoreAccount(
|
private async resolveAndStoreAccount(
|
||||||
data: UserAnalysisJobData,
|
data: UserAnalysisJobData,
|
||||||
): Promise<Account> {
|
): Promise<Account> {
|
||||||
@@ -426,32 +527,154 @@ export class AnalysisPipelineService {
|
|||||||
return extractedFeatures;
|
return extractedFeatures;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async findFeatureRecomputeTargets(
|
||||||
|
data: FeatureRecomputeJobData,
|
||||||
|
): Promise<PlayerMatchFeature[]> {
|
||||||
|
const playerMatchFeatureModel =
|
||||||
|
await this.connectionService.getModel('playerMatchFeature');
|
||||||
|
|
||||||
|
return playerMatchFeatureModel
|
||||||
|
.find(
|
||||||
|
this.featureRecomputeTargetQuery({
|
||||||
|
queueId: data.queueId,
|
||||||
|
puuid: data.puuid,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.sort({ gameCreation: -1 })
|
||||||
|
.limit(data.batchSize)
|
||||||
|
.lean<PlayerMatchFeature[]>()
|
||||||
|
.exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async recomputeStoredFeature(
|
||||||
|
targetFeature: PlayerMatchFeature,
|
||||||
|
): Promise<ExtractedPlayerMatchFeature | undefined> {
|
||||||
|
const riotMatchModel = await this.connectionService.getModel('riotMatch');
|
||||||
|
const playerMatchFeatureModel =
|
||||||
|
await this.connectionService.getModel('playerMatchFeature');
|
||||||
|
|
||||||
|
const storedMatch = await riotMatchModel
|
||||||
|
.findOne({ matchId: targetFeature.matchId })
|
||||||
|
.lean<RiotMatch>()
|
||||||
|
.exec();
|
||||||
|
if (!storedMatch) {
|
||||||
|
this.logger.warn(
|
||||||
|
`Skipping feature recompute for matchId="${this.safeLogText(targetFeature.matchId)}": stored match was not found`,
|
||||||
|
);
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const feature = this.featureExtractorService.extractPlayerMatchFeature(
|
||||||
|
storedMatch.raw,
|
||||||
|
targetFeature.puuid,
|
||||||
|
);
|
||||||
|
if (feature.queueId !== targetFeature.queueId) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isTimelinePayload(storedMatch.timeline)) {
|
||||||
|
const derived = this.timelineDerivedService.buildDerivedData(
|
||||||
|
storedMatch.timeline,
|
||||||
|
storedMatch.raw,
|
||||||
|
);
|
||||||
|
const timelineMetrics =
|
||||||
|
this.timelineMetricService.extractTimelineMetrics(
|
||||||
|
feature,
|
||||||
|
storedMatch.raw,
|
||||||
|
storedMatch.timeline,
|
||||||
|
derived,
|
||||||
|
);
|
||||||
|
feature.derived = {
|
||||||
|
...derived,
|
||||||
|
};
|
||||||
|
feature.features = {
|
||||||
|
...feature.features,
|
||||||
|
...timelineMetrics.features,
|
||||||
|
};
|
||||||
|
feature.normalized = {
|
||||||
|
...feature.normalized,
|
||||||
|
...timelineMetrics.normalized,
|
||||||
|
};
|
||||||
|
feature.vector = timelineMetrics.vector;
|
||||||
|
feature.timelineProcessed = true;
|
||||||
|
} else {
|
||||||
|
feature.derived = {
|
||||||
|
[TIMELINE_UNAVAILABLE_FEATURE_SCHEMA_VERSION_FIELD]:
|
||||||
|
FEATURE_SCHEMA_VERSION,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
await playerMatchFeatureModel
|
||||||
|
.updateOne(
|
||||||
|
{ matchId: feature.matchId, puuid: feature.puuid },
|
||||||
|
{ $set: feature },
|
||||||
|
{ upsert: true },
|
||||||
|
)
|
||||||
|
.exec();
|
||||||
|
return feature;
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.warn(
|
||||||
|
`Skipping feature recompute for matchId="${this.safeLogText(targetFeature.matchId)}": ${this.safeLogText(errorMessage(error))}`,
|
||||||
|
);
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async rebuildStoredProfile(
|
||||||
|
puuid: string,
|
||||||
|
queueId: number,
|
||||||
|
): Promise<void> {
|
||||||
|
const playerMatchFeatureModel =
|
||||||
|
await this.connectionService.getModel('playerMatchFeature');
|
||||||
|
const hasRemainingTargets =
|
||||||
|
await this.hasRemainingFeatureRecomputeTargetsForPlayer(puuid, queueId);
|
||||||
|
if (hasRemainingTargets) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const accountModel = await this.connectionService.getModel('account');
|
||||||
|
const account = await accountModel
|
||||||
|
.findOne({ puuid })
|
||||||
|
.lean<Pick<Account, 'isSeed' | 'puuid'> | null>()
|
||||||
|
.exec();
|
||||||
|
const features = await playerMatchFeatureModel
|
||||||
|
.find({
|
||||||
|
puuid,
|
||||||
|
queueId,
|
||||||
|
featureSchemaVersion: FEATURE_SCHEMA_VERSION,
|
||||||
|
})
|
||||||
|
.sort({ gameCreation: -1 })
|
||||||
|
.limit(MAX_ANALYSIS_MATCH_COUNT)
|
||||||
|
.lean<PlayerMatchFeature[]>()
|
||||||
|
.exec();
|
||||||
|
|
||||||
|
if (features.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.storeBuiltProfile({
|
||||||
|
features,
|
||||||
|
isSeed: account?.isSeed ?? false,
|
||||||
|
puuid,
|
||||||
|
queueId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private async buildAndStoreProfile(
|
private async buildAndStoreProfile(
|
||||||
account: Account,
|
account: Account,
|
||||||
data: UserAnalysisJobData,
|
data: UserAnalysisJobData,
|
||||||
extractedFeatures: ExtractedPlayerMatchFeature[],
|
extractedFeatures: ExtractedPlayerMatchFeature[],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const profile = this.profileBuilderService.buildProfile({
|
await this.storeBuiltProfile({
|
||||||
features: extractedFeatures,
|
features: extractedFeatures,
|
||||||
isSeed: account.isSeed,
|
isSeed: account.isSeed,
|
||||||
puuid: account.puuid,
|
puuid: account.puuid,
|
||||||
queueId: data.queueId,
|
queueId: data.queueId,
|
||||||
});
|
});
|
||||||
const playerProfileModel =
|
|
||||||
await this.connectionService.getModel('playerProfile');
|
|
||||||
const accountModel = await this.connectionService.getModel('account');
|
const accountModel = await this.connectionService.getModel('account');
|
||||||
const analyzedAt = new Date();
|
const analyzedAt = new Date();
|
||||||
|
|
||||||
await playerProfileModel
|
|
||||||
.updateOne(
|
|
||||||
{
|
|
||||||
puuid: account.puuid,
|
|
||||||
queueId: data.queueId,
|
|
||||||
},
|
|
||||||
{ $set: profile },
|
|
||||||
{ upsert: true },
|
|
||||||
)
|
|
||||||
.exec();
|
|
||||||
await accountModel
|
await accountModel
|
||||||
.updateOne(
|
.updateOne(
|
||||||
{ puuid: account.puuid },
|
{ puuid: account.puuid },
|
||||||
@@ -464,13 +687,82 @@ export class AnalysisPipelineService {
|
|||||||
.exec();
|
.exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async completeJob(jobId: string): Promise<void> {
|
private async hasRemainingFeatureRecomputeTargetsForPlayer(
|
||||||
await this.updateJob(jobId, {
|
puuid: string,
|
||||||
|
queueId: number,
|
||||||
|
): Promise<boolean> {
|
||||||
|
const playerMatchFeatureModel =
|
||||||
|
await this.connectionService.getModel('playerMatchFeature');
|
||||||
|
const remainingTargets = await playerMatchFeatureModel
|
||||||
|
.find(this.featureRecomputeTargetQuery({ puuid, queueId }))
|
||||||
|
.sort({ gameCreation: -1 })
|
||||||
|
.limit(1)
|
||||||
|
.lean<PlayerMatchFeature[]>()
|
||||||
|
.exec();
|
||||||
|
|
||||||
|
return remainingTargets.length > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private featureRecomputeTargetQuery(params: {
|
||||||
|
puuid?: string;
|
||||||
|
queueId: number;
|
||||||
|
}): Record<string, unknown> {
|
||||||
|
return {
|
||||||
|
...(params.puuid ? { puuid: params.puuid } : {}),
|
||||||
|
queueId: params.queueId,
|
||||||
|
$or: [
|
||||||
|
{ featureSchemaVersion: { $lt: FEATURE_SCHEMA_VERSION } },
|
||||||
|
{
|
||||||
|
timelineProcessed: false,
|
||||||
|
[TIMELINE_UNAVAILABLE_FEATURE_SCHEMA_VERSION_PATH]: {
|
||||||
|
$ne: FEATURE_SCHEMA_VERSION,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private async storeBuiltProfile(params: {
|
||||||
|
features: PlayerMatchFeature[] | ExtractedPlayerMatchFeature[];
|
||||||
|
isSeed: boolean;
|
||||||
|
puuid: string;
|
||||||
|
queueId: number;
|
||||||
|
}): Promise<void> {
|
||||||
|
const profile = this.profileBuilderService.buildProfile({
|
||||||
|
features: params.features,
|
||||||
|
isSeed: params.isSeed,
|
||||||
|
puuid: params.puuid,
|
||||||
|
queueId: params.queueId,
|
||||||
|
});
|
||||||
|
const playerProfileModel =
|
||||||
|
await this.connectionService.getModel('playerProfile');
|
||||||
|
|
||||||
|
await playerProfileModel
|
||||||
|
.updateOne(
|
||||||
|
{
|
||||||
|
puuid: params.puuid,
|
||||||
|
queueId: params.queueId,
|
||||||
|
},
|
||||||
|
{ $set: profile },
|
||||||
|
{ upsert: true },
|
||||||
|
)
|
||||||
|
.exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async completeJob(
|
||||||
|
jobId: string,
|
||||||
|
jobType = USER_ANALYSIS_JOB_NAME,
|
||||||
|
): Promise<void> {
|
||||||
|
await this.updateJob(
|
||||||
|
jobId,
|
||||||
|
{
|
||||||
status: 'completed',
|
status: 'completed',
|
||||||
stage: 'completed',
|
stage: 'completed',
|
||||||
completedAt: new Date(),
|
completedAt: new Date(),
|
||||||
error: undefined,
|
error: undefined,
|
||||||
});
|
},
|
||||||
|
jobType,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async updateJob(
|
private async updateJob(
|
||||||
@@ -483,12 +775,13 @@ export class AnalysisPipelineService {
|
|||||||
stage?: string;
|
stage?: string;
|
||||||
status?: AnalysisJobStatus;
|
status?: AnalysisJobStatus;
|
||||||
},
|
},
|
||||||
|
jobType = USER_ANALYSIS_JOB_NAME,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const analysisJobModel =
|
const analysisJobModel =
|
||||||
await this.connectionService.getModel('analysisJob');
|
await this.connectionService.getModel('analysisJob');
|
||||||
const set: Record<string, unknown> = {
|
const set: Record<string, unknown> = {
|
||||||
jobId,
|
jobId,
|
||||||
jobType: USER_ANALYSIS_JOB_NAME,
|
jobType,
|
||||||
...patch,
|
...patch,
|
||||||
};
|
};
|
||||||
const unset: Record<string, string> = {};
|
const unset: Record<string, string> = {};
|
||||||
@@ -585,6 +878,25 @@ export class AnalysisPipelineService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private logFeatureRecomputeFinalFailure(
|
||||||
|
data: FeatureRecomputeJobData,
|
||||||
|
jobId: string,
|
||||||
|
publicError: string,
|
||||||
|
error: unknown,
|
||||||
|
): void {
|
||||||
|
this.logger.error(
|
||||||
|
[
|
||||||
|
`Feature recompute job failed jobId="${this.safeLogText(jobId)}"`,
|
||||||
|
`puuid="${this.safeLogText(data.puuid ?? '')}"`,
|
||||||
|
`platformRoute="${data.platformRoute}"`,
|
||||||
|
`queueId="${data.queueId}"`,
|
||||||
|
`publicError="${this.safeLogText(publicError)}"`,
|
||||||
|
`cause="${this.safeLogText(errorMessage(error))}"`,
|
||||||
|
].join(' '),
|
||||||
|
error instanceof Error ? error.stack : undefined,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
private safeLogText(value: string): string {
|
private safeLogText(value: string): string {
|
||||||
return value.replace(/[\r\n\t]/gu, ' ').slice(0, 300);
|
return value.replace(/[\r\n\t]/gu, ' ').slice(0, 300);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
import type { AnalysisJob } from '../global/database/schemas';
|
import type { AnalysisJob } from '../global/database/schemas';
|
||||||
import type { ConnectionService } from '../global/database/connection.service';
|
import type { ConnectionService } from '../global/database/connection.service';
|
||||||
import type { UserAnalysisJobData } from './analysis.types';
|
import type { UserAnalysisJobData } from './analysis.types';
|
||||||
import { USER_ANALYSIS_JOB_NAME } from './analysis.constants';
|
import {
|
||||||
|
FEATURE_RECOMPUTE_JOB_NAME,
|
||||||
|
USER_ANALYSIS_JOB_NAME,
|
||||||
|
} from './analysis.constants';
|
||||||
import { AnalysisQueueService } from './analysis-queue.service';
|
import { AnalysisQueueService } from './analysis-queue.service';
|
||||||
|
|
||||||
function createJobData(
|
function createJobData(
|
||||||
@@ -78,6 +81,59 @@ describe('AnalysisQueueService', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('creates a feature recompute job document and enqueues a BullMQ job', async () => {
|
||||||
|
const add = jest.fn().mockResolvedValue({ id: 'job-from-bull' });
|
||||||
|
const create = jest.fn().mockResolvedValue({});
|
||||||
|
const updateOne = jest.fn(() => ({
|
||||||
|
exec: jest.fn().mockResolvedValue({}),
|
||||||
|
}));
|
||||||
|
const analysisJobModel = {
|
||||||
|
create,
|
||||||
|
updateOne,
|
||||||
|
};
|
||||||
|
const connectionService = {
|
||||||
|
getModel: jest.fn().mockResolvedValue(analysisJobModel),
|
||||||
|
} as unknown as ConnectionService;
|
||||||
|
const service = new AnalysisQueueService(
|
||||||
|
{ add } as never,
|
||||||
|
connectionService,
|
||||||
|
() => 'recompute-job-1',
|
||||||
|
);
|
||||||
|
|
||||||
|
const result = await service.enqueueFeatureRecompute({
|
||||||
|
platformRoute: 'kr',
|
||||||
|
queueId: 420,
|
||||||
|
batchSize: 25,
|
||||||
|
puuid: 'target-puuid',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toEqual({ jobId: 'recompute-job-1', status: 'queued' });
|
||||||
|
expect(create).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
jobId: 'recompute-job-1',
|
||||||
|
jobType: FEATURE_RECOMPUTE_JOB_NAME,
|
||||||
|
platformRoute: 'kr',
|
||||||
|
queueId: 420,
|
||||||
|
requestedMatchCount: 0,
|
||||||
|
puuid: 'target-puuid',
|
||||||
|
status: 'queued',
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
expect(add).toHaveBeenCalledWith(
|
||||||
|
FEATURE_RECOMPUTE_JOB_NAME,
|
||||||
|
{
|
||||||
|
platformRoute: 'kr',
|
||||||
|
queueId: 420,
|
||||||
|
batchSize: 25,
|
||||||
|
puuid: 'target-puuid',
|
||||||
|
},
|
||||||
|
expect.objectContaining({
|
||||||
|
attempts: 1,
|
||||||
|
jobId: 'recompute-job-1',
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it('returns an existing active job instead of enqueueing a duplicate', async () => {
|
it('returns an existing active job instead of enqueueing a duplicate', async () => {
|
||||||
const add = jest.fn();
|
const add = jest.fn();
|
||||||
const findOneChain = createFindOneChain({
|
const findOneChain = createFindOneChain({
|
||||||
@@ -143,6 +199,44 @@ describe('AnalysisQueueService', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('marks a pre-created feature recompute job failed when BullMQ enqueue fails', async () => {
|
||||||
|
const add = jest.fn().mockRejectedValue(new Error('redis unavailable'));
|
||||||
|
const create = jest.fn().mockResolvedValue({});
|
||||||
|
const updateOne = jest.fn(() => ({
|
||||||
|
exec: jest.fn().mockResolvedValue({}),
|
||||||
|
}));
|
||||||
|
const analysisJobModel = {
|
||||||
|
create,
|
||||||
|
updateOne,
|
||||||
|
};
|
||||||
|
const connectionService = {
|
||||||
|
getModel: jest.fn().mockResolvedValue(analysisJobModel),
|
||||||
|
} as unknown as ConnectionService;
|
||||||
|
const service = new AnalysisQueueService(
|
||||||
|
{ add } as never,
|
||||||
|
connectionService,
|
||||||
|
() => 'recompute-job-1',
|
||||||
|
);
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
service.enqueueFeatureRecompute({
|
||||||
|
platformRoute: 'kr',
|
||||||
|
queueId: 420,
|
||||||
|
batchSize: 25,
|
||||||
|
}),
|
||||||
|
).rejects.toThrow('redis unavailable');
|
||||||
|
expect(updateOne).toHaveBeenCalledWith(
|
||||||
|
{ jobId: 'recompute-job-1' },
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
status: 'failed',
|
||||||
|
stage: 'enqueue',
|
||||||
|
error: 'Analysis could not be queued.',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it('serializes identical in-process requests before creating a duplicate job', async () => {
|
it('serializes identical in-process requests before creating a duplicate job', async () => {
|
||||||
const add = jest.fn().mockResolvedValue({ id: 'analysis-job-1' });
|
const add = jest.fn().mockResolvedValue({ id: 'analysis-job-1' });
|
||||||
let activeJob: Pick<AnalysisJob, 'jobId' | 'status'> | null = null;
|
let activeJob: Pick<AnalysisJob, 'jobId' | 'status'> | null = null;
|
||||||
|
|||||||
@@ -9,12 +9,20 @@ import {
|
|||||||
ANALYSIS_JOB_ID_FACTORY,
|
ANALYSIS_JOB_ID_FACTORY,
|
||||||
ACTIVE_ANALYSIS_JOB_STATUSES,
|
ACTIVE_ANALYSIS_JOB_STATUSES,
|
||||||
ANALYSIS_QUEUE_NAME,
|
ANALYSIS_QUEUE_NAME,
|
||||||
|
FEATURE_RECOMPUTE_JOB_NAME,
|
||||||
|
normalizeFeatureRecomputeBatchSize,
|
||||||
normalizeRequestedMatchCount,
|
normalizeRequestedMatchCount,
|
||||||
USER_ANALYSIS_JOB_NAME,
|
USER_ANALYSIS_JOB_NAME,
|
||||||
} from './analysis.constants';
|
} from './analysis.constants';
|
||||||
import type { UserAnalysisJobData } from './analysis.types';
|
import type {
|
||||||
|
FeatureRecomputeJobData,
|
||||||
|
UserAnalysisJobData,
|
||||||
|
} from './analysis.types';
|
||||||
|
|
||||||
type JobIdFactory = () => string;
|
type JobIdFactory = () => string;
|
||||||
|
type AnalysisQueueJobData = UserAnalysisJobData | FeatureRecomputeJobData;
|
||||||
|
type AnalysisQueueJobName =
|
||||||
|
typeof USER_ANALYSIS_JOB_NAME | typeof FEATURE_RECOMPUTE_JOB_NAME;
|
||||||
type ActiveAnalysisJob = Pick<AnalysisJob, 'jobId'> & {
|
type ActiveAnalysisJob = Pick<AnalysisJob, 'jobId'> & {
|
||||||
status: Extract<AnalysisJob['status'], 'queued' | 'processing'>;
|
status: Extract<AnalysisJob['status'], 'queued' | 'processing'>;
|
||||||
};
|
};
|
||||||
@@ -24,9 +32,9 @@ export class AnalysisQueueService {
|
|||||||
constructor(
|
constructor(
|
||||||
@InjectQueue(ANALYSIS_QUEUE_NAME)
|
@InjectQueue(ANALYSIS_QUEUE_NAME)
|
||||||
private readonly analysisQueue: Queue<
|
private readonly analysisQueue: Queue<
|
||||||
UserAnalysisJobData,
|
AnalysisQueueJobData,
|
||||||
void,
|
void,
|
||||||
typeof USER_ANALYSIS_JOB_NAME
|
AnalysisQueueJobName
|
||||||
>,
|
>,
|
||||||
private readonly connectionService: ConnectionService,
|
private readonly connectionService: ConnectionService,
|
||||||
@Inject(ANALYSIS_JOB_ID_FACTORY)
|
@Inject(ANALYSIS_JOB_ID_FACTORY)
|
||||||
@@ -57,6 +65,56 @@ export class AnalysisQueueService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async enqueueFeatureRecompute(
|
||||||
|
data: FeatureRecomputeJobData,
|
||||||
|
): Promise<AnalysisRequestAcceptedDto> {
|
||||||
|
const normalizedData = this.normalizeFeatureRecomputeData(data);
|
||||||
|
const analysisJobModel =
|
||||||
|
await this.connectionService.getModel('analysisJob');
|
||||||
|
const jobId = this.jobIdFactory();
|
||||||
|
|
||||||
|
await analysisJobModel.create({
|
||||||
|
jobId,
|
||||||
|
jobType: FEATURE_RECOMPUTE_JOB_NAME,
|
||||||
|
puuid: normalizedData.puuid,
|
||||||
|
platformRoute: normalizedData.platformRoute,
|
||||||
|
queueId: normalizedData.queueId,
|
||||||
|
requestedMatchCount: 0,
|
||||||
|
status: 'queued',
|
||||||
|
stage: 'queued',
|
||||||
|
progress: {
|
||||||
|
fetchedMatches: 0,
|
||||||
|
totalMatches: 0,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.analysisQueue.add(FEATURE_RECOMPUTE_JOB_NAME, normalizedData, {
|
||||||
|
attempts: 1,
|
||||||
|
jobId,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
await analysisJobModel
|
||||||
|
.updateOne(
|
||||||
|
{ jobId },
|
||||||
|
{
|
||||||
|
$set: {
|
||||||
|
status: 'failed',
|
||||||
|
stage: 'enqueue',
|
||||||
|
error: this.publicErrorMessage(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.exec();
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
jobId,
|
||||||
|
status: 'queued',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private readonly pendingDuplicateRequestKeys = new Set<string>();
|
private readonly pendingDuplicateRequestKeys = new Set<string>();
|
||||||
|
|
||||||
private async enqueueUserAnalysisWithoutDuplicateRequestRace(
|
private async enqueueUserAnalysisWithoutDuplicateRequestRace(
|
||||||
@@ -148,6 +206,18 @@ export class AnalysisQueueService {
|
|||||||
return 'Analysis could not be queued.';
|
return 'Analysis could not be queued.';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private normalizeFeatureRecomputeData(
|
||||||
|
data: FeatureRecomputeJobData,
|
||||||
|
): FeatureRecomputeJobData {
|
||||||
|
const puuid = data.puuid?.trim();
|
||||||
|
return {
|
||||||
|
platformRoute: data.platformRoute,
|
||||||
|
queueId: data.queueId,
|
||||||
|
batchSize: normalizeFeatureRecomputeBatchSize(data.batchSize),
|
||||||
|
...(puuid ? { puuid } : {}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private duplicateRequestKey(data: UserAnalysisJobData): string {
|
private duplicateRequestKey(data: UserAnalysisJobData): string {
|
||||||
return [
|
return [
|
||||||
data.platformRoute,
|
data.platformRoute,
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
export const ANALYSIS_QUEUE_NAME = 'analysis';
|
export const ANALYSIS_QUEUE_NAME = 'analysis';
|
||||||
export const USER_ANALYSIS_JOB_NAME = 'user-analysis';
|
export const USER_ANALYSIS_JOB_NAME = 'user-analysis';
|
||||||
|
export const FEATURE_RECOMPUTE_JOB_NAME = 'feature-recompute';
|
||||||
export const ANALYSIS_JOB_ID_FACTORY = Symbol('ANALYSIS_JOB_ID_FACTORY');
|
export const ANALYSIS_JOB_ID_FACTORY = Symbol('ANALYSIS_JOB_ID_FACTORY');
|
||||||
|
|
||||||
export const DEFAULT_ANALYSIS_MATCH_COUNT = 20;
|
export const DEFAULT_ANALYSIS_MATCH_COUNT = 20;
|
||||||
export const MAX_ANALYSIS_MATCH_COUNT = 50;
|
export const MAX_ANALYSIS_MATCH_COUNT = 50;
|
||||||
|
export const DEFAULT_FEATURE_RECOMPUTE_BATCH_SIZE = 100;
|
||||||
|
export const MAX_FEATURE_RECOMPUTE_BATCH_SIZE = 500;
|
||||||
|
|
||||||
export const ACTIVE_ANALYSIS_JOB_STATUSES = ['queued', 'processing'] as const;
|
export const ACTIVE_ANALYSIS_JOB_STATUSES = ['queued', 'processing'] as const;
|
||||||
|
|
||||||
@@ -16,3 +19,16 @@ export function normalizeRequestedMatchCount(
|
|||||||
|
|
||||||
return Math.min(Math.trunc(requestedMatchCount), MAX_ANALYSIS_MATCH_COUNT);
|
return Math.min(Math.trunc(requestedMatchCount), MAX_ANALYSIS_MATCH_COUNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function normalizeFeatureRecomputeBatchSize(batchSize: number): number {
|
||||||
|
if (!Number.isFinite(batchSize) || batchSize <= 0) {
|
||||||
|
return DEFAULT_FEATURE_RECOMPUTE_BATCH_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalizedBatchSize = Math.trunc(batchSize);
|
||||||
|
if (normalizedBatchSize <= 0) {
|
||||||
|
return DEFAULT_FEATURE_RECOMPUTE_BATCH_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.min(normalizedBatchSize, MAX_FEATURE_RECOMPUTE_BATCH_SIZE);
|
||||||
|
}
|
||||||
@@ -1,10 +1,20 @@
|
|||||||
import type { Job } from 'bullmq';
|
import type { Job } from 'bullmq';
|
||||||
import { USER_ANALYSIS_JOB_NAME } from './analysis.constants';
|
import {
|
||||||
import type { UserAnalysisJobData } from './analysis.types';
|
DEFAULT_FEATURE_RECOMPUTE_BATCH_SIZE,
|
||||||
|
FEATURE_RECOMPUTE_JOB_NAME,
|
||||||
|
MAX_FEATURE_RECOMPUTE_BATCH_SIZE,
|
||||||
|
USER_ANALYSIS_JOB_NAME,
|
||||||
|
} from './analysis.constants';
|
||||||
|
import type {
|
||||||
|
FeatureRecomputeJobData,
|
||||||
|
UserAnalysisJobData,
|
||||||
|
} from './analysis.types';
|
||||||
import { AnalysisPipelineService } from './analysis-pipeline.service';
|
import { AnalysisPipelineService } from './analysis-pipeline.service';
|
||||||
import { AnalysisProcessor } from './analysis.processor';
|
import { AnalysisProcessor } from './analysis.processor';
|
||||||
|
|
||||||
function createJobData(): UserAnalysisJobData {
|
function createJobData(
|
||||||
|
overrides: Partial<UserAnalysisJobData> = {},
|
||||||
|
): UserAnalysisJobData {
|
||||||
return {
|
return {
|
||||||
gameName: 'Hide on bush',
|
gameName: 'Hide on bush',
|
||||||
tagLine: 'KR1',
|
tagLine: 'KR1',
|
||||||
@@ -12,6 +22,19 @@ function createJobData(): UserAnalysisJobData {
|
|||||||
regionalRoute: 'asia',
|
regionalRoute: 'asia',
|
||||||
queueId: 420,
|
queueId: 420,
|
||||||
requestedMatchCount: 20,
|
requestedMatchCount: 20,
|
||||||
|
...overrides,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function createFeatureRecomputeJobData(
|
||||||
|
overrides: Partial<FeatureRecomputeJobData> = {},
|
||||||
|
): FeatureRecomputeJobData {
|
||||||
|
return {
|
||||||
|
platformRoute: 'kr',
|
||||||
|
queueId: 420,
|
||||||
|
batchSize: 25,
|
||||||
|
puuid: 'target-puuid',
|
||||||
|
...overrides,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,6 +65,139 @@ describe('AnalysisProcessor', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('delegates feature recompute jobs to the analysis pipeline', async () => {
|
||||||
|
const processFeatureRecomputeJob = jest
|
||||||
|
.fn<
|
||||||
|
Promise<void>,
|
||||||
|
[FeatureRecomputeJobData, string, { isFinalAttempt?: boolean }]
|
||||||
|
>()
|
||||||
|
.mockResolvedValue(undefined);
|
||||||
|
const processor = new AnalysisProcessor({
|
||||||
|
processFeatureRecomputeJob,
|
||||||
|
} as unknown as AnalysisPipelineService);
|
||||||
|
|
||||||
|
await processor.process({
|
||||||
|
attemptsMade: 0,
|
||||||
|
id: 'recompute-job-1',
|
||||||
|
name: FEATURE_RECOMPUTE_JOB_NAME,
|
||||||
|
opts: { attempts: 2 },
|
||||||
|
data: createFeatureRecomputeJobData(),
|
||||||
|
} as Job<unknown>);
|
||||||
|
|
||||||
|
expect(processFeatureRecomputeJob).toHaveBeenCalledWith(
|
||||||
|
createFeatureRecomputeJobData(),
|
||||||
|
'recompute-job-1',
|
||||||
|
{ isFinalAttempt: false },
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('normalizes feature recompute batch size from valid queue payloads', async () => {
|
||||||
|
const processFeatureRecomputeJob = jest.fn().mockResolvedValue(undefined);
|
||||||
|
const processor = new AnalysisProcessor({
|
||||||
|
processFeatureRecomputeJob,
|
||||||
|
} as unknown as AnalysisPipelineService);
|
||||||
|
|
||||||
|
await processor.process({
|
||||||
|
attemptsMade: 0,
|
||||||
|
id: 'recompute-job-1',
|
||||||
|
name: FEATURE_RECOMPUTE_JOB_NAME,
|
||||||
|
opts: { attempts: 2 },
|
||||||
|
data: createFeatureRecomputeJobData({ batchSize: 999 }),
|
||||||
|
} as Job<unknown>);
|
||||||
|
|
||||||
|
expect(processFeatureRecomputeJob).toHaveBeenCalledWith(
|
||||||
|
createFeatureRecomputeJobData({
|
||||||
|
batchSize: MAX_FEATURE_RECOMPUTE_BATCH_SIZE,
|
||||||
|
}),
|
||||||
|
'recompute-job-1',
|
||||||
|
{ isFinalAttempt: false },
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('defaults fractional feature recompute batch sizes below one', async () => {
|
||||||
|
const processFeatureRecomputeJob = jest.fn().mockResolvedValue(undefined);
|
||||||
|
const processor = new AnalysisProcessor({
|
||||||
|
processFeatureRecomputeJob,
|
||||||
|
} as unknown as AnalysisPipelineService);
|
||||||
|
|
||||||
|
await processor.process({
|
||||||
|
attemptsMade: 0,
|
||||||
|
id: 'recompute-job-1',
|
||||||
|
name: FEATURE_RECOMPUTE_JOB_NAME,
|
||||||
|
opts: { attempts: 2 },
|
||||||
|
data: createFeatureRecomputeJobData({ batchSize: 0.5 }),
|
||||||
|
} as Job<unknown>);
|
||||||
|
|
||||||
|
expect(processFeatureRecomputeJob).toHaveBeenCalledWith(
|
||||||
|
createFeatureRecomputeJobData({
|
||||||
|
batchSize: DEFAULT_FEATURE_RECOMPUTE_BATCH_SIZE,
|
||||||
|
}),
|
||||||
|
'recompute-job-1',
|
||||||
|
{ isFinalAttempt: false },
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
{
|
||||||
|
data: null,
|
||||||
|
expectedError: 'Feature recompute job data must be an object.',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: {
|
||||||
|
...createFeatureRecomputeJobData(),
|
||||||
|
platformRoute: 'invalid',
|
||||||
|
},
|
||||||
|
expectedError: 'Analysis job platformRoute is unsupported.',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: {
|
||||||
|
...createFeatureRecomputeJobData(),
|
||||||
|
queueId: 430,
|
||||||
|
},
|
||||||
|
expectedError: 'Analysis job queueId is unsupported.',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: {
|
||||||
|
...createFeatureRecomputeJobData(),
|
||||||
|
batchSize: Number.NaN,
|
||||||
|
},
|
||||||
|
expectedError: 'Feature recompute job batchSize must be finite.',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: {
|
||||||
|
...createFeatureRecomputeJobData(),
|
||||||
|
puuid: ' ',
|
||||||
|
},
|
||||||
|
expectedError: 'Analysis job puuid is out of range.',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
data: {
|
||||||
|
...createFeatureRecomputeJobData(),
|
||||||
|
puuid: 'x'.repeat(129),
|
||||||
|
},
|
||||||
|
expectedError: 'Analysis job puuid is out of range.',
|
||||||
|
},
|
||||||
|
])(
|
||||||
|
'rejects malformed feature recompute payloads',
|
||||||
|
async ({ data, expectedError }) => {
|
||||||
|
const processFeatureRecomputeJob = jest.fn();
|
||||||
|
const processor = new AnalysisProcessor({
|
||||||
|
processFeatureRecomputeJob,
|
||||||
|
} as unknown as AnalysisPipelineService);
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
processor.process({
|
||||||
|
attemptsMade: 0,
|
||||||
|
id: 'recompute-job-1',
|
||||||
|
name: FEATURE_RECOMPUTE_JOB_NAME,
|
||||||
|
opts: { attempts: 2 },
|
||||||
|
data,
|
||||||
|
} as Job<unknown>),
|
||||||
|
).rejects.toThrow(expectedError);
|
||||||
|
expect(processFeatureRecomputeJob).not.toHaveBeenCalled();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
it('rejects jobs without a BullMQ id', async () => {
|
it('rejects jobs without a BullMQ id', async () => {
|
||||||
const processor = new AnalysisProcessor({
|
const processor = new AnalysisProcessor({
|
||||||
processUserAnalysisJob: jest.fn(),
|
processUserAnalysisJob: jest.fn(),
|
||||||
|
|||||||
@@ -2,11 +2,16 @@ import { Processor, WorkerHost } from '@nestjs/bullmq';
|
|||||||
import type { Job } from 'bullmq';
|
import type { Job } from 'bullmq';
|
||||||
import {
|
import {
|
||||||
ANALYSIS_QUEUE_NAME,
|
ANALYSIS_QUEUE_NAME,
|
||||||
|
FEATURE_RECOMPUTE_JOB_NAME,
|
||||||
|
normalizeFeatureRecomputeBatchSize,
|
||||||
normalizeRequestedMatchCount,
|
normalizeRequestedMatchCount,
|
||||||
USER_ANALYSIS_JOB_NAME,
|
USER_ANALYSIS_JOB_NAME,
|
||||||
} from './analysis.constants';
|
} from './analysis.constants';
|
||||||
import { AnalysisPipelineService } from './analysis-pipeline.service';
|
import { AnalysisPipelineService } from './analysis-pipeline.service';
|
||||||
import type { UserAnalysisJobData } from './analysis.types';
|
import type {
|
||||||
|
FeatureRecomputeJobData,
|
||||||
|
UserAnalysisJobData,
|
||||||
|
} from './analysis.types';
|
||||||
import {
|
import {
|
||||||
RIOT_PLATFORM_ROUTES,
|
RIOT_PLATFORM_ROUTES,
|
||||||
RIOT_REGIONAL_ROUTES,
|
RIOT_REGIONAL_ROUTES,
|
||||||
@@ -27,14 +32,27 @@ export class AnalysisProcessor extends WorkerHost {
|
|||||||
if (!job.id) {
|
if (!job.id) {
|
||||||
throw new Error('Analysis job is missing a BullMQ job id.');
|
throw new Error('Analysis job is missing a BullMQ job id.');
|
||||||
}
|
}
|
||||||
if (job.name !== USER_ANALYSIS_JOB_NAME) {
|
if (job.name === USER_ANALYSIS_JOB_NAME) {
|
||||||
throw new Error(`Unsupported analysis job name "${job.name}".`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = this.parseUserAnalysisJobData(job.data);
|
const data = this.parseUserAnalysisJobData(job.data);
|
||||||
await this.analysisPipelineService.processUserAnalysisJob(data, job.id, {
|
await this.analysisPipelineService.processUserAnalysisJob(data, job.id, {
|
||||||
isFinalAttempt: this.isFinalAttempt(job),
|
isFinalAttempt: this.isFinalAttempt(job),
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (job.name === FEATURE_RECOMPUTE_JOB_NAME) {
|
||||||
|
const data = this.parseFeatureRecomputeJobData(job.data);
|
||||||
|
await this.analysisPipelineService.processFeatureRecomputeJob(
|
||||||
|
data,
|
||||||
|
job.id,
|
||||||
|
{
|
||||||
|
isFinalAttempt: this.isFinalAttempt(job),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Unsupported analysis job name "${job.name}".`);
|
||||||
}
|
}
|
||||||
|
|
||||||
private parseUserAnalysisJobData(value: unknown): UserAnalysisJobData {
|
private parseUserAnalysisJobData(value: unknown): UserAnalysisJobData {
|
||||||
@@ -68,6 +86,30 @@ export class AnalysisProcessor extends WorkerHost {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private parseFeatureRecomputeJobData(
|
||||||
|
value: unknown,
|
||||||
|
): FeatureRecomputeJobData {
|
||||||
|
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
||||||
|
throw new Error('Feature recompute job data must be an object.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const record = value as Record<string, unknown>;
|
||||||
|
const platformRoute = this.parsePlatformRoute(record.platformRoute);
|
||||||
|
const queueId = this.parseQueueId(record.queueId);
|
||||||
|
const batchSize = this.parseFeatureRecomputeBatchSize(record.batchSize);
|
||||||
|
const puuid =
|
||||||
|
record.puuid === undefined
|
||||||
|
? undefined
|
||||||
|
: this.parseBoundedString(record.puuid, 'puuid', 128);
|
||||||
|
|
||||||
|
return {
|
||||||
|
platformRoute,
|
||||||
|
queueId,
|
||||||
|
batchSize,
|
||||||
|
...(puuid ? { puuid } : {}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private parseBoundedString(
|
private parseBoundedString(
|
||||||
value: unknown,
|
value: unknown,
|
||||||
fieldName: string,
|
fieldName: string,
|
||||||
@@ -117,6 +159,13 @@ export class AnalysisProcessor extends WorkerHost {
|
|||||||
return normalizeRequestedMatchCount(value);
|
return normalizeRequestedMatchCount(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private parseFeatureRecomputeBatchSize(value: unknown): number {
|
||||||
|
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
||||||
|
throw new Error('Feature recompute job batchSize must be finite.');
|
||||||
|
}
|
||||||
|
return normalizeFeatureRecomputeBatchSize(value);
|
||||||
|
}
|
||||||
|
|
||||||
private isFinalAttempt(job: Job<unknown>): boolean {
|
private isFinalAttempt(job: Job<unknown>): boolean {
|
||||||
const attempts =
|
const attempts =
|
||||||
typeof job.opts?.attempts === 'number' && job.opts.attempts > 0
|
typeof job.opts?.attempts === 'number' && job.opts.attempts > 0
|
||||||
|
|||||||
@@ -14,6 +14,13 @@ export interface UserAnalysisJobData {
|
|||||||
requestedMatchCount: number;
|
requestedMatchCount: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface FeatureRecomputeJobData {
|
||||||
|
platformRoute: RiotPlatformRoute;
|
||||||
|
queueId: number;
|
||||||
|
batchSize: number;
|
||||||
|
puuid?: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface ExtractedPlayerMatchFeature {
|
export interface ExtractedPlayerMatchFeature {
|
||||||
matchId: string;
|
matchId: string;
|
||||||
puuid: string;
|
puuid: string;
|
||||||
|
|||||||
Reference in new issue
Block a user