From 770c198ccd0b5de61ab927447be946843cbe7ff9 Mon Sep 17 00:00:00 2001 From: dahyeon Date: Thu, 2 Jul 2026 20:40:49 -0700 Subject: [PATCH] Add analysis pipeline --- apps/api/package.json | 2 + apps/api/src/app.module.spec.ts | 47 ++ apps/api/src/app.module.ts | 3 +- .../analysis-pipeline.service.spec.ts | 355 ++++++++++++++ .../analysis/analysis-pipeline.service.ts | 456 ++++++++++++++++++ .../analysis/analysis-queue.service.spec.ts | 186 +++++++ .../analysis/analysis-queue.service.ts | 159 ++++++ .../modules/analysis/analysis.constants.ts | 18 + .../src/modules/analysis/analysis.module.ts | 49 ++ .../analysis/analysis.processor.spec.ts | 117 +++++ .../modules/analysis/analysis.processor.ts | 127 +++++ .../src/modules/analysis/analysis.types.ts | 52 ++ .../modules/analysis/bullmq.config.spec.ts | 54 +++ .../api/src/modules/analysis/bullmq.config.ts | 73 +++ .../feature-extractor.service.spec.ts | 187 +++++++ .../analysis/feature-extractor.service.ts | 218 +++++++++ .../analysis/profile-builder.service.spec.ts | 109 +++++ .../analysis/profile-builder.service.ts | 137 ++++++ apps/api/test/app.e2e-spec.ts | 17 + pnpm-lock.yaml | 207 ++++++++ 20 files changed, 2572 insertions(+), 1 deletion(-) create mode 100644 apps/api/src/app.module.spec.ts create mode 100644 apps/api/src/modules/analysis/analysis-pipeline.service.spec.ts create mode 100644 apps/api/src/modules/analysis/analysis-pipeline.service.ts create mode 100644 apps/api/src/modules/analysis/analysis-queue.service.spec.ts create mode 100644 apps/api/src/modules/analysis/analysis-queue.service.ts create mode 100644 apps/api/src/modules/analysis/analysis.constants.ts create mode 100644 apps/api/src/modules/analysis/analysis.module.ts create mode 100644 apps/api/src/modules/analysis/analysis.processor.spec.ts create mode 100644 apps/api/src/modules/analysis/analysis.processor.ts create mode 100644 apps/api/src/modules/analysis/analysis.types.ts create mode 100644 apps/api/src/modules/analysis/bullmq.config.spec.ts create mode 100644 apps/api/src/modules/analysis/bullmq.config.ts create mode 100644 apps/api/src/modules/analysis/feature-extractor.service.spec.ts create mode 100644 apps/api/src/modules/analysis/feature-extractor.service.ts create mode 100644 apps/api/src/modules/analysis/profile-builder.service.spec.ts create mode 100644 apps/api/src/modules/analysis/profile-builder.service.ts diff --git a/apps/api/package.json b/apps/api/package.json index 4a20d2f..ff7a695 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -20,11 +20,13 @@ }, "dependencies": { "@infisical/sdk": "^5.0.2", + "@nestjs/bullmq": "^11.0.4", "@nestjs/common": "^11.0.1", "@nestjs/config": "^4.0.4", "@nestjs/core": "^11.0.1", "@nestjs/platform-express": "^11.0.1", "@persona-lenz/shared": "workspace:*", + "bullmq": "^5.79.2", "mongoose": "^9.7.3", "redis": "^5.12.1", "reflect-metadata": "^0.2.2", diff --git a/apps/api/src/app.module.spec.ts b/apps/api/src/app.module.spec.ts new file mode 100644 index 0000000..f1c8558 --- /dev/null +++ b/apps/api/src/app.module.spec.ts @@ -0,0 +1,47 @@ +import { getQueueToken } from '@nestjs/bullmq'; +import { Test } from '@nestjs/testing'; +import { AppModule } from './app.module'; +import { AnalysisQueueService } from './modules/analysis/analysis-queue.service'; +import { ANALYSIS_QUEUE_NAME } from './modules/analysis/analysis.constants'; +import { ConnectionService } from './modules/global/database/connection.service'; +import { REDIS_CLIENT } from './modules/global/redis/redis.constants'; +import { SecretService } from './modules/global/secret/secret.service'; + +function createSecretServiceStub() { + return { + get: jest.fn((key: string) => { + if (key === 'REDIS_URL') + return Promise.resolve('redis://localhost:6379/0'); + return Promise.resolve(''); + }), + ready: jest.fn().mockResolvedValue(undefined), + }; +} + +describe('AppModule', () => { + it('compiles with the analysis queue module wired', async () => { + const moduleRef = await Test.createTestingModule({ + imports: [AppModule], + }) + .overrideProvider(SecretService) + .useValue(createSecretServiceStub()) + .overrideProvider(ConnectionService) + .useValue({}) + .overrideProvider(REDIS_CLIENT) + .useValue({ + destroy: jest.fn(), + quit: jest.fn().mockResolvedValue(undefined), + }) + .overrideProvider(getQueueToken(ANALYSIS_QUEUE_NAME)) + .useValue({ + add: jest.fn(), + }) + .compile(); + + expect(moduleRef.get(AnalysisQueueService)).toBeInstanceOf( + AnalysisQueueService, + ); + + await moduleRef.close(); + }); +}); diff --git a/apps/api/src/app.module.ts b/apps/api/src/app.module.ts index 780f060..07a7dea 100644 --- a/apps/api/src/app.module.ts +++ b/apps/api/src/app.module.ts @@ -1,10 +1,11 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; +import { AnalysisModule } from './modules/analysis/analysis.module'; import { GlobalModule } from './modules/global/global.module'; @Module({ - imports: [GlobalModule], + imports: [GlobalModule, AnalysisModule], controllers: [AppController], providers: [AppService], }) diff --git a/apps/api/src/modules/analysis/analysis-pipeline.service.spec.ts b/apps/api/src/modules/analysis/analysis-pipeline.service.spec.ts new file mode 100644 index 0000000..79090ff --- /dev/null +++ b/apps/api/src/modules/analysis/analysis-pipeline.service.spec.ts @@ -0,0 +1,355 @@ +import { FEATURE_SCHEMA_VERSION } from '@persona-lenz/shared'; +import type { ConnectionService } from '../global/database/connection.service'; +import type { Account } from '../global/database/schemas'; +import type { RiotApiClient } from '../riot/riot-api.client'; +import type { UserAnalysisJobData } from './analysis.types'; +import { FeatureExtractorService } from './feature-extractor.service'; +import { AnalysisPipelineService } from './analysis-pipeline.service'; +import { ProfileBuilderService } from './profile-builder.service'; + +function createJobData(): UserAnalysisJobData { + return { + gameName: 'Hide on bush', + tagLine: 'KR1', + platformRoute: 'kr', + regionalRoute: 'asia', + queueId: 420, + requestedMatchCount: 20, + }; +} + +function createMatchPayload(matchId = 'KR_1') { + return { + metadata: { + matchId, + participants: ['target-puuid', 'ally-puuid'], + }, + info: { + gameCreation: 1_700_000_000_000, + gameDuration: 1800, + gameVersion: '26.13.123.456', + queueId: 420, + participants: [ + { + puuid: 'target-puuid', + teamId: 100, + teamPosition: 'MIDDLE', + championId: 103, + kills: 6, + deaths: 2, + assists: 8, + totalDamageDealtToChampions: 24_000, + goldEarned: 12_000, + totalMinionsKilled: 210, + neutralMinionsKilled: 12, + visionScore: 24, + wardsKilled: 4, + detectorWardsPlaced: 3, + win: true, + challenges: { + kda: 7, + killParticipation: 0.7, + teamDamagePercentage: 0.32, + damagePerMinute: 800, + goldPerMinute: 400, + visionScorePerMinute: 0.8, + controlWardsPlaced: 3, + soloKills: 2, + }, + }, + { + puuid: 'ally-puuid', + teamId: 100, + teamPosition: 'JUNGLE', + championId: 64, + kills: 10, + totalDamageDealtToChampions: 30_000, + }, + ], + }, + }; +} + +interface ExecQuery { + exec: jest.Mock, []>; +} + +type AcknowledgedUpdate = { acknowledged: boolean }; +type UpdateOneMock = jest.Mock< + ExecQuery, + [Record, Record, Record?] +>; + +function asRecord(value: unknown): Record { + if (typeof value === 'object' && value !== null && !Array.isArray(value)) { + return value as Record; + } + return {}; +} + +function execResolved(value: T): ExecQuery { + return { exec: jest.fn, []>().mockResolvedValue(value) }; +} + +function leanExecResolved(value: T) { + return { + lean: jest.fn(() => execResolved(value)), + }; +} + +function createStoredAccount(overrides: Partial = {}): Account { + return { + createdAt: new Date('2026-07-01T00:00:00.000Z'), + updatedAt: new Date('2026-07-01T00:00:00.000Z'), + puuid: 'target-puuid', + gameName: 'Hide on bush', + tagLine: 'KR1', + platformRoute: 'kr', + regionalRoute: 'asia', + profileIconId: 1, + summonerLevel: 500, + tiers: [], + isSeed: false, + ...overrides, + }; +} + +interface PipelineHarnessOptions { + accountError?: Error; + existingMatchRaw?: Record | null; + getMatch?: jest.Mock>, [string, string]>; + matchIds?: string[]; + storedAccount?: Account; +} + +function createPipelineHarness(options: PipelineHarnessOptions = {}) { + const updateAnalysisJob: UpdateOneMock = jest + .fn, Parameters>() + .mockReturnValue(execResolved({ acknowledged: true })); + const findOneAndUpdateAccount = jest + .fn() + .mockReturnValue( + leanExecResolved(options.storedAccount ?? createStoredAccount()), + ); + const updateAccount: UpdateOneMock = jest + .fn, Parameters>() + .mockReturnValue(execResolved({ acknowledged: true })); + const findOneRiotMatch = jest + .fn() + .mockReturnValue( + leanExecResolved( + options.existingMatchRaw ? { raw: options.existingMatchRaw } : null, + ), + ); + const updateRiotMatch: UpdateOneMock = jest + .fn, Parameters>() + .mockReturnValue(execResolved({ acknowledged: true })); + const updatePlayerMatchFeature: UpdateOneMock = jest + .fn, Parameters>() + .mockReturnValue(execResolved({ acknowledged: true })); + const updatePlayerProfile: UpdateOneMock = jest + .fn, Parameters>() + .mockReturnValue(execResolved({ acknowledged: true })); + + const models = { + account: { + findOneAndUpdate: findOneAndUpdateAccount, + updateOne: updateAccount, + }, + analysisJob: { + updateOne: updateAnalysisJob, + }, + playerMatchFeature: { + updateOne: updatePlayerMatchFeature, + }, + playerProfile: { + updateOne: updatePlayerProfile, + }, + riotMatch: { + findOne: findOneRiotMatch, + updateOne: updateRiotMatch, + }, + }; + const connectionService = { + getModel: jest.fn((modelKey: keyof typeof models) => + Promise.resolve(models[modelKey]), + ), + } as unknown as ConnectionService; + const getAccountByRiotId = options.accountError + ? jest.fn().mockRejectedValue(options.accountError) + : jest.fn().mockResolvedValue({ + puuid: 'target-puuid', + gameName: 'Hide on bush', + tagLine: 'KR1', + }); + const getMatch = + options.getMatch ?? + jest + .fn>, [string, string]>() + .mockResolvedValue(createMatchPayload()); + const riotApiClient = { + getAccountByRiotId, + getSummonerByPuuid: jest.fn().mockResolvedValue({ + id: 'summoner-id', + puuid: 'target-puuid', + profileIconId: 1, + summonerLevel: 500, + }), + getLeagueEntriesByPuuid: jest.fn().mockResolvedValue([]), + getMatchIds: jest.fn().mockResolvedValue(options.matchIds ?? ['KR_1']), + getMatch, + } as unknown as RiotApiClient; + + return { + findOneRiotMatch, + getMatch, + service: new AnalysisPipelineService( + connectionService, + riotApiClient, + new FeatureExtractorService(), + new ProfileBuilderService(), + ), + updateAnalysisJob, + updatePlayerMatchFeature, + updatePlayerProfile, + updateRiotMatch, + }; +} + +describe('AnalysisPipelineService', () => { + it('fetches missing match details, stores features, builds a profile, and completes the job', async () => { + const { + findOneRiotMatch, + getMatch, + service, + updateAnalysisJob, + updatePlayerMatchFeature, + updatePlayerProfile, + updateRiotMatch, + } = createPipelineHarness(); + + await service.processUserAnalysisJob(createJobData(), 'analysis-job-1'); + + expect(findOneRiotMatch).toHaveBeenCalledWith({ matchId: 'KR_1' }); + expect(getMatch).toHaveBeenCalledWith('KR_1', 'asia'); + + const riotMatchUpdate = updateRiotMatch.mock.calls[0][1]; + expect(riotMatchUpdate).toMatchObject({ + $setOnInsert: { + matchId: 'KR_1', + queueId: 420, + }, + }); + + const playerMatchFeatureUpdate = updatePlayerMatchFeature.mock.calls[0][1]; + expect(playerMatchFeatureUpdate).toMatchObject({ + $set: { + featureSchemaVersion: FEATURE_SCHEMA_VERSION, + matchId: 'KR_1', + }, + }); + + const playerProfileUpdate = updatePlayerProfile.mock.calls[0][1]; + expect(playerProfileUpdate).toMatchObject({ + $set: { + featureSchemaVersion: FEATURE_SCHEMA_VERSION, + puuid: 'target-puuid', + sampleGameCount: 1, + }, + }); + + const completedJobUpdate = updateAnalysisJob.mock.calls.find( + ([, update]) => asRecord(asRecord(update).$set).status === 'completed', + ); + expect(completedJobUpdate).toBeDefined(); + expect(asRecord(asRecord(completedJobUpdate?.[1]).$set)).toMatchObject({ + status: 'completed', + stage: 'completed', + }); + }); + + it('completes without building a profile when no ranked matches are returned', async () => { + const { service, updateAnalysisJob, updatePlayerProfile } = + createPipelineHarness({ + matchIds: [], + }); + + await service.processUserAnalysisJob(createJobData(), 'analysis-job-1'); + + expect(updatePlayerProfile).not.toHaveBeenCalled(); + const completedJobUpdate = updateAnalysisJob.mock.calls.find( + ([, update]) => asRecord(asRecord(update).$set).status === 'completed', + ); + expect(completedJobUpdate).toBeDefined(); + }); + + it('uses cached match details without recalling Riot Match-V5', async () => { + const { getMatch, service, updatePlayerMatchFeature } = + createPipelineHarness({ + existingMatchRaw: createMatchPayload(), + }); + + await service.processUserAnalysisJob(createJobData(), 'analysis-job-1'); + + expect(getMatch).not.toHaveBeenCalled(); + expect(updatePlayerMatchFeature).toHaveBeenCalledTimes(1); + }); + + it('skips individual match fetch failures and still builds from successful matches', async () => { + const getMatch = jest + .fn>, [string, string]>() + .mockRejectedValueOnce(new Error('temporary upstream failure')) + .mockResolvedValueOnce(createMatchPayload('KR_2')); + const { service, updatePlayerProfile } = createPipelineHarness({ + getMatch, + matchIds: ['KR_1', 'KR_2'], + }); + + await service.processUserAnalysisJob(createJobData(), 'analysis-job-1'); + + expect(getMatch).toHaveBeenCalledTimes(2); + const playerProfileUpdate = updatePlayerProfile.mock.calls[0][1]; + expect(playerProfileUpdate).toMatchObject({ + $set: { + sampleGameCount: 1, + }, + }); + }); + + it('does not mark the job failed before BullMQ exhausts retries', async () => { + const { service, updateAnalysisJob } = createPipelineHarness({ + accountError: new Error('upstream unavailable'), + }); + + await expect( + service.processUserAnalysisJob(createJobData(), 'analysis-job-1', { + isFinalAttempt: false, + }), + ).rejects.toThrow('upstream unavailable'); + + const failedJobUpdate = updateAnalysisJob.mock.calls.find( + ([, update]) => asRecord(asRecord(update).$set).status === 'failed', + ); + expect(failedJobUpdate).toBeUndefined(); + }); + + it('marks the job failed with a public error on the final BullMQ attempt', async () => { + const { service, updateAnalysisJob } = createPipelineHarness({ + accountError: new Error('internal host redis.local refused connection'), + }); + + await expect( + service.processUserAnalysisJob(createJobData(), 'analysis-job-1', { + isFinalAttempt: true, + }), + ).rejects.toThrow('internal host redis.local refused connection'); + + const failedJobUpdate = updateAnalysisJob.mock.calls.find( + ([, update]) => asRecord(asRecord(update).$set).status === 'failed', + ); + expect(asRecord(asRecord(failedJobUpdate?.[1]).$set)).toMatchObject({ + status: 'failed', + error: 'Analysis failed during processing.', + }); + }); +}); diff --git a/apps/api/src/modules/analysis/analysis-pipeline.service.ts b/apps/api/src/modules/analysis/analysis-pipeline.service.ts new file mode 100644 index 0000000..7234fd3 --- /dev/null +++ b/apps/api/src/modules/analysis/analysis-pipeline.service.ts @@ -0,0 +1,456 @@ +import { Injectable, Logger } from '@nestjs/common'; +import { ConnectionService } from '../global/database/connection.service'; +import type { + Account, + AnalysisJobStatus, + RiotMatch, + TierInfo, +} from '../global/database/schemas'; +import { + RiotApiClient, + type RiotLeagueEntryDto, +} from '../riot/riot-api.client'; +import { + normalizeRequestedMatchCount, + USER_ANALYSIS_JOB_NAME, +} from './analysis.constants'; +import type { + ExtractedPlayerMatchFeature, + UserAnalysisJobData, +} from './analysis.types'; +import { FeatureExtractorService } from './feature-extractor.service'; +import { ProfileBuilderService } from './profile-builder.service'; + +interface MatchRecord { + matchId: string; + raw: Record; +} + +interface ParsedMatchSummary { + gameCreation: number; + gameDuration: number; + gameVersion?: string; + queueId?: number; + participantPuuids: string[]; +} + +function asRecord(value: unknown): Record { + if (typeof value === 'object' && value !== null && !Array.isArray(value)) { + return value as Record; + } + return {}; +} + +function asNumber(value: unknown, fallback = 0): number { + return typeof value === 'number' && Number.isFinite(value) ? value : fallback; +} + +function asString(value: unknown): string | undefined { + return typeof value === 'string' ? value : undefined; +} + +function asStringArray(value: unknown): string[] { + if (!Array.isArray(value)) return []; + return value.filter((item): item is string => typeof item === 'string'); +} + +function errorMessage(error: unknown): string { + return error instanceof Error ? error.message : String(error); +} + +@Injectable() +export class AnalysisPipelineService { + private readonly logger = new Logger(AnalysisPipelineService.name); + + constructor( + private readonly connectionService: ConnectionService, + private readonly riotApiClient: RiotApiClient, + private readonly featureExtractorService: FeatureExtractorService, + private readonly profileBuilderService: ProfileBuilderService, + ) {} + + public async processUserAnalysisJob( + data: UserAnalysisJobData, + jobId: string, + options: { isFinalAttempt?: boolean } = {}, + ): Promise { + const normalizedData = { + ...data, + requestedMatchCount: normalizeRequestedMatchCount( + data.requestedMatchCount, + ), + }; + + try { + await this.updateJob(jobId, { + status: 'processing', + stage: 'resolve-account', + progress: { + fetchedMatches: 0, + totalMatches: 0, + }, + }); + + const account = await this.resolveAndStoreAccount(normalizedData); + await this.updateJob(jobId, { + puuid: account.puuid, + stage: 'fetch-match-ids', + }); + + 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, + }, + }); + + if (matchIds.length === 0) { + await this.completeJob(jobId); + return; + } + + const matchRecords = await this.fetchAndStoreMatches( + matchIds, + normalizedData, + jobId, + ); + await this.updateJob(jobId, { + stage: 'extract-features', + }); + + const extractedFeatures = await this.extractAndStoreFeatures( + matchRecords, + account.puuid, + normalizedData.queueId, + ); + await this.updateJob(jobId, { + stage: 'build-profile', + }); + + if (extractedFeatures.length > 0) { + await this.buildAndStoreProfile( + account, + normalizedData, + extractedFeatures, + ); + } + + await this.completeJob(jobId); + } catch (error) { + if (options.isFinalAttempt === true) { + await this.updateJob(jobId, { + status: 'failed', + error: this.publicErrorMessage(error), + }); + } + throw error; + } + } + + private async resolveAndStoreAccount( + data: UserAnalysisJobData, + ): Promise { + const account = await this.riotApiClient.getAccountByRiotId( + data.gameName, + data.tagLine, + data.regionalRoute, + ); + const [summoner, tiers] = await Promise.all([ + this.riotApiClient.getSummonerByPuuid(account.puuid, data.platformRoute), + this.riotApiClient.getLeagueEntriesByPuuid( + account.puuid, + data.platformRoute, + ), + ]); + const accountModel = await this.connectionService.getModel('account'); + const tierFetchedAt = new Date(); + + const storedAccount = await accountModel + .findOneAndUpdate( + { puuid: account.puuid }, + { + $set: { + puuid: account.puuid, + gameName: account.gameName, + tagLine: account.tagLine, + platformRoute: data.platformRoute, + regionalRoute: data.regionalRoute, + profileIconId: summoner.profileIconId, + summonerLevel: summoner.summonerLevel, + tiers: this.toTierInfo(tiers, tierFetchedAt), + tiersFetchedAt: tierFetchedAt, + }, + $setOnInsert: { + isSeed: false, + }, + }, + { + new: true, + setDefaultsOnInsert: true, + upsert: true, + }, + ) + .lean() + .exec(); + + if (!storedAccount) { + throw new Error('Failed to store Riot account.'); + } + return storedAccount; + } + + private async fetchAndStoreMatches( + matchIds: string[], + data: UserAnalysisJobData, + jobId: string, + ): Promise { + const matchRecords: MatchRecord[] = []; + const riotMatchModel = await this.connectionService.getModel('riotMatch'); + + for (const [index, matchId] of matchIds.entries()) { + try { + const existingMatch = await riotMatchModel + .findOne({ matchId }) + .lean() + .exec(); + + if (existingMatch) { + if (this.isEligibleMatch(existingMatch.raw, data.queueId)) { + matchRecords.push({ matchId, raw: existingMatch.raw }); + } + } else { + const rawMatch = await this.riotApiClient.getMatch( + matchId, + data.regionalRoute, + ); + await this.storeRiotMatch(matchId, rawMatch); + if (this.isEligibleMatch(rawMatch, data.queueId)) { + matchRecords.push({ matchId, raw: rawMatch }); + } + } + } catch (error) { + this.logger.warn( + `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, + }, + }); + } + } + + return matchRecords; + } + + private async storeRiotMatch( + matchId: string, + rawMatch: Record, + ): Promise { + const summary = this.parseMatchSummary(rawMatch); + const riotMatchModel = await this.connectionService.getModel('riotMatch'); + + await riotMatchModel + .updateOne( + { matchId }, + { + $setOnInsert: { + matchId, + gameCreation: summary.gameCreation, + gameDuration: summary.gameDuration, + gameVersion: summary.gameVersion, + queueId: summary.queueId, + participantPuuids: summary.participantPuuids, + raw: rawMatch, + fetchedAt: new Date(), + }, + }, + { upsert: true }, + ) + .exec(); + } + + private async extractAndStoreFeatures( + matchRecords: MatchRecord[], + puuid: string, + queueId: number, + ): Promise { + const playerMatchFeatureModel = + await this.connectionService.getModel('playerMatchFeature'); + const extractedFeatures: ExtractedPlayerMatchFeature[] = []; + + for (const matchRecord of matchRecords) { + try { + const feature = this.featureExtractorService.extractPlayerMatchFeature( + matchRecord.raw, + puuid, + ); + if (feature.queueId !== queueId) continue; + + await playerMatchFeatureModel + .updateOne( + { matchId: feature.matchId, puuid: feature.puuid }, + { $set: feature }, + { upsert: true }, + ) + .exec(); + extractedFeatures.push(feature); + } catch (error) { + this.logger.warn( + `Skipping feature extraction for matchId="${this.safeLogText(matchRecord.matchId)}": ${this.safeLogText(errorMessage(error))}`, + ); + } + } + + return extractedFeatures; + } + + private async buildAndStoreProfile( + account: Account, + data: UserAnalysisJobData, + extractedFeatures: ExtractedPlayerMatchFeature[], + ): Promise { + const profile = this.profileBuilderService.buildProfile({ + features: extractedFeatures, + isSeed: account.isSeed, + puuid: account.puuid, + queueId: data.queueId, + }); + const playerProfileModel = + await this.connectionService.getModel('playerProfile'); + const accountModel = await this.connectionService.getModel('account'); + const analyzedAt = new Date(); + + await playerProfileModel + .updateOne( + { + puuid: account.puuid, + queueId: data.queueId, + }, + { $set: profile }, + { upsert: true }, + ) + .exec(); + await accountModel + .updateOne( + { puuid: account.puuid }, + { + $set: { + lastAnalyzedAt: analyzedAt, + }, + }, + ) + .exec(); + } + + private async completeJob(jobId: string): Promise { + await this.updateJob(jobId, { + status: 'completed', + stage: 'completed', + completedAt: new Date(), + error: undefined, + }); + } + + private async updateJob( + jobId: string, + patch: { + completedAt?: Date; + error?: string; + progress?: { fetchedMatches: number; totalMatches: number }; + puuid?: string; + stage?: string; + status?: AnalysisJobStatus; + }, + ): Promise { + const analysisJobModel = + await this.connectionService.getModel('analysisJob'); + const set: Record = { + jobId, + jobType: USER_ANALYSIS_JOB_NAME, + ...patch, + }; + const unset: Record = {}; + if (patch.error === undefined) { + delete set.error; + } + if (patch.status === 'completed') { + unset.error = ''; + } + + await analysisJobModel + .updateOne( + { jobId }, + { + $set: set, + ...(Object.keys(unset).length > 0 ? { $unset: unset } : {}), + }, + ) + .exec(); + } + + private isEligibleMatch( + rawMatch: Record, + queueId: number, + ): boolean { + const info = asRecord(rawMatch.info); + return ( + asNumber(info.queueId) === queueId && asNumber(info.gameDuration) >= 300 + ); + } + + private parseMatchSummary( + rawMatch: Record, + ): ParsedMatchSummary { + const metadata = asRecord(rawMatch.metadata); + const info = asRecord(rawMatch.info); + return { + gameCreation: asNumber(info.gameCreation), + gameDuration: asNumber(info.gameDuration), + gameVersion: asString(info.gameVersion), + queueId: + typeof info.queueId === 'number' && Number.isFinite(info.queueId) + ? info.queueId + : undefined, + participantPuuids: asStringArray(metadata.participants), + }; + } + + private toTierInfo( + entries: RiotLeagueEntryDto[], + fetchedAt: Date, + ): TierInfo[] { + return entries.map((entry) => ({ + queueType: entry.queueType, + tier: entry.tier, + rank: entry.rank, + leaguePoints: entry.leaguePoints, + wins: entry.wins, + losses: entry.losses, + fetchedAt, + })); + } + + private publicErrorMessage(error: unknown): string { + if ( + error instanceof Error && + (error.name === 'RiotApiKeyError' || + error.message.includes('Riot API key')) + ) { + return 'Riot API key is invalid or expired.'; + } + return 'Analysis failed during processing.'; + } + + private safeLogText(value: string): string { + return value.replace(/[\r\n\t]/gu, ' ').slice(0, 300); + } +} diff --git a/apps/api/src/modules/analysis/analysis-queue.service.spec.ts b/apps/api/src/modules/analysis/analysis-queue.service.spec.ts new file mode 100644 index 0000000..c9e59b5 --- /dev/null +++ b/apps/api/src/modules/analysis/analysis-queue.service.spec.ts @@ -0,0 +1,186 @@ +import type { AnalysisJob } from '../global/database/schemas'; +import type { ConnectionService } from '../global/database/connection.service'; +import type { UserAnalysisJobData } from './analysis.types'; +import { USER_ANALYSIS_JOB_NAME } from './analysis.constants'; +import { AnalysisQueueService } from './analysis-queue.service'; + +function createJobData( + overrides: Partial = {}, +): UserAnalysisJobData { + return { + gameName: 'Hide on bush', + tagLine: 'KR1', + platformRoute: 'kr', + regionalRoute: 'asia', + queueId: 420, + requestedMatchCount: 20, + ...overrides, + }; +} + +function createFindOneChain( + result: Pick | null, +) { + const exec = jest + .fn | null>, []>() + .mockResolvedValue(result); + const lean = jest.fn(() => ({ exec })); + const sort = jest.fn(() => ({ lean })); + return { + chain: { sort }, + exec, + lean, + sort, + }; +} + +describe('AnalysisQueueService', () => { + it('creates an analysis job document and enqueues a BullMQ job', async () => { + const add = jest.fn().mockResolvedValue({ id: 'job-from-bull' }); + const findOneChain = createFindOneChain(null); + const create = jest.fn().mockResolvedValue({}); + const updateOne = jest.fn(() => ({ + exec: jest.fn().mockResolvedValue({}), + })); + const analysisJobModel = { + create, + findOne: jest.fn(() => findOneChain.chain), + updateOne, + }; + const connectionService = { + getModel: jest.fn().mockResolvedValue(analysisJobModel), + } as unknown as ConnectionService; + const service = new AnalysisQueueService( + { add } as never, + connectionService, + () => 'analysis-job-1', + ); + + const result = await service.enqueueUserAnalysis(createJobData()); + + expect(result).toEqual({ jobId: 'analysis-job-1', status: 'queued' }); + expect(create).toHaveBeenCalledWith( + expect.objectContaining({ + jobId: 'analysis-job-1', + jobType: USER_ANALYSIS_JOB_NAME, + gameName: 'Hide on bush', + tagLine: 'KR1', + status: 'queued', + }), + ); + expect(add).toHaveBeenCalledWith( + USER_ANALYSIS_JOB_NAME, + createJobData(), + expect.objectContaining({ + attempts: 2, + jobId: 'analysis-job-1', + }), + ); + }); + + it('returns an existing active job instead of enqueueing a duplicate', async () => { + const add = jest.fn(); + const findOneChain = createFindOneChain({ + jobId: 'existing-job', + status: 'processing', + }); + const analysisJobModel = { + create: jest.fn(), + findOne: jest.fn(() => findOneChain.chain), + }; + const connectionService = { + getModel: jest.fn().mockResolvedValue(analysisJobModel), + } as unknown as ConnectionService; + const service = new AnalysisQueueService( + { add } as never, + connectionService, + () => 'new-job', + ); + + await expect(service.enqueueUserAnalysis(createJobData())).resolves.toEqual( + { + jobId: 'existing-job', + status: 'processing', + }, + ); + expect(add).not.toHaveBeenCalled(); + expect(analysisJobModel.create).not.toHaveBeenCalled(); + }); + + it('marks the pre-created job failed when BullMQ enqueue fails', async () => { + const add = jest.fn().mockRejectedValue(new Error('redis unavailable')); + const findOneChain = createFindOneChain(null); + const create = jest.fn().mockResolvedValue({}); + const updateOne = jest.fn(() => ({ + exec: jest.fn().mockResolvedValue({}), + })); + const analysisJobModel = { + create, + findOne: jest.fn(() => findOneChain.chain), + updateOne, + }; + const connectionService = { + getModel: jest.fn().mockResolvedValue(analysisJobModel), + } as unknown as ConnectionService; + const service = new AnalysisQueueService( + { add } as never, + connectionService, + () => 'analysis-job-1', + ); + + await expect(service.enqueueUserAnalysis(createJobData())).rejects.toThrow( + 'redis unavailable', + ); + expect(updateOne).toHaveBeenCalledWith( + { jobId: 'analysis-job-1' }, + { + $set: { + status: 'failed', + stage: 'enqueue', + error: 'redis unavailable', + }, + }, + ); + }); + + it('serializes identical in-process requests before creating a duplicate job', async () => { + const add = jest.fn().mockResolvedValue({ id: 'analysis-job-1' }); + let activeJob: Pick | null = null; + const findOne = jest.fn(() => ({ + sort: jest.fn(() => ({ + lean: jest.fn(() => ({ + exec: jest + .fn | null>, []>() + .mockImplementation(() => Promise.resolve(activeJob)), + })), + })), + })); + const create = jest.fn().mockImplementation(() => { + activeJob = { jobId: 'analysis-job-1', status: 'queued' }; + return Promise.resolve({}); + }); + const analysisJobModel = { + create, + findOne, + updateOne: jest.fn(), + }; + const connectionService = { + getModel: jest.fn().mockResolvedValue(analysisJobModel), + } as unknown as ConnectionService; + const service = new AnalysisQueueService( + { add } as never, + connectionService, + () => (create.mock.calls.length === 0 ? 'analysis-job-1' : 'unexpected'), + ); + + const [firstResult, secondResult] = await Promise.all([ + service.enqueueUserAnalysis(createJobData()), + service.enqueueUserAnalysis(createJobData()), + ]); + + expect(firstResult).toEqual({ jobId: 'analysis-job-1', status: 'queued' }); + expect(secondResult).toEqual({ jobId: 'analysis-job-1', status: 'queued' }); + expect(create).toHaveBeenCalledTimes(1); + expect(add).toHaveBeenCalledTimes(1); + }); +}); diff --git a/apps/api/src/modules/analysis/analysis-queue.service.ts b/apps/api/src/modules/analysis/analysis-queue.service.ts new file mode 100644 index 0000000..ebad4e7 --- /dev/null +++ b/apps/api/src/modules/analysis/analysis-queue.service.ts @@ -0,0 +1,159 @@ +import { InjectQueue } from '@nestjs/bullmq'; +import { Inject, Injectable } from '@nestjs/common'; +import type { AnalysisRequestAcceptedDto } from '@persona-lenz/shared'; +import type { Queue } from 'bullmq'; +import { randomUUID } from 'node:crypto'; +import { ConnectionService } from '../global/database/connection.service'; +import type { AnalysisJob } from '../global/database/schemas'; +import { + ANALYSIS_JOB_ID_FACTORY, + ACTIVE_ANALYSIS_JOB_STATUSES, + ANALYSIS_QUEUE_NAME, + normalizeRequestedMatchCount, + USER_ANALYSIS_JOB_NAME, +} from './analysis.constants'; +import type { UserAnalysisJobData } from './analysis.types'; + +type JobIdFactory = () => string; +type ActiveAnalysisJob = Pick & { + status: Extract; +}; + +@Injectable() +export class AnalysisQueueService { + constructor( + @InjectQueue(ANALYSIS_QUEUE_NAME) + private readonly analysisQueue: Queue< + UserAnalysisJobData, + void, + typeof USER_ANALYSIS_JOB_NAME + >, + private readonly connectionService: ConnectionService, + @Inject(ANALYSIS_JOB_ID_FACTORY) + private readonly jobIdFactory: JobIdFactory = randomUUID, + ) {} + + public async enqueueUserAnalysis( + data: UserAnalysisJobData, + ): Promise { + const normalizedData = { + ...data, + requestedMatchCount: normalizeRequestedMatchCount( + data.requestedMatchCount, + ), + }; + const duplicateRequestKey = this.duplicateRequestKey(normalizedData); + while (this.pendingDuplicateRequestKeys.has(duplicateRequestKey)) { + await this.sleep(10); + } + + this.pendingDuplicateRequestKeys.add(duplicateRequestKey); + try { + return await this.enqueueUserAnalysisWithoutDuplicateRequestRace( + normalizedData, + ); + } finally { + this.pendingDuplicateRequestKeys.delete(duplicateRequestKey); + } + } + + private readonly pendingDuplicateRequestKeys = new Set(); + + private async enqueueUserAnalysisWithoutDuplicateRequestRace( + normalizedData: UserAnalysisJobData, + ): Promise { + const analysisJobModel = + await this.connectionService.getModel('analysisJob'); + const existingJob = await this.findActiveJob( + analysisJobModel, + normalizedData, + ); + if (existingJob) { + return { + jobId: existingJob.jobId, + status: existingJob.status, + }; + } + + const jobId = this.jobIdFactory(); + await analysisJobModel.create({ + jobId, + jobType: USER_ANALYSIS_JOB_NAME, + gameName: normalizedData.gameName, + tagLine: normalizedData.tagLine, + platformRoute: normalizedData.platformRoute, + queueId: normalizedData.queueId, + requestedMatchCount: normalizedData.requestedMatchCount, + status: 'queued', + stage: 'queued', + progress: { + fetchedMatches: 0, + totalMatches: 0, + }, + }); + + try { + await this.analysisQueue.add(USER_ANALYSIS_JOB_NAME, normalizedData, { + attempts: 2, + backoff: { + type: 'exponential', + delay: 1_000, + }, + jobId, + }); + } catch (error) { + await analysisJobModel + .updateOne( + { jobId }, + { + $set: { + status: 'failed', + stage: 'enqueue', + error: this.errorMessage(error), + }, + }, + ) + .exec(); + throw error; + } + + return { + jobId, + status: 'queued', + }; + } + + private async findActiveJob( + analysisJobModel: Awaited>, + data: UserAnalysisJobData, + ): Promise { + return analysisJobModel + .findOne({ + gameName: data.gameName, + tagLine: data.tagLine, + platformRoute: data.platformRoute, + queueId: data.queueId, + status: { $in: ACTIVE_ANALYSIS_JOB_STATUSES }, + }) + .sort({ createdAt: -1 }) + .lean() + .exec(); + } + + private errorMessage(error: unknown): string { + return error instanceof Error ? error.message : String(error); + } + + private duplicateRequestKey(data: UserAnalysisJobData): string { + return [ + data.platformRoute, + data.queueId, + data.gameName.trim().toLocaleLowerCase(), + data.tagLine.trim().toLocaleLowerCase(), + ].join(':'); + } + + private async sleep(durationMs: number): Promise { + await new Promise((resolve) => setTimeout(resolve, durationMs)); + } +} diff --git a/apps/api/src/modules/analysis/analysis.constants.ts b/apps/api/src/modules/analysis/analysis.constants.ts new file mode 100644 index 0000000..5533e94 --- /dev/null +++ b/apps/api/src/modules/analysis/analysis.constants.ts @@ -0,0 +1,18 @@ +export const ANALYSIS_QUEUE_NAME = 'analysis'; +export const USER_ANALYSIS_JOB_NAME = 'user-analysis'; +export const ANALYSIS_JOB_ID_FACTORY = Symbol('ANALYSIS_JOB_ID_FACTORY'); + +export const DEFAULT_ANALYSIS_MATCH_COUNT = 20; +export const MAX_ANALYSIS_MATCH_COUNT = 50; + +export const ACTIVE_ANALYSIS_JOB_STATUSES = ['queued', 'processing'] as const; + +export function normalizeRequestedMatchCount( + requestedMatchCount: number, +): number { + if (!Number.isFinite(requestedMatchCount) || requestedMatchCount <= 0) { + return DEFAULT_ANALYSIS_MATCH_COUNT; + } + + return Math.min(Math.trunc(requestedMatchCount), MAX_ANALYSIS_MATCH_COUNT); +} diff --git a/apps/api/src/modules/analysis/analysis.module.ts b/apps/api/src/modules/analysis/analysis.module.ts new file mode 100644 index 0000000..0252b07 --- /dev/null +++ b/apps/api/src/modules/analysis/analysis.module.ts @@ -0,0 +1,49 @@ +import { BullModule } from '@nestjs/bullmq'; +import { Module } from '@nestjs/common'; +import { randomUUID } from 'node:crypto'; +import { DatabaseModule } from '../global/database/database.module'; +import { SecretModule } from '../global/secret/secret.module'; +import { SecretService } from '../global/secret/secret.service'; +import { RiotModule } from '../riot/riot.module'; +import { AnalysisQueueService } from './analysis-queue.service'; +import { + ANALYSIS_JOB_ID_FACTORY, + ANALYSIS_QUEUE_NAME, +} from './analysis.constants'; +import { AnalysisPipelineService } from './analysis-pipeline.service'; +import { AnalysisProcessor } from './analysis.processor'; +import { createBullMqOptions } from './bullmq.config'; +import { FeatureExtractorService } from './feature-extractor.service'; +import { ProfileBuilderService } from './profile-builder.service'; + +@Module({ + imports: [ + SecretModule, + DatabaseModule, + RiotModule, + BullModule.forRootAsync({ + imports: [SecretModule], + inject: [SecretService], + useFactory: createBullMqOptions, + extraOptions: { + manualRegistration: process.env.NODE_ENV === 'test', + }, + }), + BullModule.registerQueue({ + name: ANALYSIS_QUEUE_NAME, + }), + ], + providers: [ + { + provide: ANALYSIS_JOB_ID_FACTORY, + useValue: randomUUID, + }, + AnalysisQueueService, + AnalysisPipelineService, + AnalysisProcessor, + FeatureExtractorService, + ProfileBuilderService, + ], + exports: [AnalysisQueueService, AnalysisPipelineService], +}) +export class AnalysisModule {} diff --git a/apps/api/src/modules/analysis/analysis.processor.spec.ts b/apps/api/src/modules/analysis/analysis.processor.spec.ts new file mode 100644 index 0000000..a5247c3 --- /dev/null +++ b/apps/api/src/modules/analysis/analysis.processor.spec.ts @@ -0,0 +1,117 @@ +import type { Job } from 'bullmq'; +import { USER_ANALYSIS_JOB_NAME } from './analysis.constants'; +import type { UserAnalysisJobData } from './analysis.types'; +import { AnalysisPipelineService } from './analysis-pipeline.service'; +import { AnalysisProcessor } from './analysis.processor'; + +function createJobData(): UserAnalysisJobData { + return { + gameName: 'Hide on bush', + tagLine: 'KR1', + platformRoute: 'kr', + regionalRoute: 'asia', + queueId: 420, + requestedMatchCount: 20, + }; +} + +describe('AnalysisProcessor', () => { + it('delegates BullMQ jobs to the analysis pipeline', async () => { + const processUserAnalysisJob = jest + .fn< + Promise, + [UserAnalysisJobData, string, { isFinalAttempt?: boolean }] + >() + .mockResolvedValue(undefined); + const processor = new AnalysisProcessor({ + processUserAnalysisJob, + } as unknown as AnalysisPipelineService); + + await processor.process({ + attemptsMade: 1, + id: 'analysis-job-1', + name: USER_ANALYSIS_JOB_NAME, + opts: { attempts: 2 }, + data: createJobData(), + } as Job); + + expect(processUserAnalysisJob).toHaveBeenCalledWith( + createJobData(), + 'analysis-job-1', + { isFinalAttempt: true }, + ); + }); + + it('rejects jobs without a BullMQ id', async () => { + const processor = new AnalysisProcessor({ + processUserAnalysisJob: jest.fn(), + } as unknown as AnalysisPipelineService); + + await expect( + processor.process({ + data: createJobData(), + name: USER_ANALYSIS_JOB_NAME, + } as Job), + ).rejects.toThrow('Analysis job is missing a BullMQ job id.'); + }); + + it('rejects unsupported job names before processing data', async () => { + const processUserAnalysisJob = jest.fn(); + const processor = new AnalysisProcessor({ + processUserAnalysisJob, + } as unknown as AnalysisPipelineService); + + await expect( + processor.process({ + id: 'analysis-job-1', + name: 'unexpected', + data: createJobData(), + } as Job), + ).rejects.toThrow('Unsupported analysis job name "unexpected".'); + expect(processUserAnalysisJob).not.toHaveBeenCalled(); + }); + + it('rejects malformed Redis payloads', async () => { + const processUserAnalysisJob = jest.fn(); + const processor = new AnalysisProcessor({ + processUserAnalysisJob, + } as unknown as AnalysisPipelineService); + + await expect( + processor.process({ + attemptsMade: 0, + id: 'analysis-job-1', + name: USER_ANALYSIS_JOB_NAME, + opts: { attempts: 2 }, + data: { + ...createJobData(), + regionalRoute: 'americas', + }, + } as Job), + ).rejects.toThrow( + 'Analysis job regionalRoute does not match platformRoute.', + ); + expect(processUserAnalysisJob).not.toHaveBeenCalled(); + }); + + it('normalizes requested match count from valid queue payloads', async () => { + const processUserAnalysisJob = jest.fn().mockResolvedValue(undefined); + const processor = new AnalysisProcessor({ + processUserAnalysisJob, + } as unknown as AnalysisPipelineService); + + await processor.process({ + attemptsMade: 0, + id: 'analysis-job-1', + name: USER_ANALYSIS_JOB_NAME, + opts: { attempts: 2 }, + data: createJobData({ requestedMatchCount: 999 }), + } as Job); + + expect(processUserAnalysisJob).toHaveBeenCalledWith( + createJobData({ requestedMatchCount: 50 }), + 'analysis-job-1', + { isFinalAttempt: false }, + ); + }); +}); diff --git a/apps/api/src/modules/analysis/analysis.processor.ts b/apps/api/src/modules/analysis/analysis.processor.ts new file mode 100644 index 0000000..d0bb102 --- /dev/null +++ b/apps/api/src/modules/analysis/analysis.processor.ts @@ -0,0 +1,127 @@ +import { Processor, WorkerHost } from '@nestjs/bullmq'; +import type { Job } from 'bullmq'; +import { + ANALYSIS_QUEUE_NAME, + normalizeRequestedMatchCount, + USER_ANALYSIS_JOB_NAME, +} from './analysis.constants'; +import { AnalysisPipelineService } from './analysis-pipeline.service'; +import type { UserAnalysisJobData } from './analysis.types'; +import { + RIOT_PLATFORM_ROUTES, + RIOT_REGIONAL_ROUTES, + toRegionalRoute, + type RiotPlatformRoute, + type RiotRegionalRoute, +} from '../riot/riot.types'; + +@Processor(ANALYSIS_QUEUE_NAME) +export class AnalysisProcessor extends WorkerHost { + constructor( + private readonly analysisPipelineService: AnalysisPipelineService, + ) { + super(); + } + + public async process(job: Job): Promise { + if (!job.id) { + throw new Error('Analysis job is missing a BullMQ job id.'); + } + if (job.name !== USER_ANALYSIS_JOB_NAME) { + throw new Error(`Unsupported analysis job name "${job.name}".`); + } + + const data = this.parseUserAnalysisJobData(job.data); + await this.analysisPipelineService.processUserAnalysisJob(data, job.id, { + isFinalAttempt: this.isFinalAttempt(job), + }); + } + + private parseUserAnalysisJobData(value: unknown): UserAnalysisJobData { + if (typeof value !== 'object' || value === null || Array.isArray(value)) { + throw new Error('Analysis job data must be an object.'); + } + + const record = value as Record; + const gameName = this.parseBoundedString(record.gameName, 'gameName', 64); + const tagLine = this.parseBoundedString(record.tagLine, 'tagLine', 16); + const platformRoute = this.parsePlatformRoute(record.platformRoute); + const regionalRoute = this.parseRegionalRoute(record.regionalRoute); + if (regionalRoute !== toRegionalRoute(platformRoute)) { + throw new Error( + 'Analysis job regionalRoute does not match platformRoute.', + ); + } + + const queueId = this.parseQueueId(record.queueId); + const requestedMatchCount = this.parseRequestedMatchCount( + record.requestedMatchCount, + ); + + return { + gameName, + tagLine, + platformRoute, + regionalRoute, + queueId, + requestedMatchCount, + }; + } + + private parseBoundedString( + value: unknown, + fieldName: string, + maximumLength: number, + ): string { + if (typeof value !== 'string') { + throw new Error(`Analysis job ${fieldName} must be a string.`); + } + const trimmed = value.trim(); + if (trimmed.length === 0 || trimmed.length > maximumLength) { + throw new Error(`Analysis job ${fieldName} is out of range.`); + } + return trimmed; + } + + private parsePlatformRoute(value: unknown): RiotPlatformRoute { + if ( + typeof value !== 'string' || + !RIOT_PLATFORM_ROUTES.includes(value as RiotPlatformRoute) + ) { + throw new Error('Analysis job platformRoute is unsupported.'); + } + return value as RiotPlatformRoute; + } + + private parseRegionalRoute(value: unknown): RiotRegionalRoute { + if ( + typeof value !== 'string' || + !RIOT_REGIONAL_ROUTES.includes(value as RiotRegionalRoute) + ) { + throw new Error('Analysis job regionalRoute is unsupported.'); + } + return value as RiotRegionalRoute; + } + + private parseQueueId(value: unknown): number { + if (value !== 420) { + throw new Error('Analysis job queueId is unsupported.'); + } + return value; + } + + private parseRequestedMatchCount(value: unknown): number { + if (typeof value !== 'number' || !Number.isFinite(value)) { + throw new Error('Analysis job requestedMatchCount must be finite.'); + } + return normalizeRequestedMatchCount(value); + } + + private isFinalAttempt(job: Job): boolean { + const attempts = + typeof job.opts?.attempts === 'number' && job.opts.attempts > 0 + ? job.opts.attempts + : 1; + return job.attemptsMade + 1 >= attempts; + } +} diff --git a/apps/api/src/modules/analysis/analysis.types.ts b/apps/api/src/modules/analysis/analysis.types.ts new file mode 100644 index 0000000..86e9347 --- /dev/null +++ b/apps/api/src/modules/analysis/analysis.types.ts @@ -0,0 +1,52 @@ +import type { Role } from '@persona-lenz/shared'; +import type { TeamPositionOrUnknown } from '../global/database/schemas'; +import type { RiotPlatformRoute, RiotRegionalRoute } from '../riot/riot.types'; + +export interface UserAnalysisJobData { + gameName: string; + tagLine: string; + platformRoute: RiotPlatformRoute; + regionalRoute: RiotRegionalRoute; + queueId: number; + requestedMatchCount: number; +} + +export interface ExtractedPlayerMatchFeature { + matchId: string; + puuid: string; + queueId: number; + role: TeamPositionOrUnknown; + championId: number; + gameCreation: number; + gameDurationSec: number; + win: boolean; + patch: string; + features: Record; + normalized: Record; + vector: number[]; + featureSchemaVersion: number; + normalizationMethod: 'registry-baseline'; + confidence: number; + timelineProcessed: false; +} + +export interface BuiltPlayerProfile { + puuid: string; + queueId: number; + mainRole: Role; + roleDistribution: Partial>; + sampleGameCount: number; + championPool: Array<{ + championId: number; + games: number; + wins: number; + }>; + profile: Record; + vector: number[]; + featureSchemaVersion: number; + normalizationMethod: 'registry-baseline'; + confidence: number; + patches: string[]; + lastMatchAt?: Date; + isSeed: boolean; +} diff --git a/apps/api/src/modules/analysis/bullmq.config.spec.ts b/apps/api/src/modules/analysis/bullmq.config.spec.ts new file mode 100644 index 0000000..798f1fa --- /dev/null +++ b/apps/api/src/modules/analysis/bullmq.config.spec.ts @@ -0,0 +1,54 @@ +import { + createBullMqOptions, + resolveBullMqRedisConnection, +} from './bullmq.config'; + +describe('BullMQ config', () => { + it('parses redis URLs into BullMQ connection options', () => { + expect( + resolveBullMqRedisConnection('redis://worker:secret@redis.local:6380/2'), + ).toMatchObject({ + host: 'redis.local', + port: 6380, + username: 'worker', + password: 'secret', + db: 2, + maxRetriesPerRequest: null, + }); + }); + + it('enables TLS for rediss URLs', () => { + expect(resolveBullMqRedisConnection('rediss://redis.local')).toMatchObject({ + host: 'redis.local', + port: 6379, + tls: {}, + }); + }); + + it('rejects malformed Redis URLs without an explicit host', () => { + expect(() => resolveBullMqRedisConnection('redis:///0')).toThrow( + 'REDIS_URL must include a hostname.', + ); + }); + + it('creates root options from SecretService without exposing secrets', async () => { + const secretService = { + get: jest + .fn, [string]>() + .mockResolvedValue('redis://localhost:6379/0'), + }; + + await expect(createBullMqOptions(secretService)).resolves.toMatchObject({ + connection: { + host: 'localhost', + port: 6379, + db: 0, + maxRetriesPerRequest: null, + }, + defaultJobOptions: { + attempts: 2, + }, + }); + expect(secretService.get).toHaveBeenCalledWith('REDIS_URL'); + }); +}); diff --git a/apps/api/src/modules/analysis/bullmq.config.ts b/apps/api/src/modules/analysis/bullmq.config.ts new file mode 100644 index 0000000..d13f67b --- /dev/null +++ b/apps/api/src/modules/analysis/bullmq.config.ts @@ -0,0 +1,73 @@ +import type { BullRootModuleOptions } from '@nestjs/bullmq'; +import type { QueueOptions } from 'bullmq'; +import type { SecretService } from '../global/secret/secret.service'; + +export type BullMqConnectionOptions = NonNullable; + +function parseRedisDatabase(pathname: string): number { + const trimmedPath = pathname.replace(/^\/+/u, ''); + if (!trimmedPath) return 0; + + const database = Number(trimmedPath); + if (!Number.isInteger(database) || database < 0) { + throw new Error('REDIS_URL must include a non-negative integer database.'); + } + return database; +} + +export function resolveBullMqRedisConnection( + redisUrl: string, +): BullMqConnectionOptions { + const parsedUrl = new URL(redisUrl); + if (parsedUrl.protocol !== 'redis:' && parsedUrl.protocol !== 'rediss:') { + throw new Error('REDIS_URL must use redis:// or rediss://.'); + } + + if (!parsedUrl.hostname) { + throw new Error('REDIS_URL must include a hostname.'); + } + + const connection: BullMqConnectionOptions = { + host: parsedUrl.hostname, + port: parsedUrl.port ? Number(parsedUrl.port) : 6379, + db: parseRedisDatabase(parsedUrl.pathname), + maxRetriesPerRequest: null, + }; + + if (parsedUrl.username) { + connection.username = decodeURIComponent(parsedUrl.username); + } + if (parsedUrl.password) { + connection.password = decodeURIComponent(parsedUrl.password); + } + if (parsedUrl.protocol === 'rediss:') { + connection.tls = {}; + } + + return connection; +} + +export async function createBullMqOptions( + secretService: Pick, +): Promise { + const redisUrl = await secretService.get('REDIS_URL'); + + return { + connection: resolveBullMqRedisConnection(redisUrl), + defaultJobOptions: { + attempts: 2, + backoff: { + type: 'exponential', + delay: 1_000, + }, + removeOnComplete: { + age: 86_400, + count: 1_000, + }, + removeOnFail: { + age: 604_800, + count: 5_000, + }, + }, + }; +} diff --git a/apps/api/src/modules/analysis/feature-extractor.service.spec.ts b/apps/api/src/modules/analysis/feature-extractor.service.spec.ts new file mode 100644 index 0000000..b7011aa --- /dev/null +++ b/apps/api/src/modules/analysis/feature-extractor.service.spec.ts @@ -0,0 +1,187 @@ +import { FEATURE_SCHEMA_VERSION, METRICS } from '@persona-lenz/shared'; +import { FeatureExtractorService } from './feature-extractor.service'; + +function createMatchPayload(overrides: Record = {}) { + const currentPlayer = { + puuid: 'target-puuid', + teamId: 100, + teamPosition: 'MIDDLE', + championId: 103, + kills: 6, + deaths: 2, + assists: 8, + totalDamageDealtToChampions: 24_000, + goldEarned: 12_000, + totalMinionsKilled: 210, + neutralMinionsKilled: 12, + visionScore: 24, + wardsKilled: 4, + detectorWardsPlaced: 3, + win: true, + challenges: { + kda: 7, + killParticipation: 0.7, + teamDamagePercentage: 0.32, + damagePerMinute: 800, + goldPerMinute: 400, + visionScorePerMinute: 0.8, + controlWardsPlaced: 3, + dragonTakedowns: 1, + baronTakedowns: 1, + riftHeraldTakedowns: 0, + soloKills: 2, + }, + }; + + return { + metadata: { + matchId: 'KR_1', + participants: ['target-puuid', 'ally-puuid', 'enemy-puuid'], + }, + info: { + gameCreation: 1_700_000_000_000, + gameDuration: 1800, + gameVersion: '26.13.123.456', + queueId: 420, + participants: [ + currentPlayer, + { + puuid: 'ally-puuid', + teamId: 100, + teamPosition: 'JUNGLE', + championId: 64, + kills: 10, + totalDamageDealtToChampions: 30_000, + }, + { + puuid: 'enemy-puuid', + teamId: 200, + teamPosition: 'MIDDLE', + championId: 134, + kills: 4, + totalDamageDealtToChampions: 16_000, + }, + ], + ...overrides, + }, + }; +} + +describe('FeatureExtractorService', () => { + const service = new FeatureExtractorService(); + + it('extracts MVP 1 raw metrics and vector values in registry order', () => { + const feature = service.extractPlayerMatchFeature( + createMatchPayload(), + 'target-puuid', + ); + + expect(feature.matchId).toBe('KR_1'); + expect(feature.role).toBe('MIDDLE'); + expect(feature.patch).toBe('26.13'); + expect(feature.features).toMatchObject({ + kda: 7, + killParticipation: 0.7, + damageShare: 0.32, + damagePerMin: 800, + goldPerMin: 400, + csPerMin: 7.4, + visionScorePerMin: 0.8, + wardsKilledPerMin: 4 / 30, + controlWardsPlaced: 3, + deathsPerMin: 2 / 30, + objectiveParticipation: 2, + soloKills: 2, + }); + expect(feature.vector).toEqual( + METRICS.map((metric) => feature.normalized[metric.id]), + ); + expect(feature.vector).toHaveLength(12); + expect(feature.featureSchemaVersion).toBe(FEATURE_SCHEMA_VERSION); + expect(feature.confidence).toBe(1); + }); + + it('falls back to manual calculations when challenges are missing', () => { + const payload = createMatchPayload({ + participants: [ + { + puuid: 'target-puuid', + teamId: 100, + teamPosition: 'MIDDLE', + championId: 103, + kills: 6, + deaths: 2, + assists: 8, + totalDamageDealtToChampions: 24_000, + goldEarned: 12_000, + totalMinionsKilled: 210, + neutralMinionsKilled: 12, + visionScore: 24, + wardsKilled: 4, + detectorWardsPlaced: 3, + win: true, + }, + { + puuid: 'ally-puuid', + teamId: 100, + teamPosition: 'JUNGLE', + championId: 64, + kills: 14, + totalDamageDealtToChampions: 36_000, + }, + ], + }); + + const feature = service.extractPlayerMatchFeature(payload, 'target-puuid'); + + expect(feature.features.kda).toBe(7); + expect(feature.features.killParticipation).toBe(0.7); + expect(feature.features.damageShare).toBe(0.4); + expect(feature.features.damagePerMin).toBe(800); + expect(feature.features.goldPerMin).toBe(400); + expect(feature.features.controlWardsPlaced).toBe(3); + expect(feature.features.objectiveParticipation).toBe(0); + expect(feature.features.soloKills).toBe(0); + }); + + it('marks unknown roles and short games with lower confidence', () => { + const payload = createMatchPayload({ + gameDuration: 900, + participants: [ + { + puuid: 'target-puuid', + teamId: 100, + teamPosition: '', + championId: 103, + kills: 1, + deaths: 2, + assists: 1, + totalDamageDealtToChampions: 5_000, + goldEarned: 6_000, + totalMinionsKilled: 100, + neutralMinionsKilled: 0, + visionScore: 10, + wardsKilled: 1, + detectorWardsPlaced: 0, + win: false, + }, + ], + }); + + const feature = service.extractPlayerMatchFeature(payload, 'target-puuid'); + + expect(feature.role).toBe('UNKNOWN'); + expect(feature.confidence).toBe(0.5); + }); + + it('marks known-role short games with short-game confidence only', () => { + const payload = createMatchPayload({ + gameDuration: 900, + }); + + const feature = service.extractPlayerMatchFeature(payload, 'target-puuid'); + + expect(feature.role).toBe('MIDDLE'); + expect(feature.confidence).toBe(0.8); + }); +}); diff --git a/apps/api/src/modules/analysis/feature-extractor.service.ts b/apps/api/src/modules/analysis/feature-extractor.service.ts new file mode 100644 index 0000000..8c1e105 --- /dev/null +++ b/apps/api/src/modules/analysis/feature-extractor.service.ts @@ -0,0 +1,218 @@ +import { + FEATURE_SCHEMA_VERSION, + METRICS, + type MetricDefinition, + type Role, +} from '@persona-lenz/shared'; +import { Injectable } from '@nestjs/common'; +import type { TeamPositionOrUnknown } from '../global/database/schemas'; +import type { ExtractedPlayerMatchFeature } from './analysis.types'; + +interface MatchParticipant { + assists?: number; + challenges?: Record; + championId?: number; + deaths?: number; + detectorWardsPlaced?: number; + goldEarned?: number; + kills?: number; + neutralMinionsKilled?: number; + puuid?: string; + teamId?: number; + teamPosition?: string; + totalDamageDealtToChampions?: number; + totalMinionsKilled?: number; + visionScore?: number; + wardsKilled?: number; + win?: boolean; +} + +interface MatchInfo { + gameCreation?: number; + gameDuration?: number; + gameVersion?: string; + participants?: MatchParticipant[]; + queueId?: number; +} + +interface MatchPayload { + info?: MatchInfo; + metadata?: { + matchId?: string; + }; +} + +const TEAM_POSITIONS = new Set([ + 'TOP', + 'JUNGLE', + 'MIDDLE', + 'BOTTOM', + 'UTILITY', +]); + +function asRecord(value: unknown): Record { + if (typeof value === 'object' && value !== null && !Array.isArray(value)) { + return value as Record; + } + return {}; +} + +function asNumber(value: unknown, fallback = 0): number { + return typeof value === 'number' && Number.isFinite(value) ? value : fallback; +} + +function fromChallenge( + challenges: Record, + key: string, +): number | undefined { + const value = challenges[key]; + return typeof value === 'number' && Number.isFinite(value) + ? value + : undefined; +} + +function ratio(numerator: number, denominator: number): number { + if (denominator <= 0) return 0; + return numerator / denominator; +} + +function normalizeMetric( + metric: MetricDefinition, + role: Role, + rawValue: number, +): number { + const baseline = metric.baseline?.[role]; + if (!baseline) return Math.max(0, Math.min(1, rawValue)); + return Math.max( + 0, + Math.min(1, (rawValue - baseline.min) / (baseline.max - baseline.min)), + ); +} + +function resolveRole(teamPosition: string | undefined): TeamPositionOrUnknown { + return TEAM_POSITIONS.has(teamPosition as Role) + ? (teamPosition as Role) + : 'UNKNOWN'; +} + +function resolveNormalizationRole(role: TeamPositionOrUnknown): Role { + return role === 'UNKNOWN' ? 'MIDDLE' : role; +} + +function resolvePatch(gameVersion: string | undefined): string { + const [major, minor] = (gameVersion ?? 'unknown').split('.'); + if (!major || !minor) return 'unknown'; + return `${major}.${minor}`; +} + +@Injectable() +export class FeatureExtractorService { + public extractPlayerMatchFeature( + rawMatch: Record, + puuid: string, + ): ExtractedPlayerMatchFeature { + const payload = rawMatch as MatchPayload; + const info = payload.info ?? {}; + const participants = info.participants ?? []; + const participant = participants.find( + (candidate) => candidate.puuid === puuid, + ); + + if (!participant) { + throw new Error(`Participant not found for puuid="${puuid}".`); + } + + const gameDurationSec = asNumber(info.gameDuration); + const gameMinutes = Math.max(gameDurationSec / 60, 1); + const teamId = participant.teamId; + const teamParticipants = participants.filter( + (candidate) => candidate.teamId === teamId, + ); + const teamKills = teamParticipants.reduce( + (sum, candidate) => sum + asNumber(candidate.kills), + 0, + ); + const teamDamage = teamParticipants.reduce( + (sum, candidate) => sum + asNumber(candidate.totalDamageDealtToChampions), + 0, + ); + + const challenges = asRecord(participant.challenges); + const kills = asNumber(participant.kills); + const deaths = asNumber(participant.deaths); + const assists = asNumber(participant.assists); + const damage = asNumber(participant.totalDamageDealtToChampions); + const gold = asNumber(participant.goldEarned); + const minions = + asNumber(participant.totalMinionsKilled) + + asNumber(participant.neutralMinionsKilled); + + const features: Record = { + kda: + fromChallenge(challenges, 'kda') ?? + (kills + assists) / Math.max(deaths, 1), + killParticipation: + fromChallenge(challenges, 'killParticipation') ?? + ratio(kills + assists, teamKills), + damageShare: + fromChallenge(challenges, 'teamDamagePercentage') ?? + ratio(damage, teamDamage), + damagePerMin: + fromChallenge(challenges, 'damagePerMinute') ?? damage / gameMinutes, + goldPerMin: + fromChallenge(challenges, 'goldPerMinute') ?? gold / gameMinutes, + csPerMin: minions / gameMinutes, + visionScorePerMin: + fromChallenge(challenges, 'visionScorePerMinute') ?? + asNumber(participant.visionScore) / gameMinutes, + wardsKilledPerMin: asNumber(participant.wardsKilled) / gameMinutes, + controlWardsPlaced: + fromChallenge(challenges, 'controlWardsPlaced') ?? + asNumber(participant.detectorWardsPlaced), + deathsPerMin: deaths / gameMinutes, + objectiveParticipation: + asNumber(challenges.dragonTakedowns) + + asNumber(challenges.baronTakedowns) + + asNumber(challenges.riftHeraldTakedowns), + soloKills: fromChallenge(challenges, 'soloKills') ?? 0, + }; + + const role = resolveRole(participant.teamPosition); + const normalizationRole = resolveNormalizationRole(role); + const normalized = Object.fromEntries( + METRICS.map((metric) => [ + metric.id, + normalizeMetric(metric, normalizationRole, features[metric.id] ?? 0), + ]), + ); + + return { + matchId: payload.metadata?.matchId ?? '', + puuid, + queueId: asNumber(info.queueId), + role, + championId: asNumber(participant.championId), + gameCreation: asNumber(info.gameCreation), + gameDurationSec, + win: participant.win === true, + patch: resolvePatch(info.gameVersion), + features, + normalized, + vector: METRICS.map((metric) => normalized[metric.id]), + featureSchemaVersion: FEATURE_SCHEMA_VERSION, + normalizationMethod: 'registry-baseline', + confidence: this.resolveConfidence(role, gameDurationSec), + timelineProcessed: false, + }; + } + + private resolveConfidence( + role: TeamPositionOrUnknown, + gameDurationSec: number, + ): number { + let confidence = 1; + if (role === 'UNKNOWN') confidence = Math.min(confidence, 0.5); + if (gameDurationSec < 1200) confidence = Math.min(confidence, 0.8); + return confidence; + } +} diff --git a/apps/api/src/modules/analysis/profile-builder.service.spec.ts b/apps/api/src/modules/analysis/profile-builder.service.spec.ts new file mode 100644 index 0000000..7650953 --- /dev/null +++ b/apps/api/src/modules/analysis/profile-builder.service.spec.ts @@ -0,0 +1,109 @@ +import { FEATURE_SCHEMA_VERSION, METRICS } from '@persona-lenz/shared'; +import type { PlayerMatchFeature } from '../global/database/schemas'; +import { ProfileBuilderService } from './profile-builder.service'; + +function createFeature( + overrides: Partial = {}, +): PlayerMatchFeature { + const normalized = Object.fromEntries( + METRICS.map((metric) => [metric.id, 0.5]), + ); + + 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: 1800, + win: true, + patch: '26.13', + features: {}, + normalized, + vector: METRICS.map((metric) => normalized[metric.id]), + featureSchemaVersion: FEATURE_SCHEMA_VERSION, + normalizationMethod: 'registry-baseline', + confidence: 1, + timelineProcessed: false, + ...overrides, + }; +} + +describe('ProfileBuilderService', () => { + const service = new ProfileBuilderService(); + + it('builds a confidence-weighted profile in registry vector order', () => { + const firstNormalized = Object.fromEntries( + METRICS.map((metric, index) => [metric.id, 0.8 - index * 0.01]), + ); + const secondNormalized = Object.fromEntries( + METRICS.map((metric, index) => [metric.id, 0.2 + index * 0.01]), + ); + const firstFeature = createFeature({ + matchId: 'KR_1', + role: 'MIDDLE', + championId: 103, + win: true, + normalized: firstNormalized, + confidence: 1, + patch: '26.13', + gameCreation: 1_700_000_000_000, + }); + firstFeature.vector = METRICS.map( + (metric) => firstFeature.normalized[metric.id], + ); + + const secondFeature = createFeature({ + matchId: 'KR_2', + role: 'BOTTOM', + championId: 22, + win: false, + normalized: secondNormalized, + confidence: 0.5, + patch: '26.14', + gameCreation: 1_700_100_000_000, + }); + secondFeature.vector = METRICS.map( + (metric) => secondFeature.normalized[metric.id], + ); + + const profile = service.buildProfile({ + features: [firstFeature, secondFeature], + isSeed: false, + puuid: 'target-puuid', + queueId: 420, + }); + + expect(profile.mainRole).toBe('MIDDLE'); + expect(profile.sampleGameCount).toBe(2); + for (const metric of METRICS) { + expect(profile.profile[metric.id]).toBeCloseTo( + (firstNormalized[metric.id] + secondNormalized[metric.id] * 0.5) / 1.5, + ); + } + expect(profile.vector).toEqual( + METRICS.map((metric) => profile.profile[metric.id]), + ); + expect(profile.championPool).toEqual([ + { championId: 103, games: 1, wins: 1 }, + { championId: 22, games: 1, wins: 0 }, + ]); + expect(profile.patches).toEqual(['26.13', '26.14']); + expect(profile.lastMatchAt).toEqual(new Date(1_700_100_000_000)); + expect(profile.confidence).toBeCloseTo(0.3); + }); + + it('throws when no features are available', () => { + expect(() => + service.buildProfile({ + features: [], + isSeed: false, + puuid: 'target-puuid', + queueId: 420, + }), + ).toThrow('Cannot build a profile without match features.'); + }); +}); diff --git a/apps/api/src/modules/analysis/profile-builder.service.ts b/apps/api/src/modules/analysis/profile-builder.service.ts new file mode 100644 index 0000000..d3f1dc1 --- /dev/null +++ b/apps/api/src/modules/analysis/profile-builder.service.ts @@ -0,0 +1,137 @@ +import { + FEATURE_SCHEMA_VERSION, + METRICS, + type Role, +} from '@persona-lenz/shared'; +import { Injectable } from '@nestjs/common'; +import type { PlayerMatchFeature } from '../global/database/schemas'; +import type { BuiltPlayerProfile } from './analysis.types'; + +type ProfileSourceFeature = Pick< + PlayerMatchFeature, + | 'championId' + | 'confidence' + | 'gameCreation' + | 'normalized' + | 'patch' + | 'role' + | 'win' +>; + +export interface BuildProfileParams { + features: ProfileSourceFeature[]; + isSeed: boolean; + puuid: string; + queueId: number; +} + +const ROLE_ORDER: Role[] = ['TOP', 'JUNGLE', 'MIDDLE', 'BOTTOM', 'UTILITY']; + +function isRole(value: string): value is Role { + return ROLE_ORDER.includes(value as Role); +} + +@Injectable() +export class ProfileBuilderService { + public buildProfile(params: BuildProfileParams): BuiltPlayerProfile { + if (params.features.length === 0) { + throw new Error('Cannot build a profile without match features.'); + } + + const totalConfidence = params.features.reduce( + (sum, feature) => sum + feature.confidence, + 0, + ); + const safeTotalConfidence = Math.max(totalConfidence, Number.EPSILON); + + const profile = Object.fromEntries( + METRICS.map((metric) => [ + metric.id, + params.features.reduce( + (sum, feature) => + sum + (feature.normalized[metric.id] ?? 0) * feature.confidence, + 0, + ) / safeTotalConfidence, + ]), + ); + + const roleCounts = new Map(); + const championPool = new Map< + number, + { championId: number; games: number; wins: number } + >(); + + for (const feature of params.features) { + if (isRole(feature.role)) { + roleCounts.set(feature.role, (roleCounts.get(feature.role) ?? 0) + 1); + } + + const championEntry = championPool.get(feature.championId) ?? { + championId: feature.championId, + games: 0, + wins: 0, + }; + championEntry.games += 1; + if (feature.win) championEntry.wins += 1; + championPool.set(feature.championId, championEntry); + } + + const knownRoleGameCount = [...roleCounts.values()].reduce( + (sum, count) => sum + count, + 0, + ); + const roleDistribution = Object.fromEntries( + [...roleCounts.entries()].map(([role, count]) => [ + role, + count / Math.max(knownRoleGameCount, 1), + ]), + ) as Partial>; + + const averageConfidence = totalConfidence / params.features.length; + const sampleSizeConfidenceMultiplier = Math.min( + params.features.length / 5, + 1, + ); + + return { + puuid: params.puuid, + queueId: params.queueId, + mainRole: this.resolveMainRole(roleCounts), + roleDistribution, + sampleGameCount: params.features.length, + championPool: [...championPool.values()].sort( + (left, right) => + right.games - left.games || + right.wins - left.wins || + left.championId - right.championId, + ), + profile, + vector: METRICS.map((metric) => profile[metric.id]), + featureSchemaVersion: FEATURE_SCHEMA_VERSION, + normalizationMethod: 'registry-baseline', + confidence: averageConfidence * sampleSizeConfidenceMultiplier, + patches: [ + ...new Set(params.features.map((feature) => feature.patch)), + ].sort(), + lastMatchAt: new Date( + Math.max(...params.features.map((feature) => feature.gameCreation)), + ), + isSeed: params.isSeed, + }; + } + + private resolveMainRole(roleCounts: Map): Role { + let selectedRole: Role = 'MIDDLE'; + let selectedCount = -1; + + for (const role of ROLE_ORDER) { + const count = roleCounts.get(role) ?? 0; + if (count > selectedCount) { + selectedRole = role; + selectedCount = count; + } + } + + return selectedRole; + } +} diff --git a/apps/api/test/app.e2e-spec.ts b/apps/api/test/app.e2e-spec.ts index 007e73f..8bd0266 100644 --- a/apps/api/test/app.e2e-spec.ts +++ b/apps/api/test/app.e2e-spec.ts @@ -1,11 +1,14 @@ +import { getQueueToken } from '@nestjs/bullmq'; import { Test, TestingModule } from '@nestjs/testing'; import { INestApplication } from '@nestjs/common'; import request from 'supertest'; import { App } from 'supertest/types'; import { configureApp } from './../src/app.setup'; import { AppModule } from './../src/app.module'; +import { ANALYSIS_QUEUE_NAME } from './../src/modules/analysis/analysis.constants'; import { ConnectionService } from './../src/modules/global/database/connection.service'; import { REDIS_CLIENT } from './../src/modules/global/redis/redis.constants'; +import { SecretService } from './../src/modules/global/secret/secret.service'; describe('AppController (e2e)', () => { let app: INestApplication; @@ -16,11 +19,25 @@ describe('AppController (e2e)', () => { }) .overrideProvider(ConnectionService) .useValue({}) + .overrideProvider(SecretService) + .useValue({ + get: jest.fn((key: string) => { + if (key === 'REDIS_URL') { + return Promise.resolve('redis://localhost:6379/0'); + } + return Promise.resolve(''); + }), + ready: jest.fn().mockResolvedValue(undefined), + }) .overrideProvider(REDIS_CLIENT) .useValue({ destroy: jest.fn(), quit: jest.fn().mockResolvedValue(undefined), }) + .overrideProvider(getQueueToken(ANALYSIS_QUEUE_NAME)) + .useValue({ + add: jest.fn(), + }) .compile(); app = moduleFixture.createNestApplication(); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 411a3cb..235314b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -17,6 +17,9 @@ importers: '@infisical/sdk': specifier: ^5.0.2 version: 5.0.2 + '@nestjs/bullmq': + specifier: ^11.0.4 + version: 11.0.4(@nestjs/common@11.1.27(reflect-metadata@0.2.2)(rxjs@7.8.2))(@nestjs/core@11.1.27)(bullmq@5.79.2(redis@5.12.1)) '@nestjs/common': specifier: ^11.0.1 version: 11.1.27(reflect-metadata@0.2.2)(rxjs@7.8.2) @@ -32,6 +35,9 @@ importers: '@persona-lenz/shared': specifier: workspace:* version: link:../../packages/shared + bullmq: + specifier: ^5.79.2 + version: 5.79.2(redis@5.12.1) mongoose: specifier: ^9.7.3 version: 9.7.3(@aws-sdk/credential-providers@3.993.0) @@ -758,6 +764,9 @@ packages: '@types/node': optional: true + '@ioredis/commands@1.5.1': + resolution: {integrity: sha512-JH8ZL/ywcJyR9MmJ5BNqZllXNZQqQbnVZOqpPQqE1vHiFgAw4NHbvE0FOduNU8IX9babitBT46571OnPTT0Zcw==} + '@isaacs/cliui@8.0.2': resolution: {integrity: sha512-O8jcjabXaleOG9DQ0+ARXWZBTfnP4WNAqzuiJK7ll44AmxGKv/J2M4TPjxjY3znBCfvBXFzucm1twdyFybFqEA==} engines: {node: '>=12'} @@ -881,12 +890,55 @@ packages: '@mongodb-js/saslprep@1.4.12': resolution: {integrity: sha512-QAfAMwNgnYxZ2C6D1HgeP7Gc4i/uvJRim415PCIL9ptRxWMNbWeLBYb2/9R4pGKny/s1FVu2JA2cxCUBUOggrA==} + '@msgpackr-extract/msgpackr-extract-darwin-arm64@3.0.4': + resolution: {integrity: sha512-LCkGo6JDfaBhgST7UpPWgNgLINpcpabaHfyz5OBx75nUYxBsaEPxjnyNjWpeb/xBup/682QnBfRBy2/LvPutZQ==} + cpu: [arm64] + os: [darwin] + + '@msgpackr-extract/msgpackr-extract-darwin-x64@3.0.4': + resolution: {integrity: sha512-zExlW9zUJKZH/tOtVMttwjKa4Xm/3KcNjnE3dPN92uCktwavMxpgCA3MoJK/DOnTWsQgo224OaST27/mPNAf+w==} + cpu: [x64] + os: [darwin] + + '@msgpackr-extract/msgpackr-extract-linux-arm64@3.0.4': + resolution: {integrity: sha512-dgX0P/9wGPJeHFBG+ZmhgE6bmtMt7NP5CRBGyyktpopdk/mW4POnrpQsSLtKI1dwpc+pPLuXHDh6vvskyQE/sw==} + cpu: [arm64] + os: [linux] + + '@msgpackr-extract/msgpackr-extract-linux-arm@3.0.4': + resolution: {integrity: sha512-Tg3yX65f5GbtXLkrYEHE5oibZG9epyYWas7FogTTEJeDEF9JlXJzKgXaNhT3UXlTOeA+AfZpYZYZ0uPj7Cfquw==} + cpu: [arm] + os: [linux] + + '@msgpackr-extract/msgpackr-extract-linux-x64@3.0.4': + resolution: {integrity: sha512-8TNXMEjJc3QEy7R/x1INhgiU+XakDAFUzBhaz7+Rbrs8NH5UQeHQxxmzsSBJGyV6I1jW79undiQm8tOI+D+8FQ==} + cpu: [x64] + os: [linux] + + '@msgpackr-extract/msgpackr-extract-win32-x64@3.0.4': + resolution: {integrity: sha512-CmCXPQrkbwExx3j946/PtHWHbYJiCRBRDl4BlkRQcJB/YOwQxJRTpoo7aTsortjgoJ1x7opzTSxn7C+ASSLVjQ==} + cpu: [x64] + os: [win32] + '@napi-rs/wasm-runtime@1.1.6': resolution: {integrity: sha512-ZLv/JdUfkvOy9eCnnBaGfiO+XimbjebAeO+MRQqD/B+FR1tnRN0tpKSJHRbE8sFfS6aqsXZ67TQjfwfsxULVbg==} peerDependencies: '@emnapi/core': ^1.7.1 '@emnapi/runtime': ^1.7.1 + '@nestjs/bull-shared@11.0.4': + resolution: {integrity: sha512-VBJcDHSAzxQnpcDfA0kt9MTGUD1XZzfByV70su0W0eDCQ9aqIEBlzWRW21tv9FG9dIut22ysgDidshdjlnczLw==} + peerDependencies: + '@nestjs/common': ^10.0.0 || ^11.0.0 + '@nestjs/core': ^10.0.0 || ^11.0.0 + + '@nestjs/bullmq@11.0.4': + resolution: {integrity: sha512-wBzK9raAVG0/6NTMdvLGM4/FQ1lsB35/pYS8L6a0SDgkTiLpd7mAjQ8R692oMx5s7IjvgntaZOuTUrKYLNfIkA==} + peerDependencies: + '@nestjs/common': ^10.0.0 || ^11.0.0 + '@nestjs/core': ^10.0.0 || ^11.0.0 + bullmq: ^3.0.0 || ^4.0.0 || ^5.0.0 + '@nestjs/cli@11.0.23': resolution: {integrity: sha512-2V0Bf5jz0KXhUZk3eJi9GljIyqH04otwsE/mYLbqJR+X0iiYx+6bkNJ2Qz28uHNFj1cpHgimf9xDzHkqarie0g==} engines: {node: '>= 20.11'} @@ -1823,6 +1875,15 @@ packages: buffer@5.7.1: resolution: {integrity: sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==} + bullmq@5.79.2: + resolution: {integrity: sha512-FebD+8XCZl/hnS1R4to24L4EAN70XSndKZO0776M36vGRk5MKVhKlNFM8/34zXLXKyYB4QaeIPFhVXSYYGTHpQ==} + engines: {node: '>=12.22.0'} + peerDependencies: + redis: '>=5.0.0' + peerDependenciesMeta: + redis: + optional: true + busboy@1.6.0: resolution: {integrity: sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==} engines: {node: '>=10.16.0'} @@ -1989,6 +2050,10 @@ packages: create-require@1.1.1: resolution: {integrity: sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==} + cron-parser@4.9.0: + resolution: {integrity: sha512-p0SaNjrHOnQeR8/VnfGbmg9te2kfyYSQ7Sc/j/6DtPL3JQvKxmjO9TSjNFpujqV3vEYYBvNNvXSxzyksBWAx1Q==} + engines: {node: '>=12.0.0'} + cross-spawn@7.0.6: resolution: {integrity: sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==} engines: {node: '>= 8'} @@ -2027,6 +2092,10 @@ packages: resolution: {integrity: sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==} engines: {node: '>=0.4.0'} + denque@2.1.0: + resolution: {integrity: sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==} + engines: {node: '>=0.10'} + depd@2.0.0: resolution: {integrity: sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==} engines: {node: '>= 0.8'} @@ -2492,6 +2561,10 @@ packages: inherits@2.0.4: resolution: {integrity: sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==} + ioredis@5.10.1: + resolution: {integrity: sha512-HuEDBTI70aYdx1v6U97SbNx9F1+svQKBDo30o0b9fw055LMepzpOOd0Ccg9Q6tbqmBSJaMuY0fB7yw9/vjBYCA==} + engines: {node: '>=12.22.0'} + ipaddr.js@1.9.1: resolution: {integrity: sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==} engines: {node: '>= 0.10'} @@ -2842,6 +2915,12 @@ packages: resolution: {integrity: sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==} engines: {node: '>=10'} + lodash.defaults@4.2.0: + resolution: {integrity: sha512-qjxPLHd3r5DnsdGacqOMU6pb/avJzdh9tFX2ymgoZE27BmjXrNy/y4LoaiTeAb+O3gL8AfpJGtqfX/ae2leYYQ==} + + lodash.isarguments@3.1.0: + resolution: {integrity: sha512-chi4NHZlZqZD18a0imDHnZPrDeBbTtVN7GXMwuGdRH9qotxAjYs3aVLKc7zNOG9eddR5Ksd8rvFEBc9SsggPpg==} + lodash.memoize@4.1.2: resolution: {integrity: sha512-t7j+NzmgnQzTAYXcsHYLgimltOV1MXHtlOWf6GjL9Kj8GK5FInw5JotxvbOs+IvV1/Dzo04/fCGfLVs7aXb4Ag==} @@ -2865,6 +2944,10 @@ packages: lru-cache@5.1.1: resolution: {integrity: sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==} + luxon@3.7.2: + resolution: {integrity: sha512-vtEhXh/gNjI9Yg1u4jX/0YVPMvxzHuGgCm6tC5kZyb08yjGWGnqAjGJvcXbqQR2P3MyMEFnRbpcdFS6PBcLqew==} + engines: {node: '>=12'} + magic-string@0.30.17: resolution: {integrity: sha512-sNPKHvyjVf7gyjwS4xGTaW/mCnF8wnjtifKBEhxfZ7E/S8tQ0rssrwGNn6q8JH/ohItJfSQp9mBtQYuTlH5QnA==} @@ -2997,6 +3080,13 @@ packages: ms@2.1.3: resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==} + msgpackr-extract@3.0.4: + resolution: {integrity: sha512-4kmO/MdyUIkLIvTPr8VHLil4AtoKIoniWPIEk5+CDy0xnWC84azhSFmuJ7PxZdsYtiP5kEeQsORAVIeMgxT+Hw==} + hasBin: true + + msgpackr@2.0.4: + resolution: {integrity: sha512-o1C5KRmuRt+apqMr1HuGSqWStZoRBUpEsCsl15uM9VdAF1qHLtvMOU2En747EnTyEl6c4pzPewRMFF31s1CNbA==} + multer@2.1.1: resolution: {integrity: sha512-mo+QTzKlx8R7E5ylSXxWzGoXoZbOsRMpyitcht8By2KHvMbf3tjwosZ/Mu/XYU6UuJ3VZnODIrak5ZrPiPyB6A==} engines: {node: '>= 10.16.0'} @@ -3031,6 +3121,10 @@ packages: node-emoji@1.11.0: resolution: {integrity: sha512-wo2DpQkQp7Sjm2A0cq+sN7EHKO6Sl0ctXeBdFZrL9T9+UywORbufTcTZxom8YqpLQt/FqNMUkOpkZrJVYSKD3A==} + node-gyp-build-optional-packages@5.2.2: + resolution: {integrity: sha512-s+w+rBWnpTMwSFbaE0UXsRlg7hU4FjekKU4eyAih5T8nJuNZT1nNsskXpxmeqSK9UzkBl6UgRlnKc8hz8IEqOw==} + hasBin: true + node-int64@0.4.0: resolution: {integrity: sha512-O5lz91xSOeoXP6DulyHfllpq+Eg00MWitZIbtPfoSEvqIHdl5gfcY6hYzDWnj0qD5tz52PI08u9qUvSVeUBeHw==} @@ -3229,6 +3323,14 @@ packages: resolution: {integrity: sha512-GDhwkLfywWL2s6vEjyhri+eXmfH6j1L7JE27WhqLeYzoh/A3DBaYGEj2H/HFZCn/kMfim73FXxEJTw06WtxQwg==} engines: {node: '>= 14.18.0'} + redis-errors@1.2.0: + resolution: {integrity: sha512-1qny3OExCf0UvUV/5wpYKf2YwPcOqXzkwKKSmKHiE6ZMQs5heeE/c8eXK+PNllPvmjgAbfnsbpkGZWy8cBpn9w==} + engines: {node: '>=4'} + + redis-parser@3.0.0: + resolution: {integrity: sha512-DJnGAeenTdpMEH6uAJRK/uiyEIH9WVsUmoLwzudwGJUwZPp80PDBWPHXSAGNPwNvIXAbe7MSUB1zQFugFml66A==} + engines: {node: '>=4'} + redis@5.12.1: resolution: {integrity: sha512-LDsoVvb/CpoV9EN3FXvgvSHNJWuCIzl9MiO3ppOevuGLpSGJhwfQjpEwfFJcQvNSddHADDdZaWx0HnmMxRXG7g==} engines: {node: '>= 18.19.0'} @@ -3382,6 +3484,9 @@ packages: resolution: {integrity: sha512-XlkWvfIm6RmsWtNJx+uqtKLS8eqFbxUg0ZzLXqY0caEy9l7hruX8IpiDnjsLavoBgqCCR71TqWO8MaXYheJ3RQ==} engines: {node: '>=10'} + standard-as-callback@2.1.0: + resolution: {integrity: sha512-qoRRSyROncaz1z0mvYqIE4lCd9p2R90i6GxW3uZv5ucSu8tU7B5HXUP1gG8pVZsYNVaXjk8ClXHPttLyxAL48A==} + statuses@2.0.2: resolution: {integrity: sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw==} engines: {node: '>= 0.8'} @@ -4699,6 +4804,8 @@ snapshots: optionalDependencies: '@types/node': 24.13.2 + '@ioredis/commands@1.5.1': {} + '@isaacs/cliui@8.0.2': dependencies: string-width: 5.1.2 @@ -4931,6 +5038,24 @@ snapshots: dependencies: sparse-bitfield: 3.0.3 + '@msgpackr-extract/msgpackr-extract-darwin-arm64@3.0.4': + optional: true + + '@msgpackr-extract/msgpackr-extract-darwin-x64@3.0.4': + optional: true + + '@msgpackr-extract/msgpackr-extract-linux-arm64@3.0.4': + optional: true + + '@msgpackr-extract/msgpackr-extract-linux-arm@3.0.4': + optional: true + + '@msgpackr-extract/msgpackr-extract-linux-x64@3.0.4': + optional: true + + '@msgpackr-extract/msgpackr-extract-win32-x64@3.0.4': + optional: true + '@napi-rs/wasm-runtime@1.1.6(@emnapi/core@1.10.0)(@emnapi/runtime@1.10.0)': dependencies: '@emnapi/core': 1.10.0 @@ -4945,6 +5070,20 @@ snapshots: '@tybys/wasm-util': 0.10.3 optional: true + '@nestjs/bull-shared@11.0.4(@nestjs/common@11.1.27(reflect-metadata@0.2.2)(rxjs@7.8.2))(@nestjs/core@11.1.27)': + dependencies: + '@nestjs/common': 11.1.27(reflect-metadata@0.2.2)(rxjs@7.8.2) + '@nestjs/core': 11.1.27(@nestjs/common@11.1.27(reflect-metadata@0.2.2)(rxjs@7.8.2))(@nestjs/platform-express@11.1.27)(reflect-metadata@0.2.2)(rxjs@7.8.2) + tslib: 2.8.1 + + '@nestjs/bullmq@11.0.4(@nestjs/common@11.1.27(reflect-metadata@0.2.2)(rxjs@7.8.2))(@nestjs/core@11.1.27)(bullmq@5.79.2(redis@5.12.1))': + dependencies: + '@nestjs/bull-shared': 11.0.4(@nestjs/common@11.1.27(reflect-metadata@0.2.2)(rxjs@7.8.2))(@nestjs/core@11.1.27) + '@nestjs/common': 11.1.27(reflect-metadata@0.2.2)(rxjs@7.8.2) + '@nestjs/core': 11.1.27(@nestjs/common@11.1.27(reflect-metadata@0.2.2)(rxjs@7.8.2))(@nestjs/platform-express@11.1.27)(reflect-metadata@0.2.2)(rxjs@7.8.2) + bullmq: 5.79.2(redis@5.12.1) + tslib: 2.8.1 + '@nestjs/cli@11.0.23(@types/node@24.13.2)(prettier@3.9.4)': dependencies: '@angular-devkit/core': 19.2.27(chokidar@4.0.3) @@ -5999,6 +6138,19 @@ snapshots: base64-js: 1.5.1 ieee754: 1.2.1 + bullmq@5.79.2(redis@5.12.1): + dependencies: + cron-parser: 4.9.0 + ioredis: 5.10.1 + msgpackr: 2.0.4 + node-abort-controller: 3.1.1 + semver: 7.8.5 + tslib: 2.8.1 + optionalDependencies: + redis: 5.12.1 + transitivePeerDependencies: + - supports-color + busboy@1.6.0: dependencies: streamsearch: 1.1.0 @@ -6130,6 +6282,10 @@ snapshots: create-require@1.1.1: {} + cron-parser@4.9.0: + dependencies: + luxon: 3.7.2 + cross-spawn@7.0.6: dependencies: path-key: 3.1.1 @@ -6154,6 +6310,8 @@ snapshots: delayed-stream@1.0.0: {} + denque@2.1.0: {} + depd@2.0.0: {} detect-libc@2.1.2: {} @@ -6691,6 +6849,20 @@ snapshots: inherits@2.0.4: {} + ioredis@5.10.1: + dependencies: + '@ioredis/commands': 1.5.1 + cluster-key-slot: 1.1.2 + debug: 4.4.3 + denque: 2.1.0 + lodash.defaults: 4.2.0 + lodash.isarguments: 3.1.0 + redis-errors: 1.2.0 + redis-parser: 3.0.0 + standard-as-callback: 2.1.0 + transitivePeerDependencies: + - supports-color + ipaddr.js@1.9.1: {} is-arrayish@0.2.1: {} @@ -7181,6 +7353,10 @@ snapshots: dependencies: p-locate: 5.0.0 + lodash.defaults@4.2.0: {} + + lodash.isarguments@3.1.0: {} + lodash.memoize@4.1.2: {} lodash.merge@4.6.2: {} @@ -7200,6 +7376,8 @@ snapshots: dependencies: yallist: 3.1.1 + luxon@3.7.2: {} + magic-string@0.30.17: dependencies: '@jridgewell/sourcemap-codec': 1.5.5 @@ -7301,6 +7479,22 @@ snapshots: ms@2.1.3: {} + msgpackr-extract@3.0.4: + dependencies: + node-gyp-build-optional-packages: 5.2.2 + optionalDependencies: + '@msgpackr-extract/msgpackr-extract-darwin-arm64': 3.0.4 + '@msgpackr-extract/msgpackr-extract-darwin-x64': 3.0.4 + '@msgpackr-extract/msgpackr-extract-linux-arm': 3.0.4 + '@msgpackr-extract/msgpackr-extract-linux-arm64': 3.0.4 + '@msgpackr-extract/msgpackr-extract-linux-x64': 3.0.4 + '@msgpackr-extract/msgpackr-extract-win32-x64': 3.0.4 + optional: true + + msgpackr@2.0.4: + optionalDependencies: + msgpackr-extract: 3.0.4 + multer@2.1.1: dependencies: append-field: 1.0.0 @@ -7326,6 +7520,11 @@ snapshots: dependencies: lodash: 4.18.1 + node-gyp-build-optional-packages@5.2.2: + dependencies: + detect-libc: 2.1.2 + optional: true + node-int64@0.4.0: {} node-releases@2.0.50: {} @@ -7505,6 +7704,12 @@ snapshots: readdirp@4.1.2: {} + redis-errors@1.2.0: {} + + redis-parser@3.0.0: + dependencies: + redis-errors: 1.2.0 + redis@5.12.1: dependencies: '@redis/bloom': 5.12.1(@redis/client@5.12.1) @@ -7694,6 +7899,8 @@ snapshots: dependencies: escape-string-regexp: 2.0.0 + standard-as-callback@2.1.0: {} + statuses@2.0.2: {} streamsearch@1.1.0: {}