From 2216f7560ba709c442ad7ef32c28ee9f38aac683 Mon Sep 17 00:00:00 2001 From: Richard Wall Date: Mon, 19 Apr 2010 01:47:44 +0100 Subject: Add more documentation --- README | 31 +++++++++++++ index.html | 68 +++++++++++++++++++++------- jrrd.js | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 227 insertions(+), 21 deletions(-) create mode 100644 README diff --git a/README b/README new file mode 100644 index 0000000..f5da6ed --- /dev/null +++ b/README @@ -0,0 +1,31 @@ +jrrd 0.0.1 + + +What is this? +============= +Wrappers and convenience fuctions for working with the javascriptRRD, Flot and +Collectd. + + +Debian / Ubuntu Quick Start +=========================== +To get this demo working, you will need to serve this page from a +local webserver and serve the folder that contains your RRD files. + +This demo is designed to work with the RRD files generated by +Collectd. + +# Install and configure collectd (enable the rrd plugin) +$ aptitude install collectd + +# Create a project folder +$ mkdir -p ~/Projects/jrrd +$ cd ~/Projects/jrrd + +# Link to the collectd rrd folder +$ ln -f /var/lib/collectd/rrd/localhost data + +# Start a local webserver +$ aptitude install twisted +$ twistd -n web --port 8080 --path . + diff --git a/index.html b/index.html index 2b6b5a0..840a5b3 100644 --- a/index.html +++ b/index.html @@ -7,6 +7,8 @@ - - - - - - - - - + + + + + + + + + + + + +
+

To get this demo working, you will need to serve this page from a + local webserver and serve the folder that contains your RRD files.

+

This demo is designed to work with the RRD files generated by + Collectd.

+

Debian / Ubuntu Quick Start

