This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
spark-art-dashboard/chart.js

167 lines
3.9 KiB
JavaScript
Raw Normal View History

2018-09-07 18:20:45 +01:00
// SLA Data
2018-09-11 23:05:37 +01:00
sla = null
2018-09-07 18:20:45 +01:00
const request = async () => {
2018-09-11 23:05:37 +01:00
const response = await fetch('sla')
sla = await response.json();
sla = sla[0]
2018-09-07 18:20:45 +01:00
}
request();
2018-09-11 23:05:37 +01:00
// Charts colors
2018-09-07 18:20:45 +01:00
const chartColors = [
(a=1) => `rgba( 54, 162, 235, ${a})`,
(a=1) => `rgba( 75, 192, 192, ${a})`,
(a=1) => `rgba(201, 203, 207, ${a})`,
(a=1) => `rgba(255, 159, 64, ${a})`,
(a=1) => `rgba(153, 102, 255, ${a})`,
(a=1) => `rgba(255, 99, 132, ${a})`,
(a=1) => `rgba(255, 205, 86, ${a})`
]
2018-09-11 23:05:37 +01:00
// This function loops for ever
function loop(charts) {
fetch('top.csv') // Fetches data
.then(response => response.text()) // Read it
.then(data => data.split('\n').map(x => x.split(','))) // Divide in lines and lines in columns
.then(data => {
if (!charts) {
paint(data);
} else { //We already have charts, lets update values
for (const chart of charts) {
const index = chart.meta.index
if (!chart) continue;
chart.datasets[0].data = data.map(row => row[index]) // change the values
chart.update() // update them
}
}
return charts
})
.then(charts => {
plot_lines()
setTimeout(_ => loop(charts), 3000) // update every 3 seconds
})
.catch(error => {
console.error('An error ocurred:', error)
})
}
loop(null)
2018-09-07 18:20:45 +01:00
2018-09-11 23:05:37 +01:00
// Paint charts (should only run once)
function paint (data) {
2018-09-07 18:20:45 +01:00
const headers = data[0].slice(1) // Remove the first header column (timestamp)
// Remove headers line and the empty last one and grab the first 100
2018-09-11 23:05:37 +01:00
data = data = data.slice(1, -1)
2018-09-07 18:20:45 +01:00
2018-09-11 23:05:37 +01:00
// X Axis
const xAxis = data.map((row, index) => index)
2018-09-07 18:20:45 +01:00
2018-09-11 23:05:37 +01:00
// create a chart for each header
2018-09-07 18:20:45 +01:00
const charts = headers.map((header, index) => {
const canvas = document.getElementById('chart-'+header)
if (!canvas) return null
const color = chartColors[index % chartColors.length]
const options = {
type: 'line',
responsive: true,
data: {
labels: xAxis,
datasets: [{
label: header,
data: data.map(row => row[index+1]), // Y axis
backgroundColor: color(0.5),
borderColor: color(),
borderWidth: 1,
2018-09-11 23:05:37 +01:00
pointRadius: 2 // dots' radius
2018-09-07 18:20:45 +01:00
}]
},
options: {
legend: {
display: false
},
scales: {
xAxes: [{
ticks: {
autoSkip: true,
maxTicksLimit: 15,
},
scaleLabel: {
display: true,
2018-09-11 23:05:37 +01:00
labelString: 'nth instruction'
2018-09-07 18:20:45 +01:00
}
}],
yAxes: [{
ticks: {
autoSkip: true,
maxTicksLimit: 8
},
scaleLabel: {
display: true,
labelString: header
}
}]
}
}
}
2018-09-11 23:05:37 +01:00
// Disable effects/animations
options.options.elements = {
line: {
tension: 0
}
},
options.options.animation = {
duration: 0
},
options.options.hover = {
animationDuration: 0
},
options.options.responsiveAnimationDuration = 0
const chart = new Chart(canvas.getContext('2d'), options)
chart.meta = { // recursion metadata
index
}
2018-09-07 18:20:45 +01:00
})
2018-09-11 23:05:37 +01:00
}
2018-09-07 18:20:45 +01:00
2018-09-11 23:05:37 +01:00
// Plot lines over the charts
function plot_lines() {
if (sla == null) return;
2018-09-07 18:20:45 +01:00
var execTime_line = new Graph({
canvasId: 'chart-execTime (ms)',
minX: -10,
minY: -10,
maxX: 10,
maxY: 10,
unitsPerTick: 1
});
execTime_line.drawEquation(function(x) {
return sla.maxExecTime;
}, 'green', 1);
var accuracy_line = new Graph({
canvasId: 'chart-Accuracy (%)',
minX: -10,
minY: -10,
maxX: 10,
maxY: 10,
unitsPerTick: 1
});
accuracy_line.drawEquation(function(x) {
return sla.minAccuracy;
}, 'green', 1);
var cost_line = new Graph({
canvasId: 'chart-cost',
minX: -10,
minY: -10,
maxX: 10,
maxY: 10,
unitsPerTick: 1
});
cost_line.drawEquation(function(x) {
return sla.maxCost;
}, 'green', 1);
}