From d18e36e96290300d95d0f3995921054dc4f8f383 Mon Sep 17 00:00:00 2001 From: dahyeon Date: Fri, 3 Jul 2026 01:11:21 -0700 Subject: [PATCH] Process seed accounts through analysis pipeline --- .../analysis-pipeline.service.spec.ts | 109 ++++++++++++++- .../analysis/analysis-pipeline.service.ts | 127 +++++++++++++++++- .../src/modules/analysis/analysis.types.ts | 15 +++ .../seed-collection.processor.spec.ts | 45 ++++++- .../analysis/seed-collection.processor.ts | 15 +++ .../analysis/seed-collection.service.spec.ts | 29 +++- .../analysis/seed-collection.service.ts | 14 +- 7 files changed, 343 insertions(+), 11 deletions(-) diff --git a/apps/api/src/modules/analysis/analysis-pipeline.service.spec.ts b/apps/api/src/modules/analysis/analysis-pipeline.service.spec.ts index ff2835c..9d83e82 100644 --- a/apps/api/src/modules/analysis/analysis-pipeline.service.spec.ts +++ b/apps/api/src/modules/analysis/analysis-pipeline.service.spec.ts @@ -245,6 +245,7 @@ interface PipelineHarnessOptions { accountError?: Error; existingMatchRaw?: Record | null; existingTimeline?: Record; + findOneAccountResult?: Account | null; getMatch?: jest.Mock>, [string, string]>; getMatchTimeline?: jest.Mock< Promise>, @@ -266,6 +267,15 @@ function createPipelineHarness(options: PipelineHarnessOptions = {}) { const updateAccount: UpdateOneMock = jest .fn, Parameters>() .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>, [string, string]>() .mockResolvedValue(createTimelinePayload()); + const getMatchIds = jest + .fn, [string, Record]>() + .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({ diff --git a/apps/api/src/modules/analysis/analysis-pipeline.service.ts b/apps/api/src/modules/analysis/analysis-pipeline.service.ts index 317d5db..c43d444 100644 --- a/apps/api/src/modules/analysis/analysis-pipeline.service.ts +++ b/apps/api/src/modules/analysis/analysis-pipeline.service.ts @@ -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 { + 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 { @@ -328,8 +431,9 @@ export class AnalysisPipelineService { private async fetchAndStoreMatches( matchIds: string[], - data: UserAnalysisJobData, + data: Pick, jobId: string, + jobType = USER_ANALYSIS_JOB_NAME, ): Promise { 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, extractedFeatures: ExtractedPlayerMatchFeature[], ): Promise { await this.storeBuiltProfile({ @@ -703,6 +811,11 @@ export class AnalysisPipelineService { return remainingTargets.length > 0; } + private async findSeedAccount(puuid: string): Promise { + const accountModel = await this.connectionService.getModel('account'); + return accountModel.findOne({ puuid, isSeed: true }).lean().exec(); + } + private featureRecomputeTargetQuery(params: { puuid?: string; queueId: number; diff --git a/apps/api/src/modules/analysis/analysis.types.ts b/apps/api/src/modules/analysis/analysis.types.ts index 69600c6..97cf5bd 100644 --- a/apps/api/src/modules/analysis/analysis.types.ts +++ b/apps/api/src/modules/analysis/analysis.types.ts @@ -28,6 +28,21 @@ export interface SeedCollectionJobData { sourceLimits?: Partial>; } +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; diff --git a/apps/api/src/modules/analysis/seed-collection.processor.spec.ts b/apps/api/src/modules/analysis/seed-collection.processor.spec.ts index 8be856e..b586c2b 100644 --- a/apps/api/src/modules/analysis/seed-collection.processor.spec.ts +++ b/apps/api/src/modules/analysis/seed-collection.processor.spec.ts @@ -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 { 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), ).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), ).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, diff --git a/apps/api/src/modules/analysis/seed-collection.processor.ts b/apps/api/src/modules/analysis/seed-collection.processor.ts index a265e20..bec6ac4 100644 --- a/apps/api/src/modules/analysis/seed-collection.processor.ts +++ b/apps/api/src/modules/analysis/seed-collection.processor.ts @@ -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', diff --git a/apps/api/src/modules/analysis/seed-collection.service.spec.ts b/apps/api/src/modules/analysis/seed-collection.service.spec.ts index fde9be8..86a9853 100644 --- a/apps/api/src/modules/analysis/seed-collection.service.spec.ts +++ b/apps/api/src/modules/analysis/seed-collection.service.spec.ts @@ -140,7 +140,14 @@ function createHarness(options: { existingSeedAddedAt?: Date } = {}) { ); const findOneAndUpdateAccount: FindOneAndUpdateAccountMock = jest .fn>() - .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({ diff --git a/apps/api/src/modules/analysis/seed-collection.service.ts b/apps/api/src/modules/analysis/seed-collection.service.ts index 23c594b..80321e0 100644 --- a/apps/api/src/modules/analysis/seed-collection.service.ts +++ b/apps/api/src/modules/analysis/seed-collection.service.ts @@ -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(); 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, }; }