1<!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="/tracing/model/counter_sample.html">
9<link rel="import" href="/tracing/model/event_container.html">
10
11<script>
12'use strict';
13
14tr.exportTo('tr.model', function() {
15  var CounterSample = tr.model.CounterSample;
16
17  /**
18   * A container holding all samples of a given measurement over time.
19   *
20   * As an example, a counter series might measure the throughput of data sent
21   * over a USB connection, with each sample representing the instantaneous
22   * throughput of the connection.
23   *
24   * @constructor
25   * @extends {EventContainer}
26   */
27  function CounterSeries(name, color) {
28    tr.model.EventContainer.call(this);
29
30    this.name_ = name;
31    this.color_ = color;
32
33    this.timestamps_ = [];
34    this.samples_ = [];
35
36    // Set by counter.addSeries
37    this.counter = undefined;
38    this.seriesIndex = undefined;
39  }
40
41  CounterSeries.prototype = {
42    __proto__: tr.model.EventContainer.prototype,
43
44    get length() {
45      return this.timestamps_.length;
46    },
47
48    get name() {
49      return this.name_;
50    },
51
52    get color() {
53      return this.color_;
54    },
55
56    get samples() {
57      return this.samples_;
58    },
59
60    get timestamps() {
61      return this.timestamps_;
62    },
63
64    getSample: function(idx) {
65      return this.samples_[idx];
66    },
67
68    getTimestamp: function(idx) {
69      return this.timestamps_[idx];
70    },
71
72    addCounterSample: function(ts, val) {
73      var sample = new CounterSample(this, ts, val);
74      this.addSample(sample);
75      return sample;
76    },
77
78    addSample: function(sample) {
79      this.timestamps_.push(sample.timestamp);
80      this.samples_.push(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    iterateAllEventsInThisContainer: function(eventTypePredicate,
113                                              callback, opt_this) {
114      if (eventTypePredicate.call(opt_this, tr.model.CounterSample)) {
115        this.samples_.forEach(callback, opt_this);
116      }
117    },
118
119    iterateAllChildEventContainers: function(callback, opt_this) {
120    }
121  };
122
123  return {
124    CounterSeries: CounterSeries
125  };
126});
127</script>
128
129