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.
7 * Maintains the stats table.
8 * @param {SsrcInfoManager} ssrcInfoManager The source of the ssrc info.
10 var StatsTable
= (function(ssrcInfoManager
) {
14 * @param {SsrcInfoManager} ssrcInfoManager The source of the ssrc info.
17 function StatsTable(ssrcInfoManager
) {
19 * @type {SsrcInfoManager}
22 this.ssrcInfoManager_
= ssrcInfoManager
;
25 StatsTable
.prototype = {
27 * Adds |report| to the stats table of |peerConnectionElement|.
29 * @param {!Element} peerConnectionElement The root element.
30 * @param {!Object} report The object containing stats, which is the object
31 * containing timestamp and values, which is an array of strings, whose
32 * even index entry is the name of the stat, and the odd index entry is
35 addStatsReport: function(peerConnectionElement
, report
) {
36 var statsTable
= this.ensureStatsTable_(peerConnectionElement
, report
);
39 this.addStatsToTable_(statsTable
,
40 report
.stats
.timestamp
, report
.stats
.values
);
45 * Ensure the DIV container for the stats tables is created as a child of
46 * |peerConnectionElement|.
48 * @param {!Element} peerConnectionElement The root element.
49 * @return {!Element} The stats table container.
52 ensureStatsTableContainer_: function(peerConnectionElement
) {
53 var containerId
= peerConnectionElement
.id
+ '-table-container';
54 var container
= $(containerId
);
56 container
= document
.createElement('div');
57 container
.id
= containerId
;
58 container
.className
= 'stats-table-container';
59 var head
= document
.createElement('div');
60 head
.textContent
= 'Stats Tables';
61 container
.appendChild(head
);
62 peerConnectionElement
.appendChild(container
);
68 * Ensure the stats table for track specified by |report| of PeerConnection
69 * |peerConnectionElement| is created.
71 * @param {!Element} peerConnectionElement The root element.
72 * @param {!Object} report The object containing stats, which is the object
73 * containing timestamp and values, which is an array of strings, whose
74 * even index entry is the name of the stat, and the odd index entry is
76 * @return {!Element} The stats table element.
79 ensureStatsTable_: function(peerConnectionElement
, report
) {
80 var tableId
= peerConnectionElement
.id
+ '-table-' + report
.id
;
81 var table
= $(tableId
);
83 var container
= this.ensureStatsTableContainer_(peerConnectionElement
);
84 var details
= document
.createElement('details');
85 container
.appendChild(details
);
87 var summary
= document
.createElement('summary');
88 summary
.textContent
= report
.id
;
89 details
.appendChild(summary
);
91 table
= document
.createElement('table');
92 details
.appendChild(table
);
96 table
.innerHTML
= '<tr><th colspan=2></th></tr>';
97 table
.rows
[0].cells
[0].textContent
= 'Statistics ' + report
.id
;
98 if (report
.type
== 'ssrc') {
100 table
.rows
[1].innerHTML
= '<td colspan=2></td>';
101 this.ssrcInfoManager_
.populateSsrcInfo(
102 table
.rows
[1].cells
[0], GetSsrcFromReport(report
));
109 * Update |statsTable| with |time| and |statsData|.
111 * @param {!Element} statsTable Which table to update.
112 * @param {number} time The number of miliseconds since epoch.
113 * @param {Array.<string>} statsData An array of stats name and value pairs.
116 addStatsToTable_: function(statsTable
, time
, statsData
) {
117 var date
= Date(time
);
118 this.updateStatsTableRow_(statsTable
, 'timestamp', date
.toLocaleString());
119 for (var i
= 0; i
< statsData
.length
- 1; i
= i
+ 2) {
120 this.updateStatsTableRow_(statsTable
, statsData
[i
], statsData
[i
+ 1]);
125 * Update the value column of the stats row of |rowName| to |value|.
126 * A new row is created is this is the first report of this stats.
128 * @param {!Element} statsTable Which table to update.
129 * @param {string} rowName The name of the row to update.
130 * @param {string} value The new value to set.
133 updateStatsTableRow_: function(statsTable
, rowName
, value
) {
134 var trId
= statsTable
.id
+ '-' + rowName
;
135 var trElement
= $(trId
);
137 trElement
= document
.createElement('tr');
139 statsTable
.firstChild
.appendChild(trElement
);
140 trElement
.innerHTML
= '<td>' + rowName
+ '</td><td></td>';
142 trElement
.cells
[1].textContent
= value
;
144 // Highlights the table for the active connection.
145 if (rowName
== 'googActiveConnection' && value
== 'true')
146 statsTable
.parentElement
.classList
.add('stats-table-active-connection');