1<!DOCTYPE html>
2<!--
3Copyright 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/model/event_set.html">
9<link rel="import" href="/tracing/ui/base/table.html">
10<link rel="import" href="/tracing/value/unit.html">
11
12<polymer-element name="tr-ui-a-power-sample-summary-table">
13  <template>
14    <tr-ui-b-table id="table"></tr-ui-b-table>
15  </template>
16  <script>
17  'use strict';
18
19  Polymer({
20    ready: function() {
21      this.$.table.tableColumns = [
22        {
23          title: 'Min power',
24          width: '100px',
25          value: function(row) {
26            return tr.v.Unit.byName.powerInWatts.format(row.min / 1000.0);
27          }
28        },
29        {
30          title: 'Max power',
31          width: '100px',
32          value: function(row) {
33            return tr.v.Unit.byName.powerInWatts.format(row.max / 1000.0);
34          }
35        },
36        {
37          title: 'Time-weighted average',
38          width: '100px',
39          value: function(row) {
40            return tr.v.Unit.byName.powerInWatts.format(
41                row.timeWeightedAverage / 1000.0);
42          }
43        },
44        {
45          title: 'Energy consumed',
46          width: '100px',
47          value: function(row) {
48            return tr.v.Unit.byName.energyInJoules.format(row.energyConsumed);
49          }
50        },
51        {
52          title: 'Sample count',
53          width: '100%',
54          value: function(row) { return row.sampleCount; }
55        }
56      ];
57      this.samples = new tr.model.EventSet();
58    },
59
60    get samples() {
61      return this.samples_;
62    },
63
64    set samples(samples) {
65      if (samples === this.samples)
66        return;
67
68      this.samples_ =
69          (samples === undefined) ? new tr.model.EventSet() : samples;
70      this.updateContents_();
71    },
72
73    updateContents_: function() {
74      if (this.samples.length === 0) {
75        this.$.table.tableRows = [];
76      } else {
77        this.$.table.tableRows = [{
78          min: this.getMin(),
79          max: this.getMax(),
80          timeWeightedAverage: this.getTimeWeightedAverage(),
81          energyConsumed: this.getEnergyConsumed(),
82          sampleCount: this.samples.length
83        }];
84      }
85
86      this.$.table.rebuild();
87    },
88
89    getMin: function() {
90      return Math.min.apply(null, this.samples.map(function(sample) {
91        return sample.power;
92      }));
93    },
94
95    getMax: function() {
96      return Math.max.apply(null, this.samples.map(function(sample) {
97        return sample.power;
98      }));
99    },
100
101    /**
102     * Returns a time-weighted average of the power consumption in between the
103     * first sample (inclusive) and last sample (exclusive).
104     */
105    getTimeWeightedAverage: function() {
106      var energyConsumed = this.getEnergyConsumed();
107
108      if (energyConsumed === 'N/A')
109        return 'N/A';
110
111      // Multiply by 1000 to convert Joules to milliJoules.
112      var energyInMillijoules = this.getEnergyConsumed() * 1000;
113
114      // Divide by 1000 to convert milliseconds to seconds.
115      var durationInSeconds = this.samples.bounds.duration / 1000;
116
117      // Convert energy to power in milliwatts by dividing by time in seconds.
118      return energyInMillijoules / durationInSeconds;
119    },
120
121    getEnergyConsumed: function() {
122      if (this.samples.length < 2)
123        return 'N/A';
124
125      var bounds = this.samples.bounds;
126      return this.samples[0].series.getEnergyConsumed(bounds.min, bounds.max);
127    }
128  });
129  </script>
130</polymer-element>
131