41f0c7bcba452cdff8c409aff7a757706ff4eedd
[VSoRC/.git] / js / port-stats.js
1 /*
2  * Copyright (C) 2014 SDN Hub
3  *
4  * Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.
5  * You may not use this file except in compliance with this License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.gnu.org/licenses/gpl-3.0.txt
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13  * implied.
14  */
15
16 var url = "http://" + location.hostname + ":8080";
17
18 function updatePortStats() {
19     var statsTableBody = document.getElementById('port-stats-data');
20     while (statsTableBody.firstChild) {
21             statsTableBody.removeChild(statsTableBody.firstChild);
22     }
23
24     $.getJSON(url.concat("/stats/switches"), function(switches){
25         $.each(switches, function(index, dpid){
26             var hex_dpid = parseInt(dpid).toString(16);
27
28             $.getJSON(url.concat("/stats/port/").concat(dpid), function(ports) {
29                 var portStats = ports[dpid];
30
31                 var tr = document.createElement('TR');
32                 var physicalPorts = 0;
33                 var switchColTd = document.createElement('TD');
34                 switchColTd.appendChild(document.createTextNode(hex_dpid));
35                 tr.appendChild(switchColTd);
36
37                 $.each(portStats, function(index, obj) {
38                     if (obj.port_no < 65280) {
39                         physicalPorts += 1;
40                         var statsArray = new Array(obj.port_no, obj.rx_packets, obj.rx_bytes, obj.rx_dropped, obj.rx_errors, obj.tx_packets, obj.tx_bytes, obj.tx_dropped, obj.tx_errors);
41
42                         $.each(statsArray, function(index, value) {
43                             var td = document.createElement('TD');
44                             td.appendChild(document.createTextNode(value));
45                             tr.appendChild(td);
46                         });
47                         statsTableBody.appendChild(tr);
48                         tr = document.createElement('TR');
49                     }
50                 });
51
52                 switchColTd.rowSpan = physicalPorts;
53             });
54         });
55     });
56 }
57
58 updatePortStats();
59
60 var portStatsIntervalID = setInterval(function(){updatePortStats()}, 5000);
61
62 function stopPortStatsTableRefresh() {
63     clearInterval(portStatsIntervalID);
64 }