netdata/netdata

View on GitHub
src/web/gui/v2/registry-alert-redirect.html

Summary

Maintainability
Test Coverage
<!doctype html><html><head><title>Netdata Agent Alert Redirect</title><meta name="application-name" content="netdata"/><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta charset="utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/><meta name="viewport" content="width=device-width,height=device-height,initial-scale=1,minimum-scale=1"/><meta name="apple-mobile-web-app-capable" content="yes"/><meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"/><script>function loadStyle(url, { media, insertAfter: aref, insertBefore: bref, rel, type } = {}) {
        rel = rel || 'stylesheet'
        type = type || 'text/css'
        return new Promise(function(resolve, reject) {
          let link = document.createElement('link');
          link.type = type;
          link.rel = rel;
          link.href = url;
          link.media = media || 'all';

          link.onerror = function(err) {
            reject(new URIError(`loadStyle: the stylesheet ${err.target.src} is not accessible.`));
          };

          link.onload = function() {
            resolve();
          };

          if (aref) {
            aref.parentNode.insertBefore(link, aref.nextSibling);
            return;
          }

          if (bref) {
            bref.parentNode.insertBefore(link, bref);
            return;
          }

          document.head.appendChild(link);
        });
      }

      loadStyle("v2/static/splash.css")
      loadStyle("v2/favicon.ico", {rel: "icon", type: "image/x-icon"})</script></head><body><noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-N6CBMJD" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript><div id="agent-splash-screen" class="loading"><div class="hero"><div class="logo-container"><svg width="133" height="105" viewBox="0 0 133 105" fill="none" xmlns="http://www.w3.org/2000/svg" class="logo-blur"><path fill-rule="evenodd" clip-rule="evenodd" d="M81.697 105H55.0693L0.5 0.5H77.9598C108.079 0.554913 132.484 24.7711 132.5 54.6451C132.452 82.485 109.73 105 81.697 105Z" fill="rgba(0,171,68,0.1)"/></svg> <svg width="133" height="105" viewBox="0 0 133 105" fill="none" xmlns="http://www.w3.org/2000/svg" class="logo"><path fill-rule="evenodd" clip-rule="evenodd" d="M81.697 105H55.0693L0.5 0.5H77.9598C108.079 0.554913 132.484 24.7711 132.5 54.6451C132.452 82.485 109.73 105 81.697 105Z" fill="#DDFFEB"/></svg></div><div class="headings"><h1 class="title">Netdata Alert Notifications</h1><div class="flex-center flex-column" id="main-message">Trying to find a Netdata Agent for this alert...</div><table id="mynodes"></table></div></div></div><script>let searchParams = new URLSearchParams(location.search)
        let mg = searchParams.get("agent_machine_guid")
        let tr_i = searchParams.get("transition_id")
        let token = location.hash.substring(1)

        function buildIframe(url) {
          let iframe = document.createElement('iframe');
          iframe.src = url + "/registry-hello.html" + location.search + "&url=" + url;
          iframe.style = { position: "absolute", left: "-99999999px" };
          iframe.width = 0 ;
          iframe.height = 0;
          iframe.tabindex = -1;
          iframe.title = "empty";
          iframe.classList.add("hidden");
          document.body.appendChild(iframe);
          return iframe
        }

        function urlToId(s) {
          s = s.trim();
          s = s.replace(/^https?:\/\//i, '');
          s = s.replace(/\W/g, '_');
          return s;
        }

        fetch("api/v1/registry?action=hello", { cache: "no-cache", credentials: "include" })
          .then(function(response) { return response.json() })
          .then(function(helloData) {
            if (helloData.anonymous_statistics !== false) {
              (function (w, d, s, l, i) {
                w[l] = w[l] || []
                w[l].push({ "gtm.start": new Date().getTime(), event: "gtm.js" })
                var f = d.getElementsByTagName(s)[0],
                  j = d.createElement(s),
                  dl = l != "dataLayer" ? "&l=" + l : ""
                j.async = true
                j.src = "https://www.googletagmanager.com/gtm.js?id=" + i + dl
                f.parentNode.insertBefore(j, f)
              })(window, document, "script", "dataLayer", "GTM-N6CBMJD")
            }
            fetch("api/v1/registry?action=search&for=" + mg, { cache: "no-cache", credentials: "include", headers: { ["X-Netdata-Auth"]: 'Bearer ' + (token || localStorage.getItem("registry-netdata-token")) } })
              .then(function(response) { return response.json() })
              .then(function(data) {
                let table = document.getElementById("mynodes");

                if (data.status === "ok") {
                  if (!token) {
                    if (location.protocol === "https:" && data.urls.some(function(u) {
                      return (/http:/).test(u)
                    })) {
                      return location.replace('http://' + location.href.substring(8) + "#" + localStorage.getItem("registry-netdata-token"))
                    }
                  }

                  data.urls.sort(function(a, b) {
                    return b[2] - a[2];
                  }).forEach(function(urlData) {
                    let urlMg = urlData[0]
                    let url = urlData[1]
                    let urlHostname = urlData[4]

                    let rowIframe = buildIframe(url)

                    let row = document.createElement("tr");
                    let cell1 = document.createElement("td");
                    let link = document.createElement("a");

                    link.href = (urlHostname ? (url + "/spaces/" + urlHostname + "/rooms/local/alerts/" + (tr_i || "") + location.search) : url + location.search).replace(/\/+/g, "/");
                    link.textContent = url;
                    link.target = "_blank";
                    link.classList.add("button", "ghost");

                    cell1.appendChild(link);
                    row.appendChild(cell1);

                    let cell2 = document.createElement("td");
                    cell2.id = urlToId("_" + url + urlMg);
                    cell2.textContent = "checking...";
                    row.appendChild(cell2);

                    table.appendChild(row);

                    window.addEventListener('message', function(event) {
                      if (event.source !== rowIframe.contentWindow) return;
                      if (event.data.url !== url || event.data.mg !== urlMg) return;
                      link.href = event.data.hostname ? (url + "/spaces/" + event.data.hostname + "/rooms/local/alerts/" + (tr_i || "") + location.search) : url + location.search;

                      let cellStatus = document.getElementById(urlToId("_" + url + urlMg));
                      if (event.data.isSame) {
                        cell2.textContent = "OK";
                      } else if (event.data.hasError) {
                        cell2.textContent = "can't connect";
                      } else {
                        cell2.textContent = "wrong node";
                      }
                    });

                    setTimeout(function() {
                      let cellStatus = document.getElementById(urlToId("_" + url + urlMg));
                      document.getElementById('agent-splash-screen').classList.remove("loading");
                      if (cell2.textContent !== "checking...") {
                        return;
                      }
                      cell2.textContent = "can't connect";
                    }, 5000)
                  })
                  let el = document.getElementById('agent-splash-screen');
                  el.classList.add("table");
                  document.getElementById('main-message').textContent = "Select a URL to see details about this alert:"
                } else {
                  let el = document.getElementById('main-message');
                  el.innerHTML = "<p>Can't find any Netdata Agent for this alert.</p><small>Netdata learns Agent URLs when you view them and associates them with web browsers.<br/>Probably, you have never viewed the dashboard of the Netdata Agent that sent this notification, with the browser you use now.</small>"
                }
              }).catch(function(e) {
                let el = document.getElementById('main-message');
                el.textContent = "Oops! Something went wrong."
              })

           })</script></body></html>