Skip to content

Commit

Permalink
Merge pull request nightscout#3821 from jpcunningh/devicestatus-spec-…
Browse files Browse the repository at this point in the history
…delete

Devicestatus Delete Enhancement
  • Loading branch information
PieterGit authored Sep 29, 2018
2 parents edb1a44 + 1522307 commit af5d582
Show file tree
Hide file tree
Showing 5 changed files with 790 additions and 5 deletions.
64 changes: 64 additions & 0 deletions lib/admin_plugins/cleanstatusdb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var moment = require('moment');

var cleanstatusdb = {
name: 'cleanstatusdb'
, label: 'Clean Mongo status database'
Expand All @@ -19,6 +21,13 @@ cleanstatusdb.actions = [
, buttonLabel: 'Delete all documents'
, confirmText: 'Delete all documents from devicestatus collection?'
}
, {
name: 'Delete all documents from devicestatus collection older than 30 days'
, description: 'This task removes all documents from devicestatus collection that are older than 30 days. Useful when uploader battery status is not properly updated.'
, buttonLabel: 'Delete old documents'
, confirmText: 'Delete old documents from devicestatus collection?'
, preventClose: true
}
];

cleanstatusdb.actions[0].init = function init(client, callback) {
Expand Down Expand Up @@ -67,3 +76,58 @@ cleanstatusdb.actions[0].code = function deleteRecords(client, callback) {
}
});
};

cleanstatusdb.actions[1].init = function init(client, callback) {
var translate = client.translate;
var $status = $('#admin_' + cleanstatusdb.name + '_1_status');

$status.hide();

var numDays = '<br/>'
+ '<label for="admin_devicestatus_days">'
+ translate('Number of Days to Keep:')
+ ' <input id="admin_devicestatus_days" value="30" size="3" min="1"/>'
+ '</label>';

$('#admin_' + cleanstatusdb.name + '_1_html').html(numDays);

if (callback) { callback(); }
};

cleanstatusdb.actions[1].code = function deleteOldRecords(client, callback) {
var translate = client.translate;
var $status = $('#admin_' + cleanstatusdb.name + '_1_status');
var numDays = Number($('#admin_devicestatus_days').val());

if (isNaN(numDays) || (numDays < 1)) {
alert(translate('%1 is not a valid number', { params: [$('#admin_devicestatus_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/devicestatus/?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(); }
});

};
24 changes: 22 additions & 2 deletions lib/api/devicestatus/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ function configure (app, wares, ctx) {

api.post('/devicestatus/', ctx.authorization.isPermitted('api:devicestatus:create'), doPost);

// delete record
api.delete('/devicestatus/:_id', ctx.authorization.isPermitted('api:devicestatus:delete'), function(req, res) {
ctx.devicestatus.remove(req.params._id, function (err, removed) {
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 {
Expand All @@ -54,6 +55,25 @@ 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;

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);
} else {
res.json(stat);
console.log('devicestatus records deleted');
}
});
});

}

if (app.enabled('api') || true /*TODO: auth disabled for quick UI testing...*/) {
Expand Down
7 changes: 6 additions & 1 deletion lib/server/devicestatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ function storage (collection, ctx) {
).toArray(toArray);
}

function remove (_id, fn) {
function remove (opts, fn) {
return api( ).remove(query_for(opts), fn);
}

function remove_id (_id, fn) {
var filter;
if (_id === '*') {
filter = {};
Expand All @@ -78,6 +82,7 @@ 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'
Expand Down
Loading

0 comments on commit af5d582

Please sign in to comment.