Merge "Updated function test env with new tests and features"
[nonrtric.git] / test / simulator-group / sim-monitor.js
index da71a9e..d56ad7f 100644 (file)
@@ -30,7 +30,7 @@ var express = require('express');
 var app = express();
 var fieldSize=32;
 
-
+var flagstore={}
 
 //I am alive
 app.get("/",function(req, res){
@@ -40,26 +40,32 @@ app.get("/",function(req, res){
 //Get parameter valuue from other server
 function getSimCtr(url, index, cb) {
     var data = '';
-       http.get(url, (resp) => {
-               // A chunk of data has been recieved.
-               resp.on('data', (chunk) => {
-               data += chunk;
-               });
-
-               // The whole response has been received.
-               resp.on('end', () => {
-            var code=resp.statusCode
-            if (code > 199 && code < 300) {
-                cb(data, index);
-            } else {
-                cb("not found", index);
-            }
-               });
 
-       }).on("error", (err) => {
-               console.log("Error: " + err.message);
-               cb("no response", index);
-       });
+    console.log("URL: "+ url + " - ")
+    try {
+        http.get(url, (resp) => {
+            // A chunk of data has been recieved.
+            resp.on('data', (chunk) => {
+                data += chunk;
+            });
+
+            // The whole response has been received.
+            resp.on('end', () => {
+                var code=resp.statusCode
+                if (code > 199 && code < 300) {
+                    cb(data, index);
+                } else {
+                    cb("not found", index);
+                }
+            });
+
+        }).on("error", (err) => {
+            console.log("Error: " + err.message);
+            cb("no response", index);
+        });
+    } catch(err) {
+        cb("no response", index);
+    }
 };
 
 
@@ -120,6 +126,34 @@ function padding(val, fieldSize, pad) {
        return s;
 }
 
+//Function to check if the previous call has returned, if so return true, if not return false
+//For preventing multiple calls to slow containers.
+function checkFunctionFlag(flag) {
+    if (flagstore.hasOwnProperty(flag)) {
+        if (flagstore[flag] == 0) {
+            flagstore[flag]=1
+            return true
+        } else if (flagstore[flag] > 10) {
+            //Reset flag after ten attempts
+            console.log("Force release flag "+flag)
+            flagstore[flag]=1
+            return true
+        } else {
+            //Previous call not returned
+            console.log("Flag not available "+flag)
+            flagstore[flag]=flagstore[flag]+1
+            return false
+        }
+    } else {
+        flagstore[flag]=1
+        return true
+    }
+}
+//Clear flag for parameter
+function clearFlag(flag) {
+    flagstore[flag]=0
+}
+
 //Status variables, for parameters values fetched from other simulators
 var mr1="", mr2="", mr3="", mr4="", mr5="", mr6="";
 
@@ -144,11 +178,12 @@ var simvar1=[]
 var simvar2=[]
 var simvar3=[]
 var simvar4=[]
+var simvar5=[]
 
 //Counts the number of get request for the html page
 var getCtr=0
 
-var refreshInterval=2000
+var refreshInterval=4000
 
 function fetchAllMetrics() {
     setTimeout(() => {
@@ -178,100 +213,156 @@ function fetchAllMetrics() {
 
         //Get metric values from the simulators
         for(var index=0;index<simnames.length;index++) {
-            getSimCtr(LOCALHOST+simports[index]+"/counter/num_instances", index, function(data, index) {
-                simvar1[index] = data;
+
+            if (checkFunctionFlag("simvar1_"+index)) {
+                getSimCtr(LOCALHOST+simports[index]+"/counter/num_instances", index, function(data, index) {
+                    simvar1[index] = data;
+                    clearFlag("simvar1_"+index)
+                });
+            }
+            if (checkFunctionFlag("simvar2_"+index)) {
+                getSimCtr(LOCALHOST+simports[index]+"/counter/num_types", index, function(data,index) {
+                    simvar2[index] = data;
+                    clearFlag("simvar2_"+index)
+                });
+            }
+            if (checkFunctionFlag("simvar3_"+index)) {
+                getSimCtr(LOCALHOST+simports[index]+"/policytypes", index, function(data,index) {
+                    data=data.replace(/\[/g,'');
+                    data=data.replace(/\]/g,'');
+                    data=data.replace(/ /g,'');
+                    data=data.replace(/\"/g,'');
+                    simvar3[index] = data;
+                    clearFlag("simvar3_"+index)
+                });
+            }
+            if (checkFunctionFlag("simvar4_"+index)) {
+                getSimCtr(LOCALHOST+simports[index]+"/counter/interface", index, function(data,index) {
+                    simvar4[index] = data;
+                    clearFlag("simvar4_"+index)
+                });
+            }
+            if (checkFunctionFlag("simvar5_"+index)) {
+                getSimCtr(LOCALHOST+simports[index]+"/counter/remote_hosts", index, function(data,index) {
+                    simvar5[index] = data;
+                    clearFlag("simvar5_"+index)
+                });
+            }
+        }
+
+        //MR - get metrics values from the MR stub
+        if (checkFunctionFlag("mr1")) {
+            getSimCtr(LOCALHOST+MRSTUB_PORT+"/counter/requests_submitted", 0, function(data, index) {
+                mr1 = data;
+                clearFlag("mr1")
             });
-            getSimCtr(LOCALHOST+simports[index]+"/counter/num_types", index, function(data,index) {
-                simvar2[index] = data;
+        }
+        if (checkFunctionFlag("mr2")) {
+            getSimCtr(LOCALHOST+MRSTUB_PORT+"/counter/requests_fetched", 0, function(data, index) {
+                mr2 = data;
+                clearFlag("mr2")
             });
-            getSimCtr(LOCALHOST+simports[index]+"/policytypes", index, function(data,index) {
-                data=data.replace(/\[/g,'');
-                data=data.replace(/\]/g,'');
-                data=data.replace(/ /g,'');
-                data=data.replace(/\"/g,'');
-                simvar3[index] = data;
+        }
+        if (checkFunctionFlag("mr3")) {
+            getSimCtr(LOCALHOST+MRSTUB_PORT+"/counter/current_requests", 0, function(data, index) {
+                mr3 = data;
+                clearFlag("mr3")
             });
-            getSimCtr(LOCALHOST+simports[index]+"/counter/interface", index, function(data,index) {
-                simvar4[index] = data;
+        }
+        if (checkFunctionFlag("mr4")) {
+            getSimCtr(LOCALHOST+MRSTUB_PORT+"/counter/responses_submitted", 0, function(data, index) {
+                mr4 = data;
+                clearFlag("mr4")
+            });
+        }
+        if (checkFunctionFlag("mr5")) {
+            getSimCtr(LOCALHOST+MRSTUB_PORT+"/counter/responses_fetched", 0, function(data, index) {
+                mr5 = data;
+                clearFlag("mr5")
+            });
+        }
+        if (checkFunctionFlag("mr6")) {
+            getSimCtr(LOCALHOST+MRSTUB_PORT+"/counter/current_responses", 0, function(data, index) {
+                mr6 = data;
+                clearFlag("mr6")
             });
         }
-
-        //MR - get metrics values from the MR stub
-        getSimCtr(LOCALHOST+MRSTUB_PORT+"/counter/requests_submitted", 0, function(data, index) {
-            mr1 = data;
-        });
-        getSimCtr(LOCALHOST+MRSTUB_PORT+"/counter/requests_fetched", 0, function(data, index) {
-            mr2 = data;
-        });
-        getSimCtr(LOCALHOST+MRSTUB_PORT+"/counter/current_requests", 0, function(data, index) {
-            mr3 = data;
-        });
-        getSimCtr(LOCALHOST+MRSTUB_PORT+"/counter/responses_submitted", 0, function(data, index) {
-            mr4 = data;
-        });
-        getSimCtr(LOCALHOST+MRSTUB_PORT+"/counter/responses_fetched", 0, function(data, index) {
-            mr5 = data;
-        });
-        getSimCtr(LOCALHOST+MRSTUB_PORT+"/counter/current_responses", 0, function(data, index) {
-            mr6 = data;
-        });
 
         //CR - get metrics values from the callbackreceiver
-        getSimCtr(LOCALHOST+CR_PORT+"/counter/received_callbacks", 0, function(data, index) {
-            cr1 = data;
-        });
-        getSimCtr(LOCALHOST+CR_PORT+"/counter/fetched_callbacks", 0, function(data, index) {
-            cr2 = data;
-        });
-        getSimCtr(LOCALHOST+CR_PORT+"/counter/current_messages", 0, function(data, index) {
-            cr3 = data;
-        });
-
-        //Agent - get metrics from the agent
-        getSimCtr(LOCALHOST+AGENT_PORT+"/status", 0, function(data, index) {
-            ag1 = data;
-        });
-        getSimCtr(LOCALHOST+AGENT_PORT+"/services", 0, function(data, index) {
-            ag2="";
-            try {
-                var jd=JSON.parse(data);
-                for(var key in jd) {
-                    if (ag2.length > 1) {
-                        ag2=ag2+", "
+        if (checkFunctionFlag("cr1")) {
+            getSimCtr(LOCALHOST+CR_PORT+"/counter/received_callbacks", 0, function(data, index) {
+                cr1 = data;
+                clearFlag("cr1")
+            });
+        }
+        if (checkFunctionFlag("cr2")) {
+            getSimCtr(LOCALHOST+CR_PORT+"/counter/fetched_callbacks", 0, function(data, index) {
+                cr2 = data;
+                clearFlag("cr2")
+            });
+        }
+        if (checkFunctionFlag("cr3")) {
+            getSimCtr(LOCALHOST+CR_PORT+"/counter/current_messages", 0, function(data, index) {
+                cr3 = data;
+                clearFlag("cr3")
+            });
+        }
+        //Agent - more get metrics from the agent
+        if (checkFunctionFlag("ag1")) {
+            getSimCtr(LOCALHOST+AGENT_PORT+"/status", 0, function(data, index) {
+                ag1 = data;
+                clearFlag("ag1")
+            });
+        }
+        if (checkFunctionFlag("ag2")) {
+            getSimCtr(LOCALHOST+AGENT_PORT+"/services", 0, function(data, index) {
+                ag2="";
+                try {
+                    var jd=JSON.parse(data);
+                    for(var key in jd) {
+                        if (ag2.length > 1) {
+                            ag2=ag2+", "
+                        }
+                        ag2=ag2+(jd[key]["serviceName"]).trim()
                     }
-                    ag2=ag2+(jd[key]["serviceName"]).trim()
                 }
-            }
-            catch (err) {
-                ag2=data
-            }
-        });
-        getSimCtr(LOCALHOST+AGENT_PORT+"/policy_types", 0, function(data, index) {
-            ag3="";
-            try {
-                var jd=JSON.parse(data);
-                for(var key in jd) {
-                    if (ag3.length > 0) {
-                        ag3=ag3+", "
+                catch (err) {
+                    ag2=data
+                }
+                clearFlag("ag2")
+            });
+        }
+        if (checkFunctionFlag("ag3")) {
+            getSimCtr(LOCALHOST+AGENT_PORT+"/policy_types", 0, function(data, index) {
+                ag3="";
+                try {
+                    var jd=JSON.parse(data);
+                    for(var key in jd) {
+                        if (ag3.length > 0) {
+                            ag3=ag3+", "
+                        }
+                        ag3=ag3+jd[key].trim()
                     }
-                    ag3=ag3+jd[key].trim()
                 }
-            }
-            catch (err) {
-                ag3=""
-            }
-        });
-        getSimCtr(LOCALHOST+AGENT_PORT+"/policy_ids", 0, function(data, index) {
-            ag4=""
-            try {
-                var jd=JSON.parse(data);
-                ag4=""+jd.length
-            }
-            catch (err) {
+                catch (err) {
+                    ag3=""
+                }
+                clearFlag("ag3")
+            });
+        }
+        if (checkFunctionFlag("ag4")) {
+            getSimCtr(LOCALHOST+AGENT_PORT+"/policy_ids", 0, function(data, index) {
                 ag4=""
-            }
-        });
-
+                try {
+                    var jd=JSON.parse(data);
+                    ag4=""+jd.length
+                }
+                catch (err) {
+                    ag4=""
+                }
+                clearFlag("ag4")
+            });
+        }
 
 
         fetchAllMetrics();
@@ -324,9 +415,8 @@ app.get("/mon",function(req, res){
 
             htmlStr=htmlStr+padding("Near-RT RIC Simulator name", 35,"&nbsp;")
             htmlStr=htmlStr+padding("Types", 10,"&nbsp;")
-            htmlStr=htmlStr+padding("Instances", 10,"&nbsp;")
-            htmlStr=htmlStr+"<br>"+padding("",55,"=")+"<br>"
-
+            htmlStr=htmlStr+padding("Instances", 10,"&nbsp;")+"<br>"
+            htmlStr=htmlStr+padding("",55,"=")+"<br>"
             for(var simIndex=0;simIndex<simnames.length;simIndex++) {
                 htmlStr=htmlStr+padding(simnames[simIndex]+ " ("+simports[simIndex]+")",35,"&nbsp;");
                 htmlStr=htmlStr+padding(simvar2[simIndex],10,"&nbsp;")
@@ -346,6 +436,16 @@ app.get("/mon",function(req, res){
                 htmlStr=htmlStr+"<br>";
             }
 
+            htmlStr=htmlStr+"<br>";
+            htmlStr=htmlStr+padding("Near-RT RIC Simulator name", 35,"&nbsp;")
+            htmlStr=htmlStr+padding("Remote hosts", 50,"&nbsp;")+"<br>"
+            htmlStr=htmlStr+padding("",90,"=")+"<br>"
+            for(simIndex=0;simIndex<simnames.length;simIndex++) {
+                htmlStr=htmlStr+padding(simnames[simIndex]+ " ("+simports[simIndex]+")",35,"&nbsp;");
+                htmlStr=htmlStr+padding(simvar5[simIndex],50,"&nbsp;")
+                htmlStr=htmlStr+"<br>";
+            }
+
             htmlStr=htmlStr+
            "</body>" +
           "</html>";