Skip to content

Commit a1a8230

Browse files
authored
Merge pull request #7171 from Countly/ar2rsawseen/master2
Add app dp breakdown
2 parents 2d7663d + a8808a0 commit a1a8230

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

plugins/server-stats/api/jobs/stats.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,16 @@ class StatsJob extends job.Job {
184184
utcMoment.subtract(1, 'days');
185185
}
186186

187+
// always create a dedicated array for last 30 days
188+
const last30Dates = [];
189+
const last30Moment = moment.utc();
190+
for (let i = 0; i < 30; i++) {
191+
last30Dates.push(last30Moment.format('YYYY:M:D'));
192+
last30Moment.subtract(1, 'days');
193+
}
194+
187195
const options = {
188-
dailyDates: specificDates,
196+
dailyDates: last30Dates,
189197
monthlyBreakdown: true,
190198
license_hosting: license?.license_hosting,
191199
};
@@ -205,6 +213,47 @@ class StatsJob extends job.Job {
205213
user.add_event({key: "DP", count: allData.daily[key], timestamp: timestamp, segmentation: allData.dailybreakdown ? allData.dailybreakdown[key] : {}});
206214
}
207215
}
216+
// Get all apps
217+
const apps = await db.collection('apps').find({}, {projection: {_id: 1}}).toArray();
218+
219+
// For each app, sum DP for last 30 days using allData.dailybreakdown and last30Dates
220+
const appDPs = {};
221+
for (const app of apps) {
222+
appDPs[app._id] = 0;
223+
if (allData.apps && allData.apps[app._id]) {
224+
appDPs[app._id] = allData.apps[app._id];
225+
}
226+
}
227+
228+
// Count apps in each DP range
229+
dataMonthly.appsLT10KDP = 0;
230+
dataMonthly.apps10Kto100KDP = 0;
231+
dataMonthly.apps100Kto1MDP = 0;
232+
dataMonthly.apps1Mto10MDP = 0;
233+
dataMonthly.apps10Mto100MDP = 0;
234+
dataMonthly.appsGT100MDP = 0;
235+
for (const appId in appDPs) {
236+
const dp = appDPs[appId];
237+
if (dp < 10000) {
238+
dataMonthly.appsLT10KDP++;
239+
}
240+
else if (dp < 100000) {
241+
dataMonthly.apps10Kto100KDP++;
242+
}
243+
else if (dp < 1000000) {
244+
dataMonthly.apps100Kto1MDP++;
245+
}
246+
else if (dp < 10000000) {
247+
dataMonthly.apps1Mto10MDP++;
248+
}
249+
else if (dp < 100000000) {
250+
dataMonthly.apps10Mto100MDP++;
251+
}
252+
else {
253+
dataMonthly.appsGT100MDP++;
254+
}
255+
}
256+
208257
user.user_details({'custom': dataMonthly});
209258
server.start(function() {
210259
server.stop();

plugins/server-stats/api/parts/stats.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,14 @@ function fetchDatapoints(db, filter, options, callback) {
253253
}
254254

255255
if (options.monthlyBreakdown) {
256+
var apps = {};
256257
const dataPoints = result
257258
.reduce((acc, current) => {
258259
let dp = (current.e || 0) + (current.s || 0);
260+
var appId = current.a;
261+
if (appId && typeof appId !== "undefined" && appId !== "[CLY]_consolidated" && !apps[appId]) {
262+
apps[appId] = 0;
263+
}
259264

260265
if (/^\[CLY\]_consolidated/.test(current._id)) {
261266
// do not count consolidated dp for countly hosted clients
@@ -309,14 +314,17 @@ function fetchDatapoints(db, filter, options, callback) {
309314
acc.daily[date] = (acc.daily[date] || 0) + (current.d[day][hour].e || 0) + (current.d[day][hour].s || 0);
310315
}
311316
}
317+
if (typeof apps[appId] !== "undefined") {
318+
apps[appId] += acc.daily[date];
319+
}
312320
}
313321
}
314322
});
315323
}
316324

317325
return acc;
318326
}, {});
319-
327+
dataPoints.apps = apps;
320328
return callback(dataPoints);
321329
}
322330

0 commit comments

Comments
 (0)