#6, update trigger expressions when renaming datapoints
This commit is contained in:
parent
6685f8bc46
commit
db41d360dc
32
main.go
32
main.go
@ -711,8 +711,15 @@ func actionDatapointUpdate(w http.ResponseWriter, r *http.Request, _ *session.T)
|
|||||||
var nodataSeconds int
|
var nodataSeconds int
|
||||||
nodataSeconds, _ = strconv.Atoi(r.FormValue("nodata_seconds"))
|
nodataSeconds, _ = strconv.Atoi(r.FormValue("nodata_seconds"))
|
||||||
|
|
||||||
|
// Datapoint needs to be retrieved from database for the name.
|
||||||
|
// If name has changed, trigger expressions needs to be updated.
|
||||||
var dp Datapoint
|
var dp Datapoint
|
||||||
dp.ID = id
|
dp, err = DatapointRetrieve(id, "")
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, werr.Wrap(err).WithData(id).Log())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
prevDatapointName := dp.Name
|
||||||
dp.Group = r.FormValue("group")
|
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"))
|
||||||
@ -724,6 +731,29 @@ func actionDatapointUpdate(w http.ResponseWriter, r *http.Request, _ *session.T)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update the trigger expressions using this
|
||||||
|
// datapoint name if changed.
|
||||||
|
if prevDatapointName != dp.Name {
|
||||||
|
var triggers []Trigger
|
||||||
|
triggers, err = TriggersRetrieveByDatapoint(dp.Name)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, werr.Wrap(err).WithData(dp.Name))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, trigger := range triggers {
|
||||||
|
err = trigger.RenameDatapoint(prevDatapointName, dp.Name)
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, werr.Wrap(err).WithData([]string{prevDatapointName, dp.Name}))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = trigger.Update()
|
||||||
|
if err != nil {
|
||||||
|
httpError(w, werr.Wrap(err).WithData([]string{prevDatapointName, dp.Name, trigger.Name}))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
w.Header().Add("Location", "/datapoints")
|
w.Header().Add("Location", "/datapoints")
|
||||||
w.WriteHeader(302)
|
w.WriteHeader(302)
|
||||||
} // }}}
|
} // }}}
|
||||||
|
40
trigger.go
40
trigger.go
@ -4,6 +4,8 @@ import (
|
|||||||
// External
|
// External
|
||||||
werr "git.gibonuddevalla.se/go/wrappederror"
|
werr "git.gibonuddevalla.se/go/wrappederror"
|
||||||
"github.com/expr-lang/expr"
|
"github.com/expr-lang/expr"
|
||||||
|
"github.com/expr-lang/expr/ast"
|
||||||
|
"github.com/expr-lang/expr/parser"
|
||||||
"github.com/lib/pq"
|
"github.com/lib/pq"
|
||||||
|
|
||||||
// Standard
|
// Standard
|
||||||
@ -22,6 +24,17 @@ type Trigger struct {
|
|||||||
DatapointValues map[string]any
|
DatapointValues map[string]any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ExprRenamePatcher struct {
|
||||||
|
OldName string
|
||||||
|
NewName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p ExprRenamePatcher) Visit(node *ast.Node) {
|
||||||
|
if n, ok := (*node).(*ast.IdentifierNode); ok && n.Value == p.OldName {
|
||||||
|
ast.Patch(node, &ast.IdentifierNode{Value: p.NewName})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TriggerCreate(sectionID int, name string) (t Trigger, err error) { // {{{
|
func TriggerCreate(sectionID int, name string) (t Trigger, err error) { // {{{
|
||||||
t.SectionID = sectionID
|
t.SectionID = sectionID
|
||||||
t.Name = name
|
t.Name = name
|
||||||
@ -127,6 +140,14 @@ func TriggerRetrieve(id int) (trigger Trigger, err error) { // {{{
|
|||||||
err = json.Unmarshal(jsonData, &trigger)
|
err = json.Unmarshal(jsonData, &trigger)
|
||||||
return
|
return
|
||||||
} // }}}
|
} // }}}
|
||||||
|
func TriggerDelete(id int) (err error) { // {{{
|
||||||
|
_, err = service.Db.Conn.Exec(`DELETE FROM public.trigger WHERE id=$1`, id)
|
||||||
|
if err != nil {
|
||||||
|
return werr.Wrap(err).WithData(id)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
} // }}}
|
||||||
|
|
||||||
func (t *Trigger) Validate() (ok bool, err error) { // {{{
|
func (t *Trigger) Validate() (ok bool, err error) { // {{{
|
||||||
if strings.TrimSpace(t.Name) == "" {
|
if strings.TrimSpace(t.Name) == "" {
|
||||||
err = fmt.Errorf("Name can't be empty")
|
err = fmt.Errorf("Name can't be empty")
|
||||||
@ -212,14 +233,6 @@ func (t *Trigger) Update() (err error) { // {{{
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
} // }}}
|
} // }}}
|
||||||
func TriggerDelete(id int) (err error) { // {{{
|
|
||||||
_, err = service.Db.Conn.Exec(`DELETE FROM public.trigger WHERE id=$1`, id)
|
|
||||||
if err != nil {
|
|
||||||
return werr.Wrap(err).WithData(id)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
} // }}}
|
|
||||||
|
|
||||||
func (t *Trigger) Run() (output any, err error) { // {{{
|
func (t *Trigger) Run() (output any, err error) { // {{{
|
||||||
datapoints := make(map[string]Datapoint)
|
datapoints := make(map[string]Datapoint)
|
||||||
for _, dpname := range t.Datapoints {
|
for _, dpname := range t.Datapoints {
|
||||||
@ -248,3 +261,14 @@ func (t *Trigger) Run() (output any, err error) { // {{{
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
} // }}}
|
} // }}}
|
||||||
|
func (t *Trigger) RenameDatapoint(from, to string) error { // {{{
|
||||||
|
tree, err := parser.Parse(t.Expression)
|
||||||
|
if err != nil {
|
||||||
|
return werr.Wrap(err).WithData(t.Expression)
|
||||||
|
}
|
||||||
|
|
||||||
|
ast.Walk(&tree.Node, ExprRenamePatcher{from, to})
|
||||||
|
t.Expression = tree.Node.String()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
} // }}}
|
||||||
|
Loading…
Reference in New Issue
Block a user