Add ability to browse data

This commit is contained in:
2025-03-24 12:26:31 +01:00
parent c459148d33
commit dbfbd6e164
9 changed files with 272 additions and 5 deletions

26
data.go
View File

@@ -27,3 +27,29 @@ func (a *App) GetAllRAMTypes() ([]string, error) {
}
return types, err
}
func (a *App) GetAllTypes() ([]string, error) {
var types []string
var err error
for row := range a.db.Query("SELECT type FROM assets GROUP BY type ORDER BY type ASC").Range(&err) {
var name string
err := row.Scan(&name)
if err != nil {
return nil, err
}
types = append(types, name)
}
return types, err
}
func (a *App) GetAssetCount() (int, error) {
var count int
err := a.db.Query("SELECT COUNT(*) FROM assets").ScanSingle(&count)
return count, err
}
func (a *App) GetBrandCount() (int, error) {
var count int
err := a.db.Query("SELECT COUNT(DISTINCT brand) FROM assets").ScanSingle(&count)
return count, err
}

2
go.mod
View File

@@ -5,7 +5,7 @@ go 1.24
toolchain go1.24.1
require (
gitea.seeseepuff.be/seeseemelk/mysqlite v0.7.0
gitea.seeseepuff.be/seeseemelk/mysqlite v0.9.0
github.com/gin-gonic/gin v1.10.0
)

4
go.sum
View File

@@ -1,5 +1,5 @@
gitea.seeseepuff.be/seeseemelk/mysqlite v0.7.0 h1:gq75Ce7QTQ5Rj5fzS/6eeOA/enyV0oDMVt5mejwX14Y=
gitea.seeseepuff.be/seeseemelk/mysqlite v0.7.0/go.mod h1:cgswydOxJjMlNwfcBIXnKjr47LwXnMT9BInkiHb0tXE=
gitea.seeseepuff.be/seeseemelk/mysqlite v0.9.0 h1:GaU2DSrgDfZEqST3HdnNgfKSI4sNXvMm8SSfeMvBxA4=
gitea.seeseepuff.be/seeseemelk/mysqlite v0.9.0/go.mod h1:cgswydOxJjMlNwfcBIXnKjr47LwXnMT9BInkiHb0tXE=
github.com/bytedance/sonic v1.13.1 h1:Jyd5CIvdFnkOWuKXr+wm4Nyk2h0yAFsr8ucJgEasO3g=
github.com/bytedance/sonic v1.13.1/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=

View File

