-
Notifications
You must be signed in to change notification settings - Fork 4
/
stats.js
43 lines (40 loc) · 1.04 KB
/
stats.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
//TODO: make this an object instead of a singleton
var fs = require('fs'),
mkdirp = require('mkdirp'),
stats = {},
timer;
var SAVE_INTERVAL = 3600*1000,
SAVE_ROOT = '/data/stats/';
module.exports.inc = function(domain) {
if (!stats[domain]) {
stats[domain] = 0;
}
stats[domain]++;
};
module.exports.init = function(callback) {
mkdirp(SAVE_ROOT, function(err1) {
if (err1) {
callback(err1);
} else {
timer = setInterval(function() {
mkdirp(SAVE_ROOT, function(err2) {
if (err2) {
alarm.raise('error writing stats '+JSON.stringify(err2));
} else {
fs.writeFile(SAVE_ROOT + (new Date().getTime()), JSON.stringify(stats), function(err3) {
if (err3) {
alarm.raise('error writing stats '+JSON.stringify(err3));
} else {
stats = {};
}
});
}
});
}, SAVE_INTERVAL);
callback(null);
}
});
};
module.exports.exit = function() {
clearInterval(timer);
};