blob: 992ec448726b8e26d103396e061c2da96c8ce2dd [file] [log] [blame]
Chris Craik93216d02015-03-05 13:58:42 -08001<!DOCTYPE html>
2<!--
3Copyright (c) 2013 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
8<link rel="import" href="/core/trace_model/counter_series.html">
Chris Craik44c28202015-05-12 17:25:16 -07009<link rel="import" href="/core/trace_model/event_container.html">
Chris Craik93216d02015-03-05 13:58:42 -080010<link rel="import" href="/base/guid.html">
11<link rel="import" href="/base/range.html">
12
13<script>
14'use strict';
15
16/**
17 * @fileoverview Provides the Counter class.
18 */
19tv.exportTo('tv.c.trace_model', function() {
20
21 /**
22 * Stores all the samples for a given counter.
23 * @constructor
24 */
25 function Counter(parent, id, category, name) {
Chris Craik44c28202015-05-12 17:25:16 -070026 tv.c.trace_model.EventContainer.call(this);
Chris Craik93216d02015-03-05 13:58:42 -080027 this.guid_ = tv.b.GUID.allocate();
28
29 this.parent = parent;
30 this.id = id;
31 this.category = category || '';
32 this.name = name;
33
34 this.series_ = [];
35 this.totals = [];
36 this.bounds = new tv.b.Range();
37 }
38
39 Counter.prototype = {
Chris Craik44c28202015-05-12 17:25:16 -070040 __proto__: tv.c.trace_model.EventContainer.prototype,
Chris Craik93216d02015-03-05 13:58:42 -080041
42 /*
43 * @return {Number} A globally unique identifier for this counter.
44 */
45 get guid() {
46 return this.guid_;
47 },
48
Chris Craik44c28202015-05-12 17:25:16 -070049 iterateAllEventsInThisContainer: function(eventTypePredicate,
50 callback, opt_this) {
51 },
52
53 iterateAllChildEventContainers: function(callback, opt_this) {
54 for (var i = 0; i < this.series_.length; i++)
55 callback.call(opt_this, this.series_[i]);
56 },
57
Chris Craik93216d02015-03-05 13:58:42 -080058 set timestamps(arg) {
59 throw new Error('Bad counter API. No cookie.');
60 },
61
62 set seriesNames(arg) {
63 throw new Error('Bad counter API. No cookie.');
64 },
65
66 set seriesColors(arg) {
67 throw new Error('Bad counter API. No cookie.');
68 },
69
70 set samples(arg) {
71 throw new Error('Bad counter API. No cookie.');
72 },
73
74 addSeries: function(series) {
75 series.counter = this;
76 series.seriesIndex = this.series_.length;
77 this.series_.push(series);
78 return series;
79 },
80
81 getSeries: function(idx) {
82 return this.series_[idx];
83 },
84
85 get series() {
86 return this.series_;
87 },
88
89 get numSeries() {
90 return this.series_.length;
91 },
92
93 get numSamples() {
94 if (this.series_.length === 0)
95 return 0;
96 return this.series_[0].length;
97 },
98
99 get timestamps() {
100 if (this.series_.length === 0)
101 return [];
102 return this.series_[0].timestamps;
103 },
104
105 /**
106 * Obtains min, max, avg, values, start, and end for different series for
107 * a given counter
108 * getSampleStatistics([0,1])
109 * The statistics objects that this returns are an array of objects, one
110 * object for each series for the counter in the form:
111 * {min: minVal, max: maxVal, avg: avgVal, start: startVal, end: endVal}
112 *
113 * @param {Array.<Number>} Indices to summarize.
114 * @return {Object} An array of statistics. Each element in the array
115 * has data for one of the series in the selected counter.
116 */
117 getSampleStatistics: function(sampleIndices) {
118 sampleIndices.sort();
119
120 var ret = [];
121 this.series_.forEach(function(series) {
122 ret.push(series.getStatistics(sampleIndices));
123 });
124 return ret;
125 },
126
127 /**
128 * Shifts all the timestamps inside this counter forward by the amount
129 * specified.
130 */
131 shiftTimestampsForward: function(amount) {
132 for (var i = 0; i < this.series_.length; ++i)
133 this.series_[i].shiftTimestampsForward(amount);
134 },
135
136 /**
137 * Updates the bounds for this counter based on the samples it contains.
138 */
139 updateBounds: function() {
140 this.totals = [];
141 this.maxTotal = 0;
142 this.bounds.reset();
143
144 if (this.series_.length === 0)
145 return;
146
147 var firstSeries = this.series_[0];
148 var lastSeries = this.series_[this.series_.length - 1];
149
150 this.bounds.addValue(firstSeries.getTimestamp(0));
151 this.bounds.addValue(lastSeries.getTimestamp(lastSeries.length - 1));
152
153 var numSeries = this.numSeries;
154 this.maxTotal = -Infinity;
155
156 // Sum the samples at each timestamp.
157 // Note, this assumes that all series have all timestamps.
158 for (var i = 0; i < firstSeries.length; ++i) {
159 var total = 0;
160 this.series_.forEach(function(series) {
161 total += series.getSample(i).value;
162 this.totals.push(total);
163 }.bind(this));
164
165 this.maxTotal = Math.max(total, this.maxTotal);
166 }
Chris Craik93216d02015-03-05 13:58:42 -0800167 }
168 };
169
170 /**
171 * Comparison between counters that orders by parent.compareTo, then name.
172 */
173 Counter.compare = function(x, y) {
174 var tmp = x.parent.compareTo(y);
175 if (tmp != 0)
176 return tmp;
177 var tmp = x.name.localeCompare(y.name);
178 if (tmp == 0)
179 return x.tid - y.tid;
180 return tmp;
181 };
182
183 return {
184 Counter: Counter
185 };
186});
187</script>