Trigger expression evaluated and problems managed

This commit is contained in:
Magnus Åhall 2024-04-30 21:23:05 +02:00
parent ffe0c3def2
commit b0a0f9290e
3 changed files with 82 additions and 20 deletions

36
main.go
View File

@ -172,10 +172,42 @@ func entryDatapoint(w http.ResponseWriter, r *http.Request, sess *session.T) { /
var triggers []Trigger
triggers, err = TriggersRetrieveByDatapoint(dpoint)
for _, trigger := range triggers {
trigger.Run()
if err != nil {
logger.Error("entry", "error", err)
}
for _, trigger := range triggers {
var out any
out, err = trigger.Run()
if err != nil {
err = we.Wrap(err).Log()
logger.Error("entry", "error", err)
}
logger.Debug("entry", "datapoint", dpoint, "value", value, "trigger", trigger, "result", out)
switch v := out.(type) {
case bool:
// Trigger returning true - a problem occured
if v {
err = ProblemStart(trigger)
if err != nil {
err = we.Wrap(err).Log()
logger.Error("entry", "error", err)
}
} else {
err = ProblemClose(trigger)
if err != nil {
err = we.Wrap(err).Log()
logger.Error("entry", "error", err)
}
}
default:
err := fmt.Errorf(`Expression for trigger %s not returning bool (%T)`, trigger.Name, v)
logger.Info("entry", "error", err)
we.Wrap(err).WithData(v).Log()
}
}
j, _ := json.Marshal(struct{ OK bool }{true})
w.Write(j)

View File

@ -2,10 +2,10 @@ package main
import (
// External
// we "git.gibonuddevalla.se/go/wrappederror"
we "git.gibonuddevalla.se/go/wrappederror"
// Standard
// "encoding/json"
"database/sql"
)
/*
@ -18,3 +18,39 @@ type Problem struct {
}
*/
func ProblemStart(trigger Trigger) (err error) {
row := service.Db.Conn.QueryRow(`
SELECT COUNT(id)
FROM problem
WHERE
trigger_id = $1 AND
"end" IS NULL
GROUP BY trigger_id
`,
trigger.ID,
)
var openProblems int
err = row.Scan(&openProblems)
if err != nil && err != sql.ErrNoRows {
err = we.Wrap(err).WithData(trigger.ID)
return
}
// Open up a new problem if no open exists.
if openProblems == 0 {
_, err = service.Db.Conn.Exec(`INSERT INTO problem(trigger_id) VALUES($1)`, trigger.ID)
if err != nil {
err = we.Wrap(err).WithData(trigger)
}
}
return
}
func ProblemClose(trigger Trigger) (err error) {
_, err = service.Db.Conn.Exec(`UPDATE problem SET "end"=NOW() WHERE trigger_id=$1 AND "end" IS NULL`, trigger.ID)
if err != nil {
err = we.Wrap(err).WithData(trigger)
return
}
return
}

View File

@ -4,7 +4,6 @@ import (
// External
we "git.gibonuddevalla.se/go/wrappederror"
"github.com/expr-lang/expr"
"github.com/jmoiron/sqlx"
// Standard
"encoding/json"
@ -76,31 +75,26 @@ func TriggersRetrieve() (areas []Area, err error) { // {{{
} // }}}
func TriggersRetrieveByDatapoint(datapointName string) (triggers []Trigger, err error) { // {{{
triggers = []Trigger{}
var rows *sqlx.Rows
rows, err = service.Db.Conn.Queryx(`
SELECT
*
FROM public."trigger"
row := service.Db.Conn.QueryRow(`
SELECT jsonb_agg(t.*)
FROM public."trigger" t
WHERE
datapoints @> $1
`,
fmt.Sprintf(`["%s"]`, datapointName),
)
var data []byte
err = row.Scan(&data)
if err != nil {
err = we.Wrap(err).WithData(datapointName)
return
}
defer rows.Close()
for rows.Next() {
trigger := Trigger{}
err = rows.StructScan(&trigger)
if err != nil {
err = we.Wrap(err).WithData(datapointName)
return
}
triggers = append(triggers, trigger)
err = json.Unmarshal(data, &triggers)
if err != nil {
err = we.Wrap(err).WithData(datapointName)
return
}
return