summaryrefslogtreecommitdiff
path: root/sflow-rt/app/flow-trend/scripts/inc/trend.js
blob: e550352403bdf426ef6b10cf4a698b53ca674f29 (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
function Trend(maxPoints, stepSize) {
  this.maxPoints = maxPoints;
  this.trends = {};
  this.times = new Array(maxPoints);
  var i, t = (new Date()).getTime(), stepMs = stepSize * 1000;
  for(i = maxPoints - 1; i >= 0; i--) { t -= stepMs; this.times[i] = t; }
}

Trend.prototype.addPoints = function(now,values) {
  this.times.push(now);
  
  var name, i; 
  for (name in values) {
    var points = this.trends[name];
    if(!points) {
      points = new Array(this.maxPoints);
      for(i = 0; i < this.maxPoints; i++) points[i] = 0; 
      this.trends[name] = points;
    }
    points.push(values[name]);
    points.shift();
  }
  this.times.shift();
}

Trend.prototype.after = function(tval) {
  var res = new Trend(0,0);
  res.maxPoints = this.maxPoints;
  for(var i = 0; i < this.times.length; i++) {
    var t = this.times[i];
    if(tval < t) {
      res.times.push(t);
      for (var name in this.trends) {
        var val = this.trends[name][i];
        var trend = res.trends[name];
        if(!trend) {
          trend = [];
          res.trends[name] = trend;
        }
        trend.push(val);
      }
    }
  } 
  return res;
}

Trend.prototype.remove = function(name) {
  delete this.trends[name];
}