Skip to content

Commit

Permalink
Merge pull request #4770 from pnrgenc/server-stats-duplication-issue
Browse files Browse the repository at this point in the history
[bugfix] Correctly count datapoints
  • Loading branch information
Cookiezaurs authored Dec 13, 2023
2 parents ec0d6ff + f7a7938 commit 595defb
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 3 deletions.
14 changes: 12 additions & 2 deletions plugins/server-stats/api/parts/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const moment = require("moment");

const internalEventsEnum =
{
"[CLY]_session": "ss",
"[CLY]_session": "s",
"[CLY]_view": "v",
"[CLY]_nps": "n",
"[CLY]_crash": "c",
Expand Down Expand Up @@ -48,10 +48,20 @@ function updateDataPoints(writeBatcher, appId, sessionCount, eventCount, consoli
for (var key in eventCount) {
incObject[key] = eventCount[key];
incObject[`d.${utcMoment.format("D")}.${utcMoment.format("H")}.${key}`] = eventCount[key];
sum += eventCount[key] || 0;
// sum += eventCount[key] || 0;
if (key === "e" || key === "s" || key === "p") { //because other are breakdowns from events.
sum += eventCount[key] || 0;
}
}
incObject[`d.${utcMoment.format("D")}.${utcMoment.format("H")}.dp`] = sum;
}
else if (sessionCount && (eventCount === null || typeof eventCount === 'undefined' || (typeof eventCount === 'object' && !Object.keys(eventCount).length))) {
incObject = {
s: sessionCount,
[`d.${utcMoment.format("D")}.${utcMoment.format("H")}.s`]: sessionCount,
[`d.${utcMoment.format("D")}.${utcMoment.format("H")}.dp`]: sessionCount
};
}
else if (typeof eventCount === 'number') {
incObject = {
e: eventCount,
Expand Down
141 changes: 140 additions & 1 deletion plugins/server-stats/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ function verifyAddedEvents(addedEvents, initialRequest) {
lastEventCounts[internalEventKey] += event.count;
lastEventCounts.e += event.count;
});
if (!addedEvents.filter(item => item.key === '[CLY]_session').length) { // then session included with begin_session=1 and count it also.
lastEventCounts.s++;
}

for (const key in lastEventCounts) {
if (Object.hasOwnProperty.call(lastEventCounts, key)) {
Expand Down Expand Up @@ -116,6 +119,28 @@ function generateRandomEvents(key) {
return randomInternalEvents;
}

function verifyDPCount() {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const query = {"_id": APP_ID + "_" + year + ":" + month};
return testUtils.db.collection("server_stats_data_points").find(query).toArray()
.then((res, err) => {
res = res[0];
if (err) {
throw ({err: err});
}
else {
var d = res.d[Object.keys(res.d)[0]][Object.keys(res.d[Object.keys(res.d)[0]])[0]];
if (!d.e) {
d.e = 0;
}
if (d.s + d.e !== d.dp) {
throw ({err: 'Session and event count is not equal to data point count'});
}
}
});
}

describe('Testing data points plugin', function() {
describe('Get server stats data', function() {
it('should list track of the data point consumption in (30 days)', function(done) {
Expand Down Expand Up @@ -211,7 +236,7 @@ describe('Testing data points plugin', function() {
});
});

describe('Test event types', function() {
describe('Test the accuracy of event breakdowns', function() {
for (const internalKey in statInternalEvents) {
internalEvents.push({key: internalKey, count: 1});
const value = statInternalEvents[internalKey];
Expand Down Expand Up @@ -337,4 +362,118 @@ describe('Testing data points plugin', function() {
});
});
});

describe('Verify data point number always equals to session + event count', function() {
it('should send session request', function(done) {
request
.get('/o/data_ingestion?app_key=' + APP_KEY + '&begin_session=1&device_id=ABC&app_id=' + APP_ID)
.expect(200)
.end(function(err, res) {
if (err) {
return {error: err};
}
const ob = JSON.parse(res.text);
ob.should.have.property('result', true);
setTimeout(done, dataPointTimeout * testUtils.testScalingFactor);
});
});
it('should verify session request included to data points', function(done) {
verifyDPCount().then(() => {
setTimeout(done, dataPointTimeout * testUtils.testScalingFactor);
}).catch(err => {
done(err);
});
});
it('should send session + event request', function(done) {
request
.get('/o/data_ingestion?app_key=' + APP_KEY + '&begin_session=1&device_id=ABCD&app_id=' + APP_ID + '&events=' + JSON.stringify(customEvents.single))
.expect(200)
.end(function(err, res) {
if (err) {
return {error: err};
}
const ob = JSON.parse(res.text);
ob.should.have.property('result', true);
setTimeout(done, dataPointTimeout * testUtils.testScalingFactor);
});
});
it('should verify session + event request included to data points', function(done) {
verifyDPCount().then(() => {
setTimeout(done, dataPointTimeout * testUtils.testScalingFactor);
}).catch(err => {
done(err);
});
});
it('should send session + view request', function(done) {
request
.get('/o/data_ingestion?app_key=' + APP_KEY + '&begin_session=1&device_id=ABCD&app_id=' + APP_ID + '&events=' + JSON.stringify([{key: '[CLY]_view', count: 1}]))
.expect(200)
.end(function(err, res) {
if (err) {
return {error: err};
}
const ob = JSON.parse(res.text);
ob.should.have.property('result', true);
setTimeout(done, dataPointTimeout * testUtils.testScalingFactor);
});
});
it('should verify session + view request included to data points', function(done) {
verifyDPCount().then(() => {
setTimeout(done, dataPointTimeout * testUtils.testScalingFactor);
}).catch(err => {
done(err);
});
});
it('should send session + view + custom event request', function(done) {
request
.get('/o/data_ingestion?app_key=' + APP_KEY + '&begin_session=1&device_id=ABCD&app_id=' + APP_ID + '&events=' + JSON.stringify([{key: '[CLY]_view', count: 1}, {key: 'event_1', count: 1}]))
.expect(200)
.end(function(err, res) {
if (err) {
return {error: err};
}
const ob = JSON.parse(res.text);
ob.should.have.property('result', true);
setTimeout(done, dataPointTimeout * testUtils.testScalingFactor);
});
});
it('should verify session + view + custom event request included to data points', function(done) {
verifyDPCount().then(() => {
setTimeout(done, dataPointTimeout * testUtils.testScalingFactor);
}).catch(err => {
done(err);
});
});
it('should send view + view request', function(done) {
request
.get('/o/data_ingestion?app_key=' + APP_KEY + '&begin_session=1&app_id=' + APP_ID + '&events=' + JSON.stringify([{key: '[CLY]_view', count: 1}, {key: '[CLY]_view', count: 1}]))
.expect(200)
.end(function(err, res) {
if (err) {
return {error: err};
}
const ob = JSON.parse(res.text);
ob.should.have.property('result', true);
setTimeout(done, dataPointTimeout * testUtils.testScalingFactor);
});
});
it('should verify view + view request NOT included to data points', function(done) {
verifyDPCount().then(() => {
setTimeout(done, dataPointTimeout * testUtils.testScalingFactor);
}).catch(err => {
done(err);
});
});
it('should delete data point record for the test app', function(done) {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const query = {"_id": APP_ID + "_" + year + ":" + month};
testUtils.db.collection("server_stats_data_points").deleteOne(query, function(err, res) {
if (err) {
return done(err);
}
done();
});
});
});
});

0 comments on commit 595defb

Please sign in to comment.