Last active
August 29, 2015 14:06
-
-
Save Unitech/8d61538a0311f1be19e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var timeseries = require('timeseries')({ | |
rethinkdb : { | |
port : 28015, | |
host : 'localhost', | |
auth_key : null | |
}, | |
redis : { | |
port : 6379, | |
host : 'localhost' | |
} | |
}); | |
/** | |
* The timeseries module must be state-less | |
*/ | |
timeseries.put({ | |
uid : 'specific-key', | |
table : 'websocket-monitoring', | |
rdb_database : 'specific-database', | |
fields : ['clients', 'events_1sec', 'events_1min'], | |
data : [ | |
{ | |
clients : 2, | |
events_1sec : 10, | |
events_1min : 65 | |
}, | |
{ | |
clients : 2, | |
events_1sec : 10, | |
events_1min : 65 | |
} | |
] | |
}, function(error, elementsStored){}); | |
/** | |
* 1- The data is first aggregated every minutes (in memory or Redis, memory should be enough) | |
* | |
* { | |
* clients : [0, 0, 14, 15, 43, 30], | |
* events_1sec : [0, 0, 30, 50, 89, 90], | |
* events_1min : [0, 0, 130, 400, 599, 300] | |
* } | |
* | |
* 2- When the current minute is reached a simple mean calculation is made and we append the uid and the minute: | |
* | |
* { | |
* clients : 25, | |
* events_1sec : 40, | |
* events_1min : 380, | |
* at : 'Wed Sep 24 2014 17:29:00 GMT+0200 (CEST)', | |
* uid : 'specific-key' | |
* } | |
* | |
* 3 - The data is then flushed to database | |
* | |
* r.db(opts.db_name) | |
* .table(opts.table) | |
* .insert(data_object) | |
* .run(opts.connection, function(err, cursor) { } | |
* | |
*/ | |
timeseries.get({ | |
uid : 'specific-key', | |
table : 'websocket-monitoring', | |
rdb_database : 'specific-database', | |
fields : ['clients', 'events_1sec', 'events_1min'], | |
from : moment().toDate(), | |
to : moment().subtract(7, 'days').toDate(), | |
}, function(err, data) { | |
if(err){ | |
throw err; | |
} else { | |
console.log(data); | |
} | |
}); | |
/** | |
* The outputted data should look like this: | |
* | |
* { | |
* clients : [0, 0, 14, 15, 43, 30], //you mean 1 batch for every minute? | |
* events_1sec : [0, 0, 30, 50, 89, 90], | |
* events_1min : [0, 0, 130, 400, 599, 300] | |
* time : ['Wed Sep 24 2014 17:29:00 GMT+0200 (CEST)', 'Wed Sep 24 2014 17:30:00 GMT+0200 (CEST)', '...every min...'] | |
* } | |
* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment