Trigger expression evaluated and problems managed
This commit is contained in:
parent
ffe0c3def2
commit
b0a0f9290e
36
main.go
36
main.go
@ -172,10 +172,42 @@ func entryDatapoint(w http.ResponseWriter, r *http.Request, sess *session.T) { /
|
|||||||
|
|
||||||
var triggers []Trigger
|
var triggers []Trigger
|
||||||
triggers, err = TriggersRetrieveByDatapoint(dpoint)
|
triggers, err = TriggersRetrieveByDatapoint(dpoint)
|
||||||
for _, trigger := range triggers {
|
if err != nil {
|
||||||
trigger.Run()
|
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})
|
j, _ := json.Marshal(struct{ OK bool }{true})
|
||||||
w.Write(j)
|
w.Write(j)
|
||||||
|
40
problem.go
40
problem.go
@ -2,10 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
// External
|
// External
|
||||||
// we "git.gibonuddevalla.se/go/wrappederror"
|
we "git.gibonuddevalla.se/go/wrappederror"
|
||||||
|
|
||||||
// Standard
|
// 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
|
||||||
|
}
|
||||||
|
26
trigger.go
26
trigger.go
@ -4,7 +4,6 @@ import (
|
|||||||
// External
|
// External
|
||||||
we "git.gibonuddevalla.se/go/wrappederror"
|
we "git.gibonuddevalla.se/go/wrappederror"
|
||||||
"github.com/expr-lang/expr"
|
"github.com/expr-lang/expr"
|
||||||
"github.com/jmoiron/sqlx"
|
|
||||||
|
|
||||||
// Standard
|
// Standard
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -76,31 +75,26 @@ func TriggersRetrieve() (areas []Area, err error) { // {{{
|
|||||||
} // }}}
|
} // }}}
|
||||||
func TriggersRetrieveByDatapoint(datapointName string) (triggers []Trigger, err error) { // {{{
|
func TriggersRetrieveByDatapoint(datapointName string) (triggers []Trigger, err error) { // {{{
|
||||||
triggers = []Trigger{}
|
triggers = []Trigger{}
|
||||||
var rows *sqlx.Rows
|
row := service.Db.Conn.QueryRow(`
|
||||||
|
SELECT jsonb_agg(t.*)
|
||||||
rows, err = service.Db.Conn.Queryx(`
|
FROM public."trigger" t
|
||||||
SELECT
|
|
||||||
*
|
|
||||||
FROM public."trigger"
|
|
||||||
WHERE
|
WHERE
|
||||||
datapoints @> $1
|
datapoints @> $1
|
||||||
`,
|
`,
|
||||||
fmt.Sprintf(`["%s"]`, datapointName),
|
fmt.Sprintf(`["%s"]`, datapointName),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var data []byte
|
||||||
|
err = row.Scan(&data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = we.Wrap(err).WithData(datapointName)
|
err = we.Wrap(err).WithData(datapointName)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
|
||||||
|
|
||||||
for rows.Next() {
|
err = json.Unmarshal(data, &triggers)
|
||||||
trigger := Trigger{}
|
if err != nil {
|
||||||
err = rows.StructScan(&trigger)
|
err = we.Wrap(err).WithData(datapointName)
|
||||||
if err != nil {
|
return
|
||||||
err = we.Wrap(err).WithData(datapointName)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
triggers = append(triggers, trigger)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user