summaryrefslogtreecommitdiff
path: root/sflow-rt/app/flow-trend/scripts/inc/trend.js
diff options
context:
space:
mode:
author姜萍 <[email protected]>2022-05-29 17:59:26 +0800
committer姜萍 <[email protected]>2022-05-29 17:59:26 +0800
commit9f0f12ed28357ae167cb9aab3a614da0f8cd4bab (patch)
tree0eac2d3fbfc11e7639b9a0254a8c24d8a43bd8f2 /sflow-rt/app/flow-trend/scripts/inc/trend.js
initmaster
Diffstat (limited to 'sflow-rt/app/flow-trend/scripts/inc/trend.js')
-rw-r--r--sflow-rt/app/flow-trend/scripts/inc/trend.js49
1 files changed, 49 insertions, 0 deletions
diff --git a/sflow-rt/app/flow-trend/scripts/inc/trend.js b/sflow-rt/app/flow-trend/scripts/inc/trend.js
new file mode 100644
index 0000000..e550352
--- /dev/null
+++ b/sflow-rt/app/flow-trend/scripts/inc/trend.js
@@ -0,0 +1,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];
+}