From 63ce3597a2a7e0d28cee5f9249c9824ec42c6f61 Mon Sep 17 00:00:00 2001 From: Aaron Cardenas Date: Fri, 27 Jan 2017 09:47:16 -0600 Subject: [PATCH] Add optional 'daemon' parameter to fetch Fix tests --- README.md | 3 ++- lib/db.js | 29 ++++++++++++++++++++++------- lib/proc.js | 6 +++++- test/basic.js | 2 +- test/open.js | 2 +- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 3121d68..c72d91f 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ Insert data into the database. - `values`: Object with one entry per data source to insert into - `cb`: Callback to call when the data is inserted `(err)` -#### `.fetch(cf, start, stop[, res], cb)` +#### `.fetch(cf, start, stop[, res, daemon], cb)` Fetch a span of data from the database. @@ -73,4 +73,5 @@ Fetch a span of data from the database. - `start`: Unix timestamp from where to start - `stop`: Unix timestamp of which to stop at - `res`: Resolution of the data, specified in seconds + - `daemon`: Daemon address - `cb`: Callback to call when the data is ready `(err, data)` diff --git a/lib/db.js b/lib/db.js index 647f2cb..b8ff976 100644 --- a/lib/db.js +++ b/lib/db.js @@ -79,8 +79,9 @@ DB.prototype._worker = function (args, cb) { var start = args[2]; var stop = args[3]; var res = args[4]; + var dae = args[5]; - proc.fetch(this.file, cf, start, stop, res, cb); + proc.fetch(this.file, cf, start, stop, res, dae, cb); break; case 'dump': @@ -124,17 +125,31 @@ DB.prototype.update = function (ts, values, cb) { this.queue.push([['update', ts, values]], this._cb(cb)); }; -DB.prototype.fetch = function (cf, start, stop, res, cb) { +DB.prototype.fetch = function (cf, start, stop, res, dae, cb) { // `res` is optional and defaults to highest possible + // `dae` is optional - if (arguments.length === 4) { - if (typeof res === 'function') { + switch (typeof res) { + case 'function' : cb = res; - res = null; - } + res = dae = null; + break; + case 'number' : + if (typeof dae === 'function') { + cb = dae; + dae = null; + } + break; + case 'string' : + if (typeof dae === 'function') { + cb = dae; + dae = res; + res = null; + } + break; } - this.queue.push([['fetch', cf, start, stop, res]], this._cb(cb)); + this.queue.push([['fetch', cf, start, stop, res, dae]], this._cb(cb)); }; DB.prototype._dump = function () { diff --git a/lib/proc.js b/lib/proc.js index 9e325e3..c3647bc 100644 --- a/lib/proc.js +++ b/lib/proc.js @@ -92,7 +92,7 @@ exports.update = function (file, ts, values, cb) { }); }; -exports.fetch = function (file, cf, start, stop, res, cb) { +exports.fetch = function (file, cf, start, stop, res, dae, cb) { var args = ['fetch', file, cf]; @@ -104,6 +104,10 @@ exports.fetch = function (file, cf, start, stop, res, cb) { args.push('--resolution', '' + res); } + if (dae !== null) { + args.push('--daemon', '' + dae); + } + exec(args, function (err, data) { if (err) { return cb(err); } diff --git a/test/basic.js b/test/basic.js index fb6e026..59442d5 100644 --- a/test/basic.js +++ b/test/basic.js @@ -211,7 +211,7 @@ describe('rrd', function () { db.update(src[0], { test: src[1] }); }); - db.fetch('MAX', start + 10, start + 10, function (err, data) { + db.fetch('MAX', start + 10, start + 10, 10, function (err, data) { if (err) { throw err; } assert.equal(data.length, 1); diff --git a/test/open.js b/test/open.js index aa31e23..8d4e4c9 100644 --- a/test/open.js +++ b/test/open.js @@ -24,7 +24,7 @@ describe('rrd', function () { return [start + i, v]; }); - db.fetch('MAX', start + 10, start + 10, function (err, data) { + db.fetch('MAX', start + 10, start + 10, 10, function (err, data) { assert.equal(data.length, 1); assert.equal(data[0].time, start + 10);