From 1b4fd8dec9050b9645817f7f14336336e42f8fcc Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Wed, 25 Jun 2025 17:14:19 -0400 Subject: [PATCH] Added dynamic adding and loading of results on the test page --- assets/scripts/test.coffee | 62 +++++++++++++++++++ .../20240603081359_add_results_table.php | 2 - src/Controllers/ApiController.php | 39 +++++++++++- src/Models/Result.php | 5 +- src/routes.php | 9 ++- views/test/view.twig | 15 ++--- 6 files changed, 112 insertions(+), 20 deletions(-) create mode 100644 assets/scripts/test.coffee diff --git a/assets/scripts/test.coffee b/assets/scripts/test.coffee new file mode 100644 index 0000000..be479b9 --- /dev/null +++ b/assets/scripts/test.coffee @@ -0,0 +1,62 @@ +testId = $('#results-table').data('test-id') + +$ -> + $('#result-form').on 'submit', (e) -> + e.preventDefault() + + form = $(this) + formData = $(this).serialize() + benchmarkId = $(this).find('[name="result_benchmark"]').val() + + $.post '/api/v1/result/add', formData, (response) -> + if response == 'success' + fetchTestBenchmarkResults(testId, benchmarkId) + form[0].reset() + +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.average + min_total += result.minimum if result.minimum + max_total += result.maximum if result.maximum + + tableRow = $("#results-table tr[data-benchmark-id=#{benchmarkId}]") + tableRow.empty() + + 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/db/migrations/20240603081359_add_results_table.php b/db/migrations/20240603081359_add_results_table.php index efc51c0..7d222b5 100644 --- a/db/migrations/20240603081359_add_results_table.php +++ b/db/migrations/20240603081359_add_results_table.php @@ -12,10 +12,8 @@ final class AddResultsTable extends AbstractMigration { ->addColumn('minimum', 'integer') ->addColumn('maximum', 'integer') ->addColumn('test_id', 'integer', ['null' => false]) - ->addColumn('component_id', 'integer', ['null' => false]) ->addColumn('benchmark_id', 'integer', ['null' => false]) ->addForeignKey('test_id', 'tests', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE']) - ->addForeignKey('component_id', 'components', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE']) ->addForeignKey('benchmark_id', 'benchmarks', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE']) ->addTimestamps() ->create(); diff --git a/src/Controllers/ApiController.php b/src/Controllers/ApiController.php index 10e1ea4..e6313c9 100644 --- a/src/Controllers/ApiController.php +++ b/src/Controllers/ApiController.php @@ -6,11 +6,13 @@ use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\Routing\RouteContext; use BitGoblin\Colossus\Models\Benchmark; +use BitGoblin\Colossus\Models\Result; class ApiController extends Controller { - public function getBenchmark(Request $request, Response $response, array $args): Response { - $benchmark = Benchmark::where('id', $args['benchmark_id'])->first(); + public function getBenchmarkDetails(Request $request, Response $response, array $args): Response { + $urlParams = $request->getQueryParams(); + $benchmark = Benchmark::where('id', $urlParams['benchmark_id'])->first(); $payload = json_encode($benchmark); @@ -29,4 +31,37 @@ class ApiController extends Controller { ->withHeader('Content-Type', 'application/json'); } + public function getResultList(Request $request, Response $response, array $args): Response { + $urlParams = $request->getQueryParams(); + $results = Result::where([ + ['benchmark_id', '=', $urlParams['benchmark_id']], + ['test_id', '=', $urlParams['test_id']], + ])->get(); + + $payload = json_encode($results); + + $response->getBody()->write($payload); + return $response + ->withHeader('Content-Type', 'application/json'); + } + + public function postResultAdd(Request $request, Response $response, array $args): Response { + $params = (array)$request->getParsedBody(); + + $result = new Result; + $result->test_id = $params['result_test']; + $result->benchmark_id = $params['result_benchmark']; + $result->average = $params['result_avg']; + $result->minimum = $params['result_min'] ?? null; + $result->maximum = $params['result_max'] ?? null; + + $result->save(); + + $payload = json_encode('success'); + + $response->getBody()->write($payload); + return $response + ->withHeader('Content-Type', 'application/json'); + } + } diff --git a/src/Models/Result.php b/src/Models/Result.php index 544bd03..4541e10 100644 --- a/src/Models/Result.php +++ b/src/Models/Result.php @@ -8,6 +8,7 @@ class Result extends Model { protected $fillable = [ 'test_id', + 'benchmark_id', 'average', 'minimum', 'maximum', @@ -17,10 +18,6 @@ class Result extends Model { return $this->belongsTo(Test::class); } - public function component() { - return $this->belongsTo(Component::class); - } - public function benchmark() { return $this->belongsTo(Benchmark::class); } diff --git a/src/routes.php b/src/routes.php index f0a9459..6be25b0 100644 --- a/src/routes.php +++ b/src/routes.php @@ -36,6 +36,11 @@ $app->group('/reports', function(RouteCollectorProxy $group) { }); $app->group('/api', function(RouteCollectorProxy $group) { - $group->get('/benchmark/{benchmark_id}', '\\BitGoblin\\Colossus\\Controllers\\ApiController:getBenchmark')->setName('api.benchmark'); - $group->get('/benchmark/{benchmark_id}/tests', '\\BitGoblin\\Colossus\\Controllers\\ApiController:getBenchmarkTests')->setName('api.benchmark'); + $group->group('/v1', function(RouteCollectorProxy $apiv1) { + $apiv1->get('/benchmark/details', '\\BitGoblin\\Colossus\\Controllers\\ApiController:getBenchmarkDetails')->setName('api.benchmarkDetails'); + $apiv1->get('/benchmark/tests', '\\BitGoblin\\Colossus\\Controllers\\ApiController:getBenchmarkTests')->setName('api.benchmarkTests'); + + $apiv1->get('/result/list', '\\BitGoblin\\Colossus\\Controllers\\ApiController:getResultList')->setName('api.resultList'); + $apiv1->post('/result/add', '\\BitGoblin\\Colossus\\Controllers\\ApiController:postResultAdd')->setName('api.resultAdd'); + }); }); diff --git a/views/test/view.twig b/views/test/view.twig index fa2a831..f676a2e 100644 --- a/views/test/view.twig +++ b/views/test/view.twig @@ -51,7 +51,7 @@

Benchmark results:

- +
@@ -63,18 +63,13 @@ - {% for r in test.benchmarkResults() %} - - - - - - - - + {% for b in test.benchmarks %} + {% endfor %}
Benchmark
{{ r.name }}{{ r.scoring | capitalize }}{{ r.count }}{{ r.average }}{{ r.minimum ? r.minimum : 'N/a' }}{{ r.maximum ? r.maximum : 'N/a' }}
+ + {% endblock %}