website/js/customElements/ntpGraph.js

95 lines
3 KiB
JavaScript
Raw Normal View History

2021-04-13 10:37:20 +02:00
import chart from 'chart.js'
class NtpGraph extends HTMLElement {
constructor() {
super();
let ip = this.getAttribute("data-server-ip");
2022-03-25 10:23:52 +01:00
const ntpBanner = document.createElement("span");
ntpBanner.classList.add("ntpBanner");
ntpBanner.innerText = ip;
this.appendChild(ntpBanner);
const ntpContent = document.createElement("span");
ntpContent.classList.add("ntpContent");
this.appendChild(ntpContent);
const ntpLink = document.createElement("a");
ntpLink.target = "_blank";
ntpLink.href = `https://www.ntppool.org/scores/${ip}`;
ntpLink.classList.add("linkToNtpPool");
2022-03-25 12:10:53 +01:00
ntpLink.innerText = "Server auf der Seite des NTP Pools anzeigen";
2022-03-25 10:23:52 +01:00
ntpContent.appendChild(ntpLink);
2022-03-25 12:10:53 +01:00
2022-03-25 10:23:52 +01:00
const ntpDelayCanvas = document.createElement("canvas");
ntpDelayCanvas.classList.add("graphDelay");
ntpContent.appendChild(ntpDelayCanvas);
const ntpScoreCanvas = document.createElement("canvas");
ntpScoreCanvas.classList.add("graphScore");
ntpContent.appendChild(ntpScoreCanvas);
ntpBanner.onclick = () => {
if (ntpContent.classList.contains("visible")) {
ntpContent.classList.remove("visible");
2021-04-13 11:04:10 +02:00
} else {
2022-03-25 10:23:52 +01:00
ntpContent.classList.add("visible");
2021-04-13 11:04:10 +02:00
}
let xhr = new XMLHttpRequest();
2021-04-13 10:37:20 +02:00
2021-04-13 11:04:10 +02:00
xhr.onreadystatechange = () => {
if (xhr.status === 200 && xhr.readyState === 4) {
let rawData = JSON.parse(xhr.responseText);
let x = [];
let yScore = [];
let yDelay = [];
rawData["history"].forEach((entry, index) => {
let date = new Date(entry["ts"] * 1000).toLocaleDateString();
if (index % 10 !== 0) {
date = "";
}
x.push(date);
yScore.push(entry["score"]);
yDelay.push(entry["offset"]);
});
2021-04-13 10:37:20 +02:00
2021-04-13 11:04:10 +02:00
this.drawPlot(".graphDelay", x, yDelay, "Delay");
this.drawPlot(".graphScore", x, yScore, "Score");
2021-04-13 10:37:20 +02:00
2021-04-13 11:04:10 +02:00
}
2021-04-13 10:37:20 +02:00
}
2021-04-13 11:04:10 +02:00
xhr.open("GET", "https://www.ntppool.org/scores/" + ip + "/json?monitor=*&limit=800");
xhr.send();
2021-04-13 10:37:20 +02:00
2021-04-13 11:04:10 +02:00
}
2021-04-13 10:37:20 +02:00
}
2021-04-13 11:04:10 +02:00
drawPlot(elementName, x, y, title) {
let ctx = this.querySelector(elementName).getContext('2d');
2021-04-13 10:37:20 +02:00
new Chart(ctx, {
type: 'line',
responsive: true,
scaleShowVerticalLines: false,
showTooltips: false,
data: {
labels: x,
datasets: [{
label: title,
data: y,
borderColor: '#199a87',
fill: false,
radius: 0,
}]
},
});
}
}
customElements.define("jl-ntp-graph", NtpGraph);