Commit fc9dbd8e authored by Paul Asmuth's avatar Paul Asmuth
Browse files

fnordmetric ui: parseTime in frontend

parent 1c318500
Loading
Loading
Loading
Loading
+24 −7
Original line number Diff line number Diff line
@@ -65,6 +65,13 @@ var FnordMetric = (function(pre){
  }

  function onSocketMessage(raw) {
    if (enterprise) {
      var data = raw.data;

      if (data.substr(0,5) == "ERROR")
        return console.log("[FnordMetric] error: " + data.substr(6));

    } else {
      var n, evt = JSON.parse(raw.data);

      if (evt.error)
@@ -76,6 +83,7 @@ var FnordMetric = (function(pre){
      else if (evt.widget_key && widgets[evt.widget_key])
        widgets[evt.widget_key].send(evt);
    }
  }

  function onSocketOpen() {
    console.log("[FnordMetric] connected...");
@@ -90,6 +98,11 @@ var FnordMetric = (function(pre){
    if (e.code = 1003 && e.reason == "fnordmetric_enterprise") {
      console.log("[FnordMetric] switching to fnordmetric enterprise protocol")
      enterprise = true;

      $("*[data-fnordmetric]").each(function(n, e){
        $(e).attr('data-widget-key', null)
      });

      window.setTimeout(connect, 10);
    } else {
      console.log("[FnordMetric] socket closed");
@@ -99,7 +112,11 @@ var FnordMetric = (function(pre){

  function values_in(gauges, since, until, callback) {
    if (enterprise) {
      continuation = function() {
        console.log("FFFUBAR", this);
      }

      socket.send("VALUESIN " + gauges.first + " " + since + " " + until);
    }

    else {
+25 −1
Original line number Diff line number Diff line
@@ -85,3 +85,27 @@ FnordMetric.util.dateFormatWithRange = function(timestamp, range){
FnordMetric.util.generateUUID = function () {
  return Math.floor((1 + Math.random()) * 0x1000000).toString(16);
}

FnordMetric.util.parseTime = function(str) {
  var res,
      now = parseInt(((new Date()).getTime() / 1000), 10),
      str = str.toLowerCase();

  if (str == "now") {
    return now;
  } else if ((res = str.match( /^([0-9]+)$/)) != null) {
    return parseInt(res[1], 10);
  } else if ((res = str.match( /^-([0-9]+)$/)) != null) {
    return (now - parseInt(res[1], 10));
  } else if ((res = str.match( /^-([0-9]+)s(ec(ond)?(s?))?$/)) != null) {
    return (now - parseInt(res[1], 10));
  } else if ((res = str.match( /^-([0-9]+(?:\.[0-9]+)?)m(in(ute)?(s?))?$/)) != null) {
    return parseInt(now - (parseFloat(res[1]) * 60), 10);
  } else if ((res = str.match( /^-([0-9]+(?:\.[0-9]+)?)h(our(s?))?$/)) != null) {
    return parseInt(now - (parseFloat(res[1]) * 3600), 10);
  } else if ((res = str.match( /^-([0-9]+(?:\.[0-9]+)?)d(ay(s?))?$/)) != null) {
    return parseInt(now - (parseFloat(res[1]) * 86400), 10);
  } else {
    console.log("[FnordMetric] invalid time specifiation: " + str);
  }
}
+3 −1
Original line number Diff line number Diff line
@@ -38,7 +38,9 @@ FnordMetric.widgets.counter = function(elem){
  }

  function requestDataAsync() {
    FnordMetric.value_at(gauge, at, function(){
    var _at = FnordMetric.util.parseTime(at);

    FnordMetric.value_at(gauge, (_at + ""), function(){
      elem.attr('data-value', this.value);
      updateDisplay();
    });
+2 −3
Original line number Diff line number Diff line
@@ -185,11 +185,10 @@ FnordMetric.widgets.timeseries = function(elem){
  }

  function requestDataAsync() {
    var since = elem.attr("data-since"),
        until = elem.attr("data-until");
    var since = FnordMetric.util.parseTime(elem.attr("data-since")),
        until = FnordMetric.util.parseTime(elem.attr("data-until"));

    FnordMetric.values_in(gauges, since, until, function(){

      var gauges = Object.keys(this)
      gconfig.series = [];