| peusterm | dd55951 | 2017-09-21 16:29:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2017 SONATA-NFV and Paderborn University |
| 3 | ALL RIGHTS RESERVED. |
| 4 | |
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | |
| 17 | Neither the name of the SONATA-NFV, Paderborn University |
| 18 | nor the names of its contributors may be used to endorse or promote |
| 19 | products derived from this software without specific prior written |
| 20 | permission. |
| 21 | |
| 22 | This work has been performed in the framework of the SONATA project, |
| 23 | funded by the European Commission under Grant number 671517 through |
| 24 | the Horizon 2020 and 5G-PPP programmes. The authors would like to |
| 25 | acknowledge the contributions of their colleagues of the SONATA |
| 26 | partner consortium (www.sonata-nfv.eu). |
| 27 | */ |
| peusterm | d874002 | 2019-06-18 16:08:47 +0200 | [diff] [blame] | 28 | var API_HOST = ""; // set to a remote url if dashboard is not served by REST API server |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 29 | var ERROR_ALERT = false; |
| peusterm | 0289b60 | 2017-01-11 19:16:28 +0100 | [diff] [blame] | 30 | var TIMESTAMP = 0; |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 31 | var CONNECTED = false; |
| 32 | var LATENESS_UPDATE_INTERVAL = 50; |
| peusterm | 0719f4e | 2019-06-18 16:55:26 +0200 | [diff] [blame] | 33 | var DATA_UPDATE_INTERVAL = 1000 * 10; // 30 seconds |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 34 | var LAST_UPDATE_TIMESTAMP_CONTAINER = 0; |
| 35 | var LAST_UPDATE_TIMESTAMP_DATACENTER = 0; |
| peusterm | 0289b60 | 2017-01-11 19:16:28 +0100 | [diff] [blame] | 36 | |
| 37 | |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 38 | function update_lateness_loop() { |
| 39 | lateness_datacenter= (Date.now() - LAST_UPDATE_TIMESTAMP_DATACENTER) / 1000; |
| peusterm | d874002 | 2019-06-18 16:08:47 +0200 | [diff] [blame] | 40 | $("#lbl_lateness_datacenter").text("Lateness: " + Number(lateness_datacenter).toPrecision(2) + "s"); |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 41 | lateness_container= (Date.now() - LAST_UPDATE_TIMESTAMP_CONTAINER) / 1000; |
| peusterm | d874002 | 2019-06-18 16:08:47 +0200 | [diff] [blame] | 42 | $("#lbl_lateness_container").text("Lateness: " + Number(lateness_container).toPrecision(2) + "s"); |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 43 | // loop while connected |
| 44 | if(CONNECTED) |
| 45 | setTimeout(update_lateness_loop, LATENESS_UPDATE_INTERVAL) |
| peusterm | 0289b60 | 2017-01-11 19:16:28 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| peusterm | 0289b60 | 2017-01-11 19:16:28 +0100 | [diff] [blame] | 48 | |
| 49 | function errorAjaxConnection() |
| 50 | { |
| 51 | // only do once |
| 52 | if(!ERROR_ALERT) |
| 53 | { |
| 54 | ERROR_ALERT = true; |
| 55 | // show message |
| peusterm | d874002 | 2019-06-18 16:08:47 +0200 | [diff] [blame] | 56 | //alert("API request failed. Is the emulator running?", function() { |
| 57 | // // callback |
| 58 | // ERROR_ALERT = false; |
| 59 | //}); |
| peusterm | 0289b60 | 2017-01-11 19:16:28 +0100 | [diff] [blame] | 60 | } |
| peusterm | d874002 | 2019-06-18 16:08:47 +0200 | [diff] [blame] | 61 | CONNECTED = false; |
| 62 | console.error("API request failed. Is the emulator running?") |
| peusterm | 0289b60 | 2017-01-11 19:16:28 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 65 | |
| 66 | function update_table_datacenter(data) |
| peusterm | 0289b60 | 2017-01-11 19:16:28 +0100 | [diff] [blame] | 67 | { |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 68 | console.debug(data) |
| 69 | // clear table |
| 70 | $("#table_datacenter").empty(); |
| 71 | // header |
| stevenvanrossem | ba51a81 | 2017-04-23 01:22:59 +0200 | [diff] [blame] | 72 | $("#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>'); |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 73 | // fill table |
| 74 | $.each(data, function(i, item) { |
| 75 | var row_str = ""; |
| 76 | row_str += '<tr class="tbl-row clickable_row" id="datacenter_row_' + i +'">'; |
| peusterm | d874002 | 2019-06-18 16:08:47 +0200 | [diff] [blame] | 77 | row_str += '<td>' + item.label + '</td>'; |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 78 | row_str += '<td>' + item.internalname + '</td>'; |
| 79 | row_str += '<td>' + item.switch + '</td>'; |
| 80 | row_str += '<td><span class="badge">' + item.n_running_containers + '</span></td>'; |
| stevenvanrossem | ba51a81 | 2017-04-23 01:22:59 +0200 | [diff] [blame] | 81 | //row_str += '<td><span class="badge">' + Object.keys(item.metadata).length + '</span></td>'; |
| 82 | row_str += '<td>' + item.vnf_list + '</span></td>'; |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 83 | row_str += '<tr>'; |
| 84 | $("#table_datacenter").append(row_str); |
| 85 | }); |
| 86 | $("#lbl_datacenter_count").text(data.length); |
| 87 | // update lateness counter |
| 88 | LAST_UPDATE_TIMESTAMP_DATACENTER = Date.now(); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | function update_table_container(data) |
| 93 | { |
| 94 | console.debug(data) |
| 95 | // clear table |
| 96 | $("#table_container").empty(); |
| 97 | // header |
| peusterm | d874002 | 2019-06-18 16:08:47 +0200 | [diff] [blame] | 98 | $("#table_container").append('<tr class="tbl-head"><td>Datacenter</td><td>Container</td><td>Image</td><td>docker0</td><td>Status</td></tr>'); |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 99 | // fill table |
| 100 | $.each(data, function(i, item) { |
| 101 | var row_str = ""; |
| peusterm | d874002 | 2019-06-18 16:08:47 +0200 | [diff] [blame] | 102 | row_str += '<tr class="tbl-row clickable_row" id="container_row_' + i +'">'; |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 103 | row_str += '<td>' + item[1].datacenter + '</td>'; |
| 104 | row_str += '<td>' + item[0] + '</td>'; |
| 105 | row_str += '<td>' + item[1].image + '</td>'; |
| peusterm | d874002 | 2019-06-18 16:08:47 +0200 | [diff] [blame] | 106 | row_str += '<td><code>' + item[1].docker_network + '<code></td>'; |
| 107 | if(item[1].state.Status == "running") |
| 108 | row_str += '<td><span class="label label-success">running</span></td>'; |
| 109 | else |
| 110 | row_str += '<td><span class="label label-danger">stopped</span></td>'; |
| 111 | row_str += '<tr>'; |
| 112 | $("#table_container").append(row_str); |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 113 | }); |
| 114 | $("#lbl_container_count").text(data.length); |
| 115 | // update lateness counter |
| 116 | LAST_UPDATE_TIMESTAMP_CONTAINER = Date.now(); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | function fetch_datacenter() |
| 121 | { |
| 122 | // do HTTP request and trigger gui update on success |
| 123 | var request_url = API_HOST + "/restapi/datacenter"; |
| 124 | console.debug("fetching from: " + request_url); |
| 125 | $.getJSON(request_url, update_table_datacenter); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | function fetch_container() |
| 130 | { |
| 131 | // do HTTP request and trigger gui update on success |
| 132 | var request_url = API_HOST + "/restapi/compute"; |
| 133 | console.debug("fetching from: " + request_url); |
| 134 | $.getJSON(request_url, update_table_container); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | function fetch_loop() |
| 139 | { |
| 140 | // only fetch if we are connected |
| 141 | if(!CONNECTED) |
| 142 | return; |
| 143 | |
| 144 | // download data |
| 145 | fetch_datacenter(); |
| 146 | fetch_container(); |
| 147 | |
| 148 | // loop while connected |
| 149 | if(CONNECTED) |
| 150 | setTimeout(fetch_loop, DATA_UPDATE_INTERVAL); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | function connect() |
| 155 | { |
| 156 | console.info("connect()"); |
| 157 | // get host address |
| peusterm | d874002 | 2019-06-18 16:08:47 +0200 | [diff] [blame] | 158 | //API_HOST = "http://" + $("#text_api_host").val(); |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 159 | console.debug("API address: " + API_HOST); |
| 160 | // reset data |
| 161 | LAST_UPDATE_TIMESTAMP_DATACENTER = Date.now(); |
| 162 | LAST_UPDATE_TIMESTAMP_CONTAINER = Date.now(); |
| 163 | CONNECTED = true; |
| 164 | // restart lateness counter |
| 165 | update_lateness_loop(); |
| 166 | // restart data fetch loop |
| 167 | fetch_loop(); |
| peusterm | 0289b60 | 2017-01-11 19:16:28 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | |
| 171 | $(document).ready(function(){ |
| 172 | console.info("document ready"); |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 173 | // setup global connection error handling |
| peusterm | d874002 | 2019-06-18 16:08:47 +0200 | [diff] [blame] | 174 | |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 175 | $.ajaxSetup({ |
| 176 | "error": errorAjaxConnection |
| 177 | }); |
| peusterm | 0289b60 | 2017-01-11 19:16:28 +0100 | [diff] [blame] | 178 | |
| peusterm | d874002 | 2019-06-18 16:08:47 +0200 | [diff] [blame] | 179 | // connect |
| 180 | connect(); |
| stevenvanrossem | ba51a81 | 2017-04-23 01:22:59 +0200 | [diff] [blame] | 181 | |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 182 | // additional refresh on window focus |
| peusterm | 0289b60 | 2017-01-11 19:16:28 +0100 | [diff] [blame] | 183 | $(window).focus(function () { |
| peusterm | f14b02a | 2017-01-12 19:08:34 +0100 | [diff] [blame] | 184 | if(CONNECTED) |
| 185 | { |
| 186 | fetch_datacenter(); |
| 187 | fetch_container(); |
| 188 | } |
| peusterm | 0289b60 | 2017-01-11 19:16:28 +0100 | [diff] [blame] | 189 | }); |
| 190 | |
| 191 | }); |