From 047d284b792ce7ac2b8e47e8067b142c5f081ee7 Mon Sep 17 00:00:00 2001 From: Richard Wall Date: Mon, 30 Aug 2010 22:02:51 +0100 Subject: hopefully more consistent behaviour - more like rrdtool fetch --- jarmon/jarmon.js | 71 ++++++++++++++++++++++++++++++++++++--------------- jarmon/jarmon.test.js | 11 ++++---- 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/jarmon/jarmon.js b/jarmon/jarmon.js index 24f967d..ed26702 100644 --- a/jarmon/jarmon.js +++ b/jarmon/jarmon.js @@ -156,15 +156,15 @@ jarmon.RrdQuery = function(rrd, unit) { this.unit = unit; }; -jarmon.RrdQuery.prototype.getData = function(startTime, endTime, dsId, cfName) { +jarmon.RrdQuery.prototype.getData = function(startTimeJs, endTimeJs, 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. * * @method getData - * @param startTime {Number} start timestamp - * @param endTime {Number} end timestamp + * @param startTimeJs {Number} start timestamp in microseconds + * @param endTimeJs {Number} end timestamp in microseconds * @param dsId {Variant} identifier of the RRD datasource (string or number) * @param cfName {String} The name of an RRD consolidation function (CF) * eg AVERAGE, MIN, MAX @@ -172,22 +172,31 @@ jarmon.RrdQuery.prototype.getData = function(startTime, endTime, dsId, cfName) { * eg label: '', data: [], unit: '' **/ - if (startTime >= endTime) { + if (startTimeJs >= endTimeJs) { throw new jarmon.TimeRangeError( ['starttime must be less than endtime. ', - 'starttime: ', starttime, - 'endtime: ', endtime].join('')); + 'starttime: ', startTimeJs, + 'endtime: ', endTimeJs].join('')); } - var startTimestamp = startTime/1000; - + // The startTime, endTime and lastupdate time are not necessarily on a step + // boundaries. Here we divide, round and then multiply by the step size to + // find the nearest "Primary Data Point" (PDP) time. + var minStep = this.rrd.getMinStep(); + var startTime = Math.round(startTimeJs/1000/minStep) * minStep; var lastUpdated = this.rrd.getLastUpdate(); - var endTimestamp = lastUpdated; - if(endTime) { - endTimestamp = endTime/1000; + var lastPdpTime = Math.round(lastUpdated / minStep) * minStep; + + var endTime = lastPdpTime; + if(endTimeJs) { + endTime = endTimeJs/1000; // If end time stamp is beyond the range of this rrd then reset it - if(lastUpdated < endTimestamp) { - endTimestamp = lastUpdated; + // XXX: Is this the correct behaviour. Perhaps better to throw exception + // or simply return nan for each missing PDP - like rrdtool fetch. + if(lastPdpTime < endTime) { + endTime = lastPdpTime; + } else { + endTime = Math.round(endTime / minStep) * minStep; } } @@ -200,7 +209,7 @@ jarmon.RrdQuery.prototype.getData = function(startTime, endTime, dsId, cfName) { cfName = 'AVERAGE'; } - var rra, step, rraRowCount, firstUpdated; + var rra, step, rraRowCount, firstPdpTime; for(var i=0; i