1<!DOCTYPE html>
2<!--
3Copyright (c) 2015 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/base/range.html">
9<link rel="import" href="/tracing/model/event_container.html">
10<link rel="import" href="/tracing/model/power_sample.html">
11
12<script>
13'use strict';
14
15tr.exportTo('tr.model', function() {
16
17  var PowerSample = tr.model.PowerSample;
18
19  /**
20   * A container holding a time series of power samples.
21   *
22   * @constructor
23   * @extends {EventContainer}
24   */
25  function PowerSeries(device) {
26    tr.model.EventContainer.call(this);
27
28    this.device_ = device;
29    this.samples_ = [];
30  }
31
32  PowerSeries.prototype = {
33    __proto__: tr.model.EventContainer.prototype,
34
35    get device() {
36      return this.device_;
37    },
38
39    get samples() {
40      return this.samples_;
41    },
42
43    get stableId() {
44      return this.device_.stableId + '.PowerSeries';
45    },
46
47    /**
48     * Adds a power sample to the series and returns it.
49     *
50     * Note: Samples must be added in chronological order.
51     */
52    addPowerSample: function(ts, val) {
53      var sample = new PowerSample(this, ts, val);
54      this.samples_.push(sample);
55      return sample;
56    },
57
58    /**
59     * Returns the total energy (in Joules) consumed between the specified
60     * start and end timestamps (in milliseconds).
61     */
62    getEnergyConsumed: function(start, end) {
63      var measurementRange = tr.b.Range.fromExplicitRange(start, end);
64
65      var energyConsumed = 0;
66      for (var i = 0; i < this.samples.length; i++) {
67        var sample = this.samples[i];
68        var nextSample = this.samples[i + 1];
69
70        var sampleRange = new tr.b.Range();
71        sampleRange.addValue(sample.start);
72        sampleRange.addValue(nextSample ? nextSample.start : Infinity);
73
74        var timeIntersection = measurementRange.findIntersection(sampleRange);
75
76        // Divide by 1000 to convert milliwatts to watts.
77        var powerInWatts = sample.power / 1000.0;
78
79        // Divide by 1000 to convert milliseconds to seconds.
80        var durationInSeconds = timeIntersection.duration / 1000;
81
82        energyConsumed += durationInSeconds * powerInWatts;
83      }
84
85      return energyConsumed;
86    },
87
88    shiftTimestampsForward: function(amount) {
89      for (var i = 0; i < this.samples_.length; ++i)
90        this.samples_[i].start += amount;
91    },
92
93    updateBounds: function() {
94      this.bounds.reset();
95
96      if (this.samples_.length === 0)
97        return;
98
99      this.bounds.addValue(this.samples_[0].start);
100      this.bounds.addValue(this.samples_[this.samples_.length - 1].start);
101    },
102
103    iterateAllEventsInThisContainer: function(eventTypePredicate, callback,
104                                              opt_this) {
105      if (eventTypePredicate.call(opt_this, PowerSample))
106        this.samples_.forEach(callback, opt_this);
107    },
108
109    iterateAllChildEventContainers: function(callback, opt_this) {
110    }
111  };
112
113  return {
114    PowerSeries: PowerSeries
115  };
116});
117</script>
118