forked from catapult-project/catapult
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bandwidth_view.js
364 lines (315 loc) · 12.1 KB
/
bandwidth_view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/** This view displays summary statistics on bandwidth usage. */
var BandwidthView = (function() {
'use strict';
// We inherit from DivView.
var superClass = DivView;
/**
* @constructor
*/
function BandwidthView() {
assertFirstConstructorCall(BandwidthView);
// Call superclass's constructor.
superClass.call(this, BandwidthView.MAIN_BOX_ID);
g_browser.addSessionNetworkStatsObserver(this, true);
g_browser.addHistoricNetworkStatsObserver(this, true);
// Register to receive data reduction proxy info.
g_browser.addDataReductionProxyInfoObserver(this, true);
// Register to receive bad proxy info.
g_browser.addBadProxiesObserver(this, true);
this.sessionNetworkStats_ = null;
this.historicNetworkStats_ = null;
}
BandwidthView.TAB_ID = 'tab-handle-bandwidth';
BandwidthView.TAB_NAME = 'Bandwidth';
BandwidthView.TAB_HASH = '#bandwidth';
// IDs for special HTML elements in bandwidth_view.html
BandwidthView.MAIN_BOX_ID = 'bandwidth-view-tab-content';
BandwidthView.ENABLED_ID = 'data-reduction-proxy-enabled';
BandwidthView.PROXY_CONFIG_ID = 'data-reduction-proxy-config';
BandwidthView.PROBE_STATUS_ID = 'data-reduction-proxy-probe-status';
BandwidthView.BYPASS_STATE_CONTAINER_ID =
'data-reduction-proxy-bypass-state-container';
BandwidthView.BYPASS_STATE_ID = 'data-reduction-proxy-bypass-state-details';
BandwidthView.EVENTS_TBODY_ID = 'data-reduction-proxy-view-events-tbody';
BandwidthView.EVENTS_UL = 'data-reduction-proxy-view-events-list';
BandwidthView.STATS_BOX_ID = 'bandwidth-stats-table';
BandwidthView.SAVINGS_TBODY_ID = 'bandwidth-savings-tbody';
cr.addSingletonGetter(BandwidthView);
BandwidthView.prototype = {
// Inherit the superclass's methods.
__proto__: superClass.prototype,
data_reduction_proxy_config_: null,
last_bypass_: null,
bad_proxy_config_: null,
onLoadLogFinish: function(data) {
return this.onBadProxiesChanged(data.badProxies) &&
this.onDataReductionProxyInfoChanged(data.dataReductionProxyInfo) &&
(this.onSessionNetworkStatsChanged(data.sessionNetworkStats) ||
this.onHistoricNetworkStatsChanged(data.historicNetworkStats));
},
/**
* Retains information on bandwidth usage this session.
*/
onSessionNetworkStatsChanged: function(sessionNetworkStats) {
this.sessionNetworkStats_ = sessionNetworkStats;
return this.updateBandwidthUsageTable_();
},
/**
* Displays information on bandwidth usage this session and over the
* browser's lifetime.
*/
onHistoricNetworkStatsChanged: function(historicNetworkStats) {
this.historicNetworkStats_ = historicNetworkStats;
return this.updateBandwidthUsageTable_();
},
/**
* Updates the UI based on receiving changes in information about the
* data reduction proxy summary.
*/
onDataReductionProxyInfoChanged: function(info) {
$(BandwidthView.EVENTS_TBODY_ID).innerHTML = '';
if (!info)
return false;
if (info.enabled) {
$(BandwidthView.ENABLED_ID).innerText = 'Enabled';
$(BandwidthView.PROBE_STATUS_ID).innerText =
info.probe != null ? info.probe : 'N/A';
this.last_bypass_ = info.last_bypass;
this.data_reduction_proxy_config_ = info.proxy_config.params;
} else {
$(BandwidthView.ENABLED_ID).innerText = 'Disabled';
$(BandwidthView.PROBE_STATUS_ID).innerText = 'N/A';
this.data_reduction_proxy_config_ = null;
}
this.updateDataReductionProxyConfig_();
for (var eventIndex = info.events.length - 1; eventIndex >= 0;
--eventIndex) {
var event = info.events[eventIndex];
var headerRow = addNode($(BandwidthView.EVENTS_TBODY_ID), 'tr');
var detailsRow = addNode($(BandwidthView.EVENTS_TBODY_ID), 'tr');
var timeCell = addNode(headerRow, 'td');
var actionCell = addNode(headerRow, 'td');
var detailsCell = addNode(detailsRow, 'td');
detailsCell.colSpan = 2;
detailsCell.className = 'data-reduction-proxy-view-events-details';
var eventTime = timeutil.convertTimeTicksToDate(event.time);
timeutil.addNodeWithDate(timeCell, eventTime);
this.buildEventRow_(event, actionCell, detailsCell);
}
return true;
},
/**
* Updates the UI based on receiving changes in information about bad
* proxy servers.
*/
onBadProxiesChanged: function(badProxies) {
if (!badProxies)
return false;
var newBadProxies = [];
if (badProxies.length == 0) {
this.last_bypass_ = null;
} else {
for (var i = 0; i < badProxies.length; ++i) {
var entry = badProxies[i];
newBadProxies[entry.proxy_uri] = entry.bad_until;
}
}
this.bad_proxy_config_ = newBadProxies;
this.updateDataReductionProxyConfig_();
return true;
},
/**
* Update the bandwidth usage table. Returns false on failure.
*/
updateBandwidthUsageTable_: function() {
var sessionNetworkStats = this.sessionNetworkStats_;
var historicNetworkStats = this.historicNetworkStats_;
if (!sessionNetworkStats || !historicNetworkStats)
return false;
var sessionOriginal = sessionNetworkStats.session_original_content_length;
var sessionReceived = sessionNetworkStats.session_received_content_length;
var historicOriginal =
historicNetworkStats.historic_original_content_length;
var historicReceived =
historicNetworkStats.historic_received_content_length;
var rows = [];
rows.push({
title: 'Original (KB)',
sessionValue: bytesToRoundedKilobytes_(sessionOriginal),
historicValue: bytesToRoundedKilobytes_(historicOriginal)
});
rows.push({
title: 'Received (KB)',
sessionValue: bytesToRoundedKilobytes_(sessionReceived),
historicValue: bytesToRoundedKilobytes_(historicReceived)
});
rows.push({
title: 'Savings (KB)',
sessionValue:
bytesToRoundedKilobytes_(sessionOriginal - sessionReceived),
historicValue:
bytesToRoundedKilobytes_(historicOriginal - historicReceived)
});
rows.push({
title: 'Savings (%)',
sessionValue: getPercentSavings_(sessionOriginal, sessionReceived),
historicValue: getPercentSavings_(historicOriginal, historicReceived)
});
var tbody = $(BandwidthView.SAVINGS_TBODY_ID);
tbody.innerHTML = '';
// Fill in the bandwidth savings table.
for (var i = 0; i < rows.length; ++i) {
var r = rows[i];
var tr = addNode(tbody, 'tr');
addNodeWithText(tr, 'td', r.title);
addNodeWithText(tr, 'td', r.sessionValue);
addNodeWithText(tr, 'td', r.historicValue);
}
return true;
},
/**
* Renders a Data Reduction Proxy event into the event tbody
*/
buildEventRow_: function(event, actionCell, detailsCell) {
if (event.type == EventType.DATA_REDUCTION_PROXY_ENABLED &&
event.params.enabled == 0) {
addTextNode(actionCell, 'DISABLED');
} else {
var actionText =
EventTypeNames[event.type].replace('DATA_REDUCTION_PROXY_', '');
if (event.phase == EventPhase.PHASE_BEGIN ||
event.phase == EventPhase.PHASE_END) {
actionText = actionText + ' (' +
getKeyWithValue(EventPhase, event.phase).replace('PHASE_', '') +
')';
}
addTextNode(actionCell, actionText);
this.createEventTable_(event.params, detailsCell);
}
},
/**
* Updates the data reduction proxy summary block.
*/
updateDataReductionProxyConfig_: function() {
$(BandwidthView.PROXY_CONFIG_ID).innerHTML = '';
$(BandwidthView.BYPASS_STATE_ID).innerHTML = '';
setNodeDisplay($(BandwidthView.BYPASS_STATE_CONTAINER_ID), false);
if (this.data_reduction_proxy_config_) {
var hasBypassedProxy = false;
var now = timeutil.getCurrentTimeTicks();
if (this.last_bypass_ &&
this.hasTimePassedLogTime_(+this.last_bypass_.params.expiration)) {
// Best effort on iterating the config to search for a bad proxy.
// A server could exist in a string member of
// data_reduction_proxy_config_ or within an array of servers in an
// array member of data_reduction_proxy_config_. As such, search
// through all string members and string arrays.
for (var key in this.data_reduction_proxy_config_) {
var value = this.data_reduction_proxy_config_[key];
if (typeof value == 'string') {
if (this.isMarkedAsBad_(value)) {
hasBypassedProxy = true;
break;
}
} else if (value instanceof Array) {
for (var index = 1; index < value.length; index++) {
if (this.isMarkedAsBad_(value[index])) {
hasBypassedProxy = true;
}
}
if (hasBypassedProxy) {
break;
}
}
}
}
if (hasBypassedProxy) {
this.createEventTable_(
this.last_bypass_.params, $(BandwidthView.BYPASS_STATE_ID));
}
this.createEventTable_(
this.data_reduction_proxy_config_,
$(BandwidthView.PROXY_CONFIG_ID));
setNodeDisplay(
$(BandwidthView.BYPASS_STATE_CONTAINER_ID), hasBypassedProxy);
}
},
/**
* Checks to see if a proxy server is in marked as bad.
*/
isMarkedAsBad_: function(proxy) {
for (var entry in this.bad_proxy_config_) {
if (entry == proxy &&
this.hasTimePassedLogTime_(this.bad_proxy_config_[entry])) {
return true;
}
}
return false;
},
/**
* Checks to see if a given time in ticks has passed the time of the
* the log. For real time viewing, this is "now", but for loaded logs, it
* is the time at which the logs were taken.
*/
hasTimePassedLogTime_: function(timeTicks) {
var logTime;
if (MainView.isViewingLoadedLog() && ClientInfo.numericDate) {
logTime = ClientInfo.numericDate;
} else {
logTime = timeutil.getCurrentTime();
}
return timeutil.convertTimeTicksToTime(timeTicks) > logTime;
},
/**
* Creates a table of the object obj. Certain keys are special cased for
* ease of readability.
*/
createEventTable_: function(obj, parentNode) {
if (Object.keys(obj).length > 0) {
var tableNode = addNode(parentNode, 'table');
tableNode.className = 'borderless-table';
for (var key in obj) {
var value = obj[key];
if (value != null && value.toString() != '') {
if (key == 'net_error') {
if (value == 0) {
value = 'OK';
} else {
value = netErrorToString(value);
}
} else if (key == 'bypass_type') {
value = getKeyWithValue(DataReductionProxyBypassEventType, value);
} else if (key == 'bypass_action_type') {
value =
getKeyWithValue(DataReductionProxyBypassActionType, value);
} else if (key == 'expiration') {
value = timeutil.convertTimeTicksToDate(value);
}
var tableRow = addNode(tableNode, 'tr');
addNodeWithText(tableRow, 'td', key);
addNodeWithText(tableRow, 'td', value);
}
}
}
}
};
/**
* Converts bytes to kilobytes rounded to one decimal place.
*/
function bytesToRoundedKilobytes_(val) {
return (val / 1024).toFixed(1);
}
/**
* Returns bandwidth savings as a percent rounded to one decimal place.
*/
function getPercentSavings_(original, received) {
if (original > 0) {
return ((original - received) * 100 / original).toFixed(1);
}
return '0.0';
}
return BandwidthView;
})();