display assigned ip's and interfaces on dashboard
[osm/vim-emu.git] / src / emuvim / dashboard / js / main.js
1 var API_HOST = "http://127.0.0.1:5001";
2 var ERROR_ALERT = false;
3 var TIMESTAMP = 0;
4 var CONNECTED = false;
5 var LATENESS_UPDATE_INTERVAL = 50;
6 var DATA_UPDATE_INTERVAL = 1000 * 10;
7 var LAST_UPDATE_TIMESTAMP_CONTAINER = 0;
8 var LAST_UPDATE_TIMESTAMP_DATACENTER = 0;
9
10
11 function update_lateness_loop() {
12 lateness_datacenter= (Date.now() - LAST_UPDATE_TIMESTAMP_DATACENTER) / 1000;
13 $("#lbl_lateness_datacenter").text("Lateness: " + Number(lateness_datacenter).toPrecision(3) + "s");
14 lateness_container= (Date.now() - LAST_UPDATE_TIMESTAMP_CONTAINER) / 1000;
15 $("#lbl_lateness_container").text("Lateness: " + Number(lateness_container).toPrecision(3) + "s");
16 // loop while connected
17 if(CONNECTED)
18 setTimeout(update_lateness_loop, LATENESS_UPDATE_INTERVAL)
19 }
20
21
22 function errorAjaxConnection()
23 {
24 // only do once
25 if(!ERROR_ALERT)
26 {
27 ERROR_ALERT = true;
28 // show message
29 alert("ERROR!\nAPI request failed.\n\n Please check the backend connection.", function() {
30 // callback
31 ERROR_ALERT = false;
32 });
33 }
34 }
35
36
37 function update_table_datacenter(data)
38 {
39 console.debug(data)
40 // clear table
41 $("#table_datacenter").empty();
42 // header
43 $("#table_datacenter").append('<tr class="tbl-head"><td>Label</td><td>Int. Name</td><td>Switch</td><td>Num. Containers</td><td>VNFs</td></tr>');
44 // fill table
45 $.each(data, function(i, item) {
46 var row_str = "";
47 row_str += '<tr class="tbl-row clickable_row" id="datacenter_row_' + i +'">';
48 row_str += '<td>' + item.label + '1</td>';
49 row_str += '<td>' + item.internalname + '</td>';
50 row_str += '<td>' + item.switch + '</td>';
51 row_str += '<td><span class="badge">' + item.n_running_containers + '</span></td>';
52 //row_str += '<td><span class="badge">' + Object.keys(item.metadata).length + '</span></td>';
53 row_str += '<td>' + item.vnf_list + '</span></td>';
54 row_str += '<tr>';
55 $("#table_datacenter").append(row_str);
56 });
57 $("#lbl_datacenter_count").text(data.length);
58 // update lateness counter
59 LAST_UPDATE_TIMESTAMP_DATACENTER = Date.now();
60 }
61
62
63 function update_table_container(data)
64 {
65 console.debug(data)
66 // clear table
67 $("#table_container").empty();
68 // header
69 $("#table_container").append('<tr class="tbl-head"><td>Datacenter</td><td>Container</td><td>Image</td><td>docker0</td><td>Networking [datacenter port | interface | ip]</td></tr>');
70 // fill table
71 $.each(data, function(i, item) {
72 var row_str = "";
73 row_str += '<tr class="tbl-row clickable_row" id="container_row_' + item[0] +'">';
74 row_str += '<td>' + item[1].datacenter + '</td>';
75 row_str += '<td>' + item[0] + '</td>';
76 row_str += '<td>' + item[1].image + '</td>';
77 row_str += '<td><code>' + item[1].docker_network + '</code></td>';
78 row_str += '<td><table class="interface_table" id="network_list_' + item[0] + '">';
79 //row_str += build_network_table(item[1].network, item[0]);
80 row_str += '</table></td>';
81 row_str += '</tr>';
82 $("#table_container").append(row_str);
83 build_network_table(item[1].network, item[0]);
84 });
85 $("#lbl_container_count").text(data.length);
86 // update lateness counter
87 LAST_UPDATE_TIMESTAMP_CONTAINER = Date.now();
88 }
89
90 function build_network_table(network_list, id)
91 {
92 console.debug('network list ' + id)
93 console.debug(network_list)
94 var row_str = "";
95 network_list.forEach(function(interface) {
96 row_str += '<tr class="interface_row">';
97 row_str += '<td class="interface_port">' + interface.dc_portname + '</td>';
98 row_str += '<td class="interface name">' + interface.intf_name + '</td>';
99 row_str += '<td class="interface_ip">' + interface.ip + '</td>';
100 row_str += '</tr>';
101 });
102 $("#network_list_" + id).append(row_str)
103 }
104
105 function fetch_datacenter()
106 {
107 // do HTTP request and trigger gui update on success
108 var request_url = API_HOST + "/restapi/datacenter";
109 console.debug("fetching from: " + request_url);
110 $.getJSON(request_url, update_table_datacenter);
111 }
112
113
114 function fetch_container()
115 {
116 // do HTTP request and trigger gui update on success
117 var request_url = API_HOST + "/restapi/compute";
118 console.debug("fetching from: " + request_url);
119 $.getJSON(request_url, update_table_container);
120 }
121
122
123 function fetch_d3graph()
124 {
125 // do HTTP request and trigger gui update on success
126 var request_url = API_HOST + "/restapi/network/d3jsgraph";
127 console.debug("fetching from: " + request_url);
128 //$.getJSON(request_url, update_graph);
129 }
130
131 function fetch_loop()
132 {
133 // only fetch if we are connected
134 if(!CONNECTED)
135 return;
136
137 // download data
138 fetch_datacenter();
139 fetch_container();
140
141 // loop while connected
142 if(CONNECTED)
143 setTimeout(fetch_loop, DATA_UPDATE_INTERVAL);
144 }
145
146
147 function connect()
148 {
149 console.info("connect()");
150 // get host address
151 API_HOST = "http://" + $("#text_api_host").val();
152 console.debug("API address: " + API_HOST);
153 // reset data
154 LAST_UPDATE_TIMESTAMP_DATACENTER = Date.now();
155 LAST_UPDATE_TIMESTAMP_CONTAINER = Date.now();
156 CONNECTED = true;
157 // restart lateness counter
158 update_lateness_loop();
159 // restart data fetch loop
160 fetch_loop();
161 // gui updates
162 $("#btn_disconnect").removeClass("disabled");
163 $("#btn_connect").addClass("disabled");
164 }
165
166 function disconnect()
167 {
168 console.info("disconnect()");
169 CONNECTED = false;
170 // gui updates
171 $("#btn_connect").removeClass("disabled");
172 $("#btn_disconnect").addClass("disabled");
173 }
174
175
176 $(document).ready(function(){
177 console.info("document ready");
178 // setup global connection error handling
179 /*
180 $.ajaxSetup({
181 "error": errorAjaxConnection
182 });
183
184 // add listeners
185 $("#btn_connect").click(connect);
186 $("#btn_disconnect").click(disconnect);
187 */
188 setTimeout(fetch_datacenter, 500);//fetch_datacenter();
189 setTimeout(fetch_container, 1000);//fetch_container();
190
191
192 // additional refresh on window focus
193 $(window).focus(function () {
194 if(CONNECTED)
195 {
196 fetch_datacenter();
197 fetch_container();
198 }
199 });
200
201 });