Skip to content

Commit db7842d

Browse files
authored
Merge pull request #691 from frknbasaran/serverstats
[server-stats] 6 months, 12 months support.
2 parents 2e40760 + dbd7fc6 commit db7842d

File tree

2 files changed

+69
-22
lines changed

2 files changed

+69
-22
lines changed

plugins/server-stats/api/api.js

+51-14
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ var plugins = require('../../pluginManager.js'),
107107
);
108108
}
109109

110+
/**
111+
* Update data-point object with new events and sessions counts
112+
* @param {object} object - object which will be updated
113+
* @param {object} data - passed data object which contains events and sessions count
114+
* @returns {object} Returns manipulated object
115+
**/
116+
function increaseDataPoints(object, data) {
117+
object.events += data.e;
118+
object.sessions += data.s;
119+
object["data-points"] += data.e + data.s;
120+
return object;
121+
}
122+
110123
/**
111124
* Returns last three month session, event and data point count
112125
* for all and individual apps
@@ -123,7 +136,7 @@ var plugins = require('../../pluginManager.js'),
123136
var periodsToFetch = [],
124137
utcMoment = common.moment.utc();
125138

126-
var monthBack = parseInt(params.qstring.months) || 3;
139+
var monthBack = parseInt(params.qstring.months) || 12;
127140

128141
for (let i = monthBack - 1; i > 0; i--) {
129142
utcMoment.subtract(i, "months");
@@ -143,15 +156,25 @@ var plugins = require('../../pluginManager.js'),
143156

144157
common.db.collection("server_stats_data_points").find(filter, {}).toArray(function(err, dataPerApp) {
145158
var toReturn = {
146-
"all-apps": {}
159+
"all-apps": {},
160+
};
161+
162+
toReturn["all-apps"]["12_months"] = {
163+
"events": 0,
164+
"sessions": 0,
165+
"data-points": 0
166+
};
167+
toReturn["all-apps"]["6_months"] = {
168+
"events": 0,
169+
"sessions": 0,
170+
"data-points": 0
147171
};
148172

149173
for (let i = 0; i < periodsToFetch.length; i++) {
150174
let formattedDate = periodsToFetch[i].replace(":", "-");
151-
152175
toReturn["all-apps"][formattedDate] = {
153-
"sessions": 0,
154176
"events": 0,
177+
"sessions": 0,
155178
"data-points": 0
156179
};
157180
}
@@ -166,26 +189,40 @@ var plugins = require('../../pluginManager.js'),
166189

167190
if (!toReturn[dataPerApp[i].a][formattedDate]) {
168191
toReturn[dataPerApp[i].a][formattedDate] = {
192+
"events": 0,
169193
"sessions": 0,
194+
"data-points": 0
195+
};
196+
}
197+
if (!toReturn[dataPerApp[i].a]["12_months"]) {
198+
toReturn[dataPerApp[i].a]["12_months"] = {
170199
"events": 0,
200+
"sessions": 0,
171201
"data-points": 0
172202
};
173203
}
174-
175-
if (dataPerApp[i].m === periodsToFetch[j]) {
176-
toReturn[dataPerApp[i].a][formattedDate] = {
177-
"sessions": dataPerApp[i].s,
178-
"events": dataPerApp[i].e,
179-
"data-points": dataPerApp[i].s + dataPerApp[i].e
204+
if (!toReturn[dataPerApp[i].a]["6_months"]) {
205+
toReturn[dataPerApp[i].a]["6_months"] = {
206+
"events": 0,
207+
"sessions": 0,
208+
"data-points": 0
180209
};
210+
}
181211

182-
toReturn["all-apps"][formattedDate].sessions += dataPerApp[i].s;
183-
toReturn["all-apps"][formattedDate].events += dataPerApp[i].e;
184-
toReturn["all-apps"][formattedDate]["data-points"] += dataPerApp[i].s + dataPerApp[i].e;
212+
if (dataPerApp[i].m === periodsToFetch[j]) {
213+
toReturn[dataPerApp[i].a][formattedDate] = increaseDataPoints(toReturn[dataPerApp[i].a][formattedDate], dataPerApp[i]);
214+
toReturn["all-apps"][formattedDate] = increaseDataPoints(toReturn["all-apps"][formattedDate], dataPerApp[i]);
215+
// only last 6 months
216+
if (j > 5) {
217+
toReturn["all-apps"]["6_months"] = increaseDataPoints(toReturn["all-apps"]["6_months"], dataPerApp[i]);
218+
toReturn[dataPerApp[i].a]["6_months"] = increaseDataPoints(toReturn[dataPerApp[i].a]["6_months"], dataPerApp[i]);
219+
}
220+
toReturn[dataPerApp[i].a]["12_months"] = increaseDataPoints(toReturn[dataPerApp[i].a]["12_months"], dataPerApp[i]);
221+
toReturn["all-apps"]["12_months"] = increaseDataPoints(toReturn["all-apps"]["12_months"], dataPerApp[i]);
185222
}
186223
}
187-
}
188224

225+
}
189226
common.returnOutput(params, toReturn);
190227
});
191228
}, params);

plugins/server-stats/frontend/public/javascripts/countly.models.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,27 @@
1818
dataType: "jsonp",
1919
success: function(json) {
2020
countlyDataPoints.reset();
21-
2221
_dataPointsObj = json;
2322

24-
if (_dataPointsObj["all-apps"]) {
25-
for (var period in _dataPointsObj["all-apps"]) {
26-
_selectedPeriod = period;
23+
// format months object
24+
var periods = Object.keys(json["all-apps"]).splice(0, 2).concat(Object.keys(json["all-apps"]).splice(11, 14));
2725

28-
_periods.push({
29-
period: period,
30-
text: moment(period, "YYYY-M").format("MMM YYYY")
31-
});
26+
if (_dataPointsObj["all-apps"]) {
27+
for (var i = 0; i < periods.length; i++) {
28+
_selectedPeriod = periods[i];
29+
30+
if (i > 1) {
31+
_periods.push({
32+
period: periods[i],
33+
text: moment(periods[i], "YYYY-M").format("MMM YYYY")
34+
});
35+
}
36+
else {
37+
_periods.push({
38+
period: periods[i],
39+
text: periods[i].replace("_", " ")
40+
});
41+
}
3242
}
3343
}
3444
}

0 commit comments

Comments
 (0)