Manage timeseries data storage in Redis with ease
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 = 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//tsrecordHit'your_stats_key'recordHit'another_stats_key' timestamprecordHit'another_stats_key' timestamp2 increment…exec;// Removing hits//// It's also possible to decrement the hits counter for// some keytsremoveHit'your_stats_key' timestampexec;// Querying statistics//// Returns "count" chunks of counters at the precision described by// "granularity_label"//tsgetHits'your_stats_key' granularity_label count// 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: thisminutes5 duration: 1'1minute' : ttl: thishours1 duration: thisminutes1'5minutes' : ttl: thisdays1 duration: thisminutes5'10minutes': ttl: thisdays1 duration: thisminutes10'1hour' : ttl: thisdays7 duration: thishours1'1day' : ttl: thisweeks52 duration: thisdays1
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
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 minutestsgetHits'your_stats_key' '1second' tsminutes3 callback;// Give me the number of hits per day for the last 2 weekstsgetHits'your_stats_key' '1day' 14 callback;// And so on
When creating the
TimeSeries client, you can override the default granularities with your own.