-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.js
207 lines (187 loc) · 4.87 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* wfApi,
* Copyright (C) 2018 Ilkka Kuosmanen
*
* This file is part of wfApi.
*
* wfApi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wfApi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wfApi. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
class Stat {
constructor(name, flags) {
this.name = name;
this.value = 0;
this.flags = flags;
// Setup the buffers
this.minuteBuffer = [];
this.hourBuffer = [];
this.dayBuffer = [];
for (let i = 0; i < 60; i++) {
this.minuteBuffer[i] = 0;
this.hourBuffer[i] = 0;
if (i < 24) {
this.dayBuffer[i] = 0;
}
}
let date = new Date();
if (this.flags.indexOf("s") > -1) {
this.minuteBufferIndex = date.getSeconds();
let secondInterval = setInterval(Stat._clearNextSecond, 1000, this);
secondInterval.unref();
}
if (this.flags.indexOf("m") > -1) {
this.hourBufferIndex = date.getMinutes();
let minuteInterval = setInterval(Stat._clearNextMinute, 1000 * 60, this);
minuteInterval.unref();
}
if (this.flags.indexOf("h") > -1) {
this.dayBufferIndex = date.getHours();
let hourInterval = setInterval(Stat._clearNextHour, 1000 * 60 * 60, this);
hourInterval.unref();
}
}
hit() {
let date = new Date();
this.value += 1;
this.minuteBuffer[date.getSeconds()]++;
this.hourBuffer[date.getMinutes()]++;
this.dayBuffer[date.getHours()]++;
}
getFlags() {
return this.flags;
}
getName(flag) {
let name = this.name + " since server start.";
if (flag) {
name = this.name + " / " + flag[3] + " (last ";
switch (flag[3]) {
case "s":
name += "minute)";
break;
case "m":
name += "hour)";
break;
case "h":
name += "day)";
break;
}
}
return name;
}
getTotal() {
return this.value;
}
getPerSecond() {
return Stat._average(this.minuteBuffer).toFixed(2);
}
getPerMinute() {
return Stat._average(this.hourBuffer).toFixed(2);
}
getPerHour() {
return Stat._average(this.dayBuffer, 24).toFixed(2);
}
getValueForFlag(flag) {
if (this.flags.indexOf(flag[3]) > -1) {
switch (flag[3]) {
case "s":
return this.getPerSecond();
case "m":
return this.getPerMinute();
case "h":
return this.getPerHour();
default:
return this.getTotal();
}
}
}
static _clearNextSecond(obj) {
obj.minuteBuffer[obj.minuteBufferIndex] = 0;
obj.minuteBufferIndex++;
if (obj.minuteBufferIndex > 59) {
obj.minuteBufferIndex = 0;
}
}
static _clearNextMinute(obj) {
obj.hourBuffer[obj.hourBufferIndex] = 0;
obj.hourBufferIndex++;
if (obj.hourBufferIndex > 59) {
obj.hourBufferIndex = 0;
}
}
static _clearNextHour(obj) {
obj.dayBuffer[obj.dayBufferIndex] = 0;
obj.dayBufferIndex++;
if (obj.dayBufferIndex > 59) {
obj.dayBufferIndex = 0;
}
}
static _average(arr, length) {
if (!arr) {
return 0;
}
let sum = arr.reduce((a, b) => {
return a + b;
});
let div = 60;
if (length) {
div = length;
}
return sum / div;
}
}
class StatManager {
constructor() {
this.stats = [];
}
registerStat(name, flags) {
if (this.stats.findIndex((x) => x.name === name) > -1) {
return;
}
this.stats.push(new Stat(name, flags));
}
hit(name) {
let index = this.stats.findIndex((x) => x.name === name);
if (index === -1) {
return;
}
this.stats[index].hit();
}
getArray() {
let ret = [];
for (let key in this.stats) {
if (!this.stats.hasOwnProperty(key)) {
continue;
}
let flags = this.stats[key].getFlags();
ret.push({
name: this.stats[key].getName(),
value: this.stats[key].getTotal(),
});
if (flags) {
let reg = /((\d*)(\w))/g;
let f = "";
while ((f = reg.exec(flags)) !== null) {
ret.push({
name: this.stats[key].getName(f),
value: this.stats[key].getValueForFlag(f),
});
}
}
}
return ret;
}
}
module.exports = {
StatManager,
};