@@ -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