@@ -17,37 +17,37 @@ type Constructor = {
17
17
analyticsApi : typeof analyticsApi ;
18
18
apiKey : string ;
19
19
gitService : GITService ;
20
- repoPath : string ;
20
+ repoPaths : string [ ] ;
21
21
userId : string ;
22
22
} ;
23
23
24
24
class AnalyticsService {
25
25
private analyticsApi : typeof analyticsApi ;
26
26
private apiKey : string ;
27
27
private gitService : GITService ;
28
- private repoPath : string ;
28
+ private repoPaths : string [ ] ;
29
29
private userId : string ;
30
30
31
31
public constructor ( {
32
32
analyticsApi,
33
33
apiKey,
34
34
gitService,
35
- repoPath ,
35
+ repoPaths ,
36
36
userId,
37
37
} : Constructor ) {
38
38
this . analyticsApi = analyticsApi ;
39
39
this . apiKey = apiKey ;
40
40
this . gitService = gitService ;
41
- this . repoPath = repoPath ;
41
+ this . repoPaths = repoPaths ;
42
42
this . userId = userId ;
43
43
}
44
44
45
- private async collectStatsByRepository ( ) : Promise <
45
+ private async collectStatsByRepository ( repoPath : string ) : Promise <
46
46
ActivityLogCreateItemRequestDto [ ]
47
47
> {
48
48
const stats : ActivityLogCreateItemRequestDto [ ] = [ ] ;
49
49
const shortLogResult = await executeCommand (
50
- this . gitService . getShortLogCommand ( this . repoPath , "midnight" ) ,
50
+ this . gitService . getShortLogCommand ( repoPath , "midnight" ) ,
51
51
) ;
52
52
53
53
const commitItems : CommitStatistics [ ] = [ ] ;
@@ -77,15 +77,20 @@ class AnalyticsService {
77
77
return stats ;
78
78
}
79
79
80
- private async fetchRepository ( ) : Promise < void > {
81
- await executeCommand ( this . gitService . getFetchCommand ( this . repoPath ) ) ;
82
- logger . info ( `Fetched latest updates for repo at path: ${ this . repoPath } ` ) ;
80
+ private async fetchRepository ( repoPath : string ) : Promise < void > {
81
+ await executeCommand ( this . gitService . getFetchCommand ( repoPath ) ) ;
82
+ logger . info ( `Fetched latest updates for repo at path: ${ repoPath } ` ) ;
83
83
}
84
84
85
85
public async collectAndSendStats ( ) : Promise < void > {
86
86
try {
87
- await this . fetchRepository ( ) ;
88
- const stats = await this . collectStatsByRepository ( ) ;
87
+ const statsAll = [ ]
88
+ for ( const repoPath of this . repoPaths ) {
89
+ await this . fetchRepository ( repoPath ) ;
90
+ statsAll . push ( ...await this . collectStatsByRepository ( repoPath ) )
91
+ }
92
+
93
+ const stats = mergeStats ( statsAll ) ;
89
94
90
95
if (
91
96
stats [ FIRST_ARRAY_INDEX ] &&
@@ -116,4 +121,34 @@ class AnalyticsService {
116
121
}
117
122
}
118
123
124
+ function mergeStats ( statsAll : ActivityLogCreateItemRequestDto [ ] ) : ActivityLogCreateItemRequestDto [ ] {
125
+ return mergeByCriteria ( statsAll ,
126
+ ( item1 , item2 ) => item1 . date === item2 . date ,
127
+ ( mergedItem , item ) => mergedItem . items = mergeStatsItems ( [ ...mergedItem . items , ...item . items ] )
128
+ ) ;
129
+ }
130
+
131
+ function mergeStatsItems ( items : CommitStatistics [ ] ) : CommitStatistics [ ] {
132
+ return mergeByCriteria ( items ,
133
+ ( item1 , item2 ) => item1 . authorEmail === item2 . authorEmail && item1 . authorName === item2 . authorName ,
134
+ ( mergedItem , item ) => mergedItem . commitsNumber += item . commitsNumber
135
+ ) ;
136
+ }
137
+
138
+ function mergeByCriteria < T > ( items : T [ ] , compareFn : ( item1 : T , item2 : T ) => boolean , mergeFn : ( mergedItem : T , item : T ) => void ) : T [ ] {
139
+ const mergedItems : T [ ] = [ ] ;
140
+
141
+ for ( const item of items ) {
142
+ const mergedItem = mergedItems . find ( ( mergedItem ) => compareFn ( mergedItem , item ) ) ;
143
+
144
+ if ( mergedItem ) {
145
+ mergeFn ( mergedItem , item ) ;
146
+ } else {
147
+ mergedItems . push ( item ) ;
148
+ }
149
+ }
150
+
151
+ return mergedItems ;
152
+ }
153
+
119
154
export { AnalyticsService } ;
0 commit comments