summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Wall <richard@aziz>2010-04-10 01:36:29 +0100
committerRichard Wall <richard@aziz>2010-04-10 01:36:29 +0100
commitd5edf591ea1d83af0371c9e741d01f4638fa6f94 (patch)
tree15252d4901408d42c959d13b8382cf0cc064664a
parenta84ee026e3c5dfe2589c4d52522e06b943e9cd1e (diff)
move jrrd to an external file
-rw-r--r--index.html117
-rw-r--r--jrrd.js115
2 files changed, 116 insertions, 116 deletions
diff --git a/index.html b/index.html
index 4540cc0..70acc5b 100644
--- a/index.html
+++ b/index.html
@@ -12,124 +12,9 @@
<script type="text/javascript" src="assets/javascript/javascriptrrd/rrdFile.js"></script>
<script type="text/javascript" src="assets/javascript/MochiKit/Base.js"></script>
<script type="text/javascript" src="assets/javascript/MochiKit/Async.js"></script>
+ <script type="text/javascript" src="assets/javascript/jrrd.js"></script>
<script type="text/javascript">
- function downloadBinary(url) {
- var d = new MochiKit.Async.Deferred();
-
- $.ajax({
- url: url,
- dataType: 'text',
- cache: false,
- beforeSend: function(request) {
- try {
- request.overrideMimeType('text/plain; charset=x-user-defined');
- } catch(e) {
- // IE doesn't support overrideMimeType
- }
- },
- success: function(data) {
- try {
- d.callback(new BinaryFile(data));
- } catch(e) {
- d.errback(e);
- }
- },
- error: function(xhr, textStatus, errorThrown) {
- // Special case for IE which handles binary data slightly
- // differently.
- if(textStatus == 'parsererror') {
- if (typeof xhr.responseBody != 'undefined') {
- return this.success(xhr.responseBody);
- }
- }
- d.errback(new Error(xhr.status));
- }
- });
- return d;
- }
-
-
- var RrdQuery = function(rrd) {
- this.rrd = rrd;
- }
-
- RrdQuery.prototype.getData = function(startTime, endTime) {
- var startTimestamp = startTime.getTime()/1000;
- var endTimestamp = endTime.getTime()/1000;
-
- var consolidationFunc = 'AVERAGE';
- var lastUpdated = this.rrd.getLastUpdate();
-
- var bestRRA = null;
- for(var i=0; i<this.rrd.getNrRRAs(); i++) {
- // Look through all RRAs looking for the most suitable
- // data resolution.
- var rra = this.rrd.getRRA(i);
-
- if(rra.getCFName() != consolidationFunc) {
- continue;
- }
- bestRRA = rra;
- var step = rra.getStep();
- var rraRowCount = rra.getNrRows();
- var firstUpdated = lastUpdated - (rraRowCount - 1) * step;
- if(firstUpdated <= startTimestamp) {
- break;
- }
- }
-
- if(!bestRRA) {
- throw new Error('Unrecognised consolidation function: ' + consolidationFunc);
- }
-
- startRow = rraRowCount - parseInt((lastUpdated - startTimestamp)/step);
- endRow = rraRowCount - parseInt((lastUpdated - endTimestamp)/step);
- returnData = [];
- for(var d=this.rrd.getNrDSs()-1; d>=0; d--) {
- flotData = [];
- timestamp = firstUpdated + (startRow - 1) * step;
- for (var i=startRow; i<=endRow; i++) {
- var val = bestRRA.getEl(i, d);
- flotData.push([timestamp*1000.0, val]);
- timestamp += step;
- }
- returnData.push({label: this.rrd.getDS(d).getName(), data: flotData});
- }
- return returnData;
- };
-
-
- var RrdQueryRemote = function(url) {
- this.url = url;
- this.rrd = null;
- }
-
- RrdQueryRemote.prototype.getData = function(startTime, endTime) {
- var endTimestamp = endTime.getTime()/1000;
-
- var d, self = this;
- if(!this.rrd || this.rrd.getLastUpdate() < endTimestamp) {
- d = downloadBinary(this.url)
- .addCallback(
- function(binary) {
- var rrd = new RRDFile(binary);
- self.rrd = rrd;
- return rrd;
- });
- } else {
- d = new MochiKit.Async.Deferred()
- d.callback(this.rrd);
- }
-
- d.addCallback(
- function(rrd) {
- return new RrdQuery(rrd).getData(startTime, endTime);
- });
-
- return d;
- };
-
var graphOptions = {
grid: {
clickable: true
diff --git a/jrrd.js b/jrrd.js
new file mode 100644
index 0000000..5a17f32
--- /dev/null
+++ b/jrrd.js
@@ -0,0 +1,115 @@
+function downloadBinary(url) {
+ var d = new MochiKit.Async.Deferred();
+
+ $.ajax({
+ url: url,
+ dataType: 'text',
+ cache: false,
+ beforeSend: function(request) {
+ try {
+ request.overrideMimeType('text/plain; charset=x-user-defined');
+ } catch(e) {
+ // IE doesn't support overrideMimeType
+ }
+ },
+ success: function(data) {
+ try {
+ d.callback(new BinaryFile(data));
+ } catch(e) {
+ d.errback(e);
+ }
+ },
+ error: function(xhr, textStatus, errorThrown) {
+ // Special case for IE which handles binary data slightly
+ // differently.
+ if(textStatus == 'parsererror') {
+ if (typeof xhr.responseBody != 'undefined') {
+ return this.success(xhr.responseBody);
+ }
+ }
+ d.errback(new Error(xhr.status));
+ }
+ });
+ return d;
+}
+
+
+var RrdQuery = function(rrd) {
+ this.rrd = rrd;
+}
+
+RrdQuery.prototype.getData = function(startTime, endTime) {
+ var startTimestamp = startTime.getTime()/1000;
+ var endTimestamp = endTime.getTime()/1000;
+
+ var consolidationFunc = 'AVERAGE';
+ var lastUpdated = this.rrd.getLastUpdate();
+
+ var bestRRA = null;
+ for(var i=0; i<this.rrd.getNrRRAs(); i++) {
+ // Look through all RRAs looking for the most suitable
+ // data resolution.
+ var rra = this.rrd.getRRA(i);
+
+ if(rra.getCFName() != consolidationFunc) {
+ continue;
+ }
+ bestRRA = rra;
+ var step = rra.getStep();
+ var rraRowCount = rra.getNrRows();
+ var firstUpdated = lastUpdated - (rraRowCount - 1) * step;
+ if(firstUpdated <= startTimestamp) {
+ break;
+ }
+ }
+
+ if(!bestRRA) {
+ throw new Error('Unrecognised consolidation function: ' + consolidationFunc);
+ }
+
+ startRow = rraRowCount - parseInt((lastUpdated - startTimestamp)/step);
+ endRow = rraRowCount - parseInt((lastUpdated - endTimestamp)/step);
+ returnData = [];
+ for(var d=this.rrd.getNrDSs()-1; d>=0; d--) {
+ flotData = [];
+ timestamp = firstUpdated + (startRow - 1) * step;
+ for (var i=startRow; i<=endRow; i++) {
+ var val = bestRRA.getEl(i, d);
+ flotData.push([timestamp*1000.0, val]);
+ timestamp += step;
+ }
+ returnData.push({label: this.rrd.getDS(d).getName(), data: flotData});
+ }
+ return returnData;
+};
+
+
+var RrdQueryRemote = function(url) {
+ this.url = url;
+ this.rrd = null;
+}
+
+RrdQueryRemote.prototype.getData = function(startTime, endTime) {
+ var endTimestamp = endTime.getTime()/1000;
+
+ var d, self = this;
+ if(!this.rrd || this.rrd.getLastUpdate() < endTimestamp) {
+ d = downloadBinary(this.url)
+ .addCallback(
+ function(binary) {
+ var rrd = new RRDFile(binary);
+ self.rrd = rrd;
+ return rrd;
+ });
+ } else {
+ d = new MochiKit.Async.Deferred()
+ d.callback(this.rrd);
+ }
+
+ d.addCallback(
+ function(rrd) {
+ return new RrdQuery(rrd).getData(startTime, endTime);
+ });
+
+ return d;
+};