IPTraffic: fix sortCompare functions
[tomato.git] / release / src / router / www / ipt-daily.asp
blobbbe3cfc2cd641a58fc4eb3de82621453d2a53aed
1 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0//EN'>
2 <!--
3 Tomato GUI
4 Copyright (C) 2006-2010 Jonathan Zarate
5 http://www.polarcloud.com/tomato/
7 IP Traffic enhancements
8 Copyright (C) 2011 Augusto Bott
9 http://code.google.com/p/tomato-sdhc-vlan/
11 For use with Tomato Firmware only.
12 No part of this file may be used without permission.
13 -->
14 <html>
15 <head>
16 <meta http-equiv='content-type' content='text/html;charset=utf-8'>
17 <meta name='robots' content='noindex,nofollow'>
18 <title>[<% ident(); %>] IP Traffic: Daily History</title>
19 <link rel='stylesheet' type='text/css' href='tomato.css'>
20 <% css(); %>
21 <script type='text/javascript' src='tomato.js'></script>
23 <!-- / / / -->
25 <style type='text/css'>
26 #daily-grid .co3,
27 #daily-grid .co4,
28 #daily-grid .co5 {
29 text-align: right;
31 </style>
33 <!-- / / / -->
35 <script type='text/javascript' src='debug.js'></script>
36 <script type='text/javascript' src='bwm-hist.js'></script>
37 <script type='text/javascript' src='bwm-common.js'></script>
38 <script type='text/javascript' src='interfaces.js'></script>
40 <script type='text/javascript'>
41 // <% nvram("wan_ifname,lan_ifname,wl_ifname,wan_proto,wan_iface,web_svg,cstats_enable,cstats_colors,dhcpd_static,lan_ipaddr,lan_netmask,lan1_ipaddr,lan1_netmask,lan2_ipaddr,lan2_netmask,lan3_ipaddr,lan3_netmask"); %>
43 // <% devlist(); %>
45 try {
46 // <% ipt_bandwidth("daily"); %>
48 catch (ex) {
49 daily_history = [];
51 cstats_busy = 0;
52 if (typeof(daily_history) == 'undefined') {
53 daily_history = [];
54 cstats_busy = 1;
58 var filterip = [];
59 var filteripe = [];
60 var dateFormat = -1;
61 var scale = -1;
63 hostnamecache = [];
65 function genData() {
66 var w, i, h, t;
68 w = window.open('', 'tomato_ipt_data_d');
69 w.document.writeln('<pre>');
70 for (i = 0; i < daily_history.length; ++i) {
71 h = daily_history[i];
72 t = getYMD(h[0]);
73 w.document.writeln([t[0], t[1] + 1, t[2], h[1], h[2], h[3]].join(','));
75 w.document.writeln('</pre>');
76 w.document.close();
79 function getYMD(n) {
80 // [y,m,d]
81 return [(((n >> 16) & 0xFF) + 1900), ((n >>> 8) & 0xFF), (n & 0xFF)];
84 var dg = new TomatoGrid();
86 dg.setup = function() {
87 this.init('daily-grid', 'sort');
88 this.headerSet(['Date', 'Host', 'Download', 'Upload', 'Total']);
91 function redraw() {
92 var i, b, d;
93 var fskip;
94 var tx = 0;
95 var rx = 0;
96 var hostslisted = [];
97 var subnetslisted = [];
99 if (daily_history.length > 0) {
100 dg.removeAllData();
101 for (i = 0; i < daily_history.length; ++i) {
102 fskip=0;
103 b = daily_history[i];
105 if (E('_f_ignorezeroes').checked)
106 if ((b[2] < 1) || (b[3] < 1))
107 continue;
109 if (E('_f_begin_date').value.toString() != '0') {
110 if (b[0] < E('_f_begin_date').value)
111 continue;
114 if (E('_f_end_date').value.toString() != '0') {
115 if (b[0] > E('_f_end_date').value)
116 continue;
119 if (filteripe.length>0) {
120 fskip = 0;
121 for (var x = 0; x < filteripe.length; ++x) {
122 if (b[1] == filteripe[x]){
123 fskip=1;
124 break;
127 if (fskip == 1) continue;
130 if (filterip.length>0) {
131 fskip = 1;
132 for (var x = 0; x < filterip.length; ++x) {
133 if (b[1] == filterip[x]){
134 fskip=0;
135 break;
138 if (fskip == 1) continue;
141 if ((b[1] == getNetworkAddress(nvram.lan_ipaddr,nvram.lan_netmask)) ||
142 (b[1] == getNetworkAddress(nvram.lan1_ipaddr,nvram.lan1_netmask)) ||
143 (b[1] == getNetworkAddress(nvram.lan2_ipaddr,nvram.lan2_netmask)) ||
144 (b[1] == getNetworkAddress(nvram.lan3_ipaddr,nvram.lan3_netmask))) {
145 if(E('_f_subnet').checked == 0) {
146 continue;
147 } else {
148 subnetslisted.push(b[1]);
150 } else {
151 hostslisted.push(b[1]);
152 rx += b[2];
153 tx += b[3];
156 if (hostslisted.length > 0) {
157 hostslisted.sort();
158 for (var j = 1; j < hostslisted.length; j++ ) {
159 if (hostslisted[j] === hostslisted[j - 1]) {
160 hostslisted.splice(j--, 1);
165 if (subnetslisted.length > 0) {
166 subnetslisted.sort();
167 for (var j = 1; j < subnetslisted.length; j++ ) {
168 if (subnetslisted[j] === subnetslisted[j - 1]) {
169 subnetslisted.splice(j--, 1);
174 var h = b[1];
175 if (E('_f_hostnames').checked) {
176 if(hostnamecache[b[1]] != null) {
177 h = hostnamecache[b[1]] + ((b[1].indexOf(':') != -1) ? '<br>' : ' ') + '<small>(' + b[1] + ')</small>';
180 if (E('_f_shortcuts').checked) {
181 h = h + '<br><small>';
182 h = h + '<a href="javascript:viewQosDetail(' + i + ')" title="View QoS Details">[qosdetails]</a>';
183 h = h + '<a href="javascript:viewQosCTrates(' + i + ')" title="View transfer rates per connection">[qosrates]</a>';
184 h = h + '<a href="javascript:viewIptDetail(' + i + ')" title="View real-time IP Traffic for this address">[iptraf]</a>';
185 h = h + '<a href="javascript:addExcludeList(' + i + ')" title="Filter out this address">[hide]</a>';
186 h = h + '</small>';
188 var ymd = getYMD(b[0]);
189 d = [ymdText(ymd[0], ymd[1], ymd[2]), h, rescale(b[2]), rescale(b[3]), rescale(b[2]+b[3])];
190 dg.insertData(-1, d);
193 dg.resort();
194 dg.recolor();
195 dg.footerSet([
196 'Total',
197 ('<small><i>(' +
198 (((hostslisted.length > 0) || (subnetslisted.length > 0)) ?
199 ((hostslisted.length > 0) ? (hostslisted.length + ' hosts') : '') +
200 (((hostslisted.length > 0) && (subnetslisted.length > 0)) ? ', ' : '') +
201 ((subnetslisted.length > 0) ? (subnetslisted.length + ' subnets') : '')
202 : 'no data') +
203 ')</i></small>'),
204 rescale(rx),
205 rescale(tx),
206 rescale(rx+tx)]);
210 function popupWindow(v) {
211 window.open(v, '', 'width=1000,height=600,toolbar=no,menubar=no,scrollbars=yes,resizable=yes');
214 function viewQosDetail(n) {
215 cookie.set('qos_filterip', [daily_history[n][1]], 1);
216 popupWindow('qos-detailed.asp');
219 function viewQosCTrates(n) {
220 cookie.set('qos_filterip', [daily_history[n][1]], 1);
221 popupWindow('qos-ctrate.asp');
224 function viewIptDetail(n) {
225 cookie.set('ipt_filterip', [daily_history[n][1]], 1);
226 popupWindow('ipt-details.asp');
229 function addExcludeList(n) {
230 if (E('_f_filter_ipe').value.length<6) {
231 E('_f_filter_ipe').value = daily_history[n][1];
232 } else {
233 if (E('_f_filter_ipe').value.indexOf(daily_history[n][1]) < 0) {
234 E('_f_filter_ipe').value = E('_f_filter_ipe').value + ',' + daily_history[n][1];
237 dofilter();
240 dg.dataToView = function(data) {
241 return(data);
244 dg.sortCompare = function(a, b) {
245 var col = this.sortColumn;
246 var da = a.getRowData();
247 var db = b.getRowData();
248 var r = 0;
249 switch (col) {
250 case 0: // Date
251 case 1: // Hostname
252 r = cmpText(da[col], db[col]);
253 break;
254 case 2: // Download
255 case 3: // Upload
256 case 4: // Total
257 r = cmpFloat(parseFloat(da[col].replace(",","")), parseFloat(db[col].replace(",","")));
258 break;
260 return this.sortAscending ? r : -r;
263 function init_filter_dates() {
264 var dates = [];
265 if (daily_history.length > 0) {
266 for (var i = 0; i < daily_history.length; ++i) {
267 dates.push('0x' + daily_history[i][0].toString(16));
269 if (dates.length > 0) {
270 dates.sort();
271 for (var j = 1; j < dates.length; j++ ) {
272 if (dates[j] === dates[j - 1]) {
273 dates.splice(j--, 1);
278 var b = E('_f_begin_date');
279 var e = E('_f_end_date');
280 var d = new Date();
281 for (var i = 0; i < dates.length; ++i) {
282 var ymd = getYMD(dates[i]);
283 b.options[i+1] = new Option(ymdText(ymd[0], ymd[1], ymd[2]), dates[i], false, ((ymd[0]==d.getFullYear()) && (ymd[1]==d.getMonth()) && (ymd[2]==d.getDate()) ));
284 e.options[i+1] = new Option(ymdText(ymd[0], ymd[1], ymd[2]), dates[i], false, ((ymd[0]==d.getFullYear()) && (ymd[1]==d.getMonth()) && (ymd[2]==d.getDate()) ));
288 function update_filter_dates(b) {
289 for (var i = 0; i < b.options.length; ++i) {
290 if(b.options[i].value.toString() != '0') {
291 var ymd = getYMD(b.options[i].value);
292 b.options[i].text = ymdText(ymd[0], ymd[1], ymd[2]);
297 function init() {
299 if (nvram.cstats_enable != '1') return;
301 var c;
303 init_filter_dates();
305 populateCache();
307 if ((c = cookie.get('ipt_filterip')) != null) {
308 cookie.set('ipt_filterip', '', 0);
309 if (c.length>6) {
310 E('_f_begin_date').value = 0;
311 E('_f_end_date').value = 0;
312 E('_f_filter_ip').value = c;
313 filterip = c.split(',');
317 dateFormat = fixInt(cookie.get('ipt_history_dafm'), 0, 3, 0);
318 E('_f_dafm').value = dateFormat;
320 scale = fixInt(cookie.get('ipt_history_scale'), 0, 2, 0);
321 E('_f_scale').value = scale;
323 E('_f_subnet').checked = (((c = cookie.get('ipt_history_subnet')) != null) && (c == '1'));
325 E('_f_hostnames').checked = (((c = cookie.get('ipt_history_hostnames')) != null) && (c == '1'));
327 E('_f_shortcuts').checked = (((c = cookie.get('ipt_history_shortcuts')) != null) && (c == '1'));
329 E('_f_ignorezeroes').checked = (((c = cookie.get('ipt_history_ignorezeroes')) != null) && (c == '1'));
331 if (((c = cookie.get('ipt_history_options_vis')) != null) && (c == '1')) {
332 toggleVisibility("options");
335 dg.setup();
337 redraw();
340 function toggleVisibility(whichone) {
341 if(E('sesdiv' + whichone).style.display=='') {
342 E('sesdiv' + whichone).style.display='none';
343 E('sesdiv' + whichone + 'showhide').innerHTML='(Click here to show)';
344 cookie.set('ipt_history_' + whichone + '_vis', 0);
345 } else {
346 E('sesdiv' + whichone).style.display='';
347 E('sesdiv' + whichone + 'showhide').innerHTML='(Click here to hide)';
348 cookie.set('ipt_history_' + whichone + '_vis', 1);
352 function dofilter() {
353 if (E('_f_filter_ip').value.length>6) {
354 filterip = E('_f_filter_ip').value.split(',');
355 } else {
356 filterip = [];
359 if (E('_f_filter_ipe').value.length>6) {
360 filteripe = E('_f_filter_ipe').value.split(',');
361 } else {
362 filteripe = [];
365 redraw();
368 function verifyFields(focused, quiet) {
369 dateFormat = E('_f_dafm').value * 1;
370 cookie.set('ipt_history_dafm', E('_f_dafm').value, 31);
372 scale = E('_f_scale').value * 1;
373 cookie.set('ipt_history_scale', E('_f_scale').value, 2);
375 cookie.set('ipt_history_subnet', (E('_f_subnet').checked ? '1' : '0'), 1);
377 cookie.set('ipt_history_hostnames', (E('_f_hostnames').checked ? '1' : '0'), 1);
379 cookie.set('ipt_history_shortcuts', (E('_f_shortcuts').checked ? '1' : '0'), 1);
381 cookie.set('ipt_history_ignorezeroes', (E('_f_ignorezeroes').checked ? '1' : '0'), 1);
383 update_filter_dates(E('_f_begin_date'));
384 update_filter_dates(E('_f_end_date'));
386 if ((E('_f_begin_date').value > E('_f_end_date').value) && ( E('_f_end_date').value != '0')) {
387 var tmp = E('_f_begin_date').value;
388 E('_f_begin_date').value = E('_f_end_date').value;
389 E('_f_end_date').value = tmp;
392 dofilter();
393 return 1;
395 </script>
396 </head>
397 <body onload='init()'>
398 <form>
399 <table id='container' cellspacing=0>
400 <tr><td colspan=2 id='header'>
401 <div class='title'>Tomato</div>
402 <div class='version'>Version <% version(); %></div>
403 </td></tr>
404 <tr id='body'><td id='navi'><script type='text/javascript'>navi()</script></td>
405 <td id='content'>
406 <div id='ident'><% ident(); %></div>
408 <!-- / / / -->
410 <div id='cstats'>
411 <div class='section-title'>IP Traffic Daily History</div>
412 <div class='section'>
413 <table id='daily-grid' class='tomato-grid' cellspacing=0 style='height:auto'></table>
414 </div>
416 <div class='section-title'>Options <small><i><a href='javascript:toggleVisibility("options");'><span id='sesdivoptionsshowhide'>(Click here to show)</span></a></i></small></div>
417 <div class='section' id='sesdivoptions' style='display:none'>
418 <script type='text/javascript'>
419 var c;
420 c = [];
421 c.push({ title: 'List only these IPs', name: 'f_filter_ip', size: 50, maxlen: 255, type: 'text', suffix: ' <small>(Comma separated list)</small>' });
422 c.push({ title: 'Exclude these IPs', name: 'f_filter_ipe', size: 50, maxlen: 255, type: 'text', suffix: ' <small>(Comma separated list)</small>' });
423 c.push({ title: 'Date Range', multi: [ { name: 'f_begin_date', type: 'select', options: [['0', 'Any']], suffix: ' - ' }, { name: 'f_end_date', type: 'select', options: [['0', 'Any']] } ] } );
424 c.push({ title: 'Date Format', name: 'f_dafm', type: 'select', options: [['0', 'yyyy-mm-dd'], ['1', 'mm-dd-yyyy'], ['2', 'mmm dd, yyyy'], ['3', 'dd.mm.yyyy']] });
425 c.push({ title: 'Scale', name: 'f_scale', type: 'select', options: [['0', 'KB'], ['1', 'MB'], ['2', 'GB']] });
426 c.push({ title: 'Show subnet totals', name: 'f_subnet', type: 'checkbox', suffix: ' <small>(Not considered when calculating total traffic on the last line)</small>' });
427 c.push({ title: 'Hide IPs without traffic', name: 'f_ignorezeroes', type: 'checkbox' });
428 c.push({ title: 'Show known hostnames', name: 'f_hostnames', type: 'checkbox' });
429 c.push({ title: 'Show shortcuts', name: 'f_shortcuts', type: 'checkbox' });
430 createFieldTable('',c);
431 </script>
432 <div style="float:right;text-align:right">
433 &raquo; <a href="javascript:genData()">Data</a>
434 <br>
435 &raquo; <a href="admin-iptraffic.asp">Configure</a>
436 </div>
437 </div>
438 </div>
440 <script type='text/javascript'>checkCstats();</script>
442 <!-- / / / -->
444 </td></tr>
445 <tr><td id='footer' colspan=2>
446 <input type='button' value='Refresh' onclick='reloadPage()'>
447 </td></tr>
448 </table>
449 </form>
450 </body>
451 </html>