From 936f85faa33fb7781565f8b650c9554f3125d067 Mon Sep 17 00:00:00 2001 From: Richard Wall Date: Mon, 12 Apr 2010 02:41:24 +0100 Subject: Add argument validation, a time range form and various docs and comments --- jrrd.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 54 insertions(+), 19 deletions(-) (limited to 'jrrd.js') diff --git a/jrrd.js b/jrrd.js index 9104875..9cc7580 100644 --- a/jrrd.js +++ b/jrrd.js @@ -1,3 +1,13 @@ +/* Copyright (c) 2010 Richard Wall + * See LICENSE for details. + * + * 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/ + */ if(typeof jrrd == 'undefined') { var jrrd = {}; @@ -38,46 +48,71 @@ jrrd.downloadBinary = function(url) { return d; }; - +/** + * A wrapper around an instance of javascriptrrd.RRDFile which provides a + * convenient way to query the RRDFile based on time range, RRD data source (DS) + * and RRD consolidation function (CF). + * + * @param startTime: A javascript {Date} instance representing the start of query + * time range, or {null} to return earliest available data. + * @param endTime: A javascript {Date} instance representing the end of query + * time range, or {null} to return latest available data. + * @param dsId: A {String} name of an RRD DS or an {Int} DS index number or + * {null} to return the first available DS. + * @param cfName: A {String} name of an RRD consolidation function + * @return: A flot compatible data series object + **/ jrrd.RrdQuery = function(rrd) { this.rrd = rrd; }; -jrrd.RrdQuery.prototype.getData = function(startTime, endTime, dsId) { +jrrd.RrdQuery.prototype.getData = function(startTime, endTime, dsId, cfName) { var startTimestamp = startTime.getTime()/1000; - var endTimestamp = endTime.getTime()/1000; + + var lastUpdated = this.rrd.getLastUpdate(); + var endTimestamp = lastUpdated; + if(endTime) { + endTimestamp = endTime.getTime()/1000; + // If end time stamp is beyond the range of this rrd then reset it + if(lastUpdated < endTimestamp) { + endTimestamp = lastUpdated; + } + } if(dsId == null) { dsId = 0; } var ds = this.rrd.getDS(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; + if(cfName == null) { + cfName = 'AVERAGE'; } - var bestRRA = null; + + var rra, step, rraRowCount, firstUpdated; for(var i=0; i