'use strict';

var cleanstatusdb = {
  name: 'cleanstatusdb'
  , label: 'Clean mongo status database'
  , pluginType: 'admin'
};

function init() {
  return cleanstatusdb;
}

module.exports = init;

cleanstatusdb.actions = [
    {
        name: 'Delete all documents from devicestatus collection'
      , description: 'This task removes all documents from devicestatus collection. Useful when uploader battery status is not properly updated'
      , buttonLabel: 'Delete all documents'
      , confirmText: 'Delete all documents from devicestatus collection?'
    }
  ];

cleanstatusdb.actions[0].init = function init(client, callback) {
  var translate = client.translate;
  var $status = $('#admin_' + cleanstatusdb.name + '_0_status');
  
  $status.hide().text(translate('Loading database ...')).fadeIn('slow');
  $.ajax('/api/v1/devicestatus.json?count=500000', {
    success: function (records) {
      $status.hide().text(translate('Database contains %1 records',{ params: [records.length] })).fadeIn('slow');
    },
    error: function () {
      $status.hide().text(translate('Error loading database')).fadeIn('slow');
    }
  }).done(function () { if (callback) { callback(); } });
};

cleanstatusdb.actions[0].code =  function deleteRecords(client, callback) {
  var translate = client.translate;
  var $status = $('#admin_' + cleanstatusdb.name + '_0_status');
  
  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/*'
    , headers: {
      'api-secret': client.hashauth.hash()
    }
  }).done(function success () {
    $status.hide().text(translate('All records removed ...')).fadeIn('slow');
    if (callback) {
      callback();
    }
  }).fail(function fail() {
    $status.hide().text(translate('Error')).fadeIn('slow');
    if (callback) {
      callback();
    }
  });
};

@@ -857,16 +857,13 @@
 =500
-000
 ', %7B%0A
+
     
@@ -888,24 +888,91 @@
 (records) %7B%0A
+      var recs = (records.length %3E 500 ? '500+' : records.length);%0A
       $statu
@@ -1040,27 +1040,17 @@
 ms: %5Brec
-ords.length
+s
 %5D %7D)).fa

load max 500 records