2021-01-06 06:41:08 +00:00
|
|
|
function memStuff(window,document,Chartist) {
|
2022-02-21 03:16:15 +00:00
|
|
|
'use strict';
|
|
|
|
Chartist.plugins = Chartist.plugins || {};
|
|
|
|
Chartist.plugins.byteUnits = function(options) {
|
|
|
|
options = Chartist.extend({},{},options);
|
2019-05-17 08:40:41 +00:00
|
|
|
|
|
|
|
return function byteUnits(chart) {
|
2022-02-21 03:16:15 +00:00
|
|
|
if(!chart instanceof Chartist.Line) return;
|
|
|
|
|
|
|
|
chart.on('created', function() {
|
|
|
|
log("running created")
|
|
|
|
const vbits = document.getElementsByClassName("ct-vertical");
|
|
|
|
if(vbits==null) return;
|
2019-05-17 08:40:41 +00:00
|
|
|
|
2022-02-21 03:16:15 +00:00
|
|
|
let tbits = [];
|
|
|
|
for(let i=0; i<vbits.length; i++) {
|
|
|
|
tbits[i] = vbits[i].innerHTML;
|
|
|
|
}
|
|
|
|
log("tbits",tbits);
|
|
|
|
|
|
|
|
const calc = (places) => {
|
|
|
|
if(places==3) return;
|
|
|
|
|
|
|
|
const matcher = vbits[0].innerHTML;
|
|
|
|
let allMatch = true;
|
|
|
|
for(let i=0; i<tbits.length; i++) {
|
|
|
|
let val = convertByteUnit(tbits[i], places);
|
|
|
|
if(val!=matcher) allMatch = false;
|
|
|
|
vbits[i].innerHTML = val;
|
|
|
|
}
|
|
|
|
if(allMatch) calc(places + 1);
|
|
|
|
}
|
|
|
|
calc(0);
|
2019-05-17 08:40:41 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
2018-02-22 02:27:17 +00:00
|
|
|
|
2021-01-06 06:41:08 +00:00
|
|
|
function perfStuff(window,document,Chartist) {
|
2022-02-21 03:16:15 +00:00
|
|
|
'use strict';
|
|
|
|
Chartist.plugins = Chartist.plugins || {};
|
|
|
|
Chartist.plugins.perfUnits = function(options) {
|
|
|
|
options = Chartist.extend({},{},options);
|
2020-02-23 09:08:47 +00:00
|
|
|
|
|
|
|
return function perfUnits(chart) {
|
2022-02-21 03:16:15 +00:00
|
|
|
if(!chart instanceof Chartist.Line) return;
|
|
|
|
|
|
|
|
chart.on('created', function() {
|
|
|
|
log("running created")
|
|
|
|
const vbits = document.getElementsByClassName("ct-vertical");
|
|
|
|
if(vbits==null) return;
|
2020-02-23 09:08:47 +00:00
|
|
|
|
2022-02-21 03:16:15 +00:00
|
|
|
let tbits = [];
|
|
|
|
for(let i=0; i<vbits.length; i++) {
|
|
|
|
tbits[i] = vbits[i].innerHTML;
|
|
|
|
}
|
|
|
|
log("tbits:",tbits);
|
|
|
|
|
|
|
|
const calc = (places) => {
|
|
|
|
if(places==3) return;
|
|
|
|
|
|
|
|
const matcher = vbits[0].innerHTML;
|
|
|
|
let allMatch = true;
|
|
|
|
for(let i=0; i<tbits.length; i++) {
|
|
|
|
let val = convertPerfUnit(tbits[i], places);
|
|
|
|
if(val!=matcher) allMatch = false;
|
|
|
|
vbits[i].innerHTML = val;
|
|
|
|
}
|
|
|
|
if(allMatch) calc(places + 1);
|
|
|
|
}
|
|
|
|
calc(0);
|
2020-02-23 09:08:47 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-01 06:59:51 +00:00
|
|
|
const Kilobyte = 1024;
|
|
|
|
const Megabyte = Kilobyte * 1024;
|
|
|
|
const Gigabyte = Megabyte * 1024;
|
|
|
|
const Terabyte = Gigabyte * 1024;
|
|
|
|
const Petabyte = Terabyte * 1024;
|
|
|
|
|
2019-05-03 08:34:18 +00:00
|
|
|
function convertByteUnit(bytes, places = 0) {
|
2022-02-21 03:16:15 +00:00
|
|
|
let o;
|
|
|
|
if(bytes >= Petabyte) o = [bytes / Petabyte, "PB"];
|
|
|
|
else if(bytes >= Terabyte) o = [bytes / Terabyte, "TB"];
|
|
|
|
else if(bytes >= Gigabyte) o = [bytes / Gigabyte, "GB"];
|
|
|
|
else if(bytes >= Megabyte) o = [bytes / Megabyte, "MB"];
|
|
|
|
else if(bytes >= Kilobyte) o = [bytes / Kilobyte, "KB"];
|
|
|
|
else o = [bytes,"b"];
|
2019-05-03 08:34:18 +00:00
|
|
|
|
2022-02-21 03:16:15 +00:00
|
|
|
if(places==0) return Math.ceil(o[0]) + o[1];
|
|
|
|
else {
|
|
|
|
let ex = Math.pow(10, places);
|
|
|
|
return (Math.round(o[0], ex) / ex) + o[1];
|
|
|
|
}
|
2019-05-01 06:59:51 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 09:08:47 +00:00
|
|
|
let ms = 1000;
|
|
|
|
let sec = ms * 1000;
|
|
|
|
let min = sec * 60;
|
|
|
|
let hour = min * 60;
|
|
|
|
let day = hour * 24;
|
|
|
|
function convertPerfUnit(quan, places = 0) {
|
2022-02-21 03:16:15 +00:00
|
|
|
let o;
|
|
|
|
if(quan >= day) o = [quan / day, "d"];
|
|
|
|
else if(quan >= hour) o = [quan / hour, "h"];
|
|
|
|
else if(quan >= min) o = [quan / min, "m"];
|
|
|
|
else if(quan >= sec) o = [quan / sec, "s"];
|
|
|
|
else if(quan >= ms) o = [quan / ms, "ms"];
|
|
|
|
else o = [quan,"μs"];
|
2020-02-23 09:08:47 +00:00
|
|
|
|
2022-02-21 03:16:15 +00:00
|
|
|
if(places==0) return Math.ceil(o[0]) + o[1];
|
|
|
|
else {
|
|
|
|
let ex = Math.pow(10, places);
|
|
|
|
return (Math.round(o[0], ex) / ex) + o[1];
|
|
|
|
}
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 06:29:19 +00:00
|
|
|
// TODO: Fully localise this
|
|
|
|
// TODO: Load rawLabels and seriesData dynamically rather than potentially fiddling with nonces for the CSP?
|
2020-02-23 09:08:47 +00:00
|
|
|
function buildStatsChart(rawLabels, seriesData, timeRange, legendNames, typ=0) {
|
2022-02-21 03:16:15 +00:00
|
|
|
log("buildStatsChart");
|
|
|
|
log("seriesData",seriesData);
|
|
|
|
let labels = [];
|
|
|
|
let aphrases = phraseBox["analytics"];
|
|
|
|
if(timeRange=="one-year") {
|
|
|
|
labels = [aphrases["analytics.now"],"1" + aphrases["analytics.months_short"]];
|
|
|
|
for(let i = 2; i < 12; i++) {
|
|
|
|
labels.push(i + aphrases["analytics.months_short"]);
|
|
|
|
}
|
|
|
|
} else if(timeRange=="three-months") {
|
|
|
|
labels = [aphrases["analytics.now"],"3" + aphrases["analytics.days_short"]]
|
|
|
|
for(let i = 6; i < 90; i = i + 3) {
|
|
|
|
if (i%2==0) labels.push("");
|
|
|
|
else labels.push(i + aphrases["analytics.days_short"]);
|
|
|
|
}
|
|
|
|
} else if(timeRange=="one-month") {
|
|
|
|
labels = [aphrases["analytics.now"],"1" + aphrases["analytics.days_short"]];
|
|
|
|
for(let i = 2; i < 30; i++) {
|
|
|
|
if (i%2==0) labels.push("");
|
|
|
|
else labels.push(i + aphrases["analytics.days_short"]);
|
|
|
|
}
|
|
|
|
} else if(timeRange=="one-week") {
|
|
|
|
labels = [aphrases["analytics.now"]];
|
|
|
|
for(let i = 2; i < 14; i++) {
|
|
|
|
if (i%2==0) labels.push("");
|
|
|
|
else labels.push(Math.floor(i/2) + aphrases["analytics.days"]);
|
|
|
|
}
|
|
|
|
} else if (timeRange=="two-days" || timeRange == "one-day" || timeRange == "twelve-hours") {
|
|
|
|
for(const i in rawLabels) {
|
|
|
|
if (i%2==0) {
|
|
|
|
labels.push("");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let date = new Date(rawLabels[i]*1000);
|
|
|
|
log("date",date);
|
|
|
|
let minutes = "0" + date.getMinutes();
|
|
|
|
let label = date.getHours() + ":" + minutes.substr(-2);
|
|
|
|
log("label",label);
|
|
|
|
labels.push(label);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(const i in rawLabels) {
|
|
|
|
let date = new Date(rawLabels[i]*1000);
|
|
|
|
log("date",date);
|
|
|
|
let minutes = "0" + date.getMinutes();
|
|
|
|
let label = date.getHours() + ":" + minutes.substr(-2);
|
|
|
|
log("label",label);
|
|
|
|
labels.push(label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
labels = labels.reverse()
|
|
|
|
for(let i = 0; i < seriesData.length; i++) {
|
|
|
|
seriesData[i] = seriesData[i].reverse();
|
|
|
|
}
|
2018-02-22 02:27:17 +00:00
|
|
|
|
2022-02-21 03:16:15 +00:00
|
|
|
let config = {height: '250px', plugins:[]};
|
|
|
|
if(legendNames.length > 0) config.plugins = [
|
|
|
|
Chartist.plugins.legend({legendNames: legendNames})
|
|
|
|
];
|
|
|
|
if(typ==1) config.plugins.push(Chartist.plugins.byteUnits());
|
|
|
|
else if(typ==2) config.plugins.push(Chartist.plugins.perfUnits());
|
|
|
|
Chartist.Line('.ct_chart', {
|
|
|
|
labels: labels,
|
|
|
|
series: seriesData,
|
|
|
|
}, config);
|
2019-03-21 22:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
runInitHook("analytics_loaded");
|