Process seed accounts through analysis pipeline

This commit is contained in:
dahyeon committed 2026-07-03 01:11:21 -07:00
1 parent 63a4aeace7
commit d18e36e962
7 files changed
+343 -11

No files matched your search

@@ -245,6 +245,7 @@ interface PipelineHarnessOptions {
accountError?: Error;
existingMatchRaw?: Record<string, unknown> | null;
existingTimeline?: Record<string, unknown>;
findOneAccountResult?: Account | null;
getMatch?: jest.Mock<Promise<Record<string, unknown>>, [string, string]>;
getMatchTimeline?: jest.Mock<
Promise<Record<string, unknown>>,
@@ -266,6 +267,15 @@ function createPipelineHarness(options: PipelineHarnessOptions = {}) {
const updateAccount: UpdateOneMock = jest
.fn<ExecQuery<AcknowledgedUpdate>, Parameters<UpdateOneMock>>()
.mockReturnValue(execResolved({ acknowledged: true }));
const findOneAccount = jest
.fn()
.mockReturnValue(
leanExecResolved(
options.findOneAccountResult === undefined
? (options.storedAccount ?? createStoredAccount())
: options.findOneAccountResult,
),
);
const findOneRiotMatch = jest.fn().mockReturnValue(
leanExecResolved(
options.existingMatchRaw
@@ -288,6 +298,7 @@ function createPipelineHarness(options: PipelineHarnessOptions = {}) {
const models = {
account: {
findOne: findOneAccount,
findOneAndUpdate: findOneAndUpdateAccount,
updateOne: updateAccount,
},
@@ -327,6 +338,9 @@ function createPipelineHarness(options: PipelineHarnessOptions = {}) {
jest
.fn<Promise<Record<string, unknown>>, [string, string]>()
.mockResolvedValue(createTimelinePayload());
const getMatchIds = jest
.fn<Promise<string[]>, [string, Record<string, unknown>]>()
.mockResolvedValue(options.matchIds ?? ['KR_1']);
const riotApiClient = {
getAccountByRiotId,
getSummonerByPuuid: jest.fn().mockResolvedValue({
@@ -335,13 +349,15 @@ function createPipelineHarness(options: PipelineHarnessOptions = {}) {
summonerLevel: 500,
}),
getLeagueEntriesByPuuid: jest.fn().mockResolvedValue([]),
getMatchIds: jest.fn().mockResolvedValue(options.matchIds ?? ['KR_1']),
getMatchIds,
getMatch,
getMatchTimeline,
} as unknown as RiotApiClient;
return {
findOneAccount,
findOneRiotMatch,
getMatchIds,
getMatch,
getMatchTimeline,
service: new AnalysisPipelineService(
@@ -523,6 +539,97 @@ describe('AnalysisPipelineService', () => {
});
});
it('processes a stored seed account through the shared match feature pipeline', async () => {
const {
findOneAccount,
getMatchIds,
service,
updateAnalysisJob,
updatePlayerMatchFeature,
updatePlayerProfile,
} = createPipelineHarness({
findOneAccountResult: createStoredAccount({
isSeed: true,
puuid: 'target-puuid',
}),
});
const result = await service.processSeedAccountPipeline(
{
puuid: 'target-puuid',
platformRoute: 'kr',
regionalRoute: 'asia',
queueId: 420,
requestedMatchCount: 30,
},
'seed-job-1',
);
expect(result).toEqual({
puuid: 'target-puuid',
fetchedMatches: 1,
extractedFeatures: 1,
profileBuilt: true,
});
expect(findOneAccount).toHaveBeenCalledWith({
puuid: 'target-puuid',
isSeed: true,
});
expect(getMatchIds).toHaveBeenCalledWith('target-puuid', {
count: 30,
queue: 420,
regionalRoute: 'asia',
});
expect(updatePlayerMatchFeature).toHaveBeenCalledTimes(1);
const playerProfileUpdate = updatePlayerProfile.mock.calls[0][1];
expect(playerProfileUpdate).toMatchObject({
$set: {
isSeed: true,
puuid: 'target-puuid',
queueId: 420,
sampleGameCount: 1,
},
});
const seedStageUpdate = updateAnalysisJob.mock.calls.find(
([, update]) =>
asRecord(asRecord(update).$set).jobType === 'seed-collection' &&
asRecord(asRecord(update).$set).stage === 'extract-features',
);
expect(seedStageUpdate).toBeDefined();
});
it('skips seed pipeline processing when the seed account is missing', async () => {
const { findOneAccount, getMatchIds, service, updatePlayerProfile } =
createPipelineHarness({
findOneAccountResult: null,
});
const result = await service.processSeedAccountPipeline(
{
puuid: 'missing-puuid',
platformRoute: 'kr',
regionalRoute: 'asia',
queueId: 420,
requestedMatchCount: 30,
},
'seed-job-1',
);
expect(result).toEqual({
puuid: 'missing-puuid',
fetchedMatches: 0,
extractedFeatures: 0,
profileBuilt: false,
});
expect(findOneAccount).toHaveBeenCalledWith({
puuid: 'missing-puuid',
isSeed: true,
});
expect(getMatchIds).not.toHaveBeenCalled();
expect(updatePlayerProfile).not.toHaveBeenCalled();
});
it('completes without building a profile when no ranked matches are returned', async () => {
const { service, updateAnalysisJob, updatePlayerProfile } =
createPipelineHarness({
@@ -18,11 +18,14 @@ import {
MAX_ANALYSIS_MATCH_COUNT,
normalizeFeatureRecomputeBatchSize,
normalizeRequestedMatchCount,
SEED_COLLECTION_JOB_NAME,
USER_ANALYSIS_JOB_NAME,
} from './analysis.constants';
import type {
ExtractedPlayerMatchFeature,
FeatureRecomputeJobData,
SeedAccountPipelineData,
SeedAccountPipelineResult,
UserAnalysisJobData,
} from './analysis.types';
import { FeatureExtractorService } from './feature-extractor.service';
@@ -274,6 +277,106 @@ export class AnalysisPipelineService {
}
}
public async processSeedAccountPipeline(
data: SeedAccountPipelineData,
jobId: string,
): Promise<SeedAccountPipelineResult> {
const normalizedData = {
...data,
requestedMatchCount: normalizeRequestedMatchCount(
data.requestedMatchCount,
),
};
const account = await this.findSeedAccount(normalizedData.puuid);
if (!account) {
this.logger.warn(
`Skipping missing seed account puuid="${this.safeLogText(normalizedData.puuid)}"`,
);
return {
puuid: normalizedData.puuid,
fetchedMatches: 0,
extractedFeatures: 0,
profileBuilt: false,
};
}
await this.updateJob(
jobId,
{
puuid: account.puuid,
stage: 'fetch-match-ids',
},
SEED_COLLECTION_JOB_NAME,
);
const matchIds = await this.riotApiClient.getMatchIds(account.puuid, {
count: normalizedData.requestedMatchCount,
queue: normalizedData.queueId,
regionalRoute: normalizedData.regionalRoute,
});
await this.updateJob(
jobId,
{
stage: 'fetch-match-details',
progress: {
fetchedMatches: 0,
totalMatches: matchIds.length,
},
},
SEED_COLLECTION_JOB_NAME,
);
if (matchIds.length === 0) {
return {
puuid: account.puuid,
fetchedMatches: 0,
extractedFeatures: 0,
profileBuilt: false,
};
}
const matchRecords = await this.fetchAndStoreMatches(
matchIds,
normalizedData,
jobId,
SEED_COLLECTION_JOB_NAME,
);
await this.updateJob(
jobId,
{
stage: 'extract-features',
},
SEED_COLLECTION_JOB_NAME,
);
const extractedFeatures = await this.extractAndStoreFeatures(
matchRecords,
account.puuid,
normalizedData.queueId,
);
await this.updateJob(
jobId,
{
stage: 'build-profile',
},
SEED_COLLECTION_JOB_NAME,
);
if (extractedFeatures.length > 0) {
await this.buildAndStoreProfile(
account,
normalizedData,
extractedFeatures,
);
}
return {
puuid: account.puuid,
fetchedMatches: matchRecords.length,
extractedFeatures: extractedFeatures.length,
profileBuilt: extractedFeatures.length > 0,
};
}
private async resolveAndStoreAccount(
data: UserAnalysisJobData,
): Promise<Account> {
@@ -328,8 +431,9 @@ export class AnalysisPipelineService {
private async fetchAndStoreMatches(
matchIds: string[],
data: UserAnalysisJobData,
data: Pick<UserAnalysisJobData, 'queueId' | 'regionalRoute'>,
jobId: string,
jobType = USER_ANALYSIS_JOB_NAME,
): Promise<MatchRecord[]> {
const matchRecords: MatchRecord[] = [];
const riotMatchModel = await this.connectionService.getModel('riotMatch');
@@ -378,12 +482,16 @@ export class AnalysisPipelineService {
`Skipping matchId="${this.safeLogText(matchId)}" after fetch/store failure: ${this.safeLogText(errorMessage(error))}`,
);
} finally {
await this.updateJob(jobId, {
progress: {
fetchedMatches: index + 1,
totalMatches: matchIds.length,
await this.updateJob(
jobId,
{
progress: {
fetchedMatches: index + 1,
totalMatches: matchIds.length,
},
},
});
jobType,
);
}
}
@@ -663,7 +771,7 @@ export class AnalysisPipelineService {
private async buildAndStoreProfile(
account: Account,
data: UserAnalysisJobData,
data: Pick<UserAnalysisJobData, 'queueId'>,
extractedFeatures: ExtractedPlayerMatchFeature[],
): Promise<void> {
await this.storeBuiltProfile({
@@ -703,6 +811,11 @@ export class AnalysisPipelineService {
return remainingTargets.length > 0;
}
private async findSeedAccount(puuid: string): Promise<Account | null> {
const accountModel = await this.connectionService.getModel('account');
return accountModel.findOne({ puuid, isSeed: true }).lean<Account>().exec();
}
private featureRecomputeTargetQuery(params: {
puuid?: string;
queueId: number;
@@ -28,6 +28,21 @@ export interface SeedCollectionJobData {
sourceLimits?: Partial<Record<SeedSource, number>>;
}
export interface SeedAccountPipelineData {
puuid: string;
platformRoute: RiotPlatformRoute;
regionalRoute: RiotRegionalRoute;
queueId: number;
requestedMatchCount: number;
}
export interface SeedAccountPipelineResult {
puuid: string;
fetchedMatches: number;
extractedFeatures: number;
profileBuilt: boolean;
}
export interface ExtractedPlayerMatchFeature {
matchId: string;
puuid: string;
@@ -1,7 +1,11 @@
import type { Job } from 'bullmq';
import type { ConnectionService } from '../global/database/connection.service';
import type { AnalysisPipelineService } from './analysis-pipeline.service';
import type { SeedCollectionService } from './seed-collection.service';
import { SEED_COLLECTION_JOB_NAME } from './analysis.constants';
import {
DEFAULT_SEED_COLLECTION_MATCH_COUNT,
SEED_COLLECTION_JOB_NAME,
} from './analysis.constants';
import type { SeedCollectionJobData } from './analysis.types';
import { SeedCollectionProcessor } from './seed-collection.processor';
@@ -36,10 +40,23 @@ function asRecord(value: unknown): Record<string, unknown> {
describe('SeedCollectionProcessor', () => {
it('delegates seed collection jobs and marks them completed', async () => {
const collectSeedAccounts = jest.fn().mockResolvedValue({
regionalRoute: 'asia',
selectedEntries: 150,
storedSeedAccounts: [
{
puuid: 'seed-puuid-1',
source: 'challenger',
},
],
storedAccounts: 149,
skippedAccounts: 1,
});
const processSeedAccountPipeline = jest.fn().mockResolvedValue({
puuid: 'seed-puuid-1',
fetchedMatches: 30,
extractedFeatures: 30,
profileBuilt: true,
});
const updateOne = createUpdateOneMock();
const connectionService = {
getModel: jest.fn().mockResolvedValue({ updateOne }),
@@ -48,6 +65,9 @@ describe('SeedCollectionProcessor', () => {
{
collectSeedAccounts,
} as unknown as SeedCollectionService,
{
processSeedAccountPipeline,
} as unknown as AnalysisPipelineService,
connectionService,
);
@@ -67,6 +87,16 @@ describe('SeedCollectionProcessor', () => {
master: 50,
},
});
expect(processSeedAccountPipeline).toHaveBeenCalledWith(
{
puuid: 'seed-puuid-1',
platformRoute: 'kr',
regionalRoute: 'asia',
queueId: 420,
requestedMatchCount: DEFAULT_SEED_COLLECTION_MATCH_COUNT,
},
'seed-job-1',
);
expect(updateOne).toHaveBeenCalledWith(
{ jobId: 'seed-job-1' },
{
@@ -139,6 +169,7 @@ describe('SeedCollectionProcessor', () => {
'rejects malformed seed collection payloads',
async ({ data, expectedError }) => {
const collectSeedAccounts = jest.fn();
const processSeedAccountPipeline = jest.fn();
const updateOne = createUpdateOneMock();
const connectionService = {
getModel: jest.fn().mockResolvedValue({ updateOne }),
@@ -147,6 +178,9 @@ describe('SeedCollectionProcessor', () => {
{
collectSeedAccounts,
} as unknown as SeedCollectionService,
{
processSeedAccountPipeline,
} as unknown as AnalysisPipelineService,
connectionService,
);
@@ -160,6 +194,7 @@ describe('SeedCollectionProcessor', () => {
} as Job<unknown>),
).rejects.toThrow(expectedError);
expect(collectSeedAccounts).not.toHaveBeenCalled();
expect(processSeedAccountPipeline).not.toHaveBeenCalled();
expect(updateOne).not.toHaveBeenCalled();
},
);
@@ -168,6 +203,7 @@ describe('SeedCollectionProcessor', () => {
const collectSeedAccounts = jest
.fn()
.mockRejectedValue(new Error('Riot unavailable'));
const processSeedAccountPipeline = jest.fn();
const updateOne = createUpdateOneMock();
const connectionService = {
getModel: jest.fn().mockResolvedValue({ updateOne }),
@@ -176,6 +212,9 @@ describe('SeedCollectionProcessor', () => {
{
collectSeedAccounts,
} as unknown as SeedCollectionService,
{
processSeedAccountPipeline,
} as unknown as AnalysisPipelineService,
connectionService,
);
@@ -189,6 +228,7 @@ describe('SeedCollectionProcessor', () => {
} as Job<unknown>),
).rejects.toThrow('Riot unavailable');
expect(processSeedAccountPipeline).not.toHaveBeenCalled();
expect(updateOne).toHaveBeenCalledWith(
{ jobId: 'seed-job-1' },
{
@@ -205,6 +245,9 @@ describe('SeedCollectionProcessor', () => {
{
collectSeedAccounts: jest.fn(),
} as unknown as SeedCollectionService,
{
processSeedAccountPipeline: jest.fn(),
} as unknown as AnalysisPipelineService,
{
getModel: jest.fn(),
} as unknown as ConnectionService,
@@ -3,6 +3,7 @@ import type { Job } from 'bullmq';
import { ConnectionService } from '../global/database/connection.service';
import { SEED_SOURCES, type SeedSource } from '../global/database/schemas';
import {
DEFAULT_SEED_COLLECTION_MATCH_COUNT,
SEED_COLLECTION_JOB_NAME,
SEED_COLLECTION_QUEUE_NAME,
} from './analysis.constants';
@@ -11,12 +12,14 @@ import {
RIOT_PLATFORM_ROUTES,
type RiotPlatformRoute,
} from '../riot/riot.types';
import { AnalysisPipelineService } from './analysis-pipeline.service';
import { SeedCollectionService } from './seed-collection.service';
@Processor(SEED_COLLECTION_QUEUE_NAME, { concurrency: 1 })
export class SeedCollectionProcessor extends WorkerHost {
constructor(
private readonly seedCollectionService: SeedCollectionService,
private readonly analysisPipelineService: AnalysisPipelineService,
private readonly connectionService: ConnectionService,
) {
super();
@@ -45,6 +48,18 @@ export class SeedCollectionProcessor extends WorkerHost {
platformRoute: data.platformRoute,
...(data.sourceLimits ? { sourceLimits: data.sourceLimits } : {}),
});
for (const storedSeedAccount of result.storedSeedAccounts) {
await this.analysisPipelineService.processSeedAccountPipeline(
{
puuid: storedSeedAccount.puuid,
platformRoute: data.platformRoute,
regionalRoute: result.regionalRoute,
queueId: data.queueId,
requestedMatchCount: DEFAULT_SEED_COLLECTION_MATCH_COUNT,
},
job.id,
);
}
await this.updateJob(job.id, {
status: 'completed',
@@ -140,7 +140,14 @@ function createHarness(options: { existingSeedAddedAt?: Date } = {}) {
);
const findOneAndUpdateAccount: FindOneAndUpdateAccountMock = jest
.fn<LeanAccountQuery, Parameters<FindOneAndUpdateAccountMock>>()
.mockReturnValue(leanExecResolved(createStoredAccount()));
.mockImplementation((query) => {
const puuid = asRecord(query).puuid;
return leanExecResolved(
createStoredAccount({
puuid: typeof puuid === 'string' ? puuid : 'seed-puuid',
}),
);
});
const accountModel = {
findOne: findOneAccount,
findOneAndUpdate: findOneAndUpdateAccount,
@@ -244,6 +251,20 @@ describe('SeedCollectionService', () => {
regionalRoute: 'asia',
queueType: 'RANKED_SOLO_5x5',
storedAccounts: 3,
storedSeedAccounts: [
{
puuid: 'challenger-high',
source: 'challenger',
},
{
puuid: 'grandmaster-one',
source: 'grandmaster',
},
{
puuid: 'master-one',
source: 'master',
},
],
skippedAccounts: 0,
sourceResults: [
{
@@ -370,6 +391,12 @@ describe('SeedCollectionService', () => {
expect(result).toMatchObject({
storedAccounts: 1,
storedSeedAccounts: [
{
puuid: 'stored-puuid',
source: 'challenger',
},
],
skippedAccounts: 1,
sourceResults: [
expect.objectContaining({
@@ -34,6 +34,11 @@ export interface SeedCollectionSourceResult {
duplicateAccounts: number;
}
export interface SeedCollectionStoredAccount {
puuid: string;
source: SeedSource;
}
export interface SeedCollectionResult {
platformRoute: RiotPlatformRoute;
regionalRoute: RiotRegionalRoute;
@@ -44,6 +49,7 @@ export interface SeedCollectionResult {
storedAccounts: number;
skippedAccounts: number;
duplicateAccounts: number;
storedSeedAccounts: SeedCollectionStoredAccount[];
}
@Injectable()
@@ -63,6 +69,7 @@ export class SeedCollectionService {
const regionalRoute = toRegionalRoute(platformRoute);
const queueType = options.queueType ?? DEFAULT_SEED_COLLECTION_QUEUE_TYPE;
const sourceResults: SeedCollectionSourceResult[] = [];
const storedSeedAccounts: SeedCollectionStoredAccount[] = [];
const seenPuuids = new Set<string>();
for (const source of SEED_SOURCES) {
@@ -96,13 +103,17 @@ export class SeedCollectionService {
seenPuuids.add(selectedEntry.puuid);
try {
await this.storeSeedAccount({
const storedAccount = await this.storeSeedAccount({
collectedAt,
platformRoute,
puuid: selectedEntry.puuid,
regionalRoute,
source,
});
storedSeedAccounts.push({
puuid: storedAccount.puuid,
source,
});
sourceResult.storedAccounts += 1;
} catch (error) {
sourceResult.skippedAccounts += 1;
@@ -131,6 +142,7 @@ export class SeedCollectionService {
sourceResults,
'duplicateAccounts',
),
storedSeedAccounts,
};
}