2018-02-22 02:27:17 +00:00
|
|
|
/*addHook(() => {
|
|
|
|
|
|
|
|
})*/
|
|
|
|
|
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) {
|
|
|
|
let out;
|
|
|
|
if(bytes >= Petabyte) out = [bytes / Petabyte, "PB"];
|
|
|
|
else if(bytes >= Terabyte) out = [bytes / Terabyte, "TB"];
|
|
|
|
else if(bytes >= Gigabyte) out = [bytes / Gigabyte, "GB"];
|
|
|
|
else if(bytes >= Megabyte) out = [bytes / Megabyte, "MB"];
|
|
|
|
else if(bytes >= Kilobyte) out = [bytes / Kilobyte, "KB"];
|
|
|
|
else out = [bytes,"b"];
|
|
|
|
|
|
|
|
if(places==0) return Math.ceil(out[0]) + out[1];
|
|
|
|
else {
|
|
|
|
let ex = Math.pow(10, places);
|
|
|
|
return (Math.round(out[0], ex) / ex) + out[1];
|
|
|
|
}
|
2019-05-01 06:59:51 +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?
|
2019-05-01 06:59:51 +00:00
|
|
|
function buildStatsChart(rawLabels, seriesData, timeRange, legendNames, bytes = false) {
|
2019-03-16 12:50:31 +00:00
|
|
|
console.log("buildStatsChart");
|
2018-02-22 02:27:17 +00:00
|
|
|
let labels = [];
|
2019-03-03 02:28:17 +00:00
|
|
|
let aphrases = phraseBox["analytics"];
|
2019-02-24 01:29:06 +00:00
|
|
|
if(timeRange=="one-year") {
|
2019-03-03 02:28:17 +00:00
|
|
|
labels = [aphrases["analytics.now"],"1" + aphrases["analytics.months_short"]];
|
2019-02-24 01:29:06 +00:00
|
|
|
for(let i = 2; i < 12; i++) {
|
2019-05-01 06:59:51 +00:00
|
|
|
labels.push(i + aphrases["analytics.months_short"]);
|
2019-02-24 01:29:06 +00:00
|
|
|
}
|
|
|
|
} else if(timeRange=="three-months") {
|
2019-03-03 02:28:17 +00:00
|
|
|
labels = [aphrases["analytics.now"],"3" + aphrases["analytics.days_short"]]
|
|
|
|
for(let i = 6; i < 90; i = i + 3) {
|
2019-05-01 06:59:51 +00:00
|
|
|
labels.push(i + aphrases["analytics.days_short"]);
|
2019-02-24 01:29:06 +00:00
|
|
|
}
|
|
|
|
} else if(timeRange=="one-month") {
|
2019-03-03 02:28:17 +00:00
|
|
|
labels = [aphrases["analytics.now"],"1" + aphrases["analytics.days_short"]];
|
2018-02-22 02:27:17 +00:00
|
|
|
for(let i = 2; i < 30; i++) {
|
2019-05-01 06:59:51 +00:00
|
|
|
labels.push(i + aphrases["analytics.days_short"]);
|
2018-02-22 02:27:17 +00:00
|
|
|
}
|
|
|
|
} else if(timeRange=="one-week") {
|
2019-03-03 02:28:17 +00:00
|
|
|
labels = [aphrases["analytics.now"]];
|
2018-02-22 02:27:17 +00:00
|
|
|
for(let i = 2; i < 14; i++) {
|
|
|
|
if (i%2==0) labels.push("");
|
2019-03-03 02:28:17 +00:00
|
|
|
else labels.push(Math.floor(i/2) + aphrases["analytics.days"]);
|
2018-02-22 02:27:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(const i in rawLabels) {
|
|
|
|
let date = new Date(rawLabels[i]*1000);
|
|
|
|
console.log("date: ", date);
|
|
|
|
let minutes = "0" + date.getMinutes();
|
|
|
|
let label = date.getHours() + ":" + minutes.substr(-2);
|
|
|
|
console.log("label:", label);
|
|
|
|
labels.push(label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
labels = labels.reverse()
|
2019-05-01 06:59:51 +00:00
|
|
|
for(let i = 0; i < seriesData.length; i++) {
|
2019-02-23 06:29:19 +00:00
|
|
|
seriesData[i] = seriesData[i].reverse();
|
|
|
|
}
|
2018-02-22 02:27:17 +00:00
|
|
|
|
2019-05-01 06:59:51 +00:00
|
|
|
let config = {height: '250px', plugins:[]};
|
2019-02-23 06:29:19 +00:00
|
|
|
if(legendNames.length > 0) config.plugins = [
|
2019-05-01 06:59:51 +00:00
|
|
|
Chartist.plugins.legend({legendNames: legendNames})
|
|
|
|
];
|
|
|
|
if(bytes) config.plugins.push(Chartist.plugins.byteUnits());
|
2018-02-22 02:27:17 +00:00
|
|
|
Chartist.Line('.ct_chart', {
|
|
|
|
labels: labels,
|
2019-02-23 06:29:19 +00:00
|
|
|
series: seriesData,
|
|
|
|
}, config);
|
2019-03-21 22:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
runInitHook("analytics_loaded");
|