Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / tracing / trace_model / counter_series.js
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 'use strict';
6
7 tvcm.require('tracing.trace_model.counter_sample');
8
9 /**
10  * @fileoverview Provides the CounterSeries class.
11  */
12 tvcm.exportTo('tracing.trace_model', function() {
13   var CounterSample = tracing.trace_model.CounterSample;
14
15   function CounterSeries(name, color) {
16     this.guid_ = tvcm.GUID.allocate();
17
18     this.name_ = name;
19     this.color_ = color;
20
21     this.timestamps_ = [];
22     this.samples_ = [];
23
24     // Set by counter.addSeries
25     this.counter = undefined;
26     this.seriesIndex = undefined;
27   }
28
29   CounterSeries.prototype = {
30     __proto__: Object.prototype,
31
32     toJSON: function() {
33       var obj = new Object();
34       var keys = Object.keys(this);
35       for (var i = 0; i < keys.length; i++) {
36         var key = keys[i];
37         if (typeof this[key] == 'function')
38           continue;
39         if (key == 'counter') {
40           obj[key] = this[key].guid;
41           continue;
42         }
43         obj[key] = this[key];
44       }
45       return obj;
46     },
47
48     get length() {
49       return this.timestamps_.length;
50     },
51
52     get name() {
53       return this.name_;
54     },
55
56     get color() {
57       return this.color_;
58     },
59
60     get samples() {
61       return this.samples_;
62     },
63
64     get timestamps() {
65       return this.timestamps_;
66     },
67
68     getSample: function(idx) {
69       return this.samples_[idx];
70     },
71
72     getTimestamp: function(idx) {
73       return this.timestamps_[idx];
74     },
75
76     addSample: function(ts, val) {
77       this.timestamps_.push(ts);
78       var sample = new CounterSample(this, ts, val);
79       this.samples_.push(sample);
80       return sample;
81     },
82
83     getStatistics: function(sampleIndices) {
84       var sum = 0;
85       var min = Number.MAX_VALUE;
86       var max = -Number.MAX_VALUE;
87
88       for (var i = 0; i < sampleIndices.length; ++i) {
89         var sample = this.getSample(sampleIndices[i]).value;
90
91         sum += sample;
92         min = Math.min(sample, min);
93         max = Math.max(sample, max);
94       }
95
96       return {
97         min: min,
98         max: max,
99         avg: (sum / sampleIndices.length),
100         start: this.getSample(sampleIndices[0]).value,
101         end: this.getSample(sampleIndices.length - 1).value
102       };
103     },
104
105     shiftTimestampsForward: function(amount) {
106       for (var i = 0; i < this.timestamps_.length; ++i) {
107         this.timestamps_[i] += amount;
108         this.samples_[i].timestamp = this.timestamps_[i];
109       }
110     },
111
112     iterateAllEvents: function(callback, opt_this) {
113       this.samples_.forEach(callback, opt_this);
114     }
115   };
116
117   return {
118     CounterSeries: CounterSeries
119   };
120 });