Skip to content

Commit

Permalink
feat: Group and chunk user points for efficient processing (#292)
Browse files Browse the repository at this point in the history
This Pull Request introduces an enhancement to the way we handle user
points in our system. Previously, we processed user points individually,
which was not efficient when dealing with large volumes of data.

In this PR, we have implemented a new approach where we first group the
points by user. This ensures that all points for a specific user are
processed together, improving the coherence of our data handling.

Furthermore, we have introduced a chunking mechanism. The chunking
mechanism divides the grouped points into chunks of 30 or more. The key
aspect of this chunking mechanism is that it ensures all points for a
user are in the same chunk. This means that the chunk size doesn't have
to be strictly 30 but the first possible size above 30 that can
accommodate all points for the last processed user.

This new approach not only improves the efficiency of our data
processing but also ensures that all points for a specific user are
handled together, thereby maintaining user-specific context.

Changes include:
- Grouping points by user before processing
- Dividing grouped points into chunks of size 30 or more
- Ensuring all points for a user are in the same chunk
- Adjusting chunk size to accommodate all points for the last processed
user

This PR is expected to significantly improve the efficiency and
reliability of our user points handling process.
  • Loading branch information
halaprix authored May 24, 2024
1 parent 9d643bc commit 16ca1c3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
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

0 comments on commit 16ca1c3

Please sign in to comment.