Added start of editable dashboard

This commit is contained in:
Magnus Åhall 2026-08-20 11:13:03 +02:00
parent 1af3f59b1a
commit c3fe5951d4
8 changed files with 502 additions and 92 deletions

View file

@ -30,11 +30,32 @@ type Datapoint struct {
LastValue time.Time `db:"last_value"`
DatapointValueJSON []byte `db:"datapoint_value_json"`
LastDatapointValue DatapointValue
LastDatapointID *int `db:"last_value_id"`
LastValueInt *int `db:"last_value_int"`
LastValueString *string `db:"last_value_string"`
LastValueDateTime *time.Time `db:"last_value_datetime"`
Found bool
NodataProblemSeconds int `db:"nodata_problem_seconds"`
NodataIsProblem bool `db:"nodata_is_problem"`
}
type DatapointBrief struct {
ID int
Name string
Comment string
Datatype DatapointType
LastValueInt *int `db:"last_value_int"`
LastValueString *string `db:"last_value_string"`
LastValueDateTime *time.Time `db:"last_value_datetime"`
NodataIsProblem bool `db:"nodata_is_problem"`
}
type DatapointTiny struct {
ID int
Value any
Valid bool
}
type DatapointValue struct {
ID int
DatapointID int `db:"datapoint_id"`
@ -370,3 +391,53 @@ func DatapointValues(id int, from, to time.Time) (values []DatapointValue, err e
return
} // }}}
// DatapointsValue only returns ID and type and is used primarily for the dashboard.
func DatapointsValue(ids []int) (values []DatapointTiny, err error) { // {{{
values = []DatapointTiny{}
rows, _ := db.Query(
context.Background(),
`
SELECT
id,
name,
comment,
datatype,
last_value_int,
last_value_string,
last_value_datetime,
nodata_is_problem
FROM datapoint
WHERE
id = ANY($1)
`,
ids,
)
var briefs []DatapointBrief
briefs, err = pgx.CollectRows(rows, pgx.RowToStructByNameLax[DatapointBrief])
if err != nil {
err = werr.Wrap(err)
return
}
for _, brief := range briefs {
tiny := DatapointTiny{}
tiny.ID = brief.ID
tiny.Valid = !brief.NodataIsProblem
switch brief.Datatype {
case "INT":
tiny.Value = brief.LastValueInt
case "STRING":
tiny.Value = brief.LastValueString
case "DATETIME":
tiny.Value = brief.LastValueDateTime
}
values = append(values, tiny)
}
return
} // }}}