From a99c40b54cde8611e84ddcf2ad8d2dc73b555c96 Mon Sep 17 00:00:00 2001 From: halaprix Date: Fri, 24 May 2024 08:58:04 +0200 Subject: [PATCH] feat: Group and chunk user points for efficient processing --- .../update-rays-cron-function/src/index.ts | 41 ++++++++++++++++--- .../src/point-accrual.ts | 22 +++++++++- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/background-jobs/update-rays-cron-function/src/index.ts b/background-jobs/update-rays-cron-function/src/index.ts index a5b7d6fac6..b11316ed48 100644 --- a/background-jobs/update-rays-cron-function/src/index.ts +++ b/background-jobs/update-rays-cron-function/src/index.ts @@ -173,11 +173,9 @@ export const handler = async ( await checkMigrationEligibility(db, sortedPoints) await checkOpenedPositionEligibility(db, sortedPoints) - // split points by 30 item chunks - const chunkedPoints = [] - for (let i = 0; i < sortedPoints.length; i += 30) { - chunkedPoints.push(sortedPoints.slice(i, i + 30)) - } + + const chunkedPoints: PositionPoints[] = createChunksOfUserPointsDistributions(sortedPoints, 30) + for (let i = 0; i < chunkedPoints.length; i++) { logger.info(`Processing: Chunk ${i} of ${chunkedPoints.length}`) const chunk = chunkedPoints[i] @@ -458,6 +456,39 @@ export const handler = async ( export default handler +/** + * Creates chunks of user points distributions based on a given chunk length. + * + * @remarks This function is used to create chunks of user points distributions based on a given chunk length + * and the fact each points distributions per user can't be split between chunks. + * + * @param sortedPoints - An array of points sorted by user. + * @param chunkLength - The desired length of each chunk. + * @returns An array of chunks, where each chunk contains points for each user. + */ +function createChunksOfUserPointsDistributions(sortedPoints: PositionPoints, chunkLength: number) { + // Create a map where the keys are the users and the values are arrays of points for each user. + const pointsByUser = new Map() + for (const point of sortedPoints) { + const userPoints = pointsByUser.get(point.user) || [] + userPoints.push(point) + pointsByUser.set(point.user, userPoints) + } + const chunkedPoints: PositionPoints[] = [] + let currentChunk: PositionPoints = [] + for (const userPoints of pointsByUser.values()) { + if (currentChunk.length + userPoints.length >= chunkLength) { + chunkedPoints.push(currentChunk) + currentChunk = [] + } + currentChunk.push(...userPoints) + } + if (currentChunk.length > 0) { + chunkedPoints.push(currentChunk) + } + return chunkedPoints +} + /** * Checks the migration eligibility for point distributions. * @param db - The database instance. diff --git a/background-jobs/update-rays-cron-function/src/point-accrual.ts b/background-jobs/update-rays-cron-function/src/point-accrual.ts index 07a30b9c31..6068e2185c 100644 --- a/background-jobs/update-rays-cron-function/src/point-accrual.ts +++ b/background-jobs/update-rays-cron-function/src/point-accrual.ts @@ -9,7 +9,27 @@ import { UsersData, } from '@summerfi/summer-events-subgraph' import { Logger } from '@aws-lambda-powertools/logger' - +export type PositionPointsItem = { + positionId: string + vaultId: number + user: string + protocol: string + marketId: string + positionCreated: number + points: { + openPositionsPoints: number + migrationPoints: number + swapPoints: number + } + netValue: number + multipliers: { + protocolBoostMultiplier: number + swapMultiplier: number + timeOpenMultiplier: number + automationProtectionMultiplier: number + lazyVaultMultiplier: number + } +} export type PositionPoints = { positionId: string vaultId: number