diff --git a/bin/upgrade/DEV/scripts/update_timestamps_in_consent_history.js b/bin/upgrade/DEV/scripts/update_timestamps_in_consent_history.js new file mode 100644 index 00000000000..e172748a394 --- /dev/null +++ b/bin/upgrade/DEV/scripts/update_timestamps_in_consent_history.js @@ -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(); + }); + }); +}); \ No newline at end of file diff --git a/plugins/compliance-hub/api/api.js b/plugins/compliance-hub/api/api.js index ae4d03840fe..6391c55a1ec 100644 --- a/plugins/compliance-hub/api/api.js +++ b/plugins/compliance-hub/api/api.js @@ -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, @@ -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 || {}; diff --git a/plugins/compliance-hub/tests.js b/plugins/compliance-hub/tests.js index e69de29bb2d..11f0c15ae7d 100644 --- a/plugins/compliance-hub/tests.js +++ b/plugins/compliance-hub/tests.js @@ -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}' + '×tamp=' + 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); + }); + }); + }); +}); \ No newline at end of file