Skip to content
This repository
Browse code

fnordmetric ui: parseTime in frontend

  • Loading branch information...
commit fc9dbd8e9d2daae763b4f6464c5260ef779be00e 1 parent 1c31850
Paul Asmuth authored
31 fnordmetric-ui/js/fnordmetric.js
@@ -65,16 +65,24 @@ var FnordMetric = (function(pre){
65 65 }
66 66
67 67 function onSocketMessage(raw) {
68   - var n, evt = JSON.parse(raw.data);
  68 + if (enterprise) {
  69 + var data = raw.data;
  70 +
  71 + if (data.substr(0,5) == "ERROR")
  72 + return console.log("[FnordMetric] error: " + data.substr(6));
69 73
70   - if (evt.error)
71   - return console.log("[FnordMetric] error: " + evt.error);
  74 + } else {
  75 + var n, evt = JSON.parse(raw.data);
72 76
73   - if (evt.widget_key && continuations[evt.widget_key])
74   - continuations[evt.widget_key].apply(evt);
  77 + if (evt.error)
  78 + return console.log("[FnordMetric] error: " + evt.error);
75 79
76   - else if (evt.widget_key && widgets[evt.widget_key])
77   - widgets[evt.widget_key].send(evt);
  80 + if (evt.widget_key && continuations[evt.widget_key])
  81 + continuations[evt.widget_key].apply(evt);
  82 +
  83 + else if (evt.widget_key && widgets[evt.widget_key])
  84 + widgets[evt.widget_key].send(evt);
  85 + }
78 86 }
79 87
80 88 function onSocketOpen() {
@@ -90,6 +98,11 @@ var FnordMetric = (function(pre){
90 98 if (e.code = 1003 && e.reason == "fnordmetric_enterprise") {
91 99 console.log("[FnordMetric] switching to fnordmetric enterprise protocol")
92 100 enterprise = true;
  101 +
  102 + $("*[data-fnordmetric]").each(function(n, e){
  103 + $(e).attr('data-widget-key', null)
  104 + });
  105 +
93 106 window.setTimeout(connect, 10);
94 107 } else {
95 108 console.log("[FnordMetric] socket closed");
@@ -99,7 +112,11 @@ var FnordMetric = (function(pre){
99 112
100 113 function values_in(gauges, since, until, callback) {
101 114 if (enterprise) {
  115 + continuation = function() {
  116 + console.log("FFFUBAR", this);
  117 + }
102 118
  119 + socket.send("VALUESIN " + gauges.first + " " + since + " " + until);
103 120 }
104 121
105 122 else {
26 fnordmetric-ui/js/fnordmetric.util.js
@@ -82,6 +82,30 @@ FnordMetric.util.dateFormatWithRange = function(timestamp, range){
82 82 FnordMetric.util.decPrint(t.getSeconds());
83 83 }
84 84
85   -FnordMetric.util.generateUUID = function (){
  85 +FnordMetric.util.generateUUID = function () {
86 86 return Math.floor((1 + Math.random()) * 0x1000000).toString(16);
87 87 }
  88 +
  89 +FnordMetric.util.parseTime = function(str) {
  90 + var res,
  91 + now = parseInt(((new Date()).getTime() / 1000), 10),
  92 + str = str.toLowerCase();
  93 +
  94 + if (str == "now") {
  95 + return now;
  96 + } else if ((res = str.match( /^([0-9]+)$/)) != null) {
  97 + return parseInt(res[1], 10);
  98 + } else if ((res = str.match( /^-([0-9]+)$/)) != null) {
  99 + return (now - parseInt(res[1], 10));
  100 + } else if ((res = str.match( /^-([0-9]+)s(ec(ond)?(s?))?$/)) != null) {
  101 + return (now - parseInt(res[1], 10));
  102 + } else if ((res = str.match( /^-([0-9]+(?:\.[0-9]+)?)m(in(ute)?(s?))?$/)) != null) {
  103 + return parseInt(now - (parseFloat(res[1]) * 60), 10);
  104 + } else if ((res = str.match( /^-([0-9]+(?:\.[0-9]+)?)h(our(s?))?$/)) != null) {
  105 + return parseInt(now - (parseFloat(res[1]) * 3600), 10);
  106 + } else if ((res = str.match( /^-([0-9]+(?:\.[0-9]+)?)d(ay(s?))?$/)) != null) {
  107 + return parseInt(now - (parseFloat(res[1]) * 86400), 10);
  108 + } else {
  109 + console.log("[FnordMetric] invalid time specifiation: " + str);
  110 + }
  111 +}
4 fnordmetric-ui/js/fnordmetric.widgets.counter.js
@@ -38,7 +38,9 @@ FnordMetric.widgets.counter = function(elem){
38 38 }
39 39
40 40 function requestDataAsync() {
41   - FnordMetric.value_at(gauge, at, function(){
  41 + var _at = FnordMetric.util.parseTime(at);
  42 +
  43 + FnordMetric.value_at(gauge, (_at + ""), function(){
42 44 elem.attr('data-value', this.value);
43 45 updateDisplay();
44 46 });
5 fnordmetric-ui/js/fnordmetric.widgets.timeseries.js
@@ -185,11 +185,10 @@ FnordMetric.widgets.timeseries = function(elem){
185 185 }
186 186
187 187 function requestDataAsync() {
188   - var since = elem.attr("data-since"),
189   - until = elem.attr("data-until");
  188 + var since = FnordMetric.util.parseTime(elem.attr("data-since")),
  189 + until = FnordMetric.util.parseTime(elem.attr("data-until"));
190 190
191 191 FnordMetric.values_in(gauges, since, until, function(){
192   -
193 192 var gauges = Object.keys(this)
194 193 gconfig.series = [];
195 194

0 comments on commit fc9dbd8

Please sign in to comment.
Something went wrong with that request. Please try again.