Added ability to generate a basic chart using chart.js
This commit is contained in:
76
assets/scripts/reports.coffee
Normal file
76
assets/scripts/reports.coffee
Normal file
@ -0,0 +1,76 @@
|
||||
$ ->
|
||||
$('#reports-download').on 'click', ->
|
||||
canvas = $('#benchmark-chart')[0]
|
||||
a = document.createElement 'a'
|
||||
a.href = canvas.toDataURL 'image/png'
|
||||
a.download = 'chart.png'
|
||||
a.click()
|
||||
|
||||
$('#reports-button').on 'click', (e) ->
|
||||
benchmarkId = $('#report-benchmarks').val()
|
||||
testIds = $('#report-tests').val()
|
||||
|
||||
benchmarkSearchParams = new URLSearchParams
|
||||
benchmark_id: benchmarkId
|
||||
benchmarkRes = await fetch("/api/v1/benchmark/details?#{benchmarkSearchParams}")
|
||||
benchmarkData = await benchmarkRes.json()
|
||||
|
||||
data =
|
||||
labels: []
|
||||
datasets: [
|
||||
{
|
||||
label: 'Average Score'
|
||||
data: []
|
||||
}
|
||||
]
|
||||
|
||||
for testId in testIds
|
||||
try
|
||||
testSearchParams = new URLSearchParams
|
||||
test_id: testId
|
||||
testRes = await fetch("/api/v1/test/details?#{testSearchParams}")
|
||||
testData = await testRes.json()
|
||||
|
||||
resultSearchParams = new URLSearchParams
|
||||
test_id: testId
|
||||
benchmark_id: benchmarkId
|
||||
resultRes = await fetch("/api/v1/result/list?#{resultSearchParams}")
|
||||
resultData = await resultRes.json()
|
||||
|
||||
avg_total = 0
|
||||
min_total = 0
|
||||
max_total = 0
|
||||
|
||||
for result in resultData
|
||||
avg_total += result.average
|
||||
min_total += result.minimum if result.minimum
|
||||
max_total += result.maximum if result.maximum
|
||||
|
||||
data.labels.push(testData.title)
|
||||
data.datasets[0].data.push(avg_total / resultData.length)
|
||||
catch error
|
||||
console.error 'An error occurred while fetching benchmark results.', error
|
||||
|
||||
ctx = $('#benchmark-chart')[0].getContext('2d')
|
||||
|
||||
options =
|
||||
indexAxis: 'y'
|
||||
plugins:
|
||||
datalabels:
|
||||
anchor: 'end'
|
||||
align: 'left'
|
||||
color: 'black'
|
||||
font:
|
||||
weight: 'bold'
|
||||
formatter: (value) -> value
|
||||
scales:
|
||||
y:
|
||||
beginAtZero: true
|
||||
|
||||
new Chart ctx,
|
||||
type: 'bar'
|
||||
data: data
|
||||
options: options
|
||||
plugins: [ChartDataLabels]
|
||||
|
||||
$('#reports-download').attr('disabled', false)
|
Reference in New Issue
Block a user