Store datapoint values with the problems
This commit is contained in:
parent
865f1ee184
commit
aa368c0b0d
27
problem.go
27
problem.go
@ -10,18 +10,19 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Problem struct {
|
||||
type Problem struct {// {{{
|
||||
ID int
|
||||
Start time.Time
|
||||
End sql.NullTime
|
||||
Acknowledged bool
|
||||
Datapoints map[string]any
|
||||
TriggerID int `json:"trigger_id"`
|
||||
TriggerName string `json:"trigger_name"`
|
||||
AreaName string `json:"area_name"`
|
||||
SectionName string `json:"section_name"`
|
||||
}
|
||||
}// }}}
|
||||
|
||||
func ProblemsRetrieve() (problems []Problem, err error) {
|
||||
func ProblemsRetrieve() (problems []Problem, err error) {// {{{
|
||||
problems = []Problem{}
|
||||
row := service.Db.Conn.QueryRow(`
|
||||
SELECT
|
||||
@ -83,9 +84,8 @@ func ProblemsRetrieve() (problems []Problem, err error) {
|
||||
err = we.Wrap(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ProblemStart(trigger Trigger) (problemID int, err error) {
|
||||
}// }}}
|
||||
func ProblemStart(trigger Trigger) (problemID int, err error) {// {{{
|
||||
row := service.Db.Conn.QueryRow(`
|
||||
SELECT COUNT(id)
|
||||
FROM problem
|
||||
@ -105,16 +105,16 @@ func ProblemStart(trigger Trigger) (problemID int, err error) {
|
||||
|
||||
// Open up a new problem if no open exists.
|
||||
if openProblems == 0 {
|
||||
row = service.Db.Conn.QueryRow(`INSERT INTO problem(trigger_id) VALUES($1) RETURNING id`, trigger.ID)
|
||||
datapointValuesJson, _ := json.Marshal(trigger.DatapointValues)
|
||||
row = service.Db.Conn.QueryRow(`INSERT INTO problem(trigger_id, datapoints) VALUES($1, $2) RETURNING id`, trigger.ID, datapointValuesJson)
|
||||
err = row.Scan(&problemID)
|
||||
if err != nil {
|
||||
err = we.Wrap(err).WithData(trigger)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ProblemClose(trigger Trigger) (problemID int, err error) {
|
||||
}// }}}
|
||||
func ProblemClose(trigger Trigger) (problemID int, err error) {// {{{
|
||||
row := service.Db.Conn.QueryRow(`UPDATE problem SET "end"=NOW() WHERE trigger_id=$1 AND "end" IS NULL RETURNING id`, trigger.ID)
|
||||
err = row.Scan(&problemID)
|
||||
|
||||
@ -128,13 +128,12 @@ func ProblemClose(trigger Trigger) (problemID int, err error) {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ProblemAcknowledge(id int, state bool) (err error) {
|
||||
}// }}}
|
||||
func ProblemAcknowledge(id int, state bool) (err error) {// {{{
|
||||
_, err = service.Db.Conn.Exec(`UPDATE problem SET "acknowledged"=$2 WHERE id=$1`, id, state)
|
||||
if err != nil {
|
||||
err = we.Wrap(err).WithData(id)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
}// }}}
|
||||
|
4
sql/00022.sql
Normal file
4
sql/00022.sql
Normal file
@ -0,0 +1,4 @@
|
||||
ALTER TABLE public.problem ALTER COLUMN trigger_id DROP NOT NULL;
|
||||
ALTER TABLE public.problem ADD COLUMN datapoints JSONB NOT NULL DEFAULT '{}';
|
||||
ALTER TABLE public.problem DROP CONSTRAINT problem_trigger_fk;
|
||||
ALTER TABLE public.problem ADD CONSTRAINT problem_trigger_fk FOREIGN KEY (trigger_id) REFERENCES public."trigger"(id) ON DELETE SET NULL ON UPDATE CASCADE;
|
@ -19,6 +19,7 @@ type Trigger struct {
|
||||
SectionID int `db:"section_id"`
|
||||
Expression string
|
||||
Datapoints []string
|
||||
DatapointValues map[string]any
|
||||
}
|
||||
|
||||
func TriggerCreate(sectionID int, name string) (t Trigger, err error) { // {{{
|
||||
@ -231,9 +232,9 @@ func (t *Trigger) Run() (output any, err error) { // {{{
|
||||
datapoints[dpname] = dp
|
||||
}
|
||||
|
||||
env := make(map[string]any)
|
||||
t.DatapointValues = make(map[string]any)
|
||||
for dpName, dp := range datapoints {
|
||||
env[dpName] = dp.LastDatapointValue.Value()
|
||||
t.DatapointValues[dpName] = dp.LastDatapointValue.Value()
|
||||
}
|
||||
|
||||
program, err := expr.Compile(t.Expression)
|
||||
@ -241,7 +242,7 @@ func (t *Trigger) Run() (output any, err error) { // {{{
|
||||
return
|
||||
}
|
||||
|
||||
output, err = expr.Run(program, env)
|
||||
output, err = expr.Run(program, t.DatapointValues)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user