Gameserver/website/js/serverstats.js

93 lines
2.9 KiB
JavaScript

---
---
// Collection of scripts to deploy a server hosting several open-source games
// Copyright (C) 2022 Jarno van der Kolk
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
let heightScale = d3.scaleLinear()
.domain([0,100])
.range([99,1]);
let timeScale = d3.scaleLinear()
.domain([0,60])
.range([10,190]);
let connection_lost_since = false;
function updateGraph(data, svgid, property_class, property) {
d3.select(svgid)
.selectAll('line')
.data(data, function(d) {return d.t})
.join(
function(enter) {
return enter
.append('line')
.attr('x1', function(d,i){return timeScale(i)})
.attr('x2', function(d,i){return timeScale(i)})
.attr('y1', function(d,i){return heightScale(0)})
.attr('y2', 99)
.attr('class', property_class)
.attr('vector-effect', 'non-scaling-stroke');
},
function(update) {
return update
},
function(exit) {
return exit.remove();
}
)
.transition()
.ease(d3.easeLinear)
.duration(5000)
.attr('x1', function(d,i){return timeScale(i-1)})
.attr('x2', function(d,i){return timeScale(i-1)})
.attr('y1', function(d,i){return heightScale(d[property])})
}
function update() {
d3.json('http{% if site.content.ssl %}s{% endif %}://{{ site.content.domain_name }}/monitoring/all').then(function(data){
d3.selectAll("text.connlost").remove();
updateGraph(data, '#memgraph', 'mem', 'm');
updateGraph(data, '#cpugraph', 'cpu', 'c');
})
.catch(() => {
if(!connection_lost_since) {
connection_lost_since = d3.timeSecond();
}
const seconds = [d3.timeSecond.count(connection_lost_since, d3.timeSecond())];
d3.selectAll("line.cpu").remove();
d3.selectAll("line.mem").remove();
d3.select("#cpugraph")
.selectAll("text")
.data(seconds)
.join("text")
.attr("x", 100)
.attr("y", 50)
.classed("connlost", true)
.text(d => `Connection lost ${d} seconds ago`);
d3.select("#memgraph")
.selectAll("text")
.data(seconds)
.join("text")
.attr("x", 100)
.attr("y", 50)
.classed("connlost", true)
.text(d => `Connection lost ${d} seconds ago`);
});
}
document.addEventListener("DOMContentLoaded", (event) => {
update();
setInterval(update, 5000);
});