From 0dd70986810e7772f1cf9b3a6037c5f0bd9d3b0d Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Fri, 22 Sep 2023 23:03:24 -0600 Subject: [PATCH] Started work to re-work database; add new models for benchmarks and components --- composer.json | 3 ++ .../20221125213226_add_results_table.php | 32 ----------- .../20230921024609_add_initial_tables.php | 35 ++++++++++++ ...php => 20230923020649_add_tests_table.php} | 8 +-- src/Controllers/BenchmarkController.php | 54 +++++++++++++++++++ src/Controllers/ComponentController.php | 53 ++++++++++++++++++ src/Models/Benchmark.php | 19 +++++++ src/Models/Component.php | 18 +++++++ src/Models/Result.php | 3 -- src/Models/Test.php | 13 ++++- src/routes.php | 34 +++++++++--- views/benchmark/list.twig | 32 +++++++++++ views/benchmark/view.twig | 50 +++++++++++++++++ views/component/list.twig | 30 +++++++++++ views/component/view.twig | 0 views/layout.twig | 2 +- views/{layout => partials}/navbar.twig | 2 + views/test/view.twig | 6 +-- 18 files changed, 343 insertions(+), 51 deletions(-) delete mode 100644 db/migrations/20221125213226_add_results_table.php create mode 100644 db/migrations/20230921024609_add_initial_tables.php rename db/migrations/{20221125223430_add_tests_table.php => 20230923020649_add_tests_table.php} (67%) create mode 100644 src/Controllers/BenchmarkController.php create mode 100644 src/Controllers/ComponentController.php create mode 100644 src/Models/Benchmark.php create mode 100644 src/Models/Component.php create mode 100644 views/benchmark/list.twig create mode 100644 views/benchmark/view.twig create mode 100644 views/component/list.twig create mode 100644 views/component/view.twig rename views/{layout => partials}/navbar.twig (73%) diff --git a/composer.json b/composer.json index 0c09ef6..6e3c4fa 100644 --- a/composer.json +++ b/composer.json @@ -23,5 +23,8 @@ "illuminate/database": "^9.41", "robmorgan/phinx": "^0.13.1", "hassankhan/config": "^3.0" + }, + "scripts": { + "phinx": "./vendor/bin/phinx" } } diff --git a/db/migrations/20221125213226_add_results_table.php b/db/migrations/20221125213226_add_results_table.php deleted file mode 100644 index a65aa9f..0000000 --- a/db/migrations/20221125213226_add_results_table.php +++ /dev/null @@ -1,32 +0,0 @@ -table('results'); - $table->addColumn('component', 'string', ['null' => false]) - ->addColumn('benchmark', 'string', ['null' => false]) - ->addColumn('type', 'string', ['null' => false, 'default' => 'fps']) - ->addColumn('average', 'integer', ['null' => false]) - ->addColumn('minimum', 'integer') - ->addColumn('maximum', 'integer') - ->addTimestamps() - ->addIndex(['component', 'benchmark', 'type']) - ->create(); - } -} diff --git a/db/migrations/20230921024609_add_initial_tables.php b/db/migrations/20230921024609_add_initial_tables.php new file mode 100644 index 0000000..f5474e8 --- /dev/null +++ b/db/migrations/20230921024609_add_initial_tables.php @@ -0,0 +1,35 @@ +table('components'); + $components_table->addColumn('name', 'string', ['null' => false]) + ->addColumn('type', 'string', ['null' => false]) + ->addTimestamps() + ->addIndex(['name', 'type']) + ->create(); + + // benchmarks + $benchmarks_table = $this->table('benchmarks'); + $benchmarks_table->addColumn('name', 'string', ['null' => false]) + ->addColumn('description', 'string') + ->addColumn('scoring', 'string', ['null' => false]) + ->addTimestamps() + ->addIndex(['name', 'scoring']) + ->create(); + + // benchmark test results + $table = $this->table('results'); + $table->addColumn('average', 'integer', ['null' => false]) + ->addColumn('minimum', 'integer') + ->addColumn('maximum', 'integer') + ->addTimestamps() + ->create(); + } + +} diff --git a/db/migrations/20221125223430_add_tests_table.php b/db/migrations/20230923020649_add_tests_table.php similarity index 67% rename from db/migrations/20221125223430_add_tests_table.php rename to db/migrations/20230923020649_add_tests_table.php index 08204ba..00b8a7c 100644 --- a/db/migrations/20221125223430_add_tests_table.php +++ b/db/migrations/20230923020649_add_tests_table.php @@ -19,10 +19,12 @@ final class AddTestsTable extends AbstractMigration public function change() { $table = $this->table('tests'); - $table->addColumn('name', 'string', ['null' => false]) - ->addColumn('description', 'text', ['null' => false]) + $table->addColumn('date_tag', 'string', ['null' => false]) + ->addColumn('benchmark_id', 'integer', ['null' => false]) + ->addColumn('component_id', 'integer', ['null' => false]) + ->addForeignKey('benchmark_id', 'benchmarks', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE']) + ->addForeignKey('component_id', 'components', 'id', ['delete'=> 'CASCADE', 'update'=> 'CASCADE']) ->addTimestamps() - ->addIndex(['name']) ->create(); $results = $this->table('results'); diff --git a/src/Controllers/BenchmarkController.php b/src/Controllers/BenchmarkController.php new file mode 100644 index 0000000..2f14736 --- /dev/null +++ b/src/Controllers/BenchmarkController.php @@ -0,0 +1,54 @@ +get(); + + $view = Twig::fromRequest($request); + return $view->render($response, 'benchmark/list.twig', [ + 'benchmarks' => $benchmarks, + ]); + } + + public function getView(Request $request, Response $response, array $args): Response { + $benchmark = Benchmark::where('id', $args['benchmark_id'])->first(); + + $view = Twig::fromRequest($request); + return $view->render($response, 'benchmark/view.twig', [ + 'benchmark' => $benchmark, + ]); + } + + public function getAdd(Request $request, Response $response): Response { + $view = Twig::fromRequest($request); + return $view->render($response, 'benchmark/add.twig'); + } + + public function postAdd(Request $request, Response $response): Response { + $params = (array)$request->getParsedBody(); + + $benchmark = new Benchmark; + $benchmark->name = $params['benchmark_name']; + $benchmark->description = $params['benchmark_description']; + $benchmark->scoring = $params['benchmark_scoring']; + + $benchmark->save(); + + // redirect the user back to the home page + $routeContext = RouteContext::fromRequest($request); + $routeParser = $routeContext->getRouteParser(); + return $response + ->withHeader('Location', $routeParser->urlFor('benchmark.list')) + ->withStatus(302); + } + +} diff --git a/src/Controllers/ComponentController.php b/src/Controllers/ComponentController.php new file mode 100644 index 0000000..41683b5 --- /dev/null +++ b/src/Controllers/ComponentController.php @@ -0,0 +1,53 @@ +get(); + + $view = Twig::fromRequest($request); + return $view->render($response, 'component/list.twig', [ + 'components' => $components, + ]); + } + + public function getView(Request $request, Response $response, array $args): Response { + $component = Component::where('id', $args['component_id'])->first(); + + $view = Twig::fromRequest($request); + return $view->render($response, 'components/view.twig', [ + 'component' => $component, + ]); + } + + public function getAdd(Request $request, Response $response): Response { + $view = Twig::fromRequest($request); + return $view->render($response, 'component/add.twig'); + } + + public function postAdd(Request $request, Response $response): Response { + $params = (array)$request->getParsedBody(); + + $component = new Component; + $component->name = $params['component_name']; + $component->type = $params['component_description']; + + $component->save(); + + // redirect the user back to the home page + $routeContext = RouteContext::fromRequest($request); + $routeParser = $routeContext->getRouteParser(); + return $response + ->withHeader('Location', $routeParser->urlFor('component.list')) + ->withStatus(302); + } + +} diff --git a/src/Models/Benchmark.php b/src/Models/Benchmark.php new file mode 100644 index 0000000..57f4983 --- /dev/null +++ b/src/Models/Benchmark.php @@ -0,0 +1,19 @@ +hasMany(Test::class); + } + +} \ No newline at end of file diff --git a/src/Models/Component.php b/src/Models/Component.php new file mode 100644 index 0000000..96edf1c --- /dev/null +++ b/src/Models/Component.php @@ -0,0 +1,18 @@ +hasMany(Test::class); + } + +} \ No newline at end of file diff --git a/src/Models/Result.php b/src/Models/Result.php index 57a4535..9388bfa 100644 --- a/src/Models/Result.php +++ b/src/Models/Result.php @@ -8,9 +8,6 @@ class Result extends Model { protected $fillable = [ 'test_id', - 'component', - 'benchmark', - 'type', 'average', 'minimum', 'maximum', diff --git a/src/Models/Test.php b/src/Models/Test.php index 25772b5..e5bc171 100644 --- a/src/Models/Test.php +++ b/src/Models/Test.php @@ -7,12 +7,21 @@ use Illuminate\Database\Eloquent\Model; class Test extends Model { protected $fillable = [ - 'name', - 'description', + 'date_tag', + 'benchmark_id', + 'component_id', ]; public function results() { return $this->hasMany(Result::class); } + public function benchmark() { + return $this->belongsTo(Benchmark::class); + } + + public function component() { + return $this->belongsTo(Component::class); + } + } \ No newline at end of file diff --git a/src/routes.php b/src/routes.php index d238d36..81f00c5 100644 --- a/src/routes.php +++ b/src/routes.php @@ -1,12 +1,32 @@ get('/', '\\BitGoblin\\Colossus\\Controllers\\HomeController:getIndex')->setName('dashboard'); -$app->get('/test', '\\BitGoblin\\Colossus\\Controllers\\TestController:getList')->setName('test.list'); -$app->get('/test/add', '\\BitGoblin\\Colossus\\Controllers\\TestController:getAdd')->setName('test.add'); -$app->post('/test/add', '\\BitGoblin\\Colossus\\Controllers\\TestController:postAdd'); -$app->get('/test/{test_id}', '\\BitGoblin\\Colossus\\Controllers\\TestController:getView')->setName('test.view'); +$app->group('/benchmark', function(RouteCollectorProxy $group) { + $group->get('', '\\BitGoblin\\Colossus\\Controllers\\BenchmarkController:getList')->setName('benchmark.list'); + $group->get('/add', '\\BitGoblin\\Colossus\\Controllers\\BenchmarkController:getAdd')->setName('benchmark.add'); + $group->post('/add', '\\BitGoblin\\Colossus\\Controllers\\BenchmarkController:postAdd'); + $group->get('/{test_id}', '\\BitGoblin\\Colossus\\Controllers\\BenchmarkController:getView')->setName('benchmark.view'); +}); -$app->get('/result', '\\BitGoblin\\Colossus\\Controllers\\ResultController:getList')->setName('result.list'); -$app->get('/result/add', '\\BitGoblin\\Colossus\\Controllers\\ResultController:getAdd')->setName('result.add'); -$app->post('/result/add', '\\BitGoblin\\Colossus\\Controllers\\ResultController:postAdd'); +$app->group('/component', function(RouteCollectorProxy $group) { + $group->get('', '\\BitGoblin\\Colossus\\Controllers\\ComponentController:getList')->setName('component.list'); + $group->get('/add', '\\BitGoblin\\Colossus\\Controllers\\ComponentController:getAdd')->setName('component.add'); + $group->post('/add', '\\BitGoblin\\Colossus\\Controllers\\ComponentController:postAdd'); + $group->get('/{test_id}', '\\BitGoblin\\Colossus\\Controllers\\ComponentController:getView')->setName('component.view'); +}); + +$app->group('/test', function(RouteCollectorProxy $group) { + $group->get('', '\\BitGoblin\\Colossus\\Controllers\\TestController:getList')->setName('test.list'); + $group->get('/add', '\\BitGoblin\\Colossus\\Controllers\\TestController:getAdd')->setName('test.add'); + $group->post('/add', '\\BitGoblin\\Colossus\\Controllers\\TestController:postAdd'); + $group->get('/{test_id}', '\\BitGoblin\\Colossus\\Controllers\\TestController:getView')->setName('test.view'); +}); + +$app->group('/result', function(RouteCollectorProxy $group) { + $group->get('', '\\BitGoblin\\Colossus\\Controllers\\ResultController:getList')->setName('result.list'); + $group->get('/add', '\\BitGoblin\\Colossus\\Controllers\\ResultController:getAdd')->setName('result.add'); + $group->post('/add', '\\BitGoblin\\Colossus\\Controllers\\ResultController:postAdd'); +}); diff --git a/views/benchmark/list.twig b/views/benchmark/list.twig new file mode 100644 index 0000000..0077847 --- /dev/null +++ b/views/benchmark/list.twig @@ -0,0 +1,32 @@ +{% extends 'layout.twig' %} + +{% block title %}List of Benchmarks{% endblock %} + +{% block content %} +

