2013-01-28 15:18:21 +01:00
|
|
|
// graph takes dspeed and uspeed, it queries them every second and draws
|
|
|
|
// the last 20 secs, it also takes draw as an optional attribute and only
|
|
|
|
// 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) {
|
2013-01-21 15:07:55 +01:00
|
|
|
return function(scope, elem, attrs) {
|
2013-01-28 15:18:21 +01:00
|
|
|
var canDraw = true;
|
2013-01-21 15:07:55 +01:00
|
|
|
|
|
|
|
var graphSize = 20
|
|
|
|
, dspeed = 0, uspeed = 0
|
|
|
|
, dconf = {
|
|
|
|
label: "Download Speed",
|
|
|
|
data: [],
|
|
|
|
color: "#ff0000",
|
|
|
|
lines: { show: true }
|
|
|
|
}
|
|
|
|
, uconf = {
|
|
|
|
label: "Upload Speed",
|
|
|
|
data: [],
|
|
|
|
color: "#00ff00",
|
|
|
|
lines: { show: true }
|
|
|
|
},
|
|
|
|
start = new Date
|
|
|
|
;
|
|
|
|
|
2013-01-26 17:54:17 +01:00
|
|
|
|
|
|
|
// hack for the null height for flot elem
|
2013-01-21 15:07:55 +01:00
|
|
|
elem.height(elem.width() / 2);
|
|
|
|
|
|
|
|
var graph = $.plot(elem, [dconf, uconf], {
|
|
|
|
legend: { show: true },
|
|
|
|
xaxis: {
|
|
|
|
show: true
|
|
|
|
},
|
|
|
|
yaxis: {
|
|
|
|
tickFormatter: function(val, axis) {
|
2013-01-22 08:53:30 +01:00
|
|
|
return filter('bspeed')(val);
|
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
|
|
|
|
2013-01-26 17:54:17 +01:00
|
|
|
elem.height(width / 2);
|
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() {
|
2013-01-21 15:07:55 +01:00
|
|
|
var cnt = ((new Date - start)/1000).toFixed(0);
|
|
|
|
|
|
|
|
dconf.data.push([cnt, dspeed]);
|
|
|
|
if (dconf.data.length > graphSize) dconf.data.shift();
|
|
|
|
|
|
|
|
uconf.data.push([cnt, uspeed]);
|
|
|
|
if (uconf.data.length > graphSize) uconf.data.shift();
|
|
|
|
|
2013-01-25 15:38:08 +01:00
|
|
|
// if any parents is collapsable, then confirm if it isnt
|
2013-01-28 15:18:21 +01:00
|
|
|
if (canDraw)
|
2013-01-25 15:38:08 +01:00
|
|
|
draw();
|
2013-01-21 15:07:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
scope.$watch(attrs.dspeed, function(val) {
|
2013-03-19 21:57:52 +01:00
|
|
|
dspeed = parseFloat(val) || 0;
|
2013-01-21 15:07:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
scope.$watch(attrs.useed, function(val) {
|
2013-03-19 21:57:52 +01:00
|
|
|
uspeed = parseFloat(val) || 0;
|
2013-01-21 15:07:55 +01:00
|
|
|
});
|
|
|
|
|
2013-01-28 15:18:21 +01:00
|
|
|
if (attrs.draw) {
|
|
|
|
scope.$watch(attrs.draw, function(val) {
|
|
|
|
canDraw = val;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
}]);
|