Added benchmarks routes and views

This commit is contained in:
Gregory Ballantine 2024-05-29 09:11:13 -04:00
parent 95fe5ab400
commit ba06a2ea4c
7 changed files with 176 additions and 6 deletions

View File

@ -0,0 +1,38 @@
{{ template "header" . }}
<div class="row">
<h2>Add new benchmark</h2>
<form class="twelve columns" action="/benchmark/create" method="POST">
<div class="row">
<div class="nine columns">
<label for="benchmark_name">
Benchmark name:
<input id="benchmark_name" class="u-full-width" type="text" name="benchmark_name" placeholder="Unigine Heaven">
</label>
</div>
<div class="three columns">
<label for="benchmark_scoring">
Benchmark type:
<select id="benchmark_scoring" class="u-full-width" name="benchmark_scoring">
<option value="fps">Frames per second</option>
<option value="ms">Frame time</option>
<option value="pts">Total points</option>
</select>
</label>
</div>
</div>
<div class="row">
<label for="benchmark_description">
Benchmark description:
<textarea id="benchmark_description" class="twelve columns" cols="30" rows="10" name="benchmark_description"></textarea>
</label>
</div>
<input class="button-primary u-full-width" type="submit" value="Submit">
</form>
</div>
{{ template "footer" . }}

View File

@ -0,0 +1,27 @@
{{ template "header" . }}
<div class="row">
<h2>Benchmark</h2>
<a href="/benchmark/create">Add new benchmark</a>
<table class="twelve columns">
<thead>
<tr>
<td>Name</td>
<td>Created at</td>
<td>Last updated</td>
</tr>
</thead>
<tbody>
{{ range $h := .benchmark }}
<tr>
<td><a href="/benchmark/{{ $h.ID }}">{{ $h.Name }}</a></td>
<td>{{ $h.CreatedAt.Format "01/02/2006 15:04am" }}</td>
<td>{{ $h.UpdatedAt.Format "01/02/2006 15:04am" }}</td>
</tr>
{{ end }}
</tbody>
</table>
</div>
{{ template "footer" . }}

View File

@ -0,0 +1,19 @@
{{ template "header" . }}
<div class="row">
<h2>{{ .benchmark.Name }}</h2>
<p>{{ .benchmark.ScoringType }}</p>
<hr>
<h4>Latest Benchmark Results:</h4>
<p>There are currently no results recorded using this benchmark.</p>
<hr>
<p><a href="/benchmark">Back</a></p>
</div>
{{ template "footer" . }}

7
web/forms/benchmark.go Normal file
View File

@ -0,0 +1,7 @@
package forms
type BenchmarkForm struct {
Name string `form:"benchmark_name" validate:"required"`
ScoringType string `form:"benchmark_scoring" validate:"required"`
Description string `form:"benchmark_description"`
}

View File

@ -26,6 +26,20 @@ func RegisterRoutes(f *flamego.Flame) {
f.Get("/{hardware_id}", routes.HardwareGetView)
})
// benchmark routes
f.Group("/benchmark", func() {
f.Get("", func(c flamego.Context) {
c.Redirect("/benchmark/list")
})
f.Get("/list", routes.BenchmarkGetList)
f.Get("/create", routes.BenchmarkGetCreate)
f.Post("/create", binding.Form(forms.BenchmarkForm{}), routes.BenchmarkPostCreate)
f.Get("/{benchmark_id}", routes.BenchmarkGetView)
})
// test routes
f.Group("/test", func() {
f.Get("", func(c flamego.Context) {

65
web/routes/benchmark.go Normal file
View File

@ -0,0 +1,65 @@
package routes
import (
"fmt"
"log"
"net/http"
"github.com/flamego/binding"
"github.com/flamego/flamego"
"github.com/flamego/template"
"github.com/flamego/validator"
"git.metaunix.net/bitgoblin/blt/models"
"git.metaunix.net/bitgoblin/blt/web/forms"
)
func HardwareGetList(t template.Template, data template.Data) {
// add hardwares to template
var hardware []models.Hardware
models.DB.First(&hardware)
data["hardware"] = hardware
data["title"] = "List of Hardware"
t.HTML(http.StatusOK, "hardware/list")
}
func HardwareGetView(c flamego.Context, t template.Template, data template.Data) {
// find hardware ID from request
hardwareID := c.Param("hardware_id")
// find hardware from DB
var hardware models.Hardware
models.DB.Find(&hardware, hardwareID)
data["hardware"] = hardware
data["title"] = hardware.Name
t.HTML(http.StatusOK, "hardware/view")
}
func HardwareGetCreate(t template.Template, data template.Data) {
data["title"] = "Add New Hardware"
t.HTML(http.StatusOK, "hardware/create")
}
func HardwarePostCreate(c flamego.Context, form forms.HardwareForm, errs binding.Errors) {
if len(errs) > 0 {
var err error
switch errs[0].Category {
case binding.ErrorCategoryValidation:
err = errs[0].Err.(validator.ValidationErrors)[0]
default:
err = errs[0].Err
}
log.Fatal(err)
}
hardware := models.Hardware{
Name: form.Name,
Type: form.Type,
}
_ = models.DB.Create(&hardware)
c.Redirect(fmt.Sprintf("/hardware/%d", hardware.ID))
}

View File

@ -15,13 +15,13 @@ import (
)
func HardwareGetList(t template.Template, data template.Data) {
// add hardwares to template
var hardware []models.Hardware
models.DB.First(&hardware)
data["hardware"] = hardware
// add benchmarks to template
var benchmarks []models.Benchmark
models.DB.First(&benchmarks)
data["benchmarks"] = benchmarks
data["title"] = "List of Hardware"
t.HTML(http.StatusOK, "hardware/list")
data["title"] = "List of Benchmarks"
t.HTML(http.StatusOK, "benchmark/list")
}
func HardwareGetView(c flamego.Context, t template.Template, data template.Data) {