Create new Benchmark

+ + {% if benchmarks | length > 0 %} + + + + + + + + + + + {% for b in benchmarks %} + + + + + + + {% endfor %} + +
Benchmark nameDescriptionScoring typeLast updated
{{ b.name }}{{ b.description | slice(0, 100) }}{{ b.scoring }}{{ b.updated_at | date("F jS \\a\\t g:ia") }}
+ {% else %} +

There are no benchmarks in the database - perhaps you should create one?

+ {% endif %} +{% endblock %} diff --git a/views/benchmark/view.twig b/views/benchmark/view.twig new file mode 100644 index 0000000..5c792ce --- /dev/null +++ b/views/benchmark/view.twig @@ -0,0 +1,50 @@ +{% extends 'layout.twig' %} + +{% block title %}Test: {{ test.name }}{% endblock %} + +{% block content %} +
+
+

{{ test.name }}

+

{{ test.description }}

+
+
+ +
+ +
+
+

Test results:

+

Add new result

+ + {% if test.results | length > 0 %} + + + + + + + + + + + + + {% for r in test.results %} + + + + + + + + + {% endfor %} + +
ComponentBenchmarkScoringAvg.Min.Max.
{{ r.component }}{{ r.benchmark }}{{ r.type | capitalize }}{{ r.average }}{{ r.minimum ? r.minimum : 'N/a' }}{{ r.maximum ? r.maximum : 'N/a' }}
+ {% else %} +

