github.com/rohankumardubey/aresdb@v0.0.2-0.20190517170215-e54e3ca06b9c/api/ui/debug/js/memory.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  var pieChart = null;
    16  jQuery.fn.dataTable.ext.type.order['data-size-pre'] = function (data) {
    17      var matches = data.match(/^(\d+\.\d+)?\s*([a-z]+)/i);
    18      var multipliers = {
    19  	    b: 1,
    20          kb: 1024,
    21          mb: 1048576,
    22          gb: 1073741824
    23      };
    24   
    25      if (matches) {
    26          var multiplier = multipliers[matches[2].toLowerCase()];
    27          return parseFloat(matches[1]) * multiplier;
    28      } else {
    29          return -1;
    30      }
    31  };
    32  
    33  function initHostMemoryExplorer() {
    34      $.ajax({
    35              url: "/dbg/host-memory",
    36              success: function (body) {
    37                  initTableShardList(body);
    38              },
    39              error: function (xhr) {
    40                  alert(xhr.responseText)
    41              }
    42          }
    43      )
    44  }
    45  
    46  var tableShardData = [];
    47  var rawData;
    48  function initTableShardList(data) {
    49      rawData = data;
    50      renderMemoryTable();
    51  }
    52  
    53  function processRawData(data) {
    54      tableShardData = [];
    55      Object.keys(data).forEach(function(tableShardKey) {
    56          var index = tableShardKey.lastIndexOf("_");
    57          var table = tableShardKey.substring(0, index);
    58          var shard = tableShardKey.substring(index+1);
    59  
    60          var preloadMemory = 0;
    61          var nonPreloadMemory = 0;
    62          var liveMemory = 0;
    63          var primaryKeyMemory = data[tableShardKey].pk;
    64          Object.keys(data[tableShardKey].cols).forEach(function(column) {
    65              preloadMemory += data[tableShardKey].cols[column].preloaded;
    66              nonPreloadMemory += data[tableShardKey].cols[column].nonPreloaded;
    67              liveMemory += data[tableShardKey].cols[column].live;
    68          });
    69  
    70          var tableShardItem = {
    71              table: table,
    72              shard: shard,
    73              preloadMemory: preloadMemory,
    74              nonPreloadMemory: nonPreloadMemory,
    75              liveMemory: liveMemory,
    76              primaryKeyMemory: primaryKeyMemory,
    77              total: preloadMemory + nonPreloadMemory + liveMemory + primaryKeyMemory,
    78              columns: processColumnMemoryData(tableShardKey, data[tableShardKey].cols)
    79          };
    80  
    81          tableShardData.push(tableShardItem);
    82      });
    83  }
    84  
    85  function processColumnMemoryData(tableShardKey, columns) {
    86      var columnMemoryData = [];
    87      Object.keys(columns).forEach(function(column) {
    88          var preloadMemory = columns[column].preloaded;
    89          var nonPreloadMemory = columns[column].nonPreloaded;
    90          var liveMemory = columns[column].live;
    91          var total = preloadMemory + nonPreloadMemory + liveMemory;
    92          columnMemoryData.push({
    93              column: column,
    94              preloadMemory: preloadMemory,
    95              nonPreloadMemory: nonPreloadMemory,
    96              liveMemory: liveMemory,
    97              total: total
    98          });
    99      });
   100      return columnMemoryData;
   101  }
   102  
   103  function drawPieChat(name, data, labels) {
   104      if (pieChart != null) {
   105          pieChart.destroy();
   106      }
   107  
   108      var ctx = document.getElementById("chart").getContext('2d');
   109      pieChart = new Chart(ctx, {
   110          type: 'pie',
   111          data: {
   112              datasets: [{
   113                  label: name,
   114                  data: data,
   115                  backgroundColor: palette('tol-rainbow', data.length).map(function(hex) {
   116                      return '#' + hex;
   117                  })
   118              }],
   119              labels: labels,
   120              options: {
   121                  tooltips: {
   122                      callbacks: {
   123                          label: function(tooltipItem, data) {
   124                              var datasetLabel = '';
   125                              var label = data.labels[tooltipItem.index];
   126                              return data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
   127                          }
   128                      }
   129                  }
   130              }
   131          }
   132      });
   133      $('#chart-container').show();
   134  }
   135  
   136  function renderMemoryTable() {
   137      processRawData(rawData);
   138  
   139      function renderMem(data, type, row) {
   140         var gb = 1 << 30;
   141         var mb = 1 << 20;
   142         var kb = 1 << 10;
   143         if (data > gb) {
   144           return '' + (data / gb).toFixed(2) + ' GB';
   145         } else if (data > mb) {
   146           return '' + (data / mb).toFixed(2) + ' MB';
   147         } else if (data > kb) {
   148           return '' + (data / kb).toFixed(2) + ' KB';
   149         }
   150         return '' + data.toFixed(2) + ' B'; 
   151      }
   152  
   153      var tableShardsTable = $('#table-shards-table').DataTable({
   154          paging: false,
   155          autoWidth: false,
   156          aoColumns: [
   157              {title: "Table", data: "table"},
   158              {title: "Shard", data: "shard"},
   159              {title: "Preloaded", data: "preloadMemory", type: "data-size", render: renderMem},
   160              {title: "NonPreloaded", data: "nonPreloadMemory", type: "data-size", render: renderMem},
   161              {title: "Live", data: "liveMemory", type: "data-size", render: renderMem},
   162              {title: "PrimaryKey", data: "primaryKeyMemory", type: "data-size", render: renderMem},
   163              {title: "Total", data: "total", type: "data-size", render: renderMem}
   164          ],
   165          aaData: tableShardData
   166      });
   167  
   168      var tableColumnsTable = $('#table-columns-table').DataTable({
   169          paging: false,
   170          autoWidth: false,
   171          aoColumns: [
   172              {title: "Column", data: "column"},
   173              {title: "Preloaded", data: "preloadMemory", type: "data-size", render: renderMem},
   174              {title: "NonPreloaded", data: "nonPreloadMemory", type: "data-size", render: renderMem},
   175              {title: "Live", data: "liveMemory", type: "data-size", render: renderMem},
   176              {title: "Total", data: "total", type: "data-size", render: renderMem}
   177          ],
   178          aaData: []
   179      });
   180  
   181      $('#table-columns-close').click(function(event){
   182          $('#table-columns').addClass("collapse");
   183          $("#table-shards").removeClass("col-md-6");
   184          $("#table-shards").addClass("col-md-12");
   185      });
   186  
   187      $('#table-shards-table').on('click', 'tr', function(event) {
   188          var data = tableShardsTable.row(this).data();
   189          var chartData = [];
   190          var chartLabels = [];
   191          if (!data) {
   192            return;
   193          }
   194          data.columns.forEach(function(col) {
   195              chartData.push(col.total);
   196              chartLabels.push(col.column);
   197          });
   198  
   199          drawPieChat('Memory By Columns', chartData, chartLabels);
   200  
   201          tableColumnsTable.clear();
   202          tableColumnsTable.rows.add(data.columns).draw();
   203          $("#table-shards").removeClass("col-md-12");
   204          $("#table-shards").addClass("col-md-6");
   205          $('#table-columns').removeClass("collapse");
   206      });
   207  
   208      $('#table-columns-table').on('click', 'tr', function(event) {
   209          var data = tableColumnsTable.row(this).data();
   210          if (!data) {
   211            return;
   212          }
   213          var chartData = [data.preloadMemory, data.nonPreloadMemory, data.liveMemory];
   214          var chartLabels = ['preloaded', 'nonPreloaded', 'live'];
   215          drawPieChat('Column Memory Detail', chartData, chartLabels);
   216      });
   217  
   218      $('#chart-container').hide();
   219  }