Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SER-816] update script for timestamp in consent_history #4648

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions bin/upgrade/DEV/scripts/update_timestamps_in_consent_history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var pluginManager = require('../../../../plugins/pluginManager.js'),
asyncjs = require('async');

console.log("Updating consent_history data");
pluginManager.dbConnection().then((countlyDb) => {
countlyDb.collection('apps').find({}).toArray(function(err, apps) {
function update(app, done) {
console.log("Updating consent_history for " + app.name);
var cursor = countlyDb.collection('consent_history' + app._id).find({}, {projection: {_id: 1, ts: 1}});
var requests = [];
var promises = [];
cursor.forEach(function(consent) {
var update = {};
if (consent.ts && (consent.ts + "").length === 10) {
update.ts = Math.round(parseInt(consent.ts, 10) * 1000);
}
if (Object.keys(update).length) {
requests.push({
'updateOne': {
'filter': { '_id': consent._id },
'update': { '$set': update }
}
});
}
if (requests.length === 500) {
//Execute per 500 operations and re-init
try {
promises.push(countlyDb.collection('consent_history' + app._id).bulkWrite(requests, {ordered: false}));
}
catch (ex) {
console.error(ex);
}
requests = [];
}
}, function() {
if (requests.length > 0) {
try {
promises.push(countlyDb.collection('consent_history' + app._id).bulkWrite(requests, {ordered: false}));
}
catch (ex) {
console.error(ex);
}
}
Promise.all(promises).finally(done);
});
}
asyncjs.eachSeries(apps, update, function() {
console.log("consent_history data update finished");
countlyDb.close();
});
});
});
4 changes: 2 additions & 2 deletions plugins/compliance-hub/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const FEATURE_NAME = 'compliance_hub';
after: after,
change: changes,
type: type,
ts: params.time.timestamp,
ts: params.time.mstimestamp,
cd: new Date(),
device_id: params.qstring.device_id,
uid: params.app_user.uid,
Expand Down Expand Up @@ -187,7 +187,7 @@ const FEATURE_NAME = 'compliance_hub';

if (params.qstring.period) {
countlyCommon.getPeriodObj(params);
params.qstring.query.ts = countlyCommon.getTimestampRangeQuery(params, true);
params.qstring.query.ts = countlyCommon.getTimestampRangeQuery(params, false);
}

params.qstring.project = params.qstring.project || {};
Expand Down
101 changes: 101 additions & 0 deletions plugins/compliance-hub/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
var request = require('supertest');
var should = require('should');
var testUtils = require("../../test/testUtils");
request = request(testUtils.url);

var APP_KEY = "";
var API_KEY_ADMIN = "";
var APP_ID = "";
var DEVICE_ID = "1234567890";

describe('Testing Compliance Hub', function() {
describe('Check Empty Data', function() {
it('should have empty data', function(done) {

API_KEY_ADMIN = testUtils.get("API_KEY_ADMIN");
APP_ID = testUtils.get("APP_ID");
APP_KEY = testUtils.get("APP_KEY");
DEVICE_ID = testUtils.get("DEVICE_ID");

request
.get('/o/consent/search?api_key=' + API_KEY_ADMIN + '&app_id=' + APP_ID)
.expect(200)
.end(function(err, res) {
if (err) {
return done(err);
}
var ob = JSON.parse(res.text);
ob.should.be.empty;
setTimeout(done, 100);
});
});
});
describe('Check consent_history', function() {
it('should take timestamp in milliseconds', function(done) {
var timestamp = "1234567890123";
request
.post('/i?app_key=' + APP_KEY + '&device_id=' + DEVICE_ID + '&consent={"session":true}' + '&timestamp=' + timestamp)
.expect(200)
.end(function(err, res) {
if (err) {
done(err);
}
var ob = JSON.parse(res.text);
ob.result.should.eql("Success");
setTimeout(done, 100 * testUtils.testScalingFactor);
});
});
it('should update timestamp values as milliseconds on the db', function(done) {
request
.get('/o/consent/search?api_key=' + API_KEY_ADMIN + '&app_id=' + APP_ID)
.expect(200)
.end(function(err, res) {
if (err) {
return done(err);
}
var ob = JSON.parse(res.text);
const tsControl = ob.aaData.every(item => {
const tsString = String(item.ts);
if (tsString.length === 13) {
return done();
}
else {
return done(err);
}
});
});
});
});

describe('Reset App', function() {
it('should reset data', function(done) {
var params = {app_id: APP_ID, period: "reset"};
request
.get('/i/apps/reset?api_key=' + API_KEY_ADMIN + "&args=" + JSON.stringify(params))
.expect(200)
.end(function(err, res) {
if (err) {
return done(err);
}
var ob = JSON.parse(res.text);
ob.should.have.property('result', 'Success');
setTimeout(done, 100 * testUtils.testScalingFactor);
});
});
});
describe('Verify Empty Data', function() {
it('should have empty data', function(done) {
request
.get('/o/consent/search?api_key=' + API_KEY_ADMIN + '&app_id=' + APP_ID)
.expect(200)
.end(function(err, res) {
if (err) {
return done(err);
}
var ob = JSON.parse(res.text);
ob.should.be.empty;
setTimeout(done, 100);
});
});
});
});
Loading