Rotating Counters for Nodejs
Scalable time-based counters that meant logging, graphing and providing a realtime statistics. All features in tempo are meant to have a constant size memory footprint defined by configuration.
For a quick example, please look at examples/simple-use-case.js
Lets say you are running a website and want to know in realtime where people are visiting.
var redis = require'redis'createClient;var tempo = require'tempo';// create middleware to track counts// create time counter, increment and syncvar min = tempomin;appuse// the '1' is unnecessary because increment defaults to '1'mininc'requests' 1;if mingetCount'requests' > 1000 return next'throttled';next;mingetKeysforEach console.logk mingetCountk ;mineachCount'requests'console.log"Requsts at " + timetoString + ': ' + count;;console.logtempogetCount'requests' + ' request(s) made in the last minute';
The tempo TimedCounter class allows you to create a datastore object and keep data in memory.
Example for keeping data up to an hour of history:
var tempo = require'tempo';var ds = per: 60000 buckets: 60 ;
Keeping track of how many times a user has logged in in the past hour:
var ds = require'tempo'hour;dsincuserId;
Grabbing logged in counts:
var history = dsgetHistoryuserId;
Returns an array of counts (per bucket)
* tempo's keys will look something like "<namespace>:<timestamp>"
countersyncredis 'web-stats' callback;
returns and array of all the keys in the counter O(nt) where n is the number of keys and b is the number of buckets
Runs a callback against every bucket in the counter with arguments (see examples below):
Adds a counter to the list of counters to sync at once
Shortcut to instantiate counter and add it
Syncs all counters to redis (push and pull)
Pushes data to redis
Pulls data from redis
Starts running a type of sync at given intervals between syncs
syncerstart'push' 3000 // push to redis every 3 seconds