From 759dd40dec18a89854219ec1cac8048fb16614f0 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 09:57:06 -0600 Subject: [PATCH 01/28] Add clean treatments admin tool. --- lib/admin_plugins/cleantreatmentsdb.js | 80 ++++++++++++++++++++++++++ lib/admin_plugins/index.js | 3 +- lib/api/treatments/index.js | 19 ++++++ lib/server/treatments.js | 15 ++++- 4 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 lib/admin_plugins/cleantreatmentsdb.js diff --git a/lib/admin_plugins/cleantreatmentsdb.js b/lib/admin_plugins/cleantreatmentsdb.js new file mode 100644 index 00000000000..f89472927ae --- /dev/null +++ b/lib/admin_plugins/cleantreatmentsdb.js @@ -0,0 +1,80 @@ +'use strict'; + +var moment = require('moment'); + +var cleantreatmentsdb = { + name: 'cleantreatmentsdb' + , label: 'Clean Mongo treatments database' + , pluginType: 'admin' +}; + +function init() { + return cleantreatmentsdb; +} + +module.exports = init; + +cleantreatmentsdb.actions = [ + { + name: 'Delete all documents from treatments collection older than 180 days' + , description: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + , buttonLabel: 'Delete old documents' + , confirmText: 'Delete old documents from treatments collection?' + , preventClose: true + } + ]; + +cleantreatmentsdb.actions[0].init = function init(client, callback) { + var translate = client.translate; + var $status = $('#admin_' + cleantreatmentsdb.name + '_0_status'); + + $status.hide(); + + var numDays = '
' + + ''; + + $('#admin_' + cleantreatmentsdb.name + '_0_html').html(numDays); + + if (callback) { callback(); } +}; + +cleantreatmentsdb.actions[0].code = function deleteOldRecords(client, callback) { + var translate = client.translate; + var $status = $('#admin_' + cleantreatmentsdb.name + '_1_status'); + var numDays = Number($('#admin_treatments_days').val()); + + if (isNaN(numDays) || (numDays < 1)) { + alert(translate('%1 is not a valid number', { params: [$('#admin_treatments_days').val()] })); + if (callback) { callback(); } + return; + } + var endDate = moment().subtract(numDays, 'days'); + var dateStr = endDate.format('YYYY-MM-DD'); + + if (!client.hashauth.isAuthenticated()) { + alert(translate('Your device is not authenticated yet')); + if (callback) { + callback(); + } + return; + } + + $status.hide().text(translate('Deleting records ...')).fadeIn('slow'); + $.ajax({ + method: 'DELETE' + , url: '/api/v1/treatments/?find[created_at][$lte]=' + dateStr + , headers: client.headers() + , success: function (retVal) { + $status.hide().text(translate('%1 records deleted',{ params: [retVal.n] })).fadeIn('slow'); + } + }).done(function success () { + if (callback) { callback(); } + }).fail(function fail() { + $status.hide().text(translate('Error')).fadeIn('slow'); + if (callback) { callback(); } + }); + +}; diff --git a/lib/admin_plugins/index.js b/lib/admin_plugins/index.js index c6463ecca8e..eed60a943ad 100644 --- a/lib/admin_plugins/index.js +++ b/lib/admin_plugins/index.js @@ -8,6 +8,7 @@ function init() { require('./subjects')() , require('./roles')() , require('./cleanstatusdb')() + , require('./cleantreatmentsdb')() , require('./futureitems')() ]; @@ -84,4 +85,4 @@ function init() { } -module.exports = init; \ No newline at end of file +module.exports = init; diff --git a/lib/api/treatments/index.js b/lib/api/treatments/index.js index 3fff4ceb331..c0693a1b60b 100644 --- a/lib/api/treatments/index.js +++ b/lib/api/treatments/index.js @@ -105,6 +105,25 @@ function configure(app, wares, ctx) { }); }); + // delete record that match query + api.delete('/treatments/', ctx.authorization.isPermitted('api:treatments:delete'), function(req, res) { + var query = req.query; + + console.log('Delete treatments records with query: ', query); + + // remove using the query + ctx.treatments.remove(query, function(err, stat) { + if (err) { + res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err); + console.log('Error saving treatment'); + console.log(err); + } else { + res.json({}); + console.log('treatments records deleted'); + } + }); + }); + // update record api.put('/treatments/', ctx.authorization.isPermitted('api:treatments:update'), function(req, res) { var data = req.body; diff --git a/lib/server/treatments.js b/lib/server/treatments.js index eb39dbbb901..fb3d0cb2f13 100644 --- a/lib/server/treatments.js +++ b/lib/server/treatments.js @@ -96,11 +96,20 @@ function storage (env, ctx) { return find_options(opts, storage.queryOpts); } + function remove (opts, fn) { + return api( ).remove(query_for(opts), fn); + } - function remove (_id, fn) { - var objId = new ObjectID(_id); + function remove_id (_id, fn) { + var filter; + if (_id === '*') { + filter = {}; + } else { + var objId = new ObjectID(_id); + filter = { '_id': objId }; + } - api( ).remove({ '_id': objId }, fn); + api( ).remove(filter, fn); ctx.bus.emit('data-received'); } From cd459fa37fbffcbc86778d12c3d761e537df6a30 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 10:12:11 -0600 Subject: [PATCH 02/28] Fix clean treatments status html id name --- lib/admin_plugins/cleantreatmentsdb.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/admin_plugins/cleantreatmentsdb.js b/lib/admin_plugins/cleantreatmentsdb.js index f89472927ae..1c58276ce43 100644 --- a/lib/admin_plugins/cleantreatmentsdb.js +++ b/lib/admin_plugins/cleantreatmentsdb.js @@ -43,7 +43,7 @@ cleantreatmentsdb.actions[0].init = function init(client, callback) { cleantreatmentsdb.actions[0].code = function deleteOldRecords(client, callback) { var translate = client.translate; - var $status = $('#admin_' + cleantreatmentsdb.name + '_1_status'); + var $status = $('#admin_' + cleantreatmentsdb.name + '_0_status'); var numDays = Number($('#admin_treatments_days').val()); if (isNaN(numDays) || (numDays < 1)) { From b429eef662b55187809810f65a22c8946b4b0fbe Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 10:18:49 -0600 Subject: [PATCH 03/28] Return status to treatments delete by query --- lib/api/treatments/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/api/treatments/index.js b/lib/api/treatments/index.js index c0693a1b60b..68729979b39 100644 --- a/lib/api/treatments/index.js +++ b/lib/api/treatments/index.js @@ -118,7 +118,7 @@ function configure(app, wares, ctx) { console.log('Error saving treatment'); console.log(err); } else { - res.json({}); + res.json(stat); console.log('treatments records deleted'); } }); From 9123de4be351b805d07f0b1281ea3589d1df3f21 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 10:32:42 -0600 Subject: [PATCH 04/28] Add clean entries db admin tool (cherry picked from commit f56f0b3ba42e2472069775507402e6309d29c26c) --- lib/admin_plugins/cleanentriesdb.js | 80 +++++++++++++++++++++++++++++ lib/admin_plugins/index.js | 1 + 2 files changed, 81 insertions(+) create mode 100644 lib/admin_plugins/cleanentriesdb.js diff --git a/lib/admin_plugins/cleanentriesdb.js b/lib/admin_plugins/cleanentriesdb.js new file mode 100644 index 00000000000..dd8ce0032c1 --- /dev/null +++ b/lib/admin_plugins/cleanentriesdb.js @@ -0,0 +1,80 @@ +'use strict'; + +var moment = require('moment'); + +var cleanentriesdb = { + name: 'cleanentriesdb' + , label: 'Clean Mongo entries (glucose entries) database' + , pluginType: 'admin' +}; + +function init() { + return cleanentriesdb; +} + +module.exports = init; + +cleanentriesdb.actions = [ + { + name: 'Delete all documents from entries collection older than 180 days' + , description: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + , buttonLabel: 'Delete old documents' + , confirmText: 'Delete old documents from entries collection?' + , preventClose: true + } + ]; + +cleanentriesdb.actions[0].init = function init(client, callback) { + var translate = client.translate; + var $status = $('#admin_' + cleanentriesdb.name + '_0_status'); + + $status.hide(); + + var numDays = '
' + + ''; + + $('#admin_' + cleanentriesdb.name + '_0_html').html(numDays); + + if (callback) { callback(); } +}; + +cleanentriesdb.actions[0].code = function deleteOldRecords(client, callback) { + var translate = client.translate; + var $status = $('#admin_' + cleanentriesdb.name + '_0_status'); + var numDays = Number($('#admin_entries_days').val()); + + if (isNaN(numDays) || (numDays < 1)) { + alert(translate('%1 is not a valid number', { params: [$('#admin_entries_days').val()] })); + if (callback) { callback(); } + return; + } + var endDate = moment().subtract(numDays, 'days'); + var dateStr = endDate.format('YYYY-MM-DD'); + + if (!client.hashauth.isAuthenticated()) { + alert(translate('Your device is not authenticated yet')); + if (callback) { + callback(); + } + return; + } + + $status.hide().text(translate('Deleting records ...')).fadeIn('slow'); + $.ajax({ + method: 'DELETE' + , url: '/api/v1/entries/?find[created_at][$lte]=' + dateStr + , headers: client.headers() + , success: function (retVal) { + $status.hide().text(translate('%1 records deleted',{ params: [retVal.n] })).fadeIn('slow'); + } + }).done(function success () { + if (callback) { callback(); } + }).fail(function fail() { + $status.hide().text(translate('Error')).fadeIn('slow'); + if (callback) { callback(); } + }); + +}; diff --git a/lib/admin_plugins/index.js b/lib/admin_plugins/index.js index eed60a943ad..224054538d1 100644 --- a/lib/admin_plugins/index.js +++ b/lib/admin_plugins/index.js @@ -9,6 +9,7 @@ function init() { , require('./roles')() , require('./cleanstatusdb')() , require('./cleantreatmentsdb')() + , require('./cleanentriesdb')() , require('./futureitems')() ]; From 15cbfeac8e4076b0775d26e11a0bf7d76b93dcd7 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 10:52:10 -0600 Subject: [PATCH 05/28] Correct delete query fields for entries db --- lib/admin_plugins/cleanentriesdb.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/admin_plugins/cleanentriesdb.js b/lib/admin_plugins/cleanentriesdb.js index dd8ce0032c1..249b86c2f70 100644 --- a/lib/admin_plugins/cleanentriesdb.js +++ b/lib/admin_plugins/cleanentriesdb.js @@ -52,7 +52,6 @@ cleanentriesdb.actions[0].code = function deleteOldRecords(client, callback) { return; } var endDate = moment().subtract(numDays, 'days'); - var dateStr = endDate.format('YYYY-MM-DD'); if (!client.hashauth.isAuthenticated()) { alert(translate('Your device is not authenticated yet')); @@ -65,7 +64,7 @@ cleanentriesdb.actions[0].code = function deleteOldRecords(client, callback) { $status.hide().text(translate('Deleting records ...')).fadeIn('slow'); $.ajax({ method: 'DELETE' - , url: '/api/v1/entries/?find[created_at][$lte]=' + dateStr + , url: '/api/v1/entries/?find[date][$lte]=' + endDate.valueOf() , headers: client.headers() , success: function (retVal) { $status.hide().text(translate('%1 records deleted',{ params: [retVal.n] })).fadeIn('slow'); From ff043944eb4534e132be8972fb0d2c40660fbccd Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 13:37:47 -0600 Subject: [PATCH 06/28] Make devicestatus delete consistent with entries --- lib/api/devicestatus/index.js | 63 +++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/lib/api/devicestatus/index.js b/lib/api/devicestatus/index.js index 5ea15acf5c3..df3eedc880b 100644 --- a/lib/api/devicestatus/index.js +++ b/lib/api/devicestatus/index.js @@ -55,25 +55,60 @@ function configure (app, wares, ctx) { }); }); - // delete record that match query - api.delete('/devicestatus/', ctx.authorization.isPermitted('api:devicestatus:delete'), function(req, res) { - var query = req.query; + /** + * @function delete_records + * Delete devicestatus. The query logic works the same way as find/list. This + * endpoint uses same search logic to remove records from the database. + */ + function delete_records(req, res, next) { + // bias towards model, but allow expressing a preference + if (!req.model) { + req.model = ctx.devicestatus; + } + var query = req.query; + if (!query.count) { + query.count = 10 + } - console.log('Delete records with query: ', query); + console.log('Delete records with query: ', query); - // remove using the query - ctx.devicestatus.remove(query, function(err, stat) { - if (err) { - res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err); - console.log('Error saving treatment'); - console.log(err); + // remove using the query + req.model.remove(query, function(err, stat) { + if (err) { + console.log('devicestatus delete error: ', err); + return next(err); + } + // yield some information about success of operation + res.json(stat); + + console.log('devicestatus records deleted'); + + return next(); + }); + } + + api.delete('/devicestatus/:spec', ctx.authorization.isPermitted('api:devicestatus:delete'), function(req, res, next) { + // if ID, prepare to query for one record + if (isId(req.params.spec)) { + req.query = { + find: { + _id: req.params.spec + } + }; } else { - res.json(stat); - console.log('devicestatus records deleted'); + if (!req.query.find) { + req.query.find = { + type: req.params.spec + }; + } else { + req.query.find.type = req.params.spec; + } } - }); - }); + next(); + }, delete_records); + // delete record that match query + api.delete('/devicestatus/', ctx.authorization.isPermitted('api:devicestatus:delete'), delete_records); } if (app.enabled('api') || true /*TODO: auth disabled for quick UI testing...*/) { From 769b66ad7ced519a93cdaf91f3c3f848b7f910ea Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 13:59:20 -0600 Subject: [PATCH 07/28] cleanup devicestatus delete --- lib/api/devicestatus/index.js | 16 ++++------------ lib/server/devicestatus.js | 12 ------------ 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/lib/api/devicestatus/index.js b/lib/api/devicestatus/index.js index df3eedc880b..ef573bbb313 100644 --- a/lib/api/devicestatus/index.js +++ b/lib/api/devicestatus/index.js @@ -43,18 +43,6 @@ function configure (app, wares, ctx) { api.post('/devicestatus/', ctx.authorization.isPermitted('api:devicestatus:create'), doPost); - api.delete('/devicestatus/:_id', ctx.authorization.isPermitted('api:devicestatus:delete'), function(req, res) { - console.log('Deleting id: ' + req.params._id); - - ctx.devicestatus.remove_id(req.params._id, function (err, removed) { - if (err) { - res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err); - } else { - res.json(removed); - } - }); - }); - /** * @function delete_records * Delete devicestatus. The query logic works the same way as find/list. This @@ -103,6 +91,10 @@ function configure (app, wares, ctx) { } else { req.query.find.type = req.params.spec; } + + if (req.query.find.type === '*') { + delete req.query.find.type; + } } next(); }, delete_records); diff --git a/lib/server/devicestatus.js b/lib/server/devicestatus.js index 43fdc055365..e74c0124841 100644 --- a/lib/server/devicestatus.js +++ b/lib/server/devicestatus.js @@ -68,17 +68,6 @@ function storage (collection, ctx) { return api( ).remove(query_for(opts), fn); } - function remove_id (_id, fn) { - var filter; - if (_id === '*') { - filter = {}; - } else { - var objId = new ObjectID(_id); - filter = { '_id': objId }; - } - return api( ).remove(filter, fn); - } - function api() { return ctx.store.collection(collection); } @@ -88,7 +77,6 @@ function storage (collection, ctx) { api.query_for = query_for; api.last = last; api.remove = remove; - api.remove_id = remove_id; api.aggregate = require('./aggregate')({ }, api); api.indexedFields = [ 'created_at' From 44baf2e98c73423d4da8f88c221ddc18a474a5f1 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 14:04:53 -0600 Subject: [PATCH 08/28] Add isId to api/devicestatus --- lib/api/devicestatus/index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/api/devicestatus/index.js b/lib/api/devicestatus/index.js index ef573bbb313..08f3e33a06e 100644 --- a/lib/api/devicestatus/index.js +++ b/lib/api/devicestatus/index.js @@ -2,6 +2,13 @@ var consts = require('../../constants'); +var ID_PATTERN = /^[a-f\d]{24}$/; + +function isId(value) { + //TODO: why did we need tht length check? + return value && ID_PATTERN.test(value) && value.length === 24; +} + function configure (app, wares, ctx) { var express = require('express'), api = express.Router( ); From c6e20565c9d92933662a5a8c09d496daaf8c7bb2 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 14:29:54 -0600 Subject: [PATCH 09/28] Set query options correctly for devicestatus --- lib/server/devicestatus.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/server/devicestatus.js b/lib/server/devicestatus.js index e74c0124841..b31e327c585 100644 --- a/lib/server/devicestatus.js +++ b/lib/server/devicestatus.js @@ -86,7 +86,8 @@ function storage (collection, ctx) { } storage.queryOpts = { - dateField: 'created_at' + dateField: 'date' + , useEpoch: true }; module.exports = storage; From 098f3727c185d6d494b3375beb1812ad339c2685 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 14:47:00 -0600 Subject: [PATCH 10/28] Use dateString for devicestatus date field --- lib/server/devicestatus.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/server/devicestatus.js b/lib/server/devicestatus.js index b31e327c585..711b4cbf2d4 100644 --- a/lib/server/devicestatus.js +++ b/lib/server/devicestatus.js @@ -86,8 +86,7 @@ function storage (collection, ctx) { } storage.queryOpts = { - dateField: 'date' - , useEpoch: true + dateField: 'dateString' }; module.exports = storage; From 4f10e036d305461c477d5cf813a8da55988e64c7 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 15:04:13 -0600 Subject: [PATCH 11/28] Fix devicestatus default date field --- lib/server/devicestatus.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/server/devicestatus.js b/lib/server/devicestatus.js index 711b4cbf2d4..e74c0124841 100644 --- a/lib/server/devicestatus.js +++ b/lib/server/devicestatus.js @@ -86,7 +86,7 @@ function storage (collection, ctx) { } storage.queryOpts = { - dateField: 'dateString' + dateField: 'created_at' }; module.exports = storage; From 71efc5e52f40a0d86eb75f443c73861fa79649fa Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 15:30:25 -0600 Subject: [PATCH 12/28] Fix devicestatus delete query handling --- lib/api/devicestatus/index.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/api/devicestatus/index.js b/lib/api/devicestatus/index.js index 08f3e33a06e..eb8b85653a1 100644 --- a/lib/api/devicestatus/index.js +++ b/lib/api/devicestatus/index.js @@ -91,16 +91,11 @@ function configure (app, wares, ctx) { } }; } else { - if (!req.query.find) { - req.query.find = { - type: req.params.spec - }; - } else { - req.query.find.type = req.params.spec; - } + req.query.find = req.query.find || req.params.spec; - if (req.query.find.type === '*') { - delete req.query.find.type; + if (req.query.find === '*') { + // delete the whole collection + delete req.query.find; } } next(); From 41d9d8a409c62c21e42f4c232fcbf4e70d650909 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 16:45:07 -0600 Subject: [PATCH 13/28] No 'model' concept for devicestatus --- lib/api/devicestatus/index.js | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/lib/api/devicestatus/index.js b/lib/api/devicestatus/index.js index eb8b85653a1..3b1ea70ff79 100644 --- a/lib/api/devicestatus/index.js +++ b/lib/api/devicestatus/index.js @@ -56,10 +56,6 @@ function configure (app, wares, ctx) { * endpoint uses same search logic to remove records from the database. */ function delete_records(req, res, next) { - // bias towards model, but allow expressing a preference - if (!req.model) { - req.model = ctx.devicestatus; - } var query = req.query; if (!query.count) { query.count = 10 @@ -68,7 +64,7 @@ function configure (app, wares, ctx) { console.log('Delete records with query: ', query); // remove using the query - req.model.remove(query, function(err, stat) { + ctx.devicestatus.remove(query, function(err, stat) { if (err) { console.log('devicestatus delete error: ', err); return next(err); @@ -82,21 +78,15 @@ function configure (app, wares, ctx) { }); } - api.delete('/devicestatus/:spec', ctx.authorization.isPermitted('api:devicestatus:delete'), function(req, res, next) { - // if ID, prepare to query for one record - if (isId(req.params.spec)) { - req.query = { - find: { - _id: req.params.spec - } - }; - } else { - req.query.find = req.query.find || req.params.spec; - - if (req.query.find === '*') { - // delete the whole collection - delete req.query.find; + api.delete('/devicestatus/:id', ctx.authorization.isPermitted('api:devicestatus:delete'), function(req, res, next) { + req.query = { + find: { + _id: req.params.id } + }; + if (req.query.find._id === '*') { + // match any record id + delete req.query.find._id; } next(); }, delete_records); From d1b5c56d189dc73252bedc2c05088c0eb3085715 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 16:49:52 -0600 Subject: [PATCH 14/28] initialize query find by id correctly for devicestatus delete --- lib/api/devicestatus/index.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/api/devicestatus/index.js b/lib/api/devicestatus/index.js index 3b1ea70ff79..db6585858c3 100644 --- a/lib/api/devicestatus/index.js +++ b/lib/api/devicestatus/index.js @@ -79,11 +79,14 @@ function configure (app, wares, ctx) { } api.delete('/devicestatus/:id', ctx.authorization.isPermitted('api:devicestatus:delete'), function(req, res, next) { - req.query = { - find: { + if (!req.query.find) { + req.query.find = { _id: req.params.id - } - }; + }; + } else { + req.query.find._id = req.params.id; + } + if (req.query.find._id === '*') { // match any record id delete req.query.find._id; From af3e01a2e7ba00cd6f3d2bfe4c7b8de9b6a05ada Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 16:52:19 -0600 Subject: [PATCH 15/28] Match devicestatus api indention --- lib/api/devicestatus/index.js | 58 +++++++++++++++++------------------ 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/lib/api/devicestatus/index.js b/lib/api/devicestatus/index.js index db6585858c3..741176df4a8 100644 --- a/lib/api/devicestatus/index.js +++ b/lib/api/devicestatus/index.js @@ -56,42 +56,42 @@ function configure (app, wares, ctx) { * endpoint uses same search logic to remove records from the database. */ function delete_records(req, res, next) { - var query = req.query; - if (!query.count) { - query.count = 10 - } + var query = req.query; + if (!query.count) { + query.count = 10 + } - console.log('Delete records with query: ', query); + console.log('Delete records with query: ', query); - // remove using the query - ctx.devicestatus.remove(query, function(err, stat) { - if (err) { - console.log('devicestatus delete error: ', err); - return next(err); - } - // yield some information about success of operation - res.json(stat); + // remove using the query + ctx.devicestatus.remove(query, function(err, stat) { + if (err) { + console.log('devicestatus delete error: ', err); + return next(err); + } + // yield some information about success of operation + res.json(stat); - console.log('devicestatus records deleted'); + console.log('devicestatus records deleted'); - return next(); - }); + return next(); + }); } api.delete('/devicestatus/:id', ctx.authorization.isPermitted('api:devicestatus:delete'), function(req, res, next) { - if (!req.query.find) { - req.query.find = { - _id: req.params.id - }; - } else { - req.query.find._id = req.params.id; - } - - if (req.query.find._id === '*') { - // match any record id - delete req.query.find._id; - } - next(); + if (!req.query.find) { + req.query.find = { + _id: req.params.id + }; + } else { + req.query.find._id = req.params.id; + } + + if (req.query.find._id === '*') { + // match any record id + delete req.query.find._id; + } + next(); }, delete_records); // delete record that match query From 69b73dfc5e3fe8e9e6dcf06c7421751db2c202a8 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 16:58:28 -0600 Subject: [PATCH 16/28] Make treatments delete match devicestatus --- lib/api/treatments/index.js | 48 +++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/lib/api/treatments/index.js b/lib/api/treatments/index.js index 68729979b39..94530df2e6f 100644 --- a/lib/api/treatments/index.js +++ b/lib/api/treatments/index.js @@ -105,24 +105,52 @@ function configure(app, wares, ctx) { }); }); - // delete record that match query - api.delete('/treatments/', ctx.authorization.isPermitted('api:treatments:delete'), function(req, res) { + /** + * @function delete_records + * Delete treatments. The query logic works the same way as find/list. This + * endpoint uses same search logic to remove records from the database. + */ + function delete_records(req, res, next) { var query = req.query; + if (!query.count) { + query.count = 10 + } - console.log('Delete treatments records with query: ', query); + console.log('Delete records with query: ', query); // remove using the query ctx.treatments.remove(query, function(err, stat) { if (err) { - res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err); - console.log('Error saving treatment'); - console.log(err); - } else { - res.json(stat); - console.log('treatments records deleted'); + console.log('treatments delete error: ', err); + return next(err); } + // yield some information about success of operation + res.json(stat); + + console.log('treatments records deleted'); + + return next(); }); - }); + } + + api.delete('/treatments/:id', ctx.authorization.isPermitted('api:treatments:delete'), function(req, res, next) { + if (!req.query.find) { + req.query.find = { + _id: req.params.id + }; + } else { + req.query.find._id = req.params.id; + } + + if (req.query.find._id === '*') { + // match any record id + delete req.query.find._id; + } + next(); + }, delete_records); + + // delete record that match query + api.delete('/treatments/', ctx.authorization.isPermitted('api:treatments:delete'), delete_records); // update record api.put('/treatments/', ctx.authorization.isPermitted('api:treatments:update'), function(req, res) { From ada17c8597dd6debef8a0aa2940fd90c161b2438 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 17:24:40 -0600 Subject: [PATCH 17/28] Remove old treatments id api path --- lib/api/treatments/index.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/api/treatments/index.js b/lib/api/treatments/index.js index 94530df2e6f..0362829fc7b 100644 --- a/lib/api/treatments/index.js +++ b/lib/api/treatments/index.js @@ -99,12 +99,6 @@ function configure(app, wares, ctx) { limit: 1048576 * 50 }), ctx.authorization.isPermitted('api:treatments:create'), post_response); - api.delete('/treatments/:_id', ctx.authorization.isPermitted('api:treatments:delete'), function(req, res) { - ctx.treatments.remove(req.params._id, function() { - res.json({}); - }); - }); - /** * @function delete_records * Delete treatments. The query logic works the same way as find/list. This From 50952dea5c9536a77478d4a60dca7981e4e3c548 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 17:57:39 -0600 Subject: [PATCH 18/28] Add entries support for delete query --- lib/api/entries/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/api/entries/index.js b/lib/api/entries/index.js index dea092e6bdb..6c1e873da7f 100644 --- a/lib/api/entries/index.js +++ b/lib/api/entries/index.js @@ -743,11 +743,12 @@ curl -s -g 'http://localhost:1337/api/v1/times/20{14..15}/T{13..18}:{00..15}'.js next(); }, delete_records); - + // delete record that match query + api.delete('/entries/', ctx.authorization.isPermitted('api:entries:delete'), delete_records); } return api; } // expose module -module.exports = configure; \ No newline at end of file +module.exports = configure; From b44890606b8675c635d4d9715db6aad79af3b5fb Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 18:26:37 -0600 Subject: [PATCH 19/28] Remove unused function. --- lib/server/treatments.js | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/lib/server/treatments.js b/lib/server/treatments.js index fb3d0cb2f13..f49ab45c713 100644 --- a/lib/server/treatments.js +++ b/lib/server/treatments.js @@ -100,20 +100,6 @@ function storage (env, ctx) { return api( ).remove(query_for(opts), fn); } - function remove_id (_id, fn) { - var filter; - if (_id === '*') { - filter = {}; - } else { - var objId = new ObjectID(_id); - filter = { '_id': objId }; - } - - api( ).remove(filter, fn); - - ctx.bus.emit('data-received'); - } - function save (obj, fn) { obj._id = new ObjectID(obj._id); prepareData(obj); From 7a01c4092966808f45041c5cbf67de7e608d5423 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Wed, 21 Nov 2018 18:32:14 -0600 Subject: [PATCH 20/28] Removed unused function. --- lib/api/devicestatus/index.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lib/api/devicestatus/index.js b/lib/api/devicestatus/index.js index 741176df4a8..5c665b1ffc5 100644 --- a/lib/api/devicestatus/index.js +++ b/lib/api/devicestatus/index.js @@ -2,13 +2,6 @@ var consts = require('../../constants'); -var ID_PATTERN = /^[a-f\d]{24}$/; - -function isId(value) { - //TODO: why did we need tht length check? - return value && ID_PATTERN.test(value) && value.length === 24; -} - function configure (app, wares, ctx) { var express = require('express'), api = express.Router( ); From 1734ec1731978842876d6631ffa0bc5d0e8310a0 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Sun, 2 Dec 2018 14:32:32 -0600 Subject: [PATCH 21/28] Add API updates for new delete options. --- swagger.json | 77 ++++++++++++++++++++++++++- swagger.yaml | 143 +++++++++++++++++++++++++++++++++++---------------- 2 files changed, 176 insertions(+), 44 deletions(-) diff --git a/swagger.json b/swagger.json index 537391e3983..9c0da694029 100755 --- a/swagger.json +++ b/swagger.json @@ -577,6 +577,81 @@ "description": "Treatments to be uploaded.", "required": true } + }, + "delete": { + "tags": [ + "Treatments" + ], + "summary": "Delete treatments matching query.", + "description": "Remove treatments, same search syntax as GET.", + "operationId": "remove", + "parameters": [ + { + "name": "find", + "in": "query", + "description": "The query used to find treatments to delete, support nested query syntax, for example `find[insulin][$gte]=3`. All find parameters are interpreted as strings.", + "required": false, + "schema": { + "type": "string" + } + }, + { + "name": "count", + "in": "query", + "description": "Number of entries to return.", + "required": false, + "schema": { + "type": "number" + } + } + ], + "responses": { + "200": { + "description": "Empty list is success." + } + } + } + }, + "/treatments/{spec}": { + "delete": { + "summary": "Delete treatments record with id provided in spec", + "description": "The Treatments endpoint returns information about the\nNightscout devicestatus records.\n", + "parameters": [ + { + "name": "spec", + "in": "path", + "description": "treatment id, such as `55cf81bc436037528ec75fa5`\n", + "required": true, + "schema": { + "type": "string" + } + } + ], + "tags": [ + "Treatments" + ], + "responses": { + "200": { + "description": "A status record of the delete.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DeleteStatus" + } + } + } + }, + "default": { + "description": "Unexpected error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + } } }, "/profile": { @@ -1356,4 +1431,4 @@ } } } -} +} \ No newline at end of file diff --git a/swagger.yaml b/swagger.yaml index 538fb872101..547ff8028d3 100755 --- a/swagger.yaml +++ b/swagger.yaml @@ -444,6 +444,60 @@ paths: $ref: '#/components/schemas/Treatments' description: Treatments to be uploaded. required: true + delete: + tags: + - Treatments + summary: Delete treatments matching query. + description: 'Remove treatments, same search syntax as GET.' + operationId: remove + parameters: + - name: find + in: query + description: >- + The query used to find treatments to delete, + support nested query syntax, for example `find[insulin][$gte]=3`. + All find parameters are interpreted as strings. + required: false + schema: + type: string + - name: count + in: query + description: Number of entries to return. + required: false + schema: + type: number + responses: + '200': + description: Empty list is success. + '/treatments/{spec}': + delete: + summary: Delete treatments record with id provided in spec + description: | + The Treatments endpoint returns information about the + Nightscout devicestatus records. + parameters: + - name: spec + in: path + description: | + treatment id, such as `55cf81bc436037528ec75fa5` + required: true + schema: + type: string + tags: + - Treatments + responses: + '200': + description: A status record of the delete. + content: + application/json: + schema: + $ref: '#/components/schemas/DeleteStatus' + default: + description: Unexpected error + content: + application/json: + schema: + $ref: '#/components/schemas/Error' /profile: get: summary: Profile @@ -484,10 +538,12 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' - '/devicestatus/': + /devicestatus/: get: summary: All Devicestatuses matching query - description: The Devicestatus endpoint returns information about the Nightscout devicestatus records. + description: >- + The Devicestatus endpoint returns information about the Nightscout + devicestatus records. parameters: - name: find in: query @@ -658,7 +714,9 @@ components: items: $ref: '#/components/schemas/Entry' Devicestatus: - required: ['device', 'created_at'] + required: + - device + - created_at properties: device: type: string @@ -691,28 +749,28 @@ components: $ref: '#/components/schemas/pumpbattery' reservoir: type: number - description: 'Amount of insulin remaining in pump reservoir' + description: Amount of insulin remaining in pump reservoir status: $ref: '#/components/schemas/pumpstatus' pumpbattery: properties: status: type: string - description: 'Pump Battery Status String' + description: Pump Battery Status String voltage: type: number - description: 'Pump Battery Voltage Level' + description: Pump Battery Voltage Level pumpstatus: properties: status: type: string - description: 'Pump Status String' + description: Pump Status String bolusing: type: boolean - description: 'Is Pump Bolusing' + description: Is Pump Bolusing suspended: type: boolean - description: 'Is Pump Suspended' + description: Is Pump Suspended timestamp: type: string description: 'dateString, prefer ISO `8601`' @@ -720,87 +778,87 @@ components: properties: batteryVoltage: type: number - description: 'Uploader Device Battery Voltage' + description: Uploader Device Battery Voltage battery: type: number - description: 'Uploader Device Battery Percentage Charge Remaining' + description: Uploader Device Battery Percentage Charge Remaining xdripjs: properties: state: type: number - description: 'CGM Sensor Session State Code' + description: CGM Sensor Session State Code stateString: type: string - description: 'CGM Sensor Session State String' + description: CGM Sensor Session State String stateStringShort: type: string - description: 'CGM Sensor Session State Short String' + description: CGM Sensor Session State Short String txId: type: string - description: 'CGM Transmitter ID' + description: CGM Transmitter ID txStatus: type: number - description: 'CGM Transmitter Status' + description: CGM Transmitter Status txStatusString: type: string - description: 'CGM Transmitter Status String' + description: CGM Transmitter Status String txStatusStringShort: type: string - description: 'CGM Transmitter Status Short String' + description: CGM Transmitter Status Short String txActivation: type: number - description: 'CGM Transmitter Activation Milliseconds After Epoch' + description: CGM Transmitter Activation Milliseconds After Epoch mode: type: string description: 'Mode xdrip-js Application Operationg: expired, not expired, etc.' timestamp: type: number - description: 'Last Update Milliseconds After Epoch' + description: Last Update Milliseconds After Epoch rssi: type: number - description: 'Receive Signal Strength of Transmitter' + description: Receive Signal Strength of Transmitter unfiltered: type: number - description: 'Most Recent Raw Unfiltered Glucose' + description: Most Recent Raw Unfiltered Glucose filtered: type: number - description: 'Most Recent Raw Filtered Glucose' + description: Most Recent Raw Filtered Glucose noise: type: number description: 'Calculated Noise Value - 1=Clean, 2=Light, 3=Medium, 4=Heavy' noiseString: type: number - description: 'Noise Value String' + description: Noise Value String slope: type: number - description: 'Calibration Slope Value' + description: Calibration Slope Value intercept: type: number - description: 'Calibration Intercept Value' + description: Calibration Intercept Value calType: type: string - description: 'Algorithm Used to Calculate Calibration Values' + description: Algorithm Used to Calculate Calibration Values lastCalibrationDate: type: number - description: 'Most Recent Calibration Milliseconds After Epoch' + description: Most Recent Calibration Milliseconds After Epoch sessionStart: type: number - description: 'Sensor Session Start Milliseconds After Epoch' + description: Sensor Session Start Milliseconds After Epoch batteryTimestamp: type: number - description: 'Most Recent Batter Status Read Milliseconds After Epoch' + description: Most Recent Batter Status Read Milliseconds After Epoch voltagea: type: number - description: 'Voltage of Battery A' + description: Voltage of Battery A voltageb: type: number - description: 'Voltage of Battery B' + description: Voltage of Battery B temperature: type: number - description: 'Transmitter Temperature' + description: Transmitter Temperature resistance: type: number - description: 'Sensor Resistance' + description: Sensor Resistance Treatment: properties: _id: @@ -984,32 +1042,31 @@ components: type: object DeleteStatus: properties: - n: + 'n': type: integer format: int32 - description: 'Number of records deleted' + description: Number of records deleted optime: $ref: '#/components/schemas/optime' electionId: type: string - description: 'Election id of operation' + description: Election id of operation ok: type: integer format: int32 - description: 'Status of whether delete was successful' + description: Status of whether delete was successful operationTime: type: string - description: 'Time delete operation was executed' + description: Time delete operation was executed $clusterTime: type: string - description: 'Information about execution time in cluster environment' + description: Information about execution time in cluster environment optime: properties: ts: type: string - description: 'Time the operation started' + description: Time the operation started t: type: integer format: int32 - description: 'Time the operation took to complete' - + description: Time the operation took to complete From 440807ef899fe43ac6e71168718cccd3bd2d8ea5 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Fri, 7 Dec 2018 17:38:01 -0600 Subject: [PATCH 22/28] Reload data when entries or treatments are deleted --- lib/server/entries.js | 8 +++++--- lib/server/treatments.js | 6 +++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/server/entries.js b/lib/server/entries.js index 93b1b7f1ecc..84f8f234bfa 100644 --- a/lib/server/entries.js +++ b/lib/server/entries.js @@ -45,7 +45,11 @@ function storage(env, ctx) { } function remove (opts, fn) { - api( ).remove(query_for(opts), fn); + api( ).remove(query_for(opts), function (err, stat) { + //TODO: this is triggering a read from Mongo, we can do better + ctx.bus.emit('data-received'); + fn(err, stat); + }); } // return writable stream to lint each sgv record passing through it @@ -74,8 +78,6 @@ function storage(env, ctx) { //function update (fn) { //} // - //function remove (fn) { - //} // store new documents using the storage mechanism function create (docs, fn) { diff --git a/lib/server/treatments.js b/lib/server/treatments.js index f49ab45c713..aa8f1e0d8af 100644 --- a/lib/server/treatments.js +++ b/lib/server/treatments.js @@ -97,7 +97,11 @@ function storage (env, ctx) { } function remove (opts, fn) { - return api( ).remove(query_for(opts), fn); + return api( ).remove(query_for(opts), function (err, stat) { + //TODO: this is triggering a read from Mongo, we can do better + ctx.bus.emit('data-received'); + fn(err, stat); + }); } function save (obj, fn) { From 9b59845105e72f325fef1da9dab658660e6b291e Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Sun, 9 Dec 2018 21:14:37 -0600 Subject: [PATCH 23/28] Add new strings to language.js --- lib/language.js | 386 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 385 insertions(+), 1 deletion(-) diff --git a/lib/language.js b/lib/language.js index aa8914acac9..761d2e7dc69 100644 --- a/lib/language.js +++ b/lib/language.js @@ -2319,6 +2319,30 @@ function init() { ,tr: 'Hata: Veritabanı yüklenemedi' ,zh_cn: '错误:数据库载入失败' } + ,'Error' : { + cs: 'Error' + ,he: 'Error' + ,nb: 'Error' + ,fr: 'Error' + ,ro: 'Error' + ,el: 'Error' + ,de: 'Error' + ,es: 'Error' + ,dk: 'Error' + ,sv: 'Error' + ,bg: 'Error' + ,it: 'Error' + ,fi: 'Error' + ,pl: 'Error' + ,pt: 'Error' + ,ru: 'Error' + ,sk: 'Error' + ,nl: 'Error' + ,ko: 'Error' + ,tr: 'Error' + ,zh_cn: 'Error' + ,zh_tw: 'Error' + } ,'Create new record' : { cs: 'Vytvořit nový záznam' ,de: 'Erstelle neuen Datensatz' @@ -7524,6 +7548,30 @@ function init() { ,zh_cn: '正在删除记录...' ,zh_tw: '正在刪除記錄...' } + ,'%1 records deleted' : { + cs: '%1 records deleted' + ,he: '%1 records deleted' + ,nb: '%1 records deleted' + ,fr: '%1 records deleted' + ,ro: '%1 records deleted' + ,el: '%1 records deleted' + ,de: '%1 records deleted' + ,es: '%1 records deleted' + ,dk: '%1 records deleted' + ,sv: '%1 records deleted' + ,bg: '%1 records deleted' + ,it: '%1 records deleted' + ,fi: '%1 records deleted' + ,pl: '%1 records deleted' + ,pt: '%1 records deleted' + ,ru: '%1 records deleted' + ,sk: '%1 records deleted' + ,nl: '%1 records deleted' + ,ko: '%1 records deleted' + ,tr: '%1 records deleted' + ,zh_cn: '%1 records deleted' + ,zh_tw: '%1 records deleted' + } ,'Clean Mongo status database' : { cs: 'Vyčištění Mongo databáze statusů' ,he: 'נקי מסד הנתונים מצב מונגו ' @@ -7615,7 +7663,7 @@ function init() { ,tr: 'Tüm Belgeleri sil' ,zh_cn: '删除所有文档' } - ,'Delete all documents from devicestatus collection devicestatus?' : { + ,'Delete all documents from devicestatus collection?' : { cs: 'Odstranit všechny dokumenty z kolekce devicestatus?' ,he: 'מחק את כל המסמכים מרשימת סטטוס ההתקנים ' ,nb: 'Fjern alle dokumenter fra device status tabellen?' @@ -7684,6 +7732,342 @@ function init() { ,tr: 'Tüm kayıtlar kaldırıldı ...' ,zh_cn: '所有记录已经被清除' } + ,'Delete all documents from devicestatus collection older than 30 days' : { + cs: 'Delete all documents from devicestatus collection older than 30 days' + ,he: 'Delete all documents from devicestatus collection older than 30 days' + ,nb: 'Delete all documents from devicestatus collection older than 30 days' + ,fr: 'Delete all documents from devicestatus collection older than 30 days' + ,ro: 'Delete all documents from devicestatus collection older than 30 days' + ,el: 'Delete all documents from devicestatus collection older than 30 days' + ,de: 'Delete all documents from devicestatus collection older than 30 days' + ,es: 'Delete all documents from devicestatus collection older than 30 days' + ,dk: 'Delete all documents from devicestatus collection older than 30 days' + ,sv: 'Delete all documents from devicestatus collection older than 30 days' + ,bg: 'Delete all documents from devicestatus collection older than 30 days' + ,it: 'Delete all documents from devicestatus collection older than 30 days' + ,fi: 'Delete all documents from devicestatus collection older than 30 days' + ,pl: 'Delete all documents from devicestatus collection older than 30 days' + ,pt: 'Delete all documents from devicestatus collection older than 30 days' + ,ru: 'Delete all documents from devicestatus collection older than 30 days' + ,sk: 'Delete all documents from devicestatus collection older than 30 days' + ,nl: 'Delete all documents from devicestatus collection older than 30 days' + ,ko: 'Delete all documents from devicestatus collection older than 30 days' + ,tr: 'Delete all documents from devicestatus collection older than 30 days' + ,zh_cn: 'Delete all documents from devicestatus collection older than 30 days' + ,zh_tw: 'Delete all documents from devicestatus collection older than 30 days' + } + ,'Number of Days to Keep:' : { + cs: 'Number of Days to Keep:' + ,he: 'Number of Days to Keep:' + ,nb: 'Number of Days to Keep:' + ,fr: 'Number of Days to Keep:' + ,ro: 'Number of Days to Keep:' + ,el: 'Number of Days to Keep:' + ,de: 'Number of Days to Keep:' + ,es: 'Number of Days to Keep:' + ,dk: 'Number of Days to Keep:' + ,sv: 'Number of Days to Keep:' + ,bg: 'Number of Days to Keep:' + ,it: 'Number of Days to Keep:' + ,fi: 'Number of Days to Keep:' + ,pl: 'Number of Days to Keep:' + ,pt: 'Number of Days to Keep:' + ,ru: 'Number of Days to Keep:' + ,sk: 'Number of Days to Keep:' + ,nl: 'Number of Days to Keep:' + ,ko: 'Number of Days to Keep:' + ,tr: 'Number of Days to Keep:' + ,zh_cn: 'Number of Days to Keep:' + ,zh_tw: 'Number of Days to Keep:' + } + ,'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' : { + cs: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,he: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,nb: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,fr: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,ro: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,el: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,de: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,es: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,dk: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,sv: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,bg: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,it: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,fi: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,pl: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,pt: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,ru: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,sk: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,nl: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,ko: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,tr: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,zh_cn: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + ,zh_tw: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.' + } + ,'Delete old documents from devicestatus collection?' : { + cs: 'Delete old documents from devicestatus collection?' + ,he: 'Delete old documents from devicestatus collection?' + ,nb: 'Delete old documents from devicestatus collection?' + ,fr: 'Delete old documents from devicestatus collection?' + ,ro: 'Delete old documents from devicestatus collection?' + ,el: 'Delete old documents from devicestatus collection?' + ,de: 'Delete old documents from devicestatus collection?' + ,es: 'Delete old documents from devicestatus collection?' + ,dk: 'Delete old documents from devicestatus collection?' + ,sv: 'Delete old documents from devicestatus collection?' + ,bg: 'Delete old documents from devicestatus collection?' + ,it: 'Delete old documents from devicestatus collection?' + ,fi: 'Delete old documents from devicestatus collection?' + ,pl: 'Delete old documents from devicestatus collection?' + ,pt: 'Delete old documents from devicestatus collection?' + ,ru: 'Delete old documents from devicestatus collection?' + ,sk: 'Delete old documents from devicestatus collection?' + ,nl: 'Delete old documents from devicestatus collection?' + ,ko: 'Delete old documents from devicestatus collection?' + ,tr: 'Delete old documents from devicestatus collection?' + ,zh_cn: 'Delete old documents from devicestatus collection?' + ,zh_tw: 'Delete old documents from devicestatus collection?' + } + ,'Clean Mongo entries (glucose entries) database' : { + cs: 'Clean Mongo entries (glucose entries) database' + ,he: 'Clean Mongo entries (glucose entries) database' + ,nb: 'Clean Mongo entries (glucose entries) database' + ,fr: 'Clean Mongo entries (glucose entries) database' + ,ro: 'Clean Mongo entries (glucose entries) database' + ,el: 'Clean Mongo entries (glucose entries) database' + ,de: 'Clean Mongo entries (glucose entries) database' + ,es: 'Clean Mongo entries (glucose entries) database' + ,dk: 'Clean Mongo entries (glucose entries) database' + ,sv: 'Clean Mongo entries (glucose entries) database' + ,bg: 'Clean Mongo entries (glucose entries) database' + ,it: 'Clean Mongo entries (glucose entries) database' + ,fi: 'Clean Mongo entries (glucose entries) database' + ,pl: 'Clean Mongo entries (glucose entries) database' + ,pt: 'Clean Mongo entries (glucose entries) database' + ,ru: 'Clean Mongo entries (glucose entries) database' + ,sk: 'Clean Mongo entries (glucose entries) database' + ,nl: 'Clean Mongo entries (glucose entries) database' + ,ko: 'Clean Mongo entries (glucose entries) database' + ,tr: 'Clean Mongo entries (glucose entries) database' + ,zh_cn: 'Clean Mongo entries (glucose entries) database' + ,zh_tw: 'Clean Mongo entries (glucose entries) database' + } + ,'Delete all documents from entries collection older than 180 days' : { + cs: 'Delete all documents from entries collection older than 180 days' + ,he: 'Delete all documents from entries collection older than 180 days' + ,nb: 'Delete all documents from entries collection older than 180 days' + ,fr: 'Delete all documents from entries collection older than 180 days' + ,ro: 'Delete all documents from entries collection older than 180 days' + ,el: 'Delete all documents from entries collection older than 180 days' + ,de: 'Delete all documents from entries collection older than 180 days' + ,es: 'Delete all documents from entries collection older than 180 days' + ,dk: 'Delete all documents from entries collection older than 180 days' + ,sv: 'Delete all documents from entries collection older than 180 days' + ,bg: 'Delete all documents from entries collection older than 180 days' + ,it: 'Delete all documents from entries collection older than 180 days' + ,fi: 'Delete all documents from entries collection older than 180 days' + ,pl: 'Delete all documents from entries collection older than 180 days' + ,pt: 'Delete all documents from entries collection older than 180 days' + ,ru: 'Delete all documents from entries collection older than 180 days' + ,sk: 'Delete all documents from entries collection older than 180 days' + ,nl: 'Delete all documents from entries collection older than 180 days' + ,ko: 'Delete all documents from entries collection older than 180 days' + ,tr: 'Delete all documents from entries collection older than 180 days' + ,zh_cn: 'Delete all documents from entries collection older than 180 days' + ,zh_tw: 'Delete all documents from entries collection older than 180 days' + } + ,'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' : { + cs: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,he: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,nb: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,fr: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,ro: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,el: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,de: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,es: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,dk: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,sv: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,bg: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,it: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,fi: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,pl: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,pt: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,ru: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,sk: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,nl: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,ko: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,tr: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,zh_cn: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,zh_tw: 'This task removes all documents from entries collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + } + ,'Delete old documents' : { + cs: 'Delete old documents' + ,he: 'Delete old documents' + ,nb: 'Delete old documents' + ,fr: 'Delete old documents' + ,ro: 'Delete old documents' + ,el: 'Delete old documents' + ,de: 'Delete old documents' + ,es: 'Delete old documents' + ,dk: 'Delete old documents' + ,sv: 'Delete old documents' + ,bg: 'Delete old documents' + ,it: 'Delete old documents' + ,fi: 'Delete old documents' + ,pl: 'Delete old documents' + ,pt: 'Delete old documents' + ,ru: 'Delete old documents' + ,sk: 'Delete old documents' + ,nl: 'Delete old documents' + ,ko: 'Delete old documents' + ,tr: 'Delete old documents' + ,zh_cn: 'Delete old documents' + ,zh_tw: 'Delete old documents' + } + ,'Delete old documents from entries collection?' : { + cs: 'Delete old documents from entries collection?' + ,he: 'Delete old documents from entries collection?' + ,nb: 'Delete old documents from entries collection?' + ,fr: 'Delete old documents from entries collection?' + ,ro: 'Delete old documents from entries collection?' + ,el: 'Delete old documents from entries collection?' + ,de: 'Delete old documents from entries collection?' + ,es: 'Delete old documents from entries collection?' + ,dk: 'Delete old documents from entries collection?' + ,sv: 'Delete old documents from entries collection?' + ,bg: 'Delete old documents from entries collection?' + ,it: 'Delete old documents from entries collection?' + ,fi: 'Delete old documents from entries collection?' + ,pl: 'Delete old documents from entries collection?' + ,pt: 'Delete old documents from entries collection?' + ,ru: 'Delete old documents from entries collection?' + ,sk: 'Delete old documents from entries collection?' + ,nl: 'Delete old documents from entries collection?' + ,ko: 'Delete old documents from entries collection?' + ,tr: 'Delete old documents from entries collection?' + ,zh_cn: 'Delete old documents from entries collection?' + ,zh_tw: 'Delete old documents from entries collection?' + } + ,'%1 is not a valid number' : { + cs: '%1 is not a valid number' + ,he: '%1 is not a valid number' + ,nb: '%1 is not a valid number' + ,fr: '%1 is not a valid number' + ,ro: '%1 is not a valid number' + ,el: '%1 is not a valid number' + ,de: '%1 is not a valid number' + ,es: '%1 is not a valid number' + ,dk: '%1 is not a valid number' + ,sv: '%1 is not a valid number' + ,bg: '%1 is not a valid number' + ,it: '%1 is not a valid number' + ,fi: '%1 is not a valid number' + ,pl: '%1 is not a valid number' + ,pt: '%1 is not a valid number' + ,ru: '%1 is not a valid number' + ,sk: '%1 is not a valid number' + ,nl: '%1 is not a valid number' + ,ko: '%1 is not a valid number' + ,tr: '%1 is not a valid number' + ,zh_cn: '%1 is not a valid number' + ,zh_tw: '%1 is not a valid number' + } + ,'Clean Mongo treatments database' : { + cs: 'Clean Mongo treatments database' + ,he: 'Clean Mongo treatments database' + ,nb: 'Clean Mongo treatments database' + ,fr: 'Clean Mongo treatments database' + ,ro: 'Clean Mongo treatments database' + ,el: 'Clean Mongo treatments database' + ,de: 'Clean Mongo treatments database' + ,es: 'Clean Mongo treatments database' + ,dk: 'Clean Mongo treatments database' + ,sv: 'Clean Mongo treatments database' + ,bg: 'Clean Mongo treatments database' + ,it: 'Clean Mongo treatments database' + ,fi: 'Clean Mongo treatments database' + ,pl: 'Clean Mongo treatments database' + ,pt: 'Clean Mongo treatments database' + ,ru: 'Clean Mongo treatments database' + ,sk: 'Clean Mongo treatments database' + ,nl: 'Clean Mongo treatments database' + ,ko: 'Clean Mongo treatments database' + ,tr: 'Clean Mongo treatments database' + ,zh_cn: 'Clean Mongo treatments database' + ,zh_tw: 'Clean Mongo treatments database' + } + ,'Delete all documents from treatments collection older than 180 days' : { + cs: 'Delete all documents from treatments collection older than 180 days' + ,he: 'Delete all documents from treatments collection older than 180 days' + ,nb: 'Delete all documents from treatments collection older than 180 days' + ,fr: 'Delete all documents from treatments collection older than 180 days' + ,ro: 'Delete all documents from treatments collection older than 180 days' + ,el: 'Delete all documents from treatments collection older than 180 days' + ,de: 'Delete all documents from treatments collection older than 180 days' + ,es: 'Delete all documents from treatments collection older than 180 days' + ,dk: 'Delete all documents from treatments collection older than 180 days' + ,sv: 'Delete all documents from treatments collection older than 180 days' + ,bg: 'Delete all documents from treatments collection older than 180 days' + ,it: 'Delete all documents from treatments collection older than 180 days' + ,fi: 'Delete all documents from treatments collection older than 180 days' + ,pl: 'Delete all documents from treatments collection older than 180 days' + ,pt: 'Delete all documents from treatments collection older than 180 days' + ,ru: 'Delete all documents from treatments collection older than 180 days' + ,sk: 'Delete all documents from treatments collection older than 180 days' + ,nl: 'Delete all documents from treatments collection older than 180 days' + ,ko: 'Delete all documents from treatments collection older than 180 days' + ,tr: 'Delete all documents from treatments collection older than 180 days' + ,zh_cn: 'Delete all documents from treatments collection older than 180 days' + ,zh_tw: 'Delete all documents from treatments collection older than 180 days' + } + ,'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' : { + cs: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,he: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,nb: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,fr: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,ro: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,el: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,de: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,es: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,dk: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,sv: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,bg: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,it: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,fi: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,pl: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,pt: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,ru: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,sk: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,nl: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,ko: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,tr: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,zh_cn: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + ,zh_tw: 'This task removes all documents from treatments collection that are older than 180 days. Useful when uploader battery status is not properly updated.' + } + ,'Delete old documents from treatments collection?' : { + cs: 'Delete old documents from treatments collection?' + ,he: 'Delete old documents from treatments collection?' + ,nb: 'Delete old documents from treatments collection?' + ,fr: 'Delete old documents from treatments collection?' + ,ro: 'Delete old documents from treatments collection?' + ,el: 'Delete old documents from treatments collection?' + ,de: 'Delete old documents from treatments collection?' + ,es: 'Delete old documents from treatments collection?' + ,dk: 'Delete old documents from treatments collection?' + ,sv: 'Delete old documents from treatments collection?' + ,bg: 'Delete old documents from treatments collection?' + ,it: 'Delete old documents from treatments collection?' + ,fi: 'Delete old documents from treatments collection?' + ,pl: 'Delete old documents from treatments collection?' + ,pt: 'Delete old documents from treatments collection?' + ,ru: 'Delete old documents from treatments collection?' + ,sk: 'Delete old documents from treatments collection?' + ,nl: 'Delete old documents from treatments collection?' + ,ko: 'Delete old documents from treatments collection?' + ,tr: 'Delete old documents from treatments collection?' + ,zh_cn: 'Delete old documents from treatments collection?' + ,zh_tw: 'Delete old documents from treatments collection?' + } ,'Admin Tools' : { cs: 'Nástroje pro správu' ,he: 'כלי אדמיניסטרציה ' From 8136633d8f074eae8de6d10891fe924b46add08f Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Sun, 9 Dec 2018 21:19:39 -0600 Subject: [PATCH 24/28] Prevent deleting entries or treatments more recent than 2 days ago --- lib/admin_plugins/cleanentriesdb.js | 4 ++-- lib/admin_plugins/cleantreatmentsdb.js | 4 ++-- lib/language.js | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/admin_plugins/cleanentriesdb.js b/lib/admin_plugins/cleanentriesdb.js index 249b86c2f70..0b6153bf2ca 100644 --- a/lib/admin_plugins/cleanentriesdb.js +++ b/lib/admin_plugins/cleanentriesdb.js @@ -46,8 +46,8 @@ cleanentriesdb.actions[0].code = function deleteOldRecords(client, callback) { var $status = $('#admin_' + cleanentriesdb.name + '_0_status'); var numDays = Number($('#admin_entries_days').val()); - if (isNaN(numDays) || (numDays < 1)) { - alert(translate('%1 is not a valid number', { params: [$('#admin_entries_days').val()] })); + if (isNaN(numDays) || (numDays < 3)) { + alert(translate('%1 is not a valid number - must be more than 2', { params: [$('#admin_entries_days').val()] })); if (callback) { callback(); } return; } diff --git a/lib/admin_plugins/cleantreatmentsdb.js b/lib/admin_plugins/cleantreatmentsdb.js index 1c58276ce43..19ed4d59c81 100644 --- a/lib/admin_plugins/cleantreatmentsdb.js +++ b/lib/admin_plugins/cleantreatmentsdb.js @@ -46,8 +46,8 @@ cleantreatmentsdb.actions[0].code = function deleteOldRecords(client, callback) var $status = $('#admin_' + cleantreatmentsdb.name + '_0_status'); var numDays = Number($('#admin_treatments_days').val()); - if (isNaN(numDays) || (numDays < 1)) { - alert(translate('%1 is not a valid number', { params: [$('#admin_treatments_days').val()] })); + if (isNaN(numDays) || (numDays < 3)) { + alert(translate('%1 is not a valid number - must be more than 2', { params: [$('#admin_treatments_days').val()] })); if (callback) { callback(); } return; } diff --git a/lib/language.js b/lib/language.js index 761d2e7dc69..9b3c4b2d099 100644 --- a/lib/language.js +++ b/lib/language.js @@ -7972,6 +7972,30 @@ function init() { ,zh_cn: '%1 is not a valid number' ,zh_tw: '%1 is not a valid number' } + ,'%1 is not a valid number - must be more than 2' : { + cs: '%1 is not a valid number - must be more than 2' + ,he: '%1 is not a valid number - must be more than 2' + ,nb: '%1 is not a valid number - must be more than 2' + ,fr: '%1 is not a valid number - must be more than 2' + ,ro: '%1 is not a valid number - must be more than 2' + ,el: '%1 is not a valid number - must be more than 2' + ,de: '%1 is not a valid number - must be more than 2' + ,es: '%1 is not a valid number - must be more than 2' + ,dk: '%1 is not a valid number - must be more than 2' + ,sv: '%1 is not a valid number - must be more than 2' + ,bg: '%1 is not a valid number - must be more than 2' + ,it: '%1 is not a valid number - must be more than 2' + ,fi: '%1 is not a valid number - must be more than 2' + ,pl: '%1 is not a valid number - must be more than 2' + ,pt: '%1 is not a valid number - must be more than 2' + ,ru: '%1 is not a valid number - must be more than 2' + ,sk: '%1 is not a valid number - must be more than 2' + ,nl: '%1 is not a valid number - must be more than 2' + ,ko: '%1 is not a valid number - must be more than 2' + ,tr: '%1 is not a valid number - must be more than 2' + ,zh_cn: '%1 is not a valid number - must be more than 2' + ,zh_tw: '%1 is not a valid number - must be more than 2' + } ,'Clean Mongo treatments database' : { cs: 'Clean Mongo treatments database' ,he: 'Clean Mongo treatments database' From d9527d399ba7406cb4b8a9a3a7c997237447e264 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Sun, 9 Dec 2018 22:49:55 -0600 Subject: [PATCH 25/28] Add unit test for removing old devicestatus records --- lib/admin_plugins/cleanstatusdb.js | 12 ++++++------ npm-shrinkwrap.json | 2 +- tests/admintools.test.js | 12 ++++++++++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/admin_plugins/cleanstatusdb.js b/lib/admin_plugins/cleanstatusdb.js index f2014d69b23..baa7591a99c 100644 --- a/lib/admin_plugins/cleanstatusdb.js +++ b/lib/admin_plugins/cleanstatusdb.js @@ -116,18 +116,18 @@ cleanstatusdb.actions[1].code = function deleteOldRecords(client, callback) { } $status.hide().text(translate('Deleting records ...')).fadeIn('slow'); - $.ajax({ + $.ajax('/api/v1/devicestatus/?find[created_at][$lte]=' + dateStr, { method: 'DELETE' - , url: '/api/v1/devicestatus/?find[created_at][$lte]=' + dateStr , headers: client.headers() , success: function (retVal) { - $status.hide().text(translate('%1 records deleted',{ params: [retVal.n] })).fadeIn('slow'); + $status.text(translate('%1 records deleted',{ params: [retVal.n] })); } - }).done(function success () { + , error: function () { + $status.hide().text(translate('Error')).fadeIn('slow'); + } + }).done(function success (retVal) { if (callback) { callback(); } }).fail(function fail() { - $status.hide().text(translate('Error')).fadeIn('slow'); if (callback) { callback(); } }); - }; diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 8f29d08187d..d3c30a6b3c6 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -423,7 +423,7 @@ }, "async": { "version": "0.9.2", - "resolved": "http://registry.npmjs.org/async/-/async-0.9.2.tgz", + "resolved": "https://registry.npmjs.org/async/-/async-0.9.2.tgz", "integrity": "sha1-rqdNXmHB+JlhO/ZL2mbUx48v0X0=" }, "async-each": { diff --git a/tests/admintools.test.js b/tests/admintools.test.js index b5be4858edb..817a151e456 100644 --- a/tests/admintools.test.js +++ b/tests/admintools.test.js @@ -29,6 +29,9 @@ var someData = { 'created_at': '2025-09-28T16:41:07.144Z' } ], + '/api/v1/devicestatus/?find[created_at][$lte]=': { + n: 1 + }, '/api/v1/treatments.json?&find[created_at][$gte]=': [ { '_id': '5609a9203c8104a8195b1c1e', @@ -102,6 +105,9 @@ describe('admintools', function ( ) { if (url.indexOf('/api/v1/entries.json?&find[date][$gte]=')===0) { url = '/api/v1/entries.json?&find[date][$gte]='; } + if (url.indexOf('/api/v1/devicestatus/?find[created_at][$lte]=')===0) { + url = '/api/v1/devicestatus/?find[created_at][$lte]='; + } return { done: function mockDone (fn) { if (someData[url]) { @@ -219,6 +225,12 @@ describe('admintools', function ( ) { $('#admin_cleanstatusdb_0_html + button').click(); $('#admin_cleanstatusdb_0_status').text().should.equal('All records removed ...'); // devicestatus code result + $('#admin_cleanstatusdb_1_html + button').text().should.equal('Delete old documents'); // devicestatus button + $('#admin_cleanstatusdb_1_status').text().should.equal(''); // devicestatus init result + + $('#admin_cleanstatusdb_1_html + button').click(); + $('#admin_cleanstatusdb_1_status').text().should.equal('1 records deleted'); // devicestatus code result + $('#admin_futureitems_0_html + button').text().should.equal('Remove treatments in the future'); // futureitems button 0 $('#admin_futureitems_0_status').text().should.equal('Database contains 1 future records'); // futureitems init result 0 From a34167aec3e7ef50ddcc16570b76f0615f447f27 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Sun, 9 Dec 2018 22:54:33 -0600 Subject: [PATCH 26/28] Fix codacy finding --- lib/admin_plugins/cleanstatusdb.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/admin_plugins/cleanstatusdb.js b/lib/admin_plugins/cleanstatusdb.js index baa7591a99c..b3e3032c8fe 100644 --- a/lib/admin_plugins/cleanstatusdb.js +++ b/lib/admin_plugins/cleanstatusdb.js @@ -125,7 +125,7 @@ cleanstatusdb.actions[1].code = function deleteOldRecords(client, callback) { , error: function () { $status.hide().text(translate('Error')).fadeIn('slow'); } - }).done(function success (retVal) { + }).done(function success () { if (callback) { callback(); } }).fail(function fail() { if (callback) { callback(); } From 83bd1a7d316c04f1bea368bb7e940ead557f22e6 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Mon, 10 Dec 2018 17:45:00 -0600 Subject: [PATCH 27/28] Add unit test for cleantreatmentsdb.js --- lib/admin_plugins/cleantreatmentsdb.js | 7 ++++--- tests/admintools.test.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/admin_plugins/cleantreatmentsdb.js b/lib/admin_plugins/cleantreatmentsdb.js index 19ed4d59c81..a8cc725fd82 100644 --- a/lib/admin_plugins/cleantreatmentsdb.js +++ b/lib/admin_plugins/cleantreatmentsdb.js @@ -63,17 +63,18 @@ cleantreatmentsdb.actions[0].code = function deleteOldRecords(client, callback) } $status.hide().text(translate('Deleting records ...')).fadeIn('slow'); - $.ajax({ + $.ajax('/api/v1/treatments/?find[created_at][$lte]=' + dateStr, { method: 'DELETE' - , url: '/api/v1/treatments/?find[created_at][$lte]=' + dateStr , headers: client.headers() , success: function (retVal) { $status.hide().text(translate('%1 records deleted',{ params: [retVal.n] })).fadeIn('slow'); } + , error: function () { + $status.hide().text(translate('Error')).fadeIn('slow'); + } }).done(function success () { if (callback) { callback(); } }).fail(function fail() { - $status.hide().text(translate('Error')).fadeIn('slow'); if (callback) { callback(); } }); diff --git a/tests/admintools.test.js b/tests/admintools.test.js index 817a151e456..5937c2bfe3e 100644 --- a/tests/admintools.test.js +++ b/tests/admintools.test.js @@ -41,6 +41,9 @@ var someData = { 'created_at': '2025-09-28T20:54:00.000Z' } ], + '/api/v1/treatments/?find[created_at][$lte]=': { + n: 1 + }, '/api/v1/entries.json?&find[date][$gte]=': [ { '_id': '560983f326c5a592d9b9ae0c', @@ -108,6 +111,9 @@ describe('admintools', function ( ) { if (url.indexOf('/api/v1/devicestatus/?find[created_at][$lte]=')===0) { url = '/api/v1/devicestatus/?find[created_at][$lte]='; } + if (url.indexOf('/api/v1/treatments/?find[created_at][$lte]=')===0) { + url = '/api/v1/treatments/?find[created_at][$lte]='; + } return { done: function mockDone (fn) { if (someData[url]) { @@ -243,6 +249,12 @@ describe('admintools', function ( ) { $('#admin_futureitems_1_html + button').click(); $('#admin_futureitems_1_status').text().should.equal('Record 560983f326c5a592d9b9ae0c removed ...'); // futureitems code result 1 + $('#admin_cleantreatmentsdb_0_html + button').text().should.equal('Delete old documents'); // treatments button + $('#admin_cleantreatmentsdb_0_status').text().should.equal(''); // treatments init result + + $('#admin_cleantreatmentsdb_0_html + button').click(); + $('#admin_cleantreatmentsdb_0_status').text().should.equal('1 records deleted'); // treatments code result + done(); }); From d3368e5311c3eaab986ea2c542d448ddf5a27058 Mon Sep 17 00:00:00 2001 From: Jeremy Cunningham Date: Mon, 10 Dec 2018 17:51:51 -0600 Subject: [PATCH 28/28] Add unit test for cleanentriesdb.js --- lib/admin_plugins/cleanentriesdb.js | 7 ++++--- tests/admintools.test.js | 24 ++++++++++++++++-------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/admin_plugins/cleanentriesdb.js b/lib/admin_plugins/cleanentriesdb.js index 0b6153bf2ca..3793eef8ffd 100644 --- a/lib/admin_plugins/cleanentriesdb.js +++ b/lib/admin_plugins/cleanentriesdb.js @@ -62,17 +62,18 @@ cleanentriesdb.actions[0].code = function deleteOldRecords(client, callback) { } $status.hide().text(translate('Deleting records ...')).fadeIn('slow'); - $.ajax({ + $.ajax('/api/v1/entries/?find[date][$lte]=' + endDate.valueOf(), { method: 'DELETE' - , url: '/api/v1/entries/?find[date][$lte]=' + endDate.valueOf() , headers: client.headers() , success: function (retVal) { $status.hide().text(translate('%1 records deleted',{ params: [retVal.n] })).fadeIn('slow'); } + , error: function () { + $status.hide().text(translate('Error')).fadeIn('slow'); + } }).done(function success () { if (callback) { callback(); } }).fail(function fail() { - $status.hide().text(translate('Error')).fadeIn('slow'); if (callback) { callback(); } }); diff --git a/tests/admintools.test.js b/tests/admintools.test.js index 5937c2bfe3e..2c2924c4a8c 100644 --- a/tests/admintools.test.js +++ b/tests/admintools.test.js @@ -57,8 +57,11 @@ var someData = { 'rssi': 178, 'noise': 1 } - ] - }; + ], + '/api/v1/entries/?find[date][$lte]=': { + n: 1 + }, +}; describe('admintools', function ( ) { @@ -104,15 +107,14 @@ describe('admintools', function ( ) { if (opts && opts.success && opts.success.call) { if (url.indexOf('/api/v1/treatments.json?&find[created_at][$gte]=')===0) { url = '/api/v1/treatments.json?&find[created_at][$gte]='; - } - if (url.indexOf('/api/v1/entries.json?&find[date][$gte]=')===0) { + } else if (url.indexOf('/api/v1/entries.json?&find[date][$gte]=')===0) { url = '/api/v1/entries.json?&find[date][$gte]='; - } - if (url.indexOf('/api/v1/devicestatus/?find[created_at][$lte]=')===0) { + } else if (url.indexOf('/api/v1/devicestatus/?find[created_at][$lte]=')===0) { url = '/api/v1/devicestatus/?find[created_at][$lte]='; - } - if (url.indexOf('/api/v1/treatments/?find[created_at][$lte]=')===0) { + } else if (url.indexOf('/api/v1/treatments/?find[created_at][$lte]=')===0) { url = '/api/v1/treatments/?find[created_at][$lte]='; + } else if (url.indexOf('/api/v1/entries/?find[date][$lte]=')===0) { + url = '/api/v1/entries/?find[date][$lte]='; } return { done: function mockDone (fn) { @@ -255,6 +257,12 @@ describe('admintools', function ( ) { $('#admin_cleantreatmentsdb_0_html + button').click(); $('#admin_cleantreatmentsdb_0_status').text().should.equal('1 records deleted'); // treatments code result + $('#admin_cleanentriesdb_0_html + button').text().should.equal('Delete old documents'); // entries button + $('#admin_cleanentriesdb_0_status').text().should.equal(''); // entries init result + + $('#admin_cleanentriesdb_0_html + button').click(); + $('#admin_cleanentriesdb_0_status').text().should.equal('1 records deleted'); // entries code result + done(); });