Added groups for datapoints

This commit is contained in:
Magnus Åhall 2024-05-20 19:40:19 +02:00
parent 63956de837
commit d3d6d5d13d
7 changed files with 48 additions and 8 deletions

View File

@ -22,6 +22,7 @@ const (
type Datapoint struct { type Datapoint struct {
ID int ID int
Group string
Name string Name string
Datatype DatapointType Datatype DatapointType
LastValue time.Time `db:"last_value"` LastValue time.Time `db:"last_value"`
@ -70,14 +71,16 @@ func (dp Datapoint) Update() (err error) {// {{{
if dp.ID == 0 { if dp.ID == 0 {
_, err = service.Db.Conn.Exec( _, err = service.Db.Conn.Exec(
`INSERT INTO datapoint(name, datatype) VALUES($1, $2)`, `INSERT INTO datapoint("group", name, datatype) VALUES($1, $2, $3)`,
dp.Group,
name, name,
dp.Datatype, dp.Datatype,
) )
} else { } else {
_, err = service.Db.Conn.Exec( _, err = service.Db.Conn.Exec(
`UPDATE datapoint SET name=$2, datatype=$3 WHERE id=$1`, `UPDATE datapoint SET "group"=$2, name=$3, datatype=$4 WHERE id=$1`,
dp.ID, dp.ID,
dp.Group,
name, name,
dp.Datatype, dp.Datatype,
) )
@ -131,6 +134,7 @@ func DatapointsRetrieve() (dps []Datapoint, err error) { // {{{
dp.name, dp.name,
dp.datatype, dp.datatype,
dp.last_value, dp.last_value,
dp.group,
dpv.id AS v_id, dpv.id AS v_id,
dpv.ts, dpv.ts,
@ -146,6 +150,7 @@ func DatapointsRetrieve() (dps []Datapoint, err error) { // {{{
FROM datapoint_value FROM datapoint_value
) dpv ON dpv.datapoint_id = dp.id AND rn = 1 ) dpv ON dpv.datapoint_id = dp.id AND rn = 1
ORDER BY ORDER BY
dp.group ASC,
dp.name ASC dp.name ASC
`) `)
if err != nil { if err != nil {
@ -155,6 +160,7 @@ func DatapointsRetrieve() (dps []Datapoint, err error) { // {{{
type DbRes struct { type DbRes struct {
ID int ID int
Group string
Name string Name string
Datatype DatapointType Datatype DatapointType
LastValue time.Time `db:"last_value"` LastValue time.Time `db:"last_value"`
@ -178,6 +184,7 @@ func DatapointsRetrieve() (dps []Datapoint, err error) { // {{{
dp.ID = res.ID dp.ID = res.ID
dp.Name = res.Name dp.Name = res.Name
dp.Group = res.Group
dp.Datatype = res.Datatype dp.Datatype = res.Datatype
dp.LastValue = res.LastValue dp.LastValue = res.LastValue
dp.Found = true dp.Found = true

View File

@ -555,6 +555,7 @@ func pageDatapointUpdate(w http.ResponseWriter, r *http.Request, _ *session.T) {
var dp Datapoint var dp Datapoint
dp.ID = id dp.ID = id
dp.Group = r.FormValue("group")
dp.Name = r.FormValue("name") dp.Name = r.FormValue("name")
dp.Datatype = DatapointType(r.FormValue("datatype")) dp.Datatype = DatapointType(r.FormValue("datatype"))
err = dp.Update() err = dp.Update()

1
sql/00013.sql Normal file
View File

@ -0,0 +1 @@
ALTER TABLE datapoint ADD COLUMN "group" varchar NOT NULL DEFAULT '';

View File

@ -103,8 +103,18 @@ label {
grid-gap: 8px 16px; grid-gap: 8px 16px;
margin-top: 16px; margin-top: 16px;
} }
#datapoints .group {
font-size: 1.1em;
font-weight: bold;
color: #fabd2f;
margin-top: 1.5em;
padding-bottom: 4px;
border-bottom: unset;
}
#datapoints .header { #datapoints .header {
font-weight: 500; font-weight: 500;
font-size: 0.85em;
color: #d5c4a1;
} }
#datapoints div { #datapoints div {
white-space: nowrap; white-space: nowrap;

View File

@ -6,8 +6,20 @@
grid-gap: 8px 16px; grid-gap: 8px 16px;
margin-top: 16px; margin-top: 16px;
.group {
font-size: 1.10em;
font-weight: bold;
color: @color2;
margin-top: 1.5em;
padding-bottom: 4px;
border-bottom: unset;
}
.header { .header {
font-weight: @bold; font-weight: @bold;
font-size: 0.85em;
color: @text1;
} }
div { div {

View File

@ -10,6 +10,9 @@
<form id="form-trigger" action="/datapoint/update/{{ .Data.Datapoint.ID }}" method="post"> <form id="form-trigger" action="/datapoint/update/{{ .Data.Datapoint.ID }}" method="post">
<div id="widgets" class="widgets"> <div id="widgets" class="widgets">
<div class="label">Group</div>
<div><input type="text" name="group" value="{{ .Data.Datapoint.Group }}"></div>
<div class="label">Name</div> <div class="label">Name</div>
<div><input type="text" name="name" value="{{ .Data.Datapoint.Name }}"></div> <div><input type="text" name="name" value="{{ .Data.Datapoint.Name }}"></div>

View File

@ -7,14 +7,19 @@
<a href="/datapoint/edit/0">Create</a> <a href="/datapoint/edit/0">Create</a>
<div id="datapoints"> <div id="datapoints">
<div class="header">Name</div> {{ $prevGroup := "kalle" }}
<div class="header">Datatype</div>
<div class="header">Last value</div>
<div class="header">Value</div>
<div class="header"></div>
{{ range .Data.Datapoints }} {{ range .Data.Datapoints }}
{{ if ne $prevGroup .Group }}
<div class="line group">{{ .Group }}</div>
<div class="header">Name</div>
<div class="header">Datatype</div>
<div class="header">Last value</div>
<div class="header">Value</div>
<div class="header"></div>
{{ else }}
<div class="line"></div> <div class="line"></div>
{{ end }}
<div class="name"><a href="/datapoint/edit/{{ .ID }}">{{ .Name }}</a></div> <div class="name"><a href="/datapoint/edit/{{ .ID }}">{{ .Name }}</a></div>
<div class="datatype">{{ .Datatype }}</div> <div class="datatype">{{ .Datatype }}</div>
<div class="last-value">{{ format_time .LastValue }}</div> <div class="last-value">{{ format_time .LastValue }}</div>
@ -27,6 +32,7 @@
<div class="values"><a href="/datapoint/values/{{ .ID }}"><img src="/images/{{ $version }}/values.svg"></a></div> <div class="values"><a href="/datapoint/values/{{ .ID }}"><img src="/images/{{ $version }}/values.svg"></a></div>
<div class="delete"><a href="/datapoint/delete/{{ .ID }}" onclick="confirm(`Are you sure you want to delete '{{ .Name }}'?`)"><img src="/images/{{ $version }}/delete.svg"></a></div> <div class="delete"><a href="/datapoint/delete/{{ .ID }}" onclick="confirm(`Are you sure you want to delete '{{ .Name }}'?`)"><img src="/images/{{ $version }}/delete.svg"></a></div>
</div> </div>
{{ $prevGroup = .Group }}
{{ end }} {{ end }}
</div> </div>
{{ end }} {{ end }}