-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis-timeseries-storage.js
193 lines (157 loc) · 5.9 KB
/
redis-timeseries-storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Stores counts of a list of values.
//
// Keys are stored at the following resolution:
//
// * minutes for the 120 minutes
// * hours for the 120 hours
// * days for the last 120 days
// * weeks for the last 120 weeks
//
// The storage is time independant in that key counts are not expressed in "from date" and "to date" but "from time ago" to "time ago". This avoid server and request time differences.
//
// Next idea.... set up caching, so that once a day has pass, the minutes are stored in a cache of arrays.
// ## Config
var MAX_KEYS = 120;
// ## Time Utilites
// Reference date.
var EPOCH = 1379425660487;
// Seconds.
var SECS_PER_MIN = 60;
var SECS_PER_HOUR = 60 * SECS_PER_MIN;
var SECS_PER_DAY = 24 * SECS_PER_HOUR;
var SECS_PER_WEEK = 7 * SECS_PER_DAY;
// Milliseconds.
var MS_PER_SEC = 1000;
var MS_PER_MIN = SECS_PER_MIN * MS_PER_SEC;
var MS_PER_HOUR = SECS_PER_HOUR * MS_PER_SEC;
var MS_PER_DAY = SECS_PER_DAY * MS_PER_SEC;
var MS_PER_WEEK = SECS_PER_WEEK * MS_PER_SEC;
function getMinutesSinceEpoch () {
return Math.floor( (Date.now() - EPOCH) / MS_PER_MIN );
}
function getHoursSinceEpoch () {
return Math.floor( (Date.now() - EPOCH) / MS_PER_HOUR );
}
function getDaysSinceEpoch () {
return Math.floor( (Date.now() - EPOCH) / MS_PER_DAY );
}
function getWeeksSinceEpoch () {
return Math.floor( (Date.now() - EPOCH) / MS_PER_WEEK );
}
// ## Redis incrementors
// Main function to increment a key.
function increment(redisClient, amount, keyPrepend, key, callback) {
if (typeof amount !== 'number') {
callback("increment amount is not number");
return;
}
var multi = redisClient.multi();
// Chain each redis command.
incrementMinute(multi, amount, keyPrepend, key);
incrementHour(multi, amount, keyPrepend, key);
incrementDays(multi, amount, keyPrepend, key);
incrementWeeks(multi, amount, keyPrepend, key);
// Do it
multi.exec(callback);
}
function incrementMinute(redisMutli, amount, keyPrepend, key) {
var fullKey = keyPrepend+"m:"+getMinutesSinceEpoch()+":"+key;
if (amount === 1) { redisMutli.incr( fullKey ); }
else { redisMutli.incrby( fullKey, amount ); }
redisMutli.expire( fullKey, SECS_PER_MIN * MAX_KEYS );
}
function incrementHour(redisMutli, amount, keyPrepend, key) {
var fullKey = keyPrepend+"h:"+getHoursSinceEpoch()+":"+key;
if (amount === 1) { redisMutli.incr( fullKey ); }
else { redisMutli.incrby( fullKey, amount ); }
redisMutli.expire( fullKey, SECS_PER_HOUR * MAX_KEYS );
}
function incrementDays(redisMutli, amount, keyPrepend, key) {
var fullKey = keyPrepend+"d:"+getDaysSinceEpoch()+":"+key;
if (amount === 1) { redisMutli.incr( fullKey ); }
else { redisMutli.incrby( fullKey, amount ); }
redisMutli.expire( fullKey, SECS_PER_DAY * MAX_KEYS );
}
function incrementWeeks(redisMutli, amount, keyPrepend, key) {
var fullKey = keyPrepend+"w:"+getWeeksSinceEpoch()+":"+key;
if (amount === 1) { redisMutli.incr( fullKey ); }
else { redisMutli.incrby( fullKey, amount ); }
redisMutli.expire( fullKey, SECS_PER_WEEK * MAX_KEYS );
}
// ## Redis getters
function countMinutes(redisClient, keyPrepend, key, callback) {
var multi = redisClient.multi();
var currentTimeKey = getMinutesSinceEpoch();
for (var i = currentTimeKey; i > currentTimeKey-MAX_KEYS; --i ) {
multi.get(keyPrepend+"m:"+i+":"+key);
}
multi.exec(callback);
}
function countHours(redisClient, keyPrepend, key, callback) {
var multi = redisClient.multi();
var currentTimeKey = getHoursSinceEpoch();
for (var i = currentTimeKey; i > currentTimeKey-MAX_KEYS; --i ) {
multi.get(keyPrepend+"h:"+i+":"+key);
}
multi.exec(callback);
}
function countDays(redisClient, keyPrepend, key, callback) {
var multi = redisClient.multi();
var currentTimeKey = getDaysSinceEpoch();
for (var i = currentTimeKey; i > currentTimeKey-MAX_KEYS; --i ) {
multi.get(keyPrepend+"d:"+i+":"+key);
}
multi.exec(callback);
}
function countWeeks(redisClient, keyPrepend, key, callback) {
var multi = redisClient.multi();
var currentTimeKey = getWeeksSinceEpoch();
for (var i = currentTimeKey; i > currentTimeKey-MAX_KEYS; --i ) {
multi.get(keyPrepend+"w:"+i+":"+key);
}
multi.exec(callback);
}
// ## RedisTimeseriesStorage
// Constructor.
function RedisTimeseriesStorage (options) {
this.redisClient = options.redisClient;
this.keyPrepend = "js:tss:";
if (options.key) {
this.keyPrepend += options.key+":";
}
}
// resolutions
RedisTimeseriesStorage.RES_MINUTE = "minute";
RedisTimeseriesStorage.RES_HOUR = "hour";
RedisTimeseriesStorage.RES_DAY = "day";
RedisTimeseriesStorage.RES_WEEK = "week";
RedisTimeseriesStorage.prototype.increment = function (key, callback) {
key = key.toLowerCase();
increment(this.redisClient, 1, this.keyPrepend, key, callback);
};
RedisTimeseriesStorage.prototype.incrementBy = function (key, amount, callback) {
key = key.toLowerCase();
increment(this.redisClient, amount, this.keyPrepend, key, callback);
};
RedisTimeseriesStorage.prototype.count = function (key, resolution, callback) {
key = key.toLowerCase();
switch (resolution) {
case RedisTimeseriesStorage.RES_MINUTE:
countMinutes(this.redisClient, this.keyPrepend, key, callback);
break;
case RedisTimeseriesStorage.RES_HOUR:
countHours(this.redisClient, this.keyPrepend, key, callback);
break;
case RedisTimeseriesStorage.RES_DAY:
countDays(this.redisClient, this.keyPrepend, key, callback);
break;
case RedisTimeseriesStorage.RES_WEEK:
countWeeks(this.redisClient, this.keyPrepend, key, callback);
break;
default:
callback("RedisTimeseriesStorage::count Error: no resolution provided");
break;
}
};
// ## Export
module.exports = RedisTimeseriesStorage;