Started work to re-work database; add new models for benchmarks and components

This commit is contained in:
Gregory Ballantine 2023-09-22 23:03:24 -06:00
parent e176398f41
commit 0dd7098681
18 changed files with 343 additions and 51 deletions

View File

@ -23,5 +23,8 @@
"illuminate/database": "^9.41", "illuminate/database": "^9.41",
"robmorgan/phinx": "^0.13.1", "robmorgan/phinx": "^0.13.1",
"hassankhan/config": "^3.0" "hassankhan/config": "^3.0"
},
"scripts": {
"phinx": "./vendor/bin/phinx"
} }
} }

View File

@ -1,32 +0,0 @@
<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class AddResultsTable extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->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();
}
}

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class AddInitialTables extends AbstractMigration {
public function change(): void {
// hardware components
$components_table = $this->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();
}
}

View File

@ -19,10 +19,12 @@ final class AddTestsTable extends AbstractMigration
public function change() public function change()
{ {
$table = $this->table('tests'); $table = $this->table('tests');
$table->addColumn('name', 'string', ['null' => false]) $table->addColumn('date_tag', 'string', ['null' => false])
->addColumn('description', 'text', ['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() ->addTimestamps()
->addIndex(['name'])
->create(); ->create();
$results = $this->table('results'); $results = $this->table('results');

View File

@ -0,0 +1,54 @@
<?php
namespace BitGoblin\Colossus\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Routing\RouteContext;
use Slim\Views\Twig;
use BitGoblin\Colossus\Models\Benchmark;
class BenchmarkController extends Controller {
public function getList(Request $request, Response $response): Response {
$benchmarks = Benchmark::orderByDesc('updated_at')->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);
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace BitGoblin\Colossus\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Routing\RouteContext;
use Slim\Views\Twig;
use BitGoblin\Colossus\Models\Component;
class ComponentController extends Controller {
public function getList(Request $request, Response $response): Response {
$components = Component::orderByDesc('updated_at')->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);
}
}

19
src/Models/Benchmark.php Normal file
View File

@ -0,0 +1,19 @@
<?php
namespace BitGoblin\Colossus\Models;
use Illuminate\Database\Eloquent\Model;
class Benchmark extends Model {
protected $fillable = [
'name',
'description',
'scoring',
];
public function tests() {
return $this->hasMany(Test::class);
}
}

18
src/Models/Component.php Normal file
View File

@ -0,0 +1,18 @@
<?php
namespace BitGoblin\Colossus\Models;
use Illuminate\Database\Eloquent\Model;
class Component extends Model {
protected $fillable = [
'name',
'type',
];
public function tests() {
return $this->hasMany(Test::class);
}
}

View File

@ -8,9 +8,6 @@ class Result extends Model {
protected $fillable = [ protected $fillable = [
'test_id', 'test_id',
'component',
'benchmark',
'type',
'average', 'average',
'minimum', 'minimum',
'maximum', 'maximum',

View File

@ -7,12 +7,21 @@ use Illuminate\Database\Eloquent\Model;
class Test extends Model { class Test extends Model {
protected $fillable = [ protected $fillable = [
'name', 'date_tag',
'description', 'benchmark_id',
'component_id',
]; ];
public function results() { public function results() {
return $this->hasMany(Result::class); return $this->hasMany(Result::class);
} }
public function benchmark() {
return $this->belongsTo(Benchmark::class);
}
public function component() {
return $this->belongsTo(Component::class);
}
} }

View File

@ -1,12 +1,32 @@
<?php <?php
use Slim\Routing\RouteCollectorProxy;
$app->get('/', '\\BitGoblin\\Colossus\\Controllers\\HomeController:getIndex')->setName('dashboard'); $app->get('/', '\\BitGoblin\\Colossus\\Controllers\\HomeController:getIndex')->setName('dashboard');
$app->get('/test', '\\BitGoblin\\Colossus\\Controllers\\TestController:getList')->setName('test.list'); $app->group('/benchmark', function(RouteCollectorProxy $group) {
$app->get('/test/add', '\\BitGoblin\\Colossus\\Controllers\\TestController:getAdd')->setName('test.add'); $group->get('', '\\BitGoblin\\Colossus\\Controllers\\BenchmarkController:getList')->setName('benchmark.list');
$app->post('/test/add', '\\BitGoblin\\Colossus\\Controllers\\TestController:postAdd'); $group->get('/add', '\\BitGoblin\\Colossus\\Controllers\\BenchmarkController:getAdd')->setName('benchmark.add');
$app->get('/test/{test_id}', '\\BitGoblin\\Colossus\\Controllers\\TestController:getView')->setName('test.view'); $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->group('/component', function(RouteCollectorProxy $group) {
$app->get('/result/add', '\\BitGoblin\\Colossus\\Controllers\\ResultController:getAdd')->setName('result.add'); $group->get('', '\\BitGoblin\\Colossus\\Controllers\\ComponentController:getList')->setName('component.list');
$app->post('/result/add', '\\BitGoblin\\Colossus\\Controllers\\ResultController:postAdd'); $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');
});

32
views/benchmark/list.twig Normal file
View File

@ -0,0 +1,32 @@
{% extends 'layout.twig' %}
{% block title %}List of Benchmarks{% endblock %}
{% block content %}
<p><a href="{{ url_for('benchmark.add') }}">Create new Benchmark</a></p>
{% if benchmarks | length > 0 %}
<table class="u-full-width">
<thead>
<tr>
<th>Benchmark name</th>
<th>Description</th>
<th>Scoring type</th>
<th>Last updated</th>
</tr>
</thead>
<tbody>
{% for b in benchmarks %}
<tr>
<td><a href="{{ url_for('benchmark.view', { benchmark_id: b.id }) }}">{{ b.name }}</a></td>
<td>{{ b.description | slice(0, 100) }}</td>
<td>{{ b.scoring }}</td>
<td>{{ b.updated_at | date("F jS \\a\\t g:ia") }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>There are no benchmarks in the database - perhaps you should <a href="{{ url_for('benchmark.add') }}">create one</a>?</p>
{% endif %}
{% endblock %}

50
views/benchmark/view.twig Normal file
View File

@ -0,0 +1,50 @@
{% extends 'layout.twig' %}
{% block title %}Test: {{ test.name }}{% endblock %}
{% block content %}
<div class="row">
<div class="twelve columns">
<h1>{{ test.name }}</h1>
<p>{{ test.description }}</p>
</div>
</div>
<hr>
<div class="row">
<div class="twelve columns">
<h3>Test results:</h3>
<p><a href="{{ url_for('result.add') }}">Add new result</a></p>
{% if test.results | length > 0 %}
<table class="u-full-width">
<thead>
<tr>
<th>Component</th>
<th>Benchmark</th>
<th>Scoring</th>
<th>Avg.</th>
<th>Min.</th>
<th>Max.</th>
</tr>
</thead>
<tbody>
{% for r in test.results %}
<tr>
<td>{{ r.component }}</td>
<td>{{ r.benchmark }}</td>
<td>{{ r.type | capitalize }}</td>
<td>{{ r.average }}</td>
<td>{{ r.minimum ? r.minimum : 'N/a' }}</td>
<td>{{ r.maximum ? r.maximum : 'N/a' }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>There are no results associated with this.</p>
{% endif %}
</div>
</div>
{% endblock %}

30
views/component/list.twig Normal file
View File

@ -0,0 +1,30 @@
{% extends 'layout.twig' %}
{% block title %}List of Hardware Components{% endblock %}
{% block content %}
<p><a href="{{ url_for('component.add') }}">Create new Hardware Component</a></p>
{% if components | length > 0 %}
<table class="u-full-width">
<thead>
<tr>
<th>Component name</th>
<th>Type</th>
<th>Last updated</th>
</tr>
</thead>
<tbody>
{% for c in components %}
<tr>
<td><a href="{{ url_for('component.view', { component_id: c.id }) }}">{{ c.name }}</a></td>
<td>{{ c.type }}</td>
<td>{{ c.updated_at | date("F jS \\a\\t g:ia") }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>There are no hardware components in the database - perhaps you should <a href="{{ url_for('component.add') }}">create one</a>?</p>
{% endif %}
{% endblock %}

View File

View File

@ -10,7 +10,7 @@
<script src="/js/bedabin.min.js"></script> <script src="/js/bedabin.min.js"></script>
</head> </head>
<body> <body>
{% include 'layout/navbar.twig' %} {% include 'partials/navbar.twig' %}
<div id="main-wrapper" class="container"> <div id="main-wrapper" class="container">
{% block content %}{% endblock %} {% block content %}{% endblock %}

View File

@ -3,6 +3,8 @@
<ul class="nav-left"> <ul class="nav-left">
<li class="site-logo">Colossus</li> <li class="site-logo">Colossus</li>
<li><a href="{{ url_for('dashboard') }}">Dashboard</a></li> <li><a href="{{ url_for('dashboard') }}">Dashboard</a></li>
<li><a href="/benchmark">Benchmarks</a></li>
<li><a href="/component">Components</a></li>
<li><a href="{{ url_for('test.list') }}">Test</a></li> <li><a href="{{ url_for('test.list') }}">Test</a></li>
<li><a href="/result">Results</a></li> <li><a href="/result">Results</a></li>
</ul> </ul>

View File

@ -32,9 +32,9 @@
<tbody> <tbody>
{% for r in test.results %} {% for r in test.results %}
<tr> <tr>
<td>{{ r.component }}</td> <td>{{ test.component().name }}</td>
<td>{{ r.benchmark }}</td> <td>{{ test.benchmark().name }}</td>
<td>{{ r.type | capitalize }}</td> <td>{{ test.scoring | capitalize }}</td>
<td>{{ r.average }}</td> <td>{{ r.average }}</td>
<td>{{ r.minimum ? r.minimum : 'N/a' }}</td> <td>{{ r.minimum ? r.minimum : 'N/a' }}</td>
<td>{{ r.maximum ? r.maximum : 'N/a' }}</td> <td>{{ r.maximum ? r.maximum : 'N/a' }}</td>