ln.skyscanner/www/vue/ln.skyscanner.js

261 lines
7.7 KiB
JavaScript

var SkyScanner = (function(){
var tzOffset = new Date().getTimezoneOffset() * 60;
var defaultOptions = {
};
var initializers = [];
class SkyScanner
{
constructor(options){
this.options = {}
Object.assign(this.options, defaultOptions);
Object.assign(this.options, options);
var self = this;
this.lagDetector = null;
this.currentTimeout = null;
this.serverTime = "N/A";
this.serverString = "N/A";
this.lastIssueUpdate = "N/A";
this.currentIssues = [];
this.currentNodes = [];
this.currentAction = "";
this.currentState = "";
this.issues = {
OK: 0,
WARN: 0,
CRITICAL: 0,
};
LN().option("wsError",(e)=>self.wsError(e));
LN().option("wsClose",(e)=>self.wsClose(e));
LN().option("wsUpdate",(e)=>self.wsUpdate(e));
LN().connect();
LN().rpc(null,"GetServerString",[],function(r,e){
self.serverString = r;
});
}
static getInitializers(){
return initializers;
}
action(msg){
this.currentAction = msg;
}
state(msg){
this.currentState = msg;
}
wsUpdate(state)
{
try
{
if (this.lagDetector)
clearTimeout(this.lagDetector);
this.serverTime = moment(state.currentTime).format();
this.lagDetector = setTimeout(function(){
this.serverTime = "Server lag detected";
}, 2000);
} catch (e)
{
console.log(e);
}
}
wsError(e){
this.serverTime = "WebSocket: Error: " + JSON.stringify(e);
}
wsClose(e){
this.serverTime = "WebSocket: Connection lost";
setTimeout(function(){
LN().connect();
}, 2500 );
}
updateIssues(){
var self = this;
this.action("updating issue list...");
LN().rpc("CheckService","GetIssueList",[],(r,e) => {
if (e){
self.state("Error fetching current issue list!\n" + JSON.stringify(e));
} else {
self.currentIssues = r;
self.issues = {
OK: 0,
WARN: 0,
CRITICAL: 0,
};
for (var n=0;n<self.currentIssues.length;n++){
var currentNode = self.currentIssues[n];
self.upgradeNode(currentNode);
self.issues[currentNode.checkState]++;
}
self.lastIssueUpdate = moment().format();
}
self.currentTimeout = setTimeout(function(){ self.updateIssues(); }, 10000);
this.action("");
});
}
upgradeNode(currentNode){
currentNode.highestStateTime = 0;
currentNode.checkState = "OK";
$.each(currentNode.CheckStates, function(){
var now = moment.unix((Date.now()/1000) - tzOffset);
var m1 = this.History.length > 0 ? moment.unix(this.History.slice(-1)[0].Timestamp) : moment();
var m2 = this.History.length > 1 ? moment.unix(this.History.slice(-2)[0].Timestamp) : moment();
this.currentStateTime = (now - m1)/1000;
if (STATES.indexOf(currentNode.checkState) < STATES.indexOf(this.CheckState))
{
currentNode.checkState = this.CheckState;
currentNode.highestStateTime = 0;
}
if (currentNode.highestStateTime < this.currentStateTime)
currentNode.highestStateTime = this.currentStateTime;
this.previousCheckState = this.History.length > 1 ? " ( " + this.History.slice(-2)[0].NewState + "=" + timespan((m1 - m2)/1000) + " )" : "";
m1 = now;
$.each(this.History.reverse(), function(){
m2 = moment.unix(this.Timestamp);
this.duration = (m1 - m2)/1000;
m1 = m2;
});
});
}
updateNodes(){
var self = this;
console.log("Update node list");
LN().rpc("entities","GetNodes",[],function(r,e){
if (e){
alert("Error fetching current node list!\n" + JSON.stringify(e));
} else {
self.currentNodes = r;
}
});
}
loadNode(nodeID,cb,refreshIntervall){
var self = this;
this.action("loading node " + nodeID);
LN().rpc("entities","GetNode",[nodeID,],(r,e) => {
if (refreshIntervall && self.currentTimeout)
clearTimeout(self.currentTimeout);
skyscanner.upgradeNode(r);
cb(r);
if (refreshIntervall)
setTimeout(()=>{
self.loadNode(nodeID,cb,refreshIntervall);
}, refreshIntervall);
this.action("");
});
}
encodeID( t )
{
return ("" + t).replace( /[\.\/]/g, "_");
}
loadPerformanceGraph(perfPath,interval,shift,cb){
LN().rpc("perfValues","GetPerfData",[perfPath,interval,shift],(perfData,e)=>{
if (e){
console.log(e);
} else {
cb(perfData);
}
});
}
buildGraphs(node){
let graphs = node._graphs = node._graphs || {};
node.CheckStates.forEach(checkState => {
let checkName = checkState.CheckName;
checkState.PerformanceValues.forEach(performanceValue =>{
let groupName = checkName + " " + performanceValue.Group || "";
let perfUnit = performanceValue.PerfUnit || "";
if (!graphs[groupName])
graphs[groupName] = {}
if (!graphs[groupName][perfUnit])
graphs[groupName][perfUnit] = {}
graphs[groupName][perfUnit][performanceValue.PerfName] = performanceValue;
});
});
return graphs;
}
getCheckStateTime(checkState){
var history = []
var now = moment.unix((Date.now()/1000) - tzOffset);
var m1 = checkState.History.length > 0 ? moment.unix(checkState.History.slice(-1)[0].Timestamp) : moment();
var m2 = checkState.History.length > 1 ? moment.unix(checkState.History.slice(-2)[0].Timestamp) : moment();
var currentStateTime = (now - m1)/1000;
timespan(currentStateTime) + (this.History.length > 1 ? " ( " + this.History.slice(-2)[0].NewState + "=" + timespan((m1 - m2)/1000) + " )" : "")
}
static scaleSI(value)
{
if (!value)
return null;
if (value > 1000000000)
return ((value / 1000000000) | 0) + "G";
if (value > 1000000)
return ((value / 1000000) | 0) + "M";
if (value > 1000)
return ((value / 1000) | 0) + "k";
return value;
}
}
return SkyScanner;
})()
var skyScannerRoutes = []