Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Group and chunk user points for efficient processing #292

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions background-jobs/update-rays-cron-function/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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<string, PositionPoints>()
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.
Expand Down
22 changes: 21 additions & 1 deletion background-jobs/update-rays-cron-function/src/point-accrual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading