Commit 41e4f0c0 authored by Laura Schlimmer's avatar Laura Schlimmer
Browse files

reuse functions

Conflicts:
	fnordmetric-webui/fnordmetric-webui-util.js
parent 7bc51078
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -222,7 +222,7 @@ FnordMetric.util.DatePicker = function(elem, dp_input, viewport, callback) {
    var month_title = document.createElement("span");
    month_title.className = "datepicker_title";
    month_title.innerHTML =
      FnordMetric.util.getMonthStr(month) + " " + year;
      FnordMetric.util.getHumanMonth(month, "long") + " " + year;

    /* tooltip to select previous month */
    var prev_ttp = FnordMetric.createButton(
+59 −38
Original line number Diff line number Diff line
@@ -153,6 +153,61 @@ FnordMetric.util.renderPageHeader = function(text, elem) {
  elem.appendChild(header);
}

/**
  * @param offset in seconds
  */
FnordMetric.util.parseTimeOffset = function(offset) {
  if (offset < 60) {
    var label = (offset == 1)? " second ago" : " seconds ago";
    return offset + label;
  } else if (offset < 3600) {
    var time = Math.floor(offset / 60);
    var label = (time == 1)? " minute ago" : " minutes ago";
    return time + label;
  } else if (offset < 86400) {
    var time =  Math.floor(offset / 3600);
    var label = (time == 1)? " hour ago" : " hours ago";
    return time + label;
  } else {
    var time = Math.floor(offset / 86400);
    var label = (time == 1)? " day ago" : " days ago";
    return time + label;
  }
}

FnordMetric.util.getHumanMonth = function(index, type) {
  var months = {
    "long" : [
      "January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "October",
      "November",
      "December"],
    "short" : [
      "Jan",
      "Feb",
      "Mar",
      "Apr",
      "May",
      "Jun",
      "Jul",
      "Aug",
      "Sep",
      "Oct",
      "Nov",
      "Dec"]};

  return months[type][index];
}


/**
  * creates a time description like 
  * '2 hours ago - Nov 8 2014 11:33:11
@@ -165,31 +220,14 @@ FnordMetric.util.parseTimestamp = function(timestamp) {
  var timestamp =
    FnordMetric.util.convertToMilliTS(timestamp);

  var time_str;
  var now = Date.now();
  var date = new Date(timestamp);

  var offset =  Math.floor(
    (now - timestamp) / 1000);
  if (offset < 60) {
    var label = (offset == 1)? " second ago" : " seconds ago";
    time_str  = offset + label;
  } else if (offset < 3600) {
    var time = Math.floor(offset / 60);
    var label = (time == 1)? " minute ago" : " minutes ago";
    time_str = time + label;
  } else if (offset < 86400) {
    var time =  Math.floor(offset / 3600);
    var label = (time == 1)? " hour ago" : " hours ago";
    time_str = time + label;
  } else {
    var time = Math.floor(offset / 86400);
    var label = (time == 1)? " day ago" : " days ago";
    time_str = time + label;
  }

  var months = ["Jan", "Feb", "Mar", "Apr", "May",
    "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
  var time_str = FnordMetric.util.parseTimeOffset(offset);


  var minutes = date.getMinutes();
  if (minutes < 10) {
@@ -202,7 +240,8 @@ FnordMetric.util.parseTimestamp = function(timestamp) {
  }

  time_str +=
    " - " + months[date.getMonth()] + 
    " - " + 
    FnordMetric.util.getHumanMonth(date.getMonth(), "short") + 
    " " + date.getDate() +
    " " + date.getFullYear() +
    " " + date.getHours() +
@@ -523,24 +562,6 @@ FnordMetric.util.generateSQLQueryFromParams = function(params) {
  return query;
}

FnordMetric.util.getMonthStr = function(index) {
  var months = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"];

  return months[index];
}

FnordMetric.util.isNumKey = function(keycode) {
  return (
    (keycode >= 48 && keycode <= 57));