2013-01-28 15:18:21 +01:00
|
|
|
// graph takes dspeed and uspeed, it queries them every second and draws
|
2014-02-18 01:10:45 +01:00
|
|
|
// the last 180 secs, it also takes draw as an optional attribute and only
|
2013-01-28 15:18:21 +01:00
|
|
|
// draws the graph when it is true, if not given then graph is always drawn
|
2013-01-29 13:47:40 +01:00
|
|
|
angular
|
|
|
|
.module('webui.directives.dgraph', ['webui.filters.bytes', 'webui.services.deps'])
|
|
|
|
.directive('dgraph', ['$', '$filter', '$parse', function($, filter, parse) {
|
2014-02-24 05:55:04 +01:00
|
|
|
var ratio = 0.4;
|
2014-02-24 01:04:28 +01:00
|
|
|
var xfmt = "%H:%M:%S";
|
|
|
|
var yTicks = 7; // Number of y-axis ticks (sans 0)
|
|
|
|
var yTickBase = 10240; // y-axis ticks must be a multiple of this (bytes).
|
|
|
|
try {
|
|
|
|
// Try to detect AM/PM locales.
|
|
|
|
if (!/16/.test(new Date(2000,0,1,16,7,9).toLocaleTimeString())) {
|
|
|
|
xfmt = "%I:%M:%S %P";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
// Ignore. Browser does not support toLocaleTimeString.
|
|
|
|
}
|
|
|
|
|
2013-01-21 15:07:55 +01:00
|
|
|
return function(scope, elem, attrs) {
|
2014-02-24 01:04:28 +01:00
|
|
|
var canDraw = false;
|
2013-01-21 15:07:55 +01:00
|
|
|
|
2014-02-18 01:10:45 +01:00
|
|
|
var graphSize = 180
|
2014-02-24 01:04:28 +01:00
|
|
|
, dspeed = 0, uspeed = 0, hasSpeeds = false
|
2013-01-21 15:07:55 +01:00
|
|
|
, dconf = {
|
2014-02-24 01:04:28 +01:00
|
|
|
label: "DOWN",
|
2013-01-21 15:07:55 +01:00
|
|
|
data: [],
|
2014-02-24 01:04:28 +01:00
|
|
|
color: "#00ff00",
|
2013-01-21 15:07:55 +01:00
|
|
|
lines: { show: true }
|
|
|
|
}
|
|
|
|
, uconf = {
|
2014-02-24 01:04:28 +01:00
|
|
|
label: "UP",
|
2013-01-21 15:07:55 +01:00
|
|
|
data: [],
|
2014-02-24 01:04:28 +01:00
|
|
|
color: "#0000ff",
|
2013-01-21 15:07:55 +01:00
|
|
|
lines: { show: true }
|
2014-02-24 01:04:28 +01:00
|
|
|
}
|
2013-01-21 15:07:55 +01:00
|
|
|
;
|
|
|
|
|
2013-01-26 17:54:17 +01:00
|
|
|
// hack for the null height for flot elem
|
2014-02-24 05:55:04 +01:00
|
|
|
elem.height(elem.width() * ratio);
|
2013-01-21 15:07:55 +01:00
|
|
|
var graph = $.plot(elem, [dconf, uconf], {
|
2014-02-24 01:04:28 +01:00
|
|
|
legend: {
|
2014-06-02 12:58:58 +02:00
|
|
|
show: (attrs.nolabel == undefined),
|
2014-02-24 01:04:28 +01:00
|
|
|
backgroundOpacity: 0,
|
|
|
|
margin: [10, 20],
|
|
|
|
labelFormatter: function(label, series) {
|
|
|
|
if (!series.data.length) {
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
return label + " (" + filter("bspeed")(series.data[series.data.length-1][1]) + ")";
|
|
|
|
},
|
|
|
|
position: "sw"
|
|
|
|
},
|
2013-01-21 15:07:55 +01:00
|
|
|
xaxis: {
|
2014-02-24 01:04:28 +01:00
|
|
|
show: true,
|
|
|
|
mode: "time",
|
|
|
|
timeformat: xfmt,
|
2014-06-02 13:59:07 +02:00
|
|
|
minTickSize: [+attrs.minTickSize || 30, "second"] // 180 / 30 == 6
|
2013-01-21 15:07:55 +01:00
|
|
|
},
|
|
|
|
yaxis: {
|
2014-02-24 01:04:28 +01:00
|
|
|
position: "right",
|
2014-03-14 13:52:51 +01:00
|
|
|
ticks: function(axis) {
|
2014-02-24 01:04:28 +01:00
|
|
|
var res = [0];
|
|
|
|
var round = Math.max(1, Math.ceil(axis.max / yTickBase));
|
|
|
|
var step = Math.max(1, Math.ceil(round / yTicks));
|
|
|
|
for (var s = 1; s <= yTicks; ++s) {
|
|
|
|
var c = yTickBase * step * s;
|
|
|
|
res.push(c);
|
|
|
|
if (c > axis.max) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
},
|
2013-01-21 15:07:55 +01:00
|
|
|
tickFormatter: function(val, axis) {
|
2013-01-22 08:53:30 +01:00
|
|
|
return filter('bspeed')(val);
|
2014-02-24 01:04:28 +01:00
|
|
|
},
|
2013-01-21 15:07:55 +01:00
|
|
|
min: 0
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var draw = function() {
|
2013-01-26 17:54:17 +01:00
|
|
|
var width = elem.width();
|
|
|
|
if (width == 0) return;
|
2013-01-21 15:07:55 +01:00
|
|
|
|
2014-02-24 05:55:04 +01:00
|
|
|
elem.height(width * ratio);
|
2013-01-21 15:07:55 +01:00
|
|
|
|
2013-01-25 15:38:08 +01:00
|
|
|
graph.setData([dconf, uconf]);
|
|
|
|
graph.resize();
|
|
|
|
graph.setupGrid();
|
|
|
|
graph.draw();
|
|
|
|
};
|
|
|
|
|
|
|
|
function update() {
|
2014-02-24 01:04:28 +01:00
|
|
|
if (!hasSpeeds) {
|
|
|
|
return;
|
|
|
|
}
|
2013-01-21 15:07:55 +01:00
|
|
|
|
2014-02-24 01:04:28 +01:00
|
|
|
// Convoluted way to get the local date timestamp instead of the UTC one.
|
|
|
|
var cnt = new Date();
|
|
|
|
cnt = Date.UTC(
|
|
|
|
cnt.getFullYear(),
|
|
|
|
cnt.getMonth(),
|
|
|
|
cnt.getDate(),
|
|
|
|
cnt.getHours(),
|
|
|
|
cnt.getMinutes(),
|
|
|
|
cnt.getSeconds());
|
|
|
|
|
|
|
|
if (dconf.data.length === graphSize) dconf.data.shift();
|
2013-01-21 15:07:55 +01:00
|
|
|
dconf.data.push([cnt, dspeed]);
|
|
|
|
|
2014-02-24 01:04:28 +01:00
|
|
|
if (uconf.data.length === graphSize) uconf.data.shift();
|
2013-01-21 15:07:55 +01:00
|
|
|
uconf.data.push([cnt, uspeed]);
|
|
|
|
|
2013-01-25 15:38:08 +01:00
|
|
|
// if any parents is collapsable, then confirm if it isnt
|
2014-02-24 01:04:28 +01:00
|
|
|
if (canDraw) {
|
2013-01-25 15:38:08 +01:00
|
|
|
draw();
|
2014-02-24 01:04:28 +01:00
|
|
|
}
|
2013-01-21 15:07:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
scope.$watch(attrs.dspeed, function(val) {
|
2014-02-24 01:04:28 +01:00
|
|
|
if (val === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
hasSpeeds = true;
|
2013-03-19 21:57:52 +01:00
|
|
|
dspeed = parseFloat(val) || 0;
|
2013-01-21 15:07:55 +01:00
|
|
|
});
|
|
|
|
|
2014-01-08 11:13:25 +01:00
|
|
|
scope.$watch(attrs.uspeed, function(val) {
|
2014-02-24 01:04:28 +01:00
|
|
|
if (val === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
hasSpeeds = true;
|
2013-03-19 21:57:52 +01:00
|
|
|
uspeed = parseFloat(val) || 0;
|
2013-01-21 15:07:55 +01:00
|
|
|
});
|
|
|
|
|
2014-02-24 01:04:28 +01:00
|
|
|
scope.$watch(attrs.draw, function(val) {
|
|
|
|
canDraw = val;
|
|
|
|
});
|
2013-01-28 15:18:21 +01:00
|
|
|
|
2013-01-25 15:38:08 +01:00
|
|
|
var interval = setInterval(update, 1000);
|
2013-01-21 15:07:55 +01:00
|
|
|
|
2013-01-26 17:54:17 +01:00
|
|
|
angular.element(window).bind('resize', draw);
|
2013-01-21 15:07:55 +01:00
|
|
|
elem.bind('$destroy', function() {
|
|
|
|
clearInterval(interval);
|
|
|
|
});
|
2013-01-26 17:54:17 +01:00
|
|
|
|
2013-01-21 15:07:55 +01:00
|
|
|
};
|
|
|
|
}]);
|