7
7
* node compare_drill_aggregated.js
8
8
*/
9
9
var period = "7days" ; //Chose any of formats: "Xdays" ("7days","100days") or ["1-1-2024", "1-10-2024"],
10
- var app_list = [ ] ; //List with apps
10
+ var app_list = [ ] ; //List with apps ""
11
11
//Example var eventMap = {"6075f94b7e5e0d392902520c":["Logout","Login"],"6075f94b7e5e0d392902520d":["Logout","Login","Buy"]};
12
12
var eventMap = { } ; //If left empty will run for all alls/events.
13
+
14
+ var union_with_old_collection = true ; //False if all sessions are stored in drill_events collection
15
+
13
16
var verbose = false ; //true to show more output
14
17
15
18
@@ -151,11 +154,40 @@ Promise.all([pluginManager.dbConnection("countly"), pluginManager.dbConnection("
151
154
}
152
155
}
153
156
if ( haveAnything ) {
154
- console . log ( " " + JSON . stringify ( report ) ) ;
157
+ let aggCount = totals . c || 0 ;
158
+ let drillCount = drillData . totals . c || 0 ;
159
+ let percentageDiff = 0 ;
160
+ if ( drillCount !== 0 ) {
161
+ percentageDiff = ( ( drillCount - aggCount ) / drillCount ) * 100 ;
162
+ }
163
+ else {
164
+ if ( aggCount !== 0 ) {
165
+ // If drillCount is 0, and aggCount is not 0, show a large difference
166
+ percentageDiff = ( aggCount > 0 ? 100 : - 100 ) ; // 100% or -100% depending on the sign of aggCount
167
+ }
168
+ else {
169
+ percentageDiff = 0 ; // Both counts are 0, no difference
170
+ }
171
+ }
172
+
173
+ console . log ( "----------------------------------------------" ) ;
174
+ console . log ( "- Application name:" , app . name ) ;
175
+ console . log ( "- Event name:" , event ) ;
176
+ console . log ( "- Counts in Aggregated data:" , aggCount ) ;
177
+ console . log ( "- Counts in Drill data:" , drillCount ) ;
178
+ console . log ( "- Percentage difference between Drill data and Aggregated data:" , percentageDiff . toFixed ( 2 ) + "%" ) ;
179
+ console . log ( "----------------------------------------------" ) ;
155
180
endReport [ app . _id ] [ "bad" ] ++ ;
156
181
endReport [ app . _id ] [ "events" ] = endReport [ app . _id ] [ "events" ] || { } ;
157
- endReport [ app . _id ] [ "events" ] [ event ] = { "e" : event , report : report } ;
182
+ endReport [ app . _id ] [ "events" ] [ event ] = {
183
+ "e" : event ,
184
+ "aggregated_count" : aggCount ,
185
+ "drill_count" : drillCount ,
186
+ "percentage_difference" : percentageDiff . toFixed ( 2 ) ,
187
+ "report" : report
188
+ } ;
158
189
}
190
+
159
191
resolve2 ( ) ;
160
192
} ) ;
161
193
}
@@ -164,6 +196,25 @@ Promise.all([pluginManager.dbConnection("countly"), pluginManager.dbConnection("
164
196
} ) . then ( function ( ) {
165
197
console . log ( "Finished processing app: " , app . name ) ;
166
198
resolve ( ) ;
199
+
200
+ //Complete CSV after processing the apps
201
+ console . log ( "\nSummary Report (CSV-like):" ) ;
202
+ console . log ( "App,Event,Aggregated,Drill,% Difference" ) ;
203
+ // var csvRows = ["App,Event,Aggregated,Drill,% Difference"];
204
+ for ( var appId in endReport ) {
205
+ var appData = endReport [ appId ] ;
206
+ var appName = appData . name ;
207
+ if ( appData . events ) {
208
+ for ( var event in appData . events ) {
209
+ var eventData = appData . events [ event ] ;
210
+ var row = `${ appName } ,${ event } ,${ eventData . aggregated_count } ,${ eventData . drill_count } ,${ eventData . percentage_difference } ` ;
211
+ console . log ( row ) ;
212
+ //csvRows.push(row);
213
+ }
214
+ }
215
+ }
216
+
217
+
167
218
} ) . catch ( function ( eee ) {
168
219
console . log ( "Error processing app: " , app . name ) ;
169
220
console . log ( eee ) ;
@@ -207,14 +258,17 @@ Promise.all([pluginManager.dbConnection("countly"), pluginManager.dbConnection("
207
258
}
208
259
endDate = endDate . valueOf ( ) - endDate . utcOffset ( ) * 60000 ;
209
260
210
- let collection = "drill_events" + crypto . createHash ( 'sha1' ) . update ( options . event + options . app_id ) . digest ( 'hex' ) ;
211
- var query = { "ts" : { "$gte" : startDate , "$lt" : endDate } } ;
212
- var pipeline = [
213
- { "$match" : query } ,
214
- ] ;
261
+ var query = { "ts" : { "$gte" : startDate , "$lt" : endDate } , "a" : options . app_id , "e" : options . event } ;
262
+ var pipeline = [ ] ;
263
+ pipeline . push ( { "$match" : query } ) ;
264
+ if ( union_with_old_collection ) {
265
+ let collection = "drill_events" + crypto . createHash ( 'sha1' ) . update ( options . event + options . app_id ) . digest ( 'hex' ) ;
266
+ var query2 = { "ts" : { "$gte" : startDate , "$lt" : endDate } } ;
267
+ pipeline . push ( { "$unionWith" : { "coll" : collection , "pipeline" : [ { "$match" : query2 } ] } } ) ;
268
+ }
215
269
216
270
pipeline . push ( { "$group" : { "_id" : "$d" , "c" : { "$sum" : "$c" } , "s" : { "$sum" : "$s" } , "dur" : { "$sum" : "$dur" } } } ) ;
217
- options . drillDb . collection ( collection ) . aggregate ( pipeline , { "allowDiskUse" : true } ) . toArray ( function ( err , data ) {
271
+ options . drillDb . collection ( "drill_events" ) . aggregate ( pipeline , { "allowDiskUse" : true } ) . toArray ( function ( err , data ) {
218
272
if ( err ) {
219
273
console . log ( err ) ;
220
274
}
0 commit comments