Added dynamic adding and loading of results on the test page
This commit is contained in:
		
							
								
								
									
										62
									
								
								assets/scripts/test.coffee
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								assets/scripts/test.coffee
									
									
									
									
									
										Normal file
									
								
							| @@ -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('<td><a href="/benchmark/' + benchmarkData.id + '">' + benchmarkData.name + '</a></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) | ||||
| @@ -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(); | ||||
|   | ||||
| @@ -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'); | ||||
|   } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -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); | ||||
|   } | ||||
|   | ||||
| @@ -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'); | ||||
|   }); | ||||
| }); | ||||
|   | ||||
| @@ -51,7 +51,7 @@ | ||||
|     <div class="twelve columns"> | ||||
|       <h3>Benchmark results:</h3> | ||||
|  | ||||
|       <table class="u-full-width"> | ||||
|       <table id="results-table" class="u-full-width" data-test-id="{{ test.id }}"> | ||||
|         <thead> | ||||
|           <tr> | ||||
|             <th>Benchmark</th> | ||||
| @@ -63,18 +63,13 @@ | ||||
|           </tr> | ||||
|         </thead> | ||||
|         <tbody> | ||||
|           {% for r in test.benchmarkResults() %} | ||||
|             <tr> | ||||
|               <td>{{ r.name }}</td> | ||||
|               <td>{{ r.scoring | capitalize }}</td> | ||||
|               <td>{{ r.count }}</td> | ||||
|               <td>{{ r.average }}</td> | ||||
|               <td>{{ r.minimum ? r.minimum : 'N/a' }}</td> | ||||
|               <td>{{ r.maximum ? r.maximum : 'N/a' }}</td> | ||||
|             </tr> | ||||
|           {% for b in test.benchmarks %} | ||||
|             <tr data-benchmark-id="{{ b.id }}"></tr> | ||||
|           {% endfor %} | ||||
|         </tbody> | ||||
|       </table> | ||||
|     </div> | ||||
|   </div> | ||||
|  | ||||
|   <script src="/js/test.js"></script> | ||||
| {% endblock %} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user