Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ 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.

- `cf`: Consolidation function (`AVERAGE`, `MIN`, `MAX`, `LAST`)
- `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)`
29 changes: 22 additions & 7 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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':

Expand Down Expand Up @@ -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 () {
Expand Down
6 changes: 5 additions & 1 deletion lib/proc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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); }

Expand Down
2 changes: 1 addition & 1 deletion test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/open.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down