gluon/package/gluon-status-page/javascript/status-page.min.js
2023-01-03 20:15:49 +01:00

1 line
9.0 KiB
JavaScript

"use strict";!function(){var r=JSON.parse(document.body.getAttribute("data-translations"));function i(t,e){return t.toFixed(e).replace(/\./,r["."])}function o(t,e){e--;for(var n=t;10<=n&&0<e;n/=10)e--;return i(t,e)}function a(t){var e=["","K","M","G","T"],n=1024,i=t,r=0;if(void 0===i)return"- ";for(;n<i&&r<e.length-1;)i/=n,r++;return(i=o(i,3))+" "+e[r]}String.prototype.sprintf=function(){var t=0,e=arguments;return this.replace(/%s/g,function(){return e[t++]})};var u={id:function(t){return t},decimal:function(t){return i(t,2)},percent:function(t){return r["%s used"].sprintf(o(100*t,3)+"%")},memory:function(t){t=1-t.available/t.total;return u.percent(t)},time:function(t){var t=Math.round(t/60),e=Math.floor(t/1440),n=Math.floor(t%1440/60),t=Math.floor(t%60),i="";return 1===e?i+=r["1 day"]+", ":1<e&&(i+=r["%s days"].sprintf(e)+", "),i+=n+":",t<10&&(i+="0"),i+=t},packetsDiff:function(t,e,n){if(0<n)return t=(t-e)/n,r["%s packets/s"].sprintf(i(t,0))},bytesDiff:function(t,e,n){if(0<n)return a(8*((t-e)/n))+"bps"},bytes:function(t){return a(t)+"B"},neighbour:function(t){if(!t)return"";for(var e in c){var n,i,r=c[e].lookup_neigh(t);if(r)return(n=document.createElement("span")).appendChild(document.createTextNode("via ")),(i=document.createElement("a")).href="http://["+r.get_addr()+"]/",i.textContent=r.get_hostname(),n.appendChild(i),n.appendChild(document.createTextNode(" ("+e+")")),n}return"via "+t+" (unknown iface)"},tq:function(t){return o(100/255*t,1)+"%"}};function s(e,t){return t.split("/").forEach(function(t){e=e&&e[t]}),e}function d(t,e){var n=new EventSource(t),i={};n.onmessage=function(t){t=JSON.parse(t.data);e(t,i),i=t},n.onerror=function(){n.close(),window.setTimeout(function(){d(t,e)},3e3)}}var E,k=document.body.getAttribute("data-node-address");try{E=JSON.parse(document.body.getAttribute("data-node-location"))}catch(t){}function t(t){var e=document.getElementById("mesh-vpn");if(t){e.style.display="";for(var i=document.getElementById("mesh-vpn-peers");i.lastChild;)i.removeChild(i.lastChild);t=function e(n,i){return Object.keys(i.peers||{}).forEach(function(t){n.push([t,i.peers[t]])}),Object.keys(i.groups||{}).forEach(function(t){e(n,i.groups[t])}),n}([],t);t.sort(),t.forEach(function(t){var e=document.createElement("tr"),n=document.createElement("th"),n=(n.textContent=t[0],e.appendChild(n),document.createElement("td"));t[1]&&null!=t[1].established?n.textContent=r.connected+" ("+u.time(t[1].established)+")":n.textContent=r["not connected"],e.appendChild(n),i.appendChild(e)})}else e.style.display="none"}var e=document.querySelectorAll("[data-statistics]");d("/cgi-bin/dyn/statistics",function(o,a){var c=o.uptime-a.uptime;e.forEach(function(t){var e=t.getAttribute("data-statistics"),n=t.getAttribute("data-format"),i=s(a,e),e=s(o,e);try{var r=u[n](e,i,c);"object"==typeof r?(t.lastChild&&t.removeChild(t.lastChild),t.appendChild(r)):t.textContent=r}catch(t){console.error(t)}});try{t(o.mesh_vpn)}catch(t){console.error(t)}});var c={};function A(n){var i=document.createElement("canvas"),r=i.getContext("2d"),o=null;return{canvas:i,highlight:!1,resize:function(t,e){var n;try{n=r.getImageData(0,0,t,e)}catch(t){}i.width=t,i.height=e,n&&r.putImageData(n,0,0)},draw:function(t,e){var e=e(o);r.clearRect(t,0,5,i.height),e&&(t=t,e=e,r.beginPath(),r.fillStyle=n,r.arc(t,e,1.2,0,2*Math.PI,!1),r.closePath(),r.fill())},set:function(t){o=t}}}function h(){var o=-100,a=0,c=0,u=[],s=document.createElement("canvas"),l=(s.className="signalgraph",s.height=200,s.getContext("2d"));function d(){s.width=s.clientWidth,u.forEach(function(t){t.resize(s.width,s.height)})}function n(){if(0!==s.clientWidth){s.width!==s.clientWidth&&d(),l.clearRect(0,0,s.width,s.height);var e=!1,t=(u.forEach(function(t){t.highlight&&(e=!0)}),l.save(),u.forEach(function(t){e&&(l.globalAlpha=.2),t.highlight&&(l.globalAlpha=1),t.draw(c,function(t){return e=s.height,(1-(t-o)/(a-o))*e;var e}),l.drawImage(t.canvas,0,0)}),l.restore(),l.save(),l.beginPath(),l.strokeStyle="rgba(255, 180, 0, 0.15)",l.lineWidth=5,l.moveTo(c+2.5,0),l.lineTo(c+2.5,s.height),l.stroke(),Math.floor(s.height/40));l.save(),l.lineWidth=.5,l.strokeStyle="rgba(0, 0, 0, 0.25)",l.fillStyle="rgba(0, 0, 0, 0.5)",l.textAlign="end",l.textBaseline="bottom",l.beginPath();for(var n=0;n<t;n++){var i=s.height-40*n,r=(l.moveTo(0,i-.5),l.lineTo(s.width,i-.5),Math.round((r=s.height,(o*i+a*(r-i))/r))+" dBm");l.save(),l.strokeStyle="rgba(255, 255, 255, 0.9)",l.lineWidth=4,l.miterLimit=2,l.strokeText(r,s.width-5,i-2.5),l.fillText(r,s.width-5,i-2.5),l.restore()}l.stroke(),l.strokeStyle="rgba(0, 0, 0, 0.83)",l.lineWidth=1.5,l.strokeRect(.5,.5,s.width-1,s.height-1),l.restore()}}d(),window.addEventListener("resize",n);var i=0;return window.requestAnimationFrame(function t(e){40<e-i&&(n(),c=(c+1)%s.width,i=e),window.requestAnimationFrame(t)}),{el:s,addSignal:function(t){u.push(t),t.resize(s.width,s.height)},removeSignal:function(t){u.splice(u.indexOf(t),1)}}}function f(t,a,e,n){var i,r=t.tbody.querySelector("tr"),o=t.tbody.insertRow(),c=o.insertCell(),u=(c.setAttribute("data-label",r.children[0].textContent),t.wireless&&((i=document.createElement("span")).textContent="⬤ ",i.style.color=e,c.appendChild(i)),document.createElement("span")),s=(u.textContent=a,c.appendChild(u),{});for(var l,d,h,f,g,p,m,v,b,C=0;C<r.children.length;C++)l=r.children[C],f=h=d=void 0,(f=l.getAttribute("data-key"))&&(d=l.getAttribute("data-suffix")||"",(h=o.insertCell()).textContent="-",h.setAttribute("data-label",l.textContent),s[f]={td:h,suffix:d});function y(){b&&window.clearTimeout(b),b=window.setTimeout(function(){v&&t.signalgraph.removeSignal(v),o.parentNode.removeChild(o),n()},6e4)}function w(t){var e,t=function(t){"::"==(t="::"==t.slice(0,2)?"0"+t:t).slice(-2)&&(t+="0");for(var e=t.split(":"),n=e.length,i=[],r=0;r<e.length;r++){var o=e[r];if(""===o)for(;n++<=8;)i.push(0);else{if(!/^[a-f0-9]{1,4}$/i.test(o))return;i.push(parseInt(o,16))}}return i}(t);if(t)return e="",t.forEach(function(t){e+=("0000000000000000"+t.toString(2)).slice(-16)}),e}function x(t){var i=w(k);if(t&&t[0])return(t=t.map(function(t){var e,n=w(t);return n?(e=0,[e=i?function(t,e){for(var n=0;n<t.length&&n<e.length&&t[n]===e[n];n++);return n}(i,n):e,n,t]):[-1]})).sort(function(t,e){return t[0]<e[0]?1:t[0]>e[0]||t[1]<e[1]?-1:t[1]>e[1]?1:0}),(t=t[0][2])&&!/^fe80:/i.test(t)?t:void 0}return t.wireless&&((g=o.insertCell()).textContent="-",g.setAttribute("data-label",r.children[Object.keys(s).length+1].textContent),(p=o.insertCell()).textContent="-",p.setAttribute("data-label",r.children[Object.keys(s).length+2].textContent),(m=o.insertCell()).textContent="-",m.setAttribute("data-label",r.children[Object.keys(s).length+3].textContent),v=A(e),t.signalgraph.addSignal(v)),o.onmouseenter=function(){o.classList.add("highlight"),v&&(v.highlight=!0)},o.onmouseleave=function(){o.classList.remove("highlight"),v&&(v.highlight=!1)},y(),{get_hostname:function(){return u.textContent},get_addr:function(){return a},update_nodeinfo:function(t){var e,n,i,r,o;(a=x(t.network.addresses))&&("span"===u.nodeName.toLowerCase()&&(e=u,u=document.createElement("a"),e.parentNode.replaceChild(u,e)),u.href="http://["+a+"]/"),u.textContent=t.hostname,E&&t.location&&(e=E.latitude,n=E.longitude,i=t.location.latitude,t=t.location.longitude,r=Math.PI/180,o=(i*=r)-(e*=r),t=(t*=r)-(n*=r),r=Math.sin(o/2)*Math.sin(o/2)+Math.sin(t/2)*Math.sin(t/2)*Math.cos(e)*Math.cos(i),n=6372.8*(2*Math.asin(Math.sqrt(r))),p.textContent=Math.round(1e3*n)+" m"),y()},update_mesh:function(n){Object.keys(s).forEach(function(t){var e=s[t];e.td.textContent=n[t]+e.suffix}),y()},update_wifi:function(t){g.textContent=t.signal,m.textContent=Math.round(t.inactive/1e3)+" s",o.classList.toggle("inactive",200<t.inactive),v.set(200<t.inactive?null:t.signal),y()}}}function l(t,e,n){var i,o={},r=(n&&(i=h(),t.appendChild(i.el)),{tbody:t.querySelector("tbody"),signalgraph:i,ifname:e,wireless:n}),a=!1,c={},u=[];function s(){var t;a||(a=!0,(t=new EventSource("/cgi-bin/dyn/neighbours-nodeinfo?"+encodeURIComponent(e))).addEventListener("neighbour",function(t){try{var n=JSON.parse(t.data);i=[],r=n.network.mesh,Object.keys(r).forEach(function(t){var e=r[t].interfaces;Object.keys(e).forEach(function(t){e[t].forEach(function(t){i.push(t)})})}),i.forEach(function(t){var e=o[t];if(e){delete c[t];try{e.update_nodeinfo(n)}catch(t){console.error(t)}}})}catch(t){console.error(t)}var i,r},!1),t.onerror=function(){t.close(),a=!1,Object.keys(c).forEach(function(t){0<c[t]&&(c[t]--,s())})})}function l(t){var e=o[t];return e||(c[t]=3,e=o[t]=f(r,t,(u=u[0]?u:["#396AB1","#DA7C30","#3E9651","#CC2529","#535154","#6B4C9A","#922428","#948B3D"]).shift(),function(){delete c[t],delete o[t]}),s()),e}return n&&d("/cgi-bin/dyn/stations?"+encodeURIComponent(e),function(n){Object.keys(n).forEach(function(t){var e=n[t];l(t).update_wifi(e)})}),{get_neigh:l,lookup_neigh:function(t){return o[t]}}}document.querySelectorAll("[data-interface]").forEach(function(t){var e=t.getAttribute("data-interface"),n=(t.getAttribute("data-interface-address"),!!t.getAttribute("data-interface-wireless"));c[e]=l(t,e,n)});var n=document.body.getAttribute("data-mesh-provider");n&&d(n,function(i){Object.keys(i).forEach(function(t){var e=i[t],n=c[e.ifname];n&&n.get_neigh(t).update_mesh(e)})})}();