There are no results associated with this.

+ {% endif %} +
+
+{% endblock %} diff --git a/views/component/list.twig b/views/component/list.twig new file mode 100644 index 0000000..06aeb9f --- /dev/null +++ b/views/component/list.twig @@ -0,0 +1,30 @@ +{% extends 'layout.twig' %} + +{% block title %}List of Hardware Components{% endblock %} + +{% block content %} +

Create new Hardware Component

+ + {% if components | length > 0 %} + + + + + + + + + + {% for c in components %} + + + + + + {% endfor %} + +
Component nameTypeLast updated
{{ c.name }}{{ c.type }}{{ c.updated_at | date("F jS \\a\\t g:ia") }}
+ {% else %} +

There are no hardware components in the database - perhaps you should create one?

+ {% endif %} +{% endblock %} diff --git a/views/component/view.twig b/views/component/view.twig new file mode 100644 index 0000000..e69de29 diff --git a/views/layout.twig b/views/layout.twig index 5c3cf55..f0e3b79 100644 --- a/views/layout.twig +++ b/views/layout.twig @@ -10,7 +10,7 @@ - {% include 'layout/navbar.twig' %} + {% include 'partials/navbar.twig' %}
{% block content %}{% endblock %} diff --git a/views/layout/navbar.twig b/views/partials/navbar.twig similarity index 73% rename from views/layout/navbar.twig rename to views/partials/navbar.twig index ece3a27..a480e16 100644 --- a/views/layout/navbar.twig +++ b/views/partials/navbar.twig @@ -3,6 +3,8 @@ diff --git a/views/test/view.twig b/views/test/view.twig index 5c792ce..afd7d51 100644 --- a/views/test/view.twig +++ b/views/test/view.twig @@ -32,9 +32,9 @@ {% for r in test.results %} - {{ r.component }} - {{ r.benchmark }} - {{ r.type | capitalize }} + {{ test.component().name }} + {{ test.benchmark().name }} + {{ test.scoring | capitalize }} {{ r.average }} {{ r.minimum ? r.minimum : 'N/a' }} {{ r.maximum ? r.maximum : 'N/a' }}