github.com/rohankumardubey/aresdb@v0.0.2-0.20190517170215-e54e3ca06b9c/api/ui/debug/js/purge.js (about)

     1  //  Copyright (c) 2017-2018 Uber Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  function initSchedulerViewer() {
    16      // Disable null value warning from data table.
    17      $.fn.dataTable.ext.errMode = 'none';
    18  
    19      $.ajax({
    20              url: "/dbg/jobs/purge",
    21              success: function (body) {
    22                  var runningJobData = [];
    23                  var pastRunsData = [];
    24                  for (var key in body) {
    25                      var strs = key.split("|");
    26                      var row = body[key];
    27                      row['table'] = strs[0];
    28                      row['shard'] = strs[1];
    29                      row['type'] = strs[2];
    30                      if ('lastDuration' in row) {
    31                          row['lastDuration'] = row['lastDuration'].toDuration();
    32                      }
    33  
    34                      if (row['status'] == 'running') {
    35                          runningJobData.push(row);
    36                      } else {
    37                          pastRunsData.push(row);
    38                      }
    39                  }
    40                  initRunningJobTable(runningJobData);
    41                  initPastRunTable(pastRunsData);
    42                  initSafePurgeButton();
    43              },
    44              error: function (xhr) {
    45                  alert(xhr.responseText);
    46              }
    47          }
    48      );
    49      initDatePickModal();
    50  
    51  }
    52  
    53  function initSafePurgeButton() {
    54      $('#purge-it').click(function() {
    55          const button = $(this);
    56          const table = button.data('table');
    57          const shard = button.data('shard');
    58          submitPurgeJob(table, shard, 0, 0, true);
    59      });
    60  }
    61  
    62  function initDatePickModal() {
    63      $('#start-batch-pick').datepicker();
    64      $('#end-batch-pick').datepicker();
    65      $('#date-pick-modal').on('show.bs.modal', function (event) {
    66          var button = $(event.relatedTarget);
    67          var table = button.data('table');
    68          var shard = button.data('shard');
    69          var modal = $(this);
    70          modal.find('#purge-table-name').val(table);
    71          modal.find('#purge-shard-id').val(shard)
    72      });
    73  
    74      $('#purge-button').click(function () {
    75          var startDate = $('#start-batch-pick').val();
    76          var endDate = $('#end-batch-pick').val();
    77          var table = $('#purge-table-name').val();
    78          var shard = $('#purge-shard-id').val();
    79          var batchIDStart = 0;
    80          var batchIDEnd = 0;
    81          if (startDate) {
    82              batchIDStart = Math.floor(new Date(startDate).getTime() / 1000 / 86400);
    83          }
    84          if (endDate) {
    85              batchIDEnd = Math.floor(new Date(endDate).getTime() / 1000 / 86400);
    86          }
    87  
    88          if (batchIDEnd <= batchIDStart) {
    89              alert("invalid batch start and end: [" + batchIDStart + "," + batchIDEnd + ")");
    90          }
    91  
    92          $.ajax({
    93              url: "/schema/tables/{0}".format(table),
    94              method: 'GET',
    95              success: function (schema) {
    96                  var retentionDays = schema.config.recordRetentionInDays;
    97                  if (retentionDays > 0) {
    98                      var now = Math.floor(Date.now() / 1000 / 86400);
    99                      if (batchIDEnd >= (now - retentionDays)) {
   100                          if (!confirm('Are you sure to purge data within retention from ' + (now-retentionDays) + ' to ' + batchIDEnd)) {
   101                              return;
   102                          }
   103                      }
   104                  }
   105                  submitPurgeJob(table, shard, batchIDStart, batchIDEnd, false);
   106              },
   107              error: function (xhr) {
   108                  alert(xhr.responseText);
   109              }
   110          });
   111      });
   112  }
   113  
   114  function submitPurgeJob(table, shard, batchIDStart, batchIDEnd, safePurge) {
   115      $.ajax({
   116          url: "/dbg/{0}/{1}/purge".format(table, shard),
   117          method: "POST",
   118          dataType: 'json',
   119          data: {
   120              batchIDStart: batchIDStart,
   121              batchIDEnd: batchIDEnd,
   122              safePurge: safePurge,
   123          },
   124          success: function (body) {
   125              alert(body);
   126              reloadCurrentTab();
   127          },
   128          error: function (xhr) {
   129              alert(xhr.responseText);
   130          }
   131      })
   132  }
   133  
   134  function initRunningJobTable(data) {
   135      $('#running-job-table').DataTable({
   136          paging: false,
   137          searching: false,
   138          aoColumns: [
   139              {title: "Table", data: "table"},
   140              {title: "Shard", data: "shard", type: "num"},
   141              {title: "Type", data: "type"},
   142              {title: "Stage", data: "stage"},
   143              {title: "Current", data: "current"},
   144              {title: "Total", data: "total"}
   145          ],
   146          aaData: data
   147      });
   148  }
   149  
   150  function initPastRunTable(data) {
   151      $('#past-runs-table').DataTable({
   152          paging: true,
   153          searching: true,
   154          pageLength: 20,
   155          lengthMenu: [[1, 10, 25, 50, 100], [1, 10, 25, 50, 100]],
   156          aoColumns: [
   157              {title: "Table", data: "table"},
   158              {title: "Shard", data: "shard", type: "num"},
   159              {title: "Type", data: "type"},
   160              {title: "Status", data: "status"},
   161              {title: "Number of Batches Purged", data: "numBatches"},
   162              {title: "Batch Start", data: "batchIDStart"},
   163              {title: "Batch End", data: "batchIDEnd"},
   164              {
   165                  title: "Action",
   166                  mData: null,
   167                  bSortable: false,
   168                  mRender: function (data, type, row) {
   169                      var table = row['table'];
   170                      var shard = row['shard'];
   171                      return $("<div />").append($(
   172                          "<button class='ui-button' id='purge-it' data-table='" + table + "' data-shard='" + shard + "'>safe purge</button>")).append($(
   173                          "<button class='ui-button' data-toggle='modal' data-target='#date-pick-modal' data-table='" + table + "' data-shard='" + shard + "'>customized purge</button>")
   174                      ).html();
   175                  }
   176              },
   177              {
   178                  title: "Last Error",
   179                  data: "lastError",
   180                  type: "string",
   181                  render: function (data) {
   182                      return JSON.stringify(data);
   183                  }
   184              },
   185              {
   186                  title: "Last Start Time",
   187                  data: "lastStartTime",
   188                  type: "date",
   189                  render: function (data) {
   190                      return new Date(data).toLocaleString();
   191                  }
   192              },
   193              {title: "Last Duration", data: "lastDuration", type: "string"},
   194          ],
   195          aaData: data,
   196      });
   197  }