Added API routes to provide some information for JS; added CoffeeScript to grab benchmark results for a test

This commit is contained in:
2025-06-19 00:38:36 -04:00
parent d17b66920a
commit 8cd47d7e70
8 changed files with 96 additions and 16 deletions

View File

@ -1,2 +1,3 @@
$ ->
# run foundation scripts
$(document).foundation()

View File

@ -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/results?#{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('<td>' + benchmarkData.name + '</td>')
tableRow.append('<td>' + benchmarkData.scoring + '</td>')
tableRow.append('<td>' + resultData.length + '</td>')
if resultData.length != 0
tableRow.append('<td>' + (avg_total / resultData.length) + '</td>')
else
tableRow.append('<td>N/a</td>')
if min_total != 0
tableRow.append('<td>' + (min_total / resultData.length) + '</td>')
tableRow.append('<td>' + (max_total / resultData.length) + '</td>')
else
tableRow.append('<td>N/a</td>')
tableRow.append('<td>N/a</td>')
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)
)

23
src/routes/api1.rb Normal file
View File

@ -0,0 +1,23 @@
# frozen_string_literal: true
# /api/v1 routes
class GameData < Sinatra::Base
get '/api/v1/benchmark/details' do
benchmark_id = params[:benchmark_id]
benchmark = Benchmark.where(id: benchmark_id).first()
json benchmark.values()
end
get '/api/v1/results' do
test_id = params[:test_id]
benchmark_id = params[:benchmark_id]
results = Result.where(test_id: test_id, benchmark_id: benchmark_id).all()
json results.map(&:values)
end
end

View File

@ -6,3 +6,5 @@ require_relative 'benchmark'
require_relative 'result'
require_relative 'reports'
require_relative 'test'
require_relative 'api1'

View File

@ -27,7 +27,7 @@ class GameData < Sinatra::Base
result_maximum = params[:result_maximum] if params.key?(:result_maximum)
Result.create(
hardware_id: params[:result_hardware],
test_id: params[:result_test],
benchmark_id: params[:result_benchmark],
avg_score: params[:result_average],
min_score: result_minimum,
@ -35,8 +35,8 @@ class GameData < Sinatra::Base
)
if params.key?(:result_referrer)
if params[:result_referrer] == 'hardware'
redirect "/hardware/#{params[:result_hardware]}"
if params[:result_referrer] == 'test'
redirect "/test/#{params[:result_test]}"
elsif params[:result_referrer] == 'benchmark'
redirect "/benchmark/#{params[:result_benchmark]}"
end

View File

@ -40,11 +40,9 @@ class GameData < Sinatra::Base
get '/test/:test_id' do
tst = Test.where(id: params[:test_id]).first()
benchmarks = Benchmark.order(:name).all()
erb :'test/view', locals: {
title: tst.name,
test: tst,
benchmarks: benchmarks
test: tst
}
end

View File

@ -25,7 +25,7 @@
<tbody>
<% tests.each do |t| %>
<tr>
<td><a href="/hardware/<%= t.id %>"><%= t.name %></a></td>
<td><a href="/test/<%= t.id %>"><%= t.name %></a></td>
<td><%= t.benchmark.length %></td>
<td><%= date_format(t.created_at) %></td>
<td><%= date_format(t.updated_at) %></td>

View File

@ -21,7 +21,7 @@
<div class="grix-x grix-margin-x">
<div class="cell small-12">
<form class="u-full-width" action="/result/add" method="post">
<input type="hidden" name="result_hardware" value="<%= test.id %>">
<input type="hidden" name="result_test" value="<%= test.id %>">
<input type="hidden" name="result_referrer" value="test">
<div class="grid-x grid-margin-x">
@ -67,18 +67,25 @@
<h4>Benchmark results for this test:</h4>
<table>
<table id="results-table" data-test-id="<%= test.id %>">
<thead>
<th>Benchmark name</th>
<th>Scoring type</th>
<th># results</th>
<th>Average</th>
<th>Minimum</th>
<th>Maximum</th>
<tr>
<th>Benchmark name</th>
<th>Scoring type</th>
<th># results</th>
<th>Average</th>
<th>Minimum</th>
<th>Maximum</th>
</tr>
</thead>
<tbody>
<% test.benchmark.each do |benchmark| %>
<tr data-benchmark-id="<%= benchmark.id %>"></tr>
<% end %>
</tbody>
</table>
</div>
</div>
<!-- load test data fetching functionality -->
<script src="/js/test.js" charset="utf-8"></script>