blob: b6e4f973a0f9d5aa65d40211c97e4aa767de6496 (
plain)
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
|
/**
* Simple generic stats collection script for events occurances (etc)
* @property {Object} data - The current stats data
* @author Marzavec ( https://github.com/marzavec )
* @version v2.0.0
* @license WTFPL ( http://www.wtfpl.net/txt/copying/ )
*/
class StatsManager {
/**
* Create a stats instance
*/
constructor() {
/**
* Data holder for the stats class
* @type {Object}
*/
this.data = {};
}
/**
* Retrieve value of arbitrary `key` reference
* @param {String} key Reference to the arbitrary store name
* @example
* // Find previously set `start-time`
* stats.get('start-time');
* @public
* @return {*} Data referenced by `key`
*/
get(key) {
return this.data[key];
}
/**
* Set value of arbitrary `key` reference
* @param {String} key Reference to the arbitrary store name
* @param {Number} value New value for `key`
* @example
* // Set `start-time`
* stats.set('start-time', process.hrtime());
* @public
* @return {void}
*/
set(key, value) {
this.data[key] = value;
}
/**
* Increase value of arbitrary `key` reference, by 1 or `amount`
* @param {String} key Reference to the arbitrary store name
* @param {?Number} [amount=1] Value to increase `key` by, or 1 if omitted
* @example
* // Increment by `amount`
* stats.increment('users', 6);
* // Increment by 1
* stats.increment('users');
* @public
* @return {void}
*/
increment(key, amount = 1) {
this.set(key, (this.get(key) || 0) + amount);
}
/**
* Reduce value of arbitrary `key` reference, by 1 or `amount`
* @param {String} key Reference to the arbitrary store name
* @param {?Number} [amount=1] Value to decrease `key` by, or 1 if omitted
* @example
* // Decrement by `amount`
* stats.decrement('users', 6);
* // Decrement by 1
* stats.decrement('users');
* @public
* @return {void}
*/
decrement(key, amount = 1) {
this.set(key, (this.get(key) || 0) - amount);
}
}
module.exports = StatsManager;
|