Commit 99a4becd authored by Paul Asmuth's avatar Paul Asmuth
Browse files

updated fnordmetric-ui.js

parent be1dcecf
Loading
Loading
Loading
Loading
+223 −30
Original line number Diff line number Diff line
@@ -9383,8 +9383,12 @@ fnord3.time.scale.utc = function() {
})();
var FnordMetric = (function(pre){

  var wsAddress, socket, currentNamespace;
  var widgets = {};
  var wsAddress, socket, currentNamespace,
     continuations = {},
     continuation = false,
     stack = [],
     widgets = {},
     enterprise = false;

  function setup(opts) {
    if (typeof $ == 'undefined') {
@@ -9401,7 +9405,11 @@ var FnordMetric = (function(pre){
  }

  function connect() {
    if (enterprise)
      socket = new WebSocket(wsAddress, "fnordmetric_enterprise");
    else
      socket = new WebSocket(wsAddress);

    socket.onmessage = onSocketMessage;
    socket.onclose = onSocketClose;
    socket.onopen = onSocketOpen;
@@ -9442,14 +9450,35 @@ 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 if (continuation) {
        continuation(data);
        continuation = false;

        if (stack.length > 0) {
          var nxt = stack.shift();
          execute(nxt[0], nxt[1])
        }
      }

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

      if (evt.error)
        return console.log("[FnordMetric] error: " + evt.error);

    if (evt.widget_key && widgets[evt.widget_key])
      if (evt.widget_key && continuations[evt.widget_key])
        continuations[evt.widget_key].apply(evt);

      else if (evt.widget_key && widgets[evt.widget_key])
        widgets[evt.widget_key].send(evt);
    }
  }

  function onSocketOpen() {
    console.log("[FnordMetric] connected...");
@@ -9460,16 +9489,123 @@ var FnordMetric = (function(pre){
    });
  }

  function onSocketClose() {
  function onSocketClose(e) {
    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");
      window.setTimeout(connect, 1000);
    }
  }

  function execute(cmd, cb) {
    if (continuation === false) {
      socket.send(cmd);
      continuation = cb;
    } else {
      stack.push([cmd, cb]);
    }
  }

  function values_in(gauges, since, until, callback) {
    if (enterprise) {
      var all_resp = {};

      function values_in_fetch_next() {
        var this_resp = gauges.shift();

        execute(
          "VALUESIN " + this_resp + " " + since + " " + until,
          function(resp) {
            var vals = {},
                parts = resp.split(" ");

            if (parts[0] != "null")
              for (ind in parts) {
                var tuple = parts[ind].split(":");
                tuple[0] = parseInt(parseInt(tuple[0], 10) / 1000, 10);
                vals[tuple[0]] = tuple[1];
              }

            all_resp[this_resp] = vals;

            if (gauges.length == 0)
              callback.apply(FnordMetric.util.zeroFill(all_resp));
            else
              values_in_fetch_next();
          }
        );
      }

      values_in_fetch_next();
    }

    else {
      var txid = FnordMetric.util.generateUUID();

      continuations[txid] = function(){
        var result = {};

        for (ind in this.gauges)
          result[this.gauges[ind].key] = this.gauges[ind].vals;

        callback.apply(result);
      }

      FnordMetric.publish({
        "type": "widget_request",
        "klass": "generic",
        "gauges": gauges,
        "cmd": "values_at",
        "since": since,
        "until": until,
        "widget_key": txid
      });
    }
  }

  function value_at(gauge, at, callback) {
    if (enterprise) {
      execute(
        "VALUEAT " + gauge + " " + at,
        function(resp) {
          callback.apply({ "value": eval(resp) });
        }
      );
    }

    else {
      var txid = FnordMetric.util.generateUUID();

      continuations[txid] = function(){
        callback.apply({ "value": this.value });
      }

      FnordMetric.publish({
        "type": "widget_request",
        "klass": "generic",
        "cmd": "values_for",
        "gauge": gauge,
        "at": at,
        "widget_key": txid
      })
    }
  }

  return {
    setup: setup,
    publish: publish,
    refresh: refresh,
    resize: resize
    resize: resize,
    value_at: value_at,
    values_in: values_in
  };

})(FnordMetric);
@@ -11460,6 +11596,50 @@ 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);
  }
}

FnordMetric.util.zeroFill = function(obj) {
  var ticks = {};

  for (key in obj)
    for (tick in obj[key])
      ticks[tick] = 1;

  ticks = Object.keys(ticks);

  if (ticks.length == 0)
    ticks.push(0);

  for (key in obj)
    for (ind in ticks)
      if (typeof obj[key][ticks[ind]] == 'undefined')
        obj[key][ticks[ind]] = 0;

  return obj;
}
if (typeof FnordMetric == 'undefined')
  FnordMetric = {};

@@ -11647,15 +11827,30 @@ FnordMetric.widgets.timeseries = function(elem){
  }

  function requestDataAsync() {
    FnordMetric.publish({
      "type": "widget_request",
      "klass": "generic",
      "gauges": gauges,
      "cmd": "values_at",
      "tick": 30,
      "since": elem.attr("data-since"),
      "until": elem.attr("data-until"),
      "widget_key": widget_key
    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 = [];

      for (ind in gauges) {
        var gauge = gauges[ind];

        gconfig.series.push({
          name: gauge,
          color: colors[ind],
          data: []
        });

        for(_time in this[gauge]){
          gconfig.series[ind].data.push(
            { x: parseInt(_time), y: parseInt(this[gauge][_time] || 0) }
          );
        }
      }

      renderChart();
    });
  }

@@ -11713,14 +11908,12 @@ FnordMetric.widgets.counter = function(elem){
  }

  function requestDataAsync() {
    FnordMetric.publish({
      "type": "widget_request",
      "klass": "generic",
      "cmd": "values_for",
      "gauge": gauge,
      "at": at,
      "widget_key": widget_key
    })
    var _at = FnordMetric.util.parseTime(at);

    FnordMetric.value_at(gauge, (_at + ""), function(){
      elem.attr('data-value', this.value);
      updateDisplay();
    });
  }

  function destroy() {