smon/dashboard.go
2026-08-21 11:03:14 +02:00

69 lines
1.1 KiB
Go

package main
import (
// External
werr "git.ahall.se/go/wrappederror"
"github.com/jackc/pgx/v5"
// Standard
"context"
"encoding/json"
)
type WidgetType int
const (
WIDGET_SPACE WidgetType = iota
WIDGET_LABEL
WIDGET_ONOFF
)
type Widget struct {
Type WidgetType
X int
Y int
SpanX int
SpanY int
DatapointID int
Attributes map[string]string
}
type Dashboard struct {
ID int
Name string
Widgets []Widget
}
func DashboardGet(name string) (dashboard Dashboard, err error) {
var rows pgx.Rows
rows, err = db.Query(context.Background(), `SELECT * FROM dashboard WHERE name = $1`, name)
if err != nil {
err = werr.Wrap(err)
return
}
dashboard, err = pgx.CollectExactlyOneRow(rows, pgx.RowToStructByNameLax[Dashboard])
if err != nil {
err = werr.Wrap(err)
return
}
return
}
func DashboardUpdate(dashboard Dashboard) error {
widgets, _ := json.Marshal(dashboard.Widgets)
_, err := db.Exec(
context.Background(),
`UPDATE dashboard SET name=$2, widgets=$3 WHERE id=$1`,
dashboard.ID,
dashboard.Name,
widgets,
)
if err != nil {
return werr.Wrap(err)
}
return nil
}