Update copyright dates
[src/app-framework-binder.git] / test / monitoring / monitor.js
index bee87e4..4bc04fd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 "IoT.bzh"
+ * Copyright (C) 2015-2020 "IoT.bzh"
  * Author: José Bollo <jose.bollo@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -39,6 +39,8 @@ var logmsgs_node;
 var apis_node;
 var all_node;
 
+var styles;
+
 /* flags */
 var show_perms = false;
 var show_monitor_events = false;
@@ -81,7 +83,14 @@ function on_connect(evt) {
        connect();
 }
 
+function next_style(evt) {
+       styles.next();
+}
+
 function init() {
+       styles = makecss();
+       at("style").onclick = next_style;
+
        /* prepare the DOM templates */
        t_api = at("t-api").content.firstElementChild;
        t_verb = at("t-verb").content.firstElementChild;
@@ -122,7 +131,7 @@ function init() {
        at("param-host").value = document.location.hostname;
        at("param-port").value = document.location.port;
        var args = new URLSearchParams(document.location.search.substring(1));
-       at("param-token").value = args.get("x-afb-token") || args.get("token") || "hello";
+       at("param-token").value = args.get("x-afb-token") || args.get("token") || "HELLO";
 
        document.onbeforeunload = on_disconnect;
 
@@ -282,7 +291,7 @@ function on_got_apis(obj) {
                                name: api_name
                        };
                        api.node.API = api;
-                       api.node.dataset.api = api_name;
+                       api.node.dataset.apiname = api_name;
                        api.vnode = get(".verbs", api.node);
                        get(".name", api.node).textContent = api_name;
                        var s = get(".verbosity select", api.node);
@@ -296,32 +305,36 @@ function on_got_apis(obj) {
                        for_all_nodes(api.node, ".trace-box", update_trace_box);
                }
                apis[api_name] = api;
-               get(".desc", api.node).textContent = api_desc.info.description || "";
-               _.each(api_desc.paths, function(verb_desc, path_name){
-                       var verb_name = path_name.substring(1);
-                       var verb = api.verbs[verb_name];
-                       if (!verb) {
-                               verb = {
-                                       node: document.importNode(t_verb, true),
-                                       name: verb_name,
-                                       api: api
-                               };
-                               verb.node.VERB = verb;
-                               verb.node.dataset.verb = verb_name;
-                               api.verbs[verb_name] = verb;
-                               get(".name", verb.node).textContent = verb_name;
-                               var g = verb_desc.get ||{};
-                               var r = g["responses"] || {};
-                               var t = r["200"] || {};
-                               var d = t.description || "";
-                               get(".desc", verb.node).textContent = d;
-                               if (show_perms) {
-                                       var p = g["x-permissions"] || "";
-                                       get(".perm", verb.node).textContent = p ? JSON.stringify(p, null, 1) : "";
+               if (api_desc == null) {
+                       get(".desc", api.node).textContent = "?? unrecoverable ??";
+               } else {
+                       get(".desc", api.node).textContent = api_desc.info.description || "";
+                       _.each(api_desc.paths, function(verb_desc, path_name){
+                               var verb_name = path_name.substring(1);
+                               var verb = api.verbs[verb_name];
+                               if (!verb) {
+                                       verb = {
+                                               node: document.importNode(t_verb, true),
+                                               name: verb_name,
+                                               api: api
+                                       };
+                                       verb.node.VERB = verb;
+                                       verb.node.dataset.verb = verb_name;
+                                       api.verbs[verb_name] = verb;
+                                       get(".name", verb.node).textContent = verb_name;
+                                       var g = verb_desc.get ||{};
+                                       var r = g["responses"] || {};
+                                       var t = r["200"] || {};
+                                       var d = t.description || "";
+                                       get(".desc", verb.node).textContent = d;
+                                       if (show_perms) {
+                                               var p = g["x-permissions"] || "";
+                                               get(".perm", verb.node).textContent = p ? JSON.stringify(p, null, 1) : "";
+                                       }
+                                       api.vnode.append(verb.node);
                                }
-                               api.vnode.append(verb.node);
-                       }
-               });
+                       });
+               }
                apis_node.append(api.node);
        });
        inhibit = false;
@@ -342,9 +355,9 @@ function update_trace_box(node) {
 
 function set_trace_box(node, clear) {
        var api = node;
-       while (api && !api.dataset.api)
+       while (api && !api.dataset.apiname)
                api = api.parentElement;
-       var tag = api.dataset.api + "/" + node.dataset.trace;
+       var tag = api.dataset.apiname + "/" + node.dataset.trace;
        var value = false;
        for_all_nodes(node, "input", function(n){ if (n.checked) value = n.value; });
        if (clear)
@@ -352,8 +365,8 @@ function set_trace_box(node, clear) {
        if (value != "no") {
                var spec = {tag: tag, name: "trace"};
                spec[node.dataset.trace] = value;
-               if (api.dataset.api != "*")
-                       spec.api = api.dataset.api;
+               if (api.dataset.apiname != "*")
+                       spec.apiname = api.dataset.apiname;
                do_call("monitor/trace", {add: spec});
        }
 }
@@ -441,12 +454,12 @@ function gottraceevent(obj) {
        x.className = x.className + " " + type;
        get(".time", x).textContent = data.time;
        get(".tag", x).textContent = ({
-               request: function(r) { return r.api + "/" + r.verb + "  [" + r.index + "] " + r.action; },
+               request: function(r,d) { return r.api + "/" + r.verb + "  [" + r.index + "] " + r.action + (r.action == 'reply' ? ' '+d.data.error  : ''); },
                service: function(r) { return r.api + "@" + r.action; },
                daemon: function(r) { return r.api + ":" + r.action; },
                event: function(r) { return r.name + "!" + r.action; },
                global: function(r) { return "$" + r.action; },
-               })[type](desc);
+               })[type](desc,data);
        var tab = makeobj(desc, 4);
        if ("data" in data)
                makeobjitem(tab, 2, "data", data.data);
@@ -495,6 +508,7 @@ function obj2html(json) {
                                        cls = 'key';
                                } else {
                                        cls = 'string';
+                                       match = match.replace(/\\n/g, "\\n<br>");
                                }
                        } else if (/true|false/.test(match)) {
                                cls = 'boolean';
@@ -505,3 +519,44 @@ function obj2html(json) {
                });
 }
 
+function makecss()
+{
+       var i, l, a, links, x;
+
+       x = { idx: 0, byidx: [], byname: {}, names: [] };
+       links = document.getElementsByTagName("link");
+       for (i = 0 ; i < links.length ; i++) {
+               l = links[i];
+               if (l.title && l.rel.indexOf( "stylesheet" ) != -1) {
+                       if (!(l.title in x.byname)) {
+                               x.byname[l.title] = x.byidx.length;
+                               x.names.push(l.title);
+                               x.byidx.push([]);
+                       }
+                       x.byidx[x.byname[l.title]].push(l);
+               }
+       }
+
+       x.set = function(id) {
+               if (id in x.byname)
+                       id = x.byname[id];
+               if (id in x.byidx) {
+                       var i, j, a, b;
+                       x.idx = id;
+                       a = x.byidx;
+                       for (i = 0 ; i < a.length ; i++) {
+                               b = a[i];
+                               for (j = 0 ; j < b.length ; j++)
+                                       b[j].disabled = i != id;
+                       }
+               }
+       };
+
+       x.next = function() {
+               x.set((x.idx + 1) % x.byidx.length);
+       };
+
+       x.set(0);
+       return x;
+}
+