Convenient module for storing and querying time series statistics in Redis using Node.js.
The design (and even parts of the implementation) were picked from the ApiAxle project.
You can find basic usage examples in examples
. This module also powers a real-time dashboard written in Node.js. Check the sources out for more insight.
redis-timeseries
has no dependencies, and will work along the redis
module you'll install in your own project. redis@~0.9.0
versions are compatible.
var TimeSeries = require('redis-timeseries'),
redis = require('redis').createClient();
// Create the TimeSeries client
//
// "stats" is the Redis namespace which will be used
// for storing all the TimeSeries related keys
//
// "granularities" encodes the granularities at which
// you want to store statistics. More on that in the next section
//
var ts = new TimeSeries(redis, "stats", granularities);
// Recording hits
//
// This increments the counters for the
// stats keys you provide
//
// "timestamp" defaults to the current time
// "increment" defaults to 1
//
ts.recordHit('your_stats_key')
.recordHit('another_stats_key', timestamp)
.recordHit('another_stats_key', timestamp2, increment)
…
.exec();
// Removing hits
//
// It's also possible to decrement the hits counter for
// some key
ts.removeHit('your_stats_key', [timestamp]).exec();
// Recording values
//
// This sets the value for the
// stats keys you provide
//
// "timestamp" defaults to the current time
// "increment" defaults to 1
//
ts.recordValue('your_stats_key', value)
.recordValue('another_stats_key', value, timestamp)
.recordValue('another_stats_key', value, timestamp2, increment)
…
.exec();
// Querying statistics
//
// Returns "count" chunks of counters at the precision described by
// "granularity_label"
//
ts.getHits('your_stats_key', granularity_label, count, function(err, data) {
// data.length == count
// data = [ [ts1, count1], [ts2, count2]... ]
});
// getValues is identical to getHits except that it returns a null value for unset timestamps
ts.getValues('your_stats_key', granularity_label, count, function(err, data) {
// data.length == count
// data = [ [ts1, count1], [ts2, count2]... ]
});
For each key, TimeSeries
stores statistics at different granularities. For further information about this, please refer to the detailed blog post from the ApiAxle project.
The default granularities are:
{
'1second' : { ttl: this.minutes(5), duration: 1 },
'1minute' : { ttl: this.hours(1) , duration: this.minutes(1) },
'5minutes' : { ttl: this.days(1) , duration: this.minutes(5) },
'10minutes': { ttl: this.days(1) , duration: this.minutes(10) },
'1hour' : { ttl: this.days(7) , duration: this.hours(1) },
'1day' : { ttl: this.weeks(52) , duration: this.days(1) }
}
This means that the number of hits per second
will be stored for 5 minutes
, and the corresponding hashset will expire afterwards. Likewise, the number of hits per minute
for a given key will be kept for an hour
. Daily
counters on the other hand are kept for a full year.
When querying for statistics, a granularity label is expected:
// Give me the hits/second for the last 3 minutes
ts.getHits('your_stats_key', '1second', ts.minutes(3), callback);
// Give me the number of hits per day for the last 2 weeks
ts.getHits('your_stats_key', '1day', 14, callback);
// And so on
When creating the TimeSeries
client, you can override the default granularities with your own.