Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sflow-rt
GitHub Repository: sflow-rt/ddos-protect
Path: blob/master/scripts/inc/trend.js
50 views
1
function Trend(maxPoints, stepSize) {
2
this.maxPoints = maxPoints;
3
this.trends = {};
4
this.times = new Array(maxPoints);
5
var i, t = Date.now(), stepMs = stepSize * 1000;
6
for(i = maxPoints - 1; i >= 0; i--) { t -= stepMs; this.times[i] = t; }
7
}
8
9
Trend.prototype.addPoints = function(now,values) {
10
this.times.push(now);
11
12
var name, i;
13
for (name in values) {
14
var points = this.trends[name];
15
if(!points) {
16
points = new Array(this.maxPoints);
17
for(i = 0; i < this.maxPoints; i++) points[i] = 0;
18
this.trends[name] = points;
19
}
20
points.push(values[name]);
21
points.shift();
22
}
23
this.times.shift();
24
}
25
26
Trend.prototype.after = function(tval) {
27
var res = new Trend(0,0);
28
res.maxPoints = this.maxPoints;
29
for(var i = 0; i < this.times.length; i++) {
30
var t = this.times[i];
31
if(tval < t) {
32
res.times.push(t);
33
for (var name in this.trends) {
34
var val = this.trends[name][i];
35
var trend = res.trends[name];
36
if(!trend) {
37
trend = [];
38
res.trends[name] = trend;
39
}
40
trend.push(val);
41
}
42
}
43
}
44
return res;
45
}
46
47
Trend.prototype.remove = function(name) {
48
delete this.trends[name];
49
}
50
51