Redis time series statistics with Node.js
Latest commit 894960b Jun 15, 2016 @alph486 alph486 committed on GitHub Merge pull request #14 from dangerdespain/packageFix
Allowing newer versions of Redis to be used

Node.js API for storing and querying time series in Redis

Build Status

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. If providing
    // a timestamp, it should be in unix timestamp format (seconds
    // since epoch).
    // "increment" defaults to 1
    // .exec takes an optional callback with no arguments.
      .recordHit('another_stats_key', timestamp)
      .recordHit('another_stats_key', timestamp2, increment)

    // Removing hits
    // Decrements the hits for a specified point in time.
    ts.removeHit('your_stats_key', [timestamp]).exec();

    // Decrement defaults to 1, but can be specified explicitly (below).
    ts.removeHit('your_stats_key', [timestamp], 5).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]... ]

Defining custom statistics granularities

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), function(err, data){
        //process the data

    // Give me the number of hits per day for the last 2 weeks
    ts.getHits('your_stats_key', '1day', 14, function(err, data){
        //process the data

    // And so on

When creating the TimeSeries client, you can override the default granularities with your own.

Bitdeli Badge