From 887896a5019a4e5bd1fe6d1550eae3248faeb18e Mon Sep 17 00:00:00 2001 From: Richard Wall Date: Sat, 10 Apr 2010 17:06:34 +0100 Subject: pass arguments where possible --- index.html | 29 ++++++++++++++++++----------- jrrd.js | 56 ++++++++++++++++++++++++++++++++------------------------ 2 files changed, 50 insertions(+), 35 deletions(-) diff --git a/index.html b/index.html index 550e4d3..0b43bb0 100644 --- a/index.html +++ b/index.html @@ -71,21 +71,22 @@ return c; } - var RrdQueryDsProxy = function(rrdQuery, dsId) { - this.rrdQuery = rrdQuery; - this.dsId = dsId; - }; - - RrdQueryDsProxy.prototype.getData = function(startTime, endTime) { - return this.rrdQuery.getData(startTime, endTime, this.dsId); - }; - function loadChartFactory(template) { var c = new jrrd.Chart(template, baseOptions); var data = new jrrd.RrdQueryRemote('data/localhost/load/load.rrd'); var rrdDSs = ['shortterm', 'midterm', 'longterm']; jQuery.each(rrdDSs, function(i, rrdDS) { - c.addData(rrdDS, new RrdQueryDsProxy(data, rrdDS)); + c.addData(rrdDS, new jrrd.RrdQueryDsProxy(data, rrdDS)); + }); + return c; + } + + function nicChartFactory(template, nicname) { + var c = new jrrd.Chart(template, baseOptions); + var data = new jrrd.RrdQueryRemote('data/localhost/interface/if_octets-' + nicname + '.rrd'); + var rrdDSs = ['tx', 'rx']; + jQuery.each(rrdDSs, function(i, rrdDS) { + c.addData(rrdDS, new jrrd.RrdQueryDsProxy(data, rrdDS)); }); return c; } @@ -96,6 +97,12 @@ loadChartFactory( chartTemplate.clone().appendTo('.charts')), + nicChartFactory( + chartTemplate.clone().appendTo('.charts'), 'lo'), + + nicChartFactory( + chartTemplate.clone().appendTo('.charts'), 'eth0'), + cpuChartFactory( chartTemplate.clone().appendTo('.charts')), @@ -105,7 +112,7 @@ jQuery.each(charts, function(i, chart) { chart.draw(new Date('7 April 2010 09:30:00'), - new Date('7 April 2010 15:00:00')); + new Date('10 April 2010 15:00:00')); }); $('.chart').bind("plotselected", function(event, ranges) { diff --git a/jrrd.js b/jrrd.js index 3f2c69b..9104875 100644 --- a/jrrd.js +++ b/jrrd.js @@ -54,6 +54,10 @@ jrrd.RrdQuery.prototype.getData = function(startTime, endTime, dsId) { var consolidationFunc = 'AVERAGE'; var lastUpdated = this.rrd.getLastUpdate(); + // If end time stamp is beyond the range of this rrd then reset it + if(lastUpdated < endTimestamp) { + endTimestamp = lastUpdated; + } var bestRRA = null; for(var i=0; i -1 && this.lastUpdate < endTimestamp )) { this._download = jrrd.downloadBinary(this.url) .addCallback( - function(binary) { + function(self, binary) { // Upon successful download convert the resulting binary // into an RRD file and pass it on to the next callback // in the chain. var rrd = new RRDFile(binary); self.lastUpdate = rrd.getLastUpdate(); return rrd; - }); + }, this); } // Set up a deferred which will call getData on the local RrdQuery object // returning a flot compatible data object to the caller. var ret = new MochiKit.Async.Deferred().addCallback( - function(rrd) { + function(startTime, endTime, dsId, rrd) { return new jrrd.RrdQuery(rrd).getData(startTime, endTime, dsId); - }); + }, startTime, endTime, dsId); // Add a pair of callbacks to the current download which will callback the // result which we setup above. - this._download.addCallbacks( - function(res) { - ret.callback(res); + this._download.addBoth( + function(ret, res) { + if(res instanceof Error) { + ret.errback(res); + } else { + ret.callback(res); + } return res; - }, - function(err) { - ret.errback(err); - return err; - }); + }, ret); return ret; }; -jrrd.Chart = function(template, options) { - var self = this; +jrrd.RrdQueryDsProxy = function(rrdQuery, dsId) { + this.rrdQuery = rrdQuery; + this.dsId = dsId; +}; +jrrd.RrdQueryDsProxy.prototype.getData = function(startTime, endTime) { + return this.rrdQuery.getData(startTime, endTime, this.dsId); +}; + + +jrrd.Chart = function(template, options) { this.template = template; this.options = options; this.data = []; @@ -155,8 +165,6 @@ jrrd.Chart.prototype.addData = function(label, db) { }; jrrd.Chart.prototype.draw = function(startTime, endTime) { - var self = this; - var results = []; for(var i=0; i