Skip to content

Commit 8d136e2

Browse files
committed
Group jobs by hodler before splitting them into batches
1 parent 791ef00 commit 8d136e2

2 files changed

Lines changed: 30 additions & 19 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "staking-rewards-controller",
3-
"version": "0.1.0",
3+
"version": "0.1.2",
44
"description": "",
55
"contributors": [
66
"ANYONE Protocol Labs",

src/distribution/distribution.service.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,37 @@ export class DistributionService {
5454
}
5555

5656
public groupScoreJobs(data: ScoreData[]): ScoreData[][] {
57-
const result = data.reduce<ScoreData[][]>((curr, score): ScoreData[][] => {
58-
if (curr.length == 0) {
59-
curr.push([score])
60-
} else {
61-
if (curr[curr.length - 1].length < DistributionService.scoresPerBatch) {
62-
const last = curr.pop()
63-
if (last != undefined) {
64-
last.push(score)
65-
curr.push(last)
66-
} else {
67-
this.logger.error('Last element not found, this should not happen')
68-
}
69-
} else {
70-
curr.push([score])
71-
}
57+
const scoresByHodler = new Map<string, ScoreData[]>()
58+
for (const score of data) {
59+
const hodlerScores = scoresByHodler.get(score.Hodler) || []
60+
hodlerScores.push(score)
61+
scoresByHodler.set(score.Hodler, hodlerScores)
62+
}
63+
64+
const hodlerGroups = Array.from(scoresByHodler.values())
65+
66+
const result: ScoreData[][] = []
67+
let currentBatch: ScoreData[] = []
68+
69+
for (const hodlerScores of hodlerGroups) {
70+
if (
71+
currentBatch.length > 0 &&
72+
currentBatch.length + hodlerScores.length > DistributionService.scoresPerBatch
73+
) {
74+
result.push(currentBatch)
75+
currentBatch = []
7276
}
73-
return curr
74-
}, [])
7577

76-
this.logger.debug(`Created ${result.length} groups out of ${data.length}`)
78+
currentBatch.push(...hodlerScores)
79+
}
80+
81+
if (currentBatch.length > 0) {
82+
result.push(currentBatch)
83+
}
84+
85+
this.logger.debug(
86+
`Created ${result.length} groups out of ${data.length} scores from ${hodlerGroups.length} hodlers`
87+
)
7788

7889
return result
7990
}

0 commit comments

Comments
 (0)