Skip to content

Commit 01b6820

Browse files
authored
Merge pull request #5795 from Countly/mrmeghana-patch-1
Update compare_drill_aggregated.js
2 parents ee10275 + 7a9d2ba commit 01b6820

File tree

1 file changed

+63
-9
lines changed

1 file changed

+63
-9
lines changed

bin/scripts/data-reports/compare_drill_aggregated.js

+63-9
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
* node compare_drill_aggregated.js
88
*/
99
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 ""
1111
//Example var eventMap = {"6075f94b7e5e0d392902520c":["Logout","Login"],"6075f94b7e5e0d392902520d":["Logout","Login","Buy"]};
1212
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+
1316
var verbose = false; //true to show more output
1417

1518

@@ -151,11 +154,40 @@ Promise.all([pluginManager.dbConnection("countly"), pluginManager.dbConnection("
151154
}
152155
}
153156
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("----------------------------------------------");
155180
endReport[app._id]["bad"]++;
156181
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+
};
158189
}
190+
159191
resolve2();
160192
});
161193
}
@@ -164,6 +196,25 @@ Promise.all([pluginManager.dbConnection("countly"), pluginManager.dbConnection("
164196
}).then(function() {
165197
console.log("Finished processing app: ", app.name);
166198
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+
167218
}).catch(function(eee) {
168219
console.log("Error processing app: ", app.name);
169220
console.log(eee);
@@ -207,14 +258,17 @@ Promise.all([pluginManager.dbConnection("countly"), pluginManager.dbConnection("
207258
}
208259
endDate = endDate.valueOf() - endDate.utcOffset() * 60000;
209260

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+
}
215269

216270
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) {
218272
if (err) {
219273
console.log(err);
220274
}

0 commit comments

Comments
 (0)