Browse files

fnordmetric ui: parseTime in frontend

  • Loading branch information...
1 parent 1c31850 commit fc9dbd8e9d2daae763b4f6464c5260ef779be00e @paulasmuth committed Mar 5, 2013
View
31 fnordmetric-ui/js/fnordmetric.js
@@ -65,16 +65,24 @@ var FnordMetric = (function(pre){
}
function onSocketMessage(raw) {
- var n, evt = JSON.parse(raw.data);
+ if (enterprise) {
+ var data = raw.data;
+
+ if (data.substr(0,5) == "ERROR")
+ return console.log("[FnordMetric] error: " + data.substr(6));
- if (evt.error)
- return console.log("[FnordMetric] error: " + evt.error);
+ } else {
+ var n, evt = JSON.parse(raw.data);
- if (evt.widget_key && continuations[evt.widget_key])
- continuations[evt.widget_key].apply(evt);
+ if (evt.error)
+ return console.log("[FnordMetric] error: " + evt.error);
- else if (evt.widget_key && widgets[evt.widget_key])
- widgets[evt.widget_key].send(evt);
+ 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() {
@@ -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 {
View
26 fnordmetric-ui/js/fnordmetric.util.js
@@ -82,6 +82,30 @@ FnordMetric.util.dateFormatWithRange = function(timestamp, range){
FnordMetric.util.decPrint(t.getSeconds());
}
-FnordMetric.util.generateUUID = function (){
+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);
+ }
+}
View
4 fnordmetric-ui/js/fnordmetric.widgets.counter.js
@@ -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();
});
View
5 fnordmetric-ui/js/fnordmetric.widgets.timeseries.js
@@ -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 = [];

0 comments on commit fc9dbd8

Please sign in to comment.