accumulo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From joshelser <...@git.apache.org>
Subject [GitHub] accumulo pull request #242: ACCUMULO-2181/3005 REST API and new Monitor UI
Date Thu, 06 Apr 2017 04:36:10 GMT
Github user joshelser commented on a diff in the pull request:

    https://github.com/apache/accumulo/pull/242#discussion_r110076870
  
    --- Diff: server/monitor/src/main/resources/resources/functions.js ---
    @@ -0,0 +1,686 @@
    +/*
    +* Licensed to the Apache Software Foundation (ASF) under one or more
    +* contributor license agreements.  See the NOTICE file distributed with
    +* this work for additional information regarding copyright ownership.
    +* The ASF licenses this file to You under the Apache License, Version 2.0
    +* (the "License"); you may not use this file except in compliance with
    +* the License.  You may obtain a copy of the License at
    +*
    +*     http://www.apache.org/licenses/LICENSE-2.0
    +*
    +* Unless required by applicable law or agreed to in writing, software
    +* distributed under the License is distributed on an "AS IS" BASIS,
    +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +* See the License for the specific language governing permissions and
    +* limitations under the License.
    +*/
    +
    +// Suffixes for quantity
    +var QUANTITY_SUFFIX = ['', 'K', 'M', 'B', 'T', 'e15', 'e18', 'e21'];
    +// Suffixes for size
    +var SIZE_SUFFIX = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z'];
    +
    +/**
    + * Initializes Auto Refresh to false if it is not set,
    + * and creates listeners for auto refresh
    + */
    +function setupAutoRefresh() {
    +  // Sets auto refresh to true or false
    +  if (!sessionStorage.autoRefresh) {
    +    sessionStorage.autoRefresh = 'false';
    +  }
    +  // Need this to set the initial value for the autorefresh on page load
    +  if (sessionStorage.autoRefresh == 'false') {
    +    $('.auto-refresh').parent().removeClass('active');
    +  } else {
    +    $('.auto-refresh').parent().addClass('active');
    +  }
    +  // Initializes the auto refresh on click listener
    +  $('.auto-refresh').click(function(e) {
    +    if ($(this).parent().attr('class') == 'active') {
    +      $(this).parent().removeClass('active');
    +      sessionStorage.autoRefresh = 'false';
    +    } else {
    +      $(this).parent().addClass('active');
    +      sessionStorage.autoRefresh = 'true';
    +    }
    +  });
    +}
    +
    +/**
    + * Global timer that checks for auto refresh status every 5 seconds
    + */
    +TIMER = setInterval(function() {
    +  if (sessionStorage.autoRefresh == 'true') {
    +    $('.auto-refresh').parent().addClass('active');
    +    refresh();
    +    refreshNavBar();
    +  } else {
    +    $('.auto-refresh').parent().removeClass('active');
    +  }
    +}, 5000);
    +
    +/**
    + * Empty function in case there is no refresh implementation
    + */
    +function refresh() {
    +}
    +
    +/**
    + * Converts a number to a size with suffix
    + *
    + * @param {number} size Number to convert
    + * @return {string} Number with suffix added
    + */
    +function bigNumberForSize(size) {
    +  if (size === null)
    +    size = 0;
    +  return bigNumber(size, SIZE_SUFFIX, 1024);
    +}
    +
    +/**
    + * Converts a number to a quantity with suffix
    + *
    + * @param {number} quantity Number to convert
    + * @return {string} Number with suffix added
    + */
    +function bigNumberForQuantity(quantity) {
    +  if (quantity === null)
    +    quantity = 0;
    +  return bigNumber(quantity, QUANTITY_SUFFIX, 1000);
    +}
    +
    +/**
    + * Adds the suffix to the number, converts the number to one close to the base
    + *
    + * @param {number} big Number to convert
    + * @param {array} suffixes Suffixes to use for convertion
    + * @param {number} base Base to use for convertion
    + * @return {string} The new value with the suffix
    + */
    +function bigNumber(big, suffixes, base) {
    +  // If the number is smaller than the base, return thee number with no suffix
    +  if (big < base) {
    +    return big + suffixes[0];
    +  }
    +  // Finds which suffix to use
    +  var exp = Math.floor(Math.log(big) / Math.log(base));
    +  // Divides the bumber by the equivalent suffix number
    +  var val = big / Math.pow(base, exp);
    +  // Keeps the number to 2 decimal places and adds the suffix
    +  return val.toFixed(2) + suffixes[exp];
    +}
    +
    +/**
    + * Converts the time to short number and adds unit
    + *
    + * @param {number} time Time in microseconds
    + * @return {string} The time with units
    + */
    +function timeDuration(time) {
    +  var ms, sec, min, hr, day, yr;
    +  ms = sec = min = hr = day = yr = -1;
    +
    +  time = Math.floor(time);
    +
    +  // If time is 0 return a dash
    +  if (time == 0) {
    +    return '&mdash;';
    +  }
    +
    +  // Obtains the milliseconds, if time is 0, return milliseconds, and units
    +  ms = time % 1000;
    +  time = Math.floor(time / 1000);
    +  if (time == 0) {
    +    return ms + 'ms';
    +  }
    +
    +  // Obtains the seconds, if time is 0, return seconds, milliseconds, and units
    +  sec = time % 60;
    +  time = Math.floor(time / 60);
    +  if (time == 0) {
    +    return sec + 's' + '&nbsp;' + ms + 'ms';
    +  }
    +
    +  // Obtains the minutes, if time is 0, return minutes, seconds, and units
    +  min = time % 60;
    +  time = Math.floor(time / 60);
    +  if (time == 0) {
    +    return min + 'm' + '&nbsp;' + sec + 's';
    +  }
    +
    +  // Obtains the hours, if time is 0, return hours, minutes, and units
    +  hr = time % 24;
    +  time = Math.floor(time / 24);
    +  if (time == 0) {
    +    return hr + 'h' + '&nbsp;' + min + 'm';
    +  }
    +
    +  // Obtains the days, if time is 0, return days, hours, and units
    +  day = time % 365;
    +  time = Math.floor(time / 365);
    +  if (time == 0) {
    +    return day + 'd' + '&nbsp;' + hr + 'h';
    +  }
    +
    +  // Obtains the years, if time is 0, return years, days, and units
    +  yr = Math.floor(time);
    +  return yr + 'y' + '&nbsp;' + day + 'd';
    +}
    +
    +/**
    + * Sorts the selected table by column in the direction chosen
    + *
    + * @param {string} tableID Table to sort
    + * @param {string} direction Direction to sort table, asc or desc
    + * @param {number} n Column to sort
    + */
    +function sortTables(tableID, direction, n) {
    +  var table, rows, switching, i, x, y, h, shouldSwitch, dir, xFinal, yFinal;
    +  table = document.getElementById(tableID);
    +  switching = true;
    +
    +  dir = direction;
    +  sessionStorage.direction = dir;
    +
    +  // Select the rows of the table
    +  rows = table.getElementsByTagName('tr');
    +
    +  // Clears the sortable class from the table columns
    +  var count = 0;
    +  while (rows[0].getElementsByTagName('th').length > count) {
    +    var tmpH = rows[0].getElementsByTagName('th')[count];
    +    tmpH.classList.remove('sortable');
    +    if (rows.length > 2) {
    +      tmpH.classList.add('sortable');
    +    }
    +    $(tmpH.getElementsByTagName('span')).remove();
    +    count += 1;
    +  }
    +
    +  // If there are more than 2 rows, add arrow to the selected column
    +  if (rows.length <= 2) {
    +      switching = false;
    +  } else {
    +    h = rows[0].getElementsByTagName('th')[n];
    +    if (dir == 'asc') {
    +      $(h).append('<span class="glyphicon glyphicon-chevron-up"' +
    +          ' width="10px" height="10px" />');
    +    } else if (dir == 'desc') {
    +      $(h).append('<span class="glyphicon glyphicon-chevron-down"' +
    +          ' width="10px" height="10px" />');
    +    }
    +  }
    +
    +  /*
    +   * Make a loop that will continue until
    +   * no switching has been done:
    +   */
    +  while (switching) {
    +    switching = false;
    +    rows = table.getElementsByTagName('tr');
    +
    +    /*
    +     * Loop through all table rows (except the
    +     * first, which contains table headers):
    +     */
    +    for (i = 1; i < (rows.length - 1); i++) {
    +      shouldSwitch = false;
    +      /*
    +       * Get two elements to compare,
    +       * one from current row and one from the next:
    +       * If the element is a dash, convert to null, otherwise,
    +       * if it is a string, convert to number
    +       */
    +      x = rows[i].getElementsByTagName('td')[n].getAttribute('data-value');
    +      xFinal = (x === '-' || x === '&mdash;' ?
    +          null : (Number(x) == x ? Number(x) : x));
    +
    +      y = rows[i + 1].getElementsByTagName('td')[n].getAttribute('data-value');
    +      yFinal = (y === '-' || y === '&mdash;' ?
    +          null : (Number(y) == y ? Number(y) : y));
    +
    +      /*
    +       * Check if the two rows should switch place,
    +       * based on the direction, asc or desc:
    +       */
    +      if (dir == 'asc') {
    +        if (xFinal > yFinal || (xFinal !== null && yFinal === null)) {
    +          // if so, mark as a switch and break the loop:
    +          shouldSwitch = true;
    +          break;
    +        }
    +      } else if (dir == 'desc') {
    +        if (xFinal < yFinal || (yFinal !== null && xFinal === null)) {
    +          // if so, mark as a switch and break the loop:
    +          shouldSwitch = true;
    +          break;
    +        }
    +      }
    +    }
    +    if (shouldSwitch) {
    +      /*
    +       * If a switch has been marked, make the switch
    +       * and mark that a switch has been done:
    +       */
    +      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
    +      switching = true;
    +    }
    +  }
    +}
    +
    +/**
    + * Clears the selected table while leaving the headers
    + *
    + * @param {string} tableID Table to clear
    + */
    +function clearTable(tableID) {
    +  // JQuery selector to select all rows except for the first row (header)
    +  $('#' + tableID).find('tr:not(:first)').remove();
    +}
    +
    +///// REST Calls /////////////
    +
    +/**
    + * REST GET call for the master information,
    + * stores it on a sessionStorage variable
    + */
    +function getMaster() {
    +  $.getJSON('/rest/master', function(data) {
    +    sessionStorage.master = JSON.stringify(data);
    --- End diff --
    
    How does jquery react if it receives some non HTTP/200 response? (e.g. if there was an
Exception due to a bug in the Monitor rest call). Do we get a nice message?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message