From 175bfa0dfd685530a27853ecb2bd3b2af6eb66d7 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Mon, 23 Jun 2025 18:05:28 -0400 Subject: [PATCH] Added ability to load benchmark results when viewing a test --- assets/scripts/test.coffee | 49 ++++++++++++++++++++++++++++++++++++++ src/models/index.js | 4 ---- src/routes/api_v1.js | 39 ++++++++++++++++++++++++++++++ src/routes/index.js | 5 ++++ views/test/view.twig | 13 ++++------ 5 files changed, 98 insertions(+), 12 deletions(-) create mode 100644 assets/scripts/test.coffee create mode 100644 src/routes/api_v1.js diff --git a/assets/scripts/test.coffee b/assets/scripts/test.coffee new file mode 100644 index 0000000..aebedbf --- /dev/null +++ b/assets/scripts/test.coffee @@ -0,0 +1,49 @@ +testId = $('#results-table').data('test-id') + +fetchTestBenchmarkResults = (testId, benchmarkId) -> + try + benchmarkSearchParams = new URLSearchParams + benchmark_id: benchmarkId + benchmarkRes = await fetch("/api/v1/benchmark/details?#{benchmarkSearchParams}") + benchmarkData = await benchmarkRes.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.avg_score + min_total += result.min_score + max_total += result.max_score + + tableRow = $("#results-table tr[data-benchmark-id=#{benchmarkId}]") + + tableRow.append('' + benchmarkData.name + '') + tableRow.append('' + benchmarkData.scoring + '') + tableRow.append('' + resultData.length + '') + + if resultData.length != 0 + tableRow.append('' + (avg_total / resultData.length) + '') + else + tableRow.append('N/a') + + if min_total != 0 + tableRow.append('' + (min_total / resultData.length) + '') + tableRow.append('' + (max_total / resultData.length) + '') + else + tableRow.append('N/a') + tableRow.append('N/a') + catch error + console.error 'An error occurred while fetching benchmark results.', error + +$('#results-table tbody tr').each((index, tr) -> + benchmarkId = $(tr).data('benchmark-id') + console.log("Fetching results for benchmark id: " + benchmarkId) + fetchTestBenchmarkResults(testId, benchmarkId) +) diff --git a/src/models/index.js b/src/models/index.js index e7e632e..3f620d8 100644 --- a/src/models/index.js +++ b/src/models/index.js @@ -26,8 +26,4 @@ Benchmark.hasMany(Result); Result.belongsTo(Test); Test.hasMany(Result); -// Result/Hardware many-to-one -Result.belongsTo(Hardware); -Hardware.hasMany(Result); - module.exports = sequelize; diff --git a/src/routes/api_v1.js b/src/routes/api_v1.js new file mode 100644 index 0000000..5f5b16a --- /dev/null +++ b/src/routes/api_v1.js @@ -0,0 +1,39 @@ +const Benchmark = require('../models').models.Benchmark; +const Result = require('../models').models.Result; + +// GET /api/v1/benchmark/details +exports.getBenchmarkDetails = async function(req, res) { + try { + const benchmark = await Benchmark.findByPk(req.query.benchmark_id); + + if (!benchmark) { + return res.status(404).json({ + error: 'Benchmark not found.' + }) + } + + res.json(benchmark); + } catch (err) { + res.status(500).json({ + error: 'Internal server error occurred while fetching benchmark details.' + }); + } +}; + +// GET /api/v1/result/list - list of results for a test +exports.getResultList = async function(req, res) { + try { + var results = await Result.findAll({ + where: { + TestId: req.query.test_id, + BenchmarkId: req.query.benchmark_id + } + }); + + res.json(results); + } catch (err) { + res.status(500).json({ + error: 'Internal server error occurred while fetching benchmark results.' + }); + } +}; diff --git a/src/routes/index.js b/src/routes/index.js index 0a9294e..f5632f9 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -3,6 +3,7 @@ const topRoutes = require('./toplevel'); const testRoutes = require('./test'); const hardwareRoutes = require('./hardware'); const benchmarkRoutes = require('./benchmark'); +const apiv1Routes = require('./api_v1'); module.exports = function(app) { @@ -30,4 +31,8 @@ module.exports = function(app) { app.post('/test/add', testRoutes.postAdd); app.get('/test/:test_id', testRoutes.getView); + // API v1 routes + app.get('/api/v1/benchmark/details', apiv1Routes.getBenchmarkDetails); + app.get('/api/v1/result/list', apiv1Routes.getResultList); + }; diff --git a/views/test/view.twig b/views/test/view.twig index adbfeaa..ab25d24 100644 --- a/views/test/view.twig +++ b/views/test/view.twig @@ -56,10 +56,11 @@

Benchmarks

- +
+ @@ -68,13 +69,7 @@ {% for b in test.getBenchmarks() %} - - - - - - - + {% endfor %}
BenchmarkScoring type # Results Avg. Min.
{{ b.name }}{{ test.getResults({where: {testId: test.id}})|length }}N/aN/aN/a
@@ -84,4 +79,6 @@

Back

+ + {% endblock %}