+
+                # Install and configure collectd (enable the rrd plugin)
+                $ aptitude install collectd
+
+                # Create a project folder
+                $ mkdir -p ~/Projects/jrrd
+                $ cd ~/Projects/jrrd
+
+                # Link to the collectd rrd folder
+                $ ln -f /var/lib/collectd/rrd/localhost data
+
+                # Start a local webserver
+                $ aptitude install twisted
+                $ twistd -n web --port 8080 --path .
+            
+
+
diff --git a/jrrd.js b/jrrd.js index 5fcb670..3f9070a 100644 --- a/jrrd.js +++ b/jrrd.js @@ -4,15 +4,27 @@ * Wrappers and convenience fuctions for working with the javascriptRRD, jQuery, * and flot charting packages. * - * javascriptRRD - http://javascriptrrd.sourceforge.net/ - * jQuery - http://jquery.com/ - * flot - http://code.google.com/p/flot/ + * Designed to work well with the RRD files generated by Collectd: + * - http://collectd.org/ + * + * Requirements: + * - JavascriptRRD: http://javascriptrrd.sourceforge.net/ + * - jQuery: http://jquery.com/ + * - Flot: http://code.google.com/p/flot/ + * - MochiKit.Async: http://www.mochikit.com/ */ if(typeof jrrd == 'undefined') { var jrrd = {}; } +/** + * Download a binary file asynchronously using the jQuery.ajax function + * + * @param url: The url of the object to be downloaded + * @return: A I{MochiKit.Async.Deferred} which will callback with an instance of + * I{javascriptrrd.BinaryFile} + **/ jrrd.downloadBinary = function(url) { var d = new MochiKit.Async.Deferred(); @@ -68,6 +80,20 @@ jrrd.RrdQuery = function(rrd, unit) { }; jrrd.RrdQuery.prototype.getData = function(startTime, endTime, dsId, cfName) { + /** + * Generate a Flot compatible data object containing rows between start and + * end time. The rows are taken from the first RRA whose data spans the + * requested time range. + * + * @param startTime: The I{Date} start time + * @param endTime: The I{Date} end time + * @param dsId: An index I{Number} or key I{String} identifying the RRD + * datasource (DS). + * @param cfName: The name I{String} of an RRD consolidation function (CF) + * eg AVERAGE, MIN, MAX + * @return: A Flot compatible data series I{Object} + * eg {label:'', data:[], unit: ''} + **/ var startTimestamp = startTime.getTime()/1000; var lastUpdated = this.rrd.getLastUpdate(); @@ -130,7 +156,13 @@ jrrd.RrdQuery.prototype.getData = function(startTime, endTime, dsId, cfName) { return {label: ds.getName(), data: flotData, unit: this.unit}; }; - +/** + * A wrapper around RrdQuery which provides asynchronous access to the data in a + * remote RRD file. + * + * @param url: The url I{String} of a remote RRD file + * @param unit: The unit suffix I{String} of this data eg 'bit/sec' + **/ jrrd.RrdQueryRemote = function(url, unit) { this.url = url; this.unit = unit; @@ -139,6 +171,14 @@ jrrd.RrdQueryRemote = function(url, unit) { }; jrrd.RrdQueryRemote.prototype.getData = function(startTime, endTime, dsId) { + /** + * Return a Flot compatible data series asynchronously. + * + * @param startTime: The start time I{Date} + * @param endTime: The end time I{Date} + * @returns: A I{MochiKit.Async.Deferred} which calls back with a flot data + * series object I{Object} + **/ var endTimestamp = endTime.getTime()/1000; // Download the rrd if there has never been a download or if the last @@ -180,7 +220,13 @@ jrrd.RrdQueryRemote.prototype.getData = function(startTime, endTime, dsId) { return ret; }; - +/** + * Wraps a I{RrdQueryRemote} to provide access to a different RRD DSs within a + * single RrdDataSource. + * + * @param rrdQuery: An I{RrdQueryRemote} + * @param dsId: An index or keyname of an RRD DS + **/ jrrd.RrdQueryDsProxy = function(rrdQuery, dsId) { this.rrdQuery = rrdQuery; this.dsId = dsId; @@ -188,21 +234,43 @@ jrrd.RrdQueryDsProxy = function(rrdQuery, dsId) { }; jrrd.RrdQueryDsProxy.prototype.getData = function(startTime, endTime) { + /** + * Call I{RrdQueryRemote.getData} with a particular dsId + **/ return this.rrdQuery.getData(startTime, endTime, this.dsId); }; +/** + * A class for creating a Flot chart from a series of RRD Queries + * + * @param template: A I{jQuery} containing a single element into which the chart + * will be drawn + * @param options: An I{Object} containing Flot options which describe how the + * chart should be drawn. + **/ jrrd.Chart = function(template, options) { this.template = template; this.options = jQuery.extend(true, {yaxis: {}}, options); this.data = []; var self = this; + + // Listen for clicks on the legend items - onclick enable / disable the + // corresponding data source. $('.legend tr', this.template[0]).live('click', function(e) { self.switchDataEnabled($(this).children('.legendLabel').text()); self.draw(); }); + this.options['yaxis']['ticks'] = function(axis) { + /** + * Choose a suitable SI multiplier based on the min and max values from + * the axis and then generate appropriate yaxis tick labels. + * + * @param axis: An I{Object} with min and max properties + * @return: An array of ~5 tick labels + **/ var siPrefixes = { 0: '', 1: 'K', @@ -250,6 +318,16 @@ jrrd.Chart = function(template, options) { }; jrrd.Chart.prototype.addData = function(label, db, enabled) { + /** + * Add details of a remote RRD data source whose data will be added to this + * chart. + * + * @param label: A I{String} label for this data which will be shown in the + * chart legend + * @param db: The url of the remote RRD database + * @param enabled: true if you want this data plotted on the chart, false + * if not. + **/ if(typeof enabled == 'undefined') { enabled = true; } @@ -257,6 +335,12 @@ jrrd.Chart.prototype.addData = function(label, db, enabled) { }; jrrd.Chart.prototype.switchDataEnabled = function(label) { + /** + * Enable / Disable a single data source + * + * @param label: The label I{String} of the data source to be enabled / + * disabled + **/ for(var i=0; i