@@ -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 <
46
- ActivityLogCreateItemRequestDto [ ]
47
- > {
45
+ private async collectStatsByRepository (
46
+ repoPath : string ,
47
+ ) : Promise < ActivityLogCreateItemRequestDto [ ] > {
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,50 @@ class AnalyticsService {
116
121
}
117
122
}
118
123
124
+ function mergeStats (
125
+ statsAll : ActivityLogCreateItemRequestDto [ ] ,
126
+ ) : ActivityLogCreateItemRequestDto [ ] {
127
+ return mergeByCriteria (
128
+ statsAll ,
129
+ ( item1 , item2 ) => item1 . date === item2 . date ,
130
+ ( mergedItem , item ) =>
131
+ ( mergedItem . items = mergeStatsItems ( [
132
+ ...mergedItem . items ,
133
+ ...item . items ,
134
+ ] ) ) ,
135
+ ) ;
136
+ }
137
+
138
+ function mergeStatsItems ( items : CommitStatistics [ ] ) : CommitStatistics [ ] {
139
+ return mergeByCriteria (
140
+ items ,
141
+ ( item1 , item2 ) =>
142
+ item1 . authorEmail === item2 . authorEmail &&
143
+ item1 . authorName === item2 . authorName ,
144
+ ( mergedItem , item ) => ( mergedItem . commitsNumber += item . commitsNumber ) ,
145
+ ) ;
146
+ }
147
+
148
+ function mergeByCriteria < T > (
149
+ items : T [ ] ,
150
+ compareFn : ( item1 : T , item2 : T ) => boolean ,
151
+ mergeFn : ( mergedItem : T , item : T ) => void ,
152
+ ) : T [ ] {
153
+ const mergedItems : T [ ] = [ ] ;
154
+
155
+ for ( const item of items ) {
156
+ const mergedItem = mergedItems . find ( ( mergedItem ) =>
157
+ compareFn ( mergedItem , item ) ,
158
+ ) ;
159
+
160
+ if ( mergedItem ) {
161
+ mergeFn ( mergedItem , item ) ;
162
+ } else {
163
+ mergedItems . push ( item ) ;
164
+ }
165
+ }
166
+
167
+ return mergedItems ;
168
+ }
169
+
119
170
export { AnalyticsService } ;
0 commit comments