@@ -37,6 +37,8 @@ func main() {
Funcs(template.FuncMap{
"statusText": http.StatusText,
"createDeviceLink": createDeviceLink,
"formatMemorySize": formatMemorySize,
"formatType": formatType,
}).
ParseFS(templateFS, "templates/*.gohtml")
@@ -54,6 +56,7 @@ func main() {
r.GET("/device", app.getDevice)
r.GET("/create", app.getCreateDevice)
r.POST("/create", app.postCreateDevice)
r.GET("/browse", app.getBrowse)
err = r.Run()
if err != nil {
log.Fatalf("error serving website: %v", err)

View File

@@ -1,5 +1,9 @@
package main
import (
"fmt"
)
func createDeviceLink(deviceType, name string, qr *int) CreateDeviceLink {
return CreateDeviceLink{
Type: deviceType,
@@ -13,3 +17,31 @@ type CreateDeviceLink struct {
Name string
Qr *int
}
func formatMemorySize(size int) string {
const (
KB = 1024
MB = KB * 1024
GB = MB * 1024
)
switch {
case size >= GB:
return fmt.Sprintf("%.2f GB", float64(size)/GB)
case size >= MB:
return fmt.Sprintf("%.2f MB", float64(size)/MB)
case size >= KB:
return fmt.Sprintf("%.2f KB", float64(size)/KB)
default:
return fmt.Sprintf("%d B", size)
}
}
func formatType(t string) string {
switch t {
case "ram":
return "Random Access Memory"
default:
return t
}
}

31
templates/browse.gohtml Normal file
View File

@@ -0,0 +1,31 @@
{{- /*gotype: main.BrowseVM */}}
{{define "browse"}}
{{template "header" "Search Results"}}
<table border="1">
<tr>
<th>QR</th>
<th>Type</th>
<th>Name</th>
<th>Brand</th>
<th>Description</th>
{{if .HasRam}}
<th>RAM Type</th>
<th>RAM Capacity</th>
{{end}}
</tr>
{{range .Assets}}
<tr>
<td><a href="/device?id={{.Qr}}">{{.Qr}}</a></td>
<td><a href="/device?id={{.Qr}}">{{.Type | formatType}}</a></td>
<td>{{.Name}}</td>
<td>{{.Brand}}</td>
<td>{{.Description}}</td>
{{if $.HasRam}}
<td>{{.RamType}}</td>
<td>{{.RamCapacity | formatMemorySize}}</td>
{{end}}
</tr>
{{end}}
</table>
{{template "footer"}}
{{end}}

33
templates/device.gohtml Normal file
View File

@@ -0,0 +1,33 @@
{{- /*gotype: main.DeviceVM */}}
{{define "device"}}
{{template "header" "Device Details"}}
<table border="1">
<tr>
<td>Name:</td>
<td>{{.Name}}</td>
</tr>
<tr>
<td>Brand:</td>
<td>{{.Brand}}</td>
</tr>
<tr>
<td>Type:</td>
<td>{{.Type}}</td>
</tr>
<tr>
<td>Description:</td>
<td>{{.Description}}</td>
</tr>
{{if eq .Type "ram"}}
<tr>
<td>RAM Type:</td>
<td>{{.RamType}}</td>
</tr>
<tr>
<td>RAM Capacity:</td>
<td>{{.RamCapacity | formatMemorySize}}</td>
</tr>
{{end}}
</table>
{{template "footer"}}
{{end}}

View File

@@ -1,9 +1,32 @@
{{- /*gotype: main.IndexVM */}}
{{define "index"}}
{{template "header"}}
Some statistics:
<h2>Some statistics</h2>
<ul>
<li>Database contains {{.AssetCount}} assets.</li>
<li>Database contains {{.BrandCount}} brands.</li>
</ul>
<h2>Filter Devices</h2>
<form action="/browse" method="get">
<h3>Select Brands:</h3>
{{range .Brands}}
<label>
<input type="checkbox" name="brand" checked value="{{.}}">
{{.}}
</label><br>
{{end}}
<h3>Select Types:</h3>
{{range .Types}}
<label>
<input type="checkbox" name="type" checked value="{{.}}">
{{. | formatType}}
</label><br>
{{end}}
<button type="submit">Browse</button>
</form>
{{template "footer"}}
{{end}}

121
views.go
View File

@@ -16,11 +16,33 @@ type App struct {
type IndexVM struct {
AssetCount int
BrandCount int
Brands []string
Types []string
}
func (a *App) getIndex(c *gin.Context) {
vm := &IndexVM{}
err := a.db.Query("SELECT COUNT(*) FROM assets").ScanSingle(&vm.AssetCount)
var err error
vm.AssetCount, err = a.GetAssetCount()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
vm.BrandCount, err = a.GetBrandCount()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
vm.Brands, err = a.GetAllBrands()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
vm.Types, err = a.GetAllTypes()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
@@ -29,6 +51,15 @@ func (a *App) getIndex(c *gin.Context) {
c.HTML(http.StatusOK, "index", vm)
}
type DeviceVM struct {
Name string
Brand string
Type string
Description string
RamType string
RamCapacity int
}
func (a *App) getDevice(c *gin.Context) {
qr, err := strconv.Atoi(c.Query("id"))
if err != nil {
@@ -49,6 +80,27 @@ func (a *App) getDevice(c *gin.Context) {
c.Redirect(http.StatusTemporaryRedirect, "/create?id="+strconv.Itoa(qr))
return
}
vm := &DeviceVM{}
err = a.db.Query("SELECT name, brand, type, description FROM assets WHERE qr = ?").
Bind(qr).
ScanSingle(&vm.Name, &vm.Brand, &vm.Type, &vm.Description)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if vm.Type == "ram" {
err = a.db.Query("SELECT type, capacity FROM info_ram WHERE asset = ?").
Bind(qr).
ScanSingle(&vm.RamType, &vm.RamCapacity)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
c.HTML(http.StatusOK, "device", vm)
}
type CreateDeviceVM struct {
@@ -153,3 +205,70 @@ func (a *App) postCreateDeviceRam(c *gin.Context, qr int) error {
}
return tx.Commit()
}
type BrowseVM struct {
Assets []Asset
HasRam bool
}
type Asset struct {
Qr int
Name string
Brand string
Type string
Description string
RamType string
RamCapacity int
}
func (a *App) getBrowse(c *gin.Context) {
brands := c.QueryArray("brand")
types := c.QueryArray("type")
query := `SELECT assets.qr, assets.name, assets.brand, assets.type, assets.description,
info_ram.type, info_ram.capacity
FROM assets
JOIN info_ram ON info_ram.asset = assets.qr
WHERE 1=1`
if len(brands) > 0 {
query += " AND assets.brand IN (" + placeholders(len(brands)) + ")"
}
if len(types) > 0 {
query += " AND assets.type IN (" + placeholders(len(types)) + ")"
}
vm := &BrowseVM{}
var err error
q := a.db.Query(query).Bind(brands, types)
for row := range q.Range(&err) {
var asset Asset
err := row.Scan(&asset.Qr, &asset.Name, &asset.Brand, &asset.Type, &asset.Description, &asset.RamType, &asset.RamCapacity)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
vm.Assets = append(vm.Assets, asset)
if asset.Type == "ram" {
vm.HasRam = true
}
}
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.HTML(http.StatusOK, "browse", vm)
}
func placeholders(count int) string {
if count == 0 {
return ""
}
placeholder := "?"
for count > 1 {
placeholder += ", ?"
count--
}
return placeholder
}