Adding datapoints to triggers

This commit is contained in:
Magnus Åhall 2024-05-01 10:02:33 +02:00
parent b0a0f9290e
commit c746343dc0
15 changed files with 269 additions and 90 deletions

View file

@ -4,6 +4,7 @@ import (
// External
we "git.gibonuddevalla.se/go/wrappederror"
"github.com/expr-lang/expr"
"github.com/lib/pq"
// Standard
"encoding/json"
@ -19,9 +20,6 @@ type Trigger struct {
Datapoints []string
}
func Foo() {
}
func TriggersRetrieve() (areas []Area, err error) { // {{{
areas = []Area{}
@ -130,21 +128,50 @@ func (t *Trigger) Update() (err error) { // {{{
return
}
logger.Info("FOO", "trigger", t)
_, err = service.Db.Conn.Exec(`
UPDATE "trigger"
SET
name=$2,
expression=$3
WHERE
id=$1
`,
t.ID,
t.Name,
t.Expression,
)
if err != nil {
err = we.Wrap(err)
if t.Datapoints == nil {
t.Datapoints = []string{}
}
jsonDatapoints, _ := json.Marshal(t.Datapoints)
if t.ID == 0 {
_, err = service.Db.Conn.Exec(`
INSERT INTO "trigger"(name, section_id, expression, datapoints)
VALUES($1, $2, $3, $4)
`,
t.Name,
t.SectionID,
t.Expression,
jsonDatapoints,
)
} else {
_, err = service.Db.Conn.Exec(`
UPDATE "trigger"
SET
name=$2,
expression=$3,
datapoints=$4
WHERE
id=$1
`,
t.ID,
t.Name,
t.Expression,
jsonDatapoints,
)
}
if pqErr, ok := err.(*pq.Error); ok {
err = we.Wrap(err).WithData(
struct {
Trigger *Trigger
PostgresCode pq.ErrorCode
PostgresMsg string
}{
t,
pqErr.Code,
pqErr.Code.Name(),
})
} else if err != nil {
err = we.Wrap(err).WithData(t)
}
return
} // }}}