Compare commits
No commits in common. "7cf2b60803220baaa51b57c78c16249c6c9c48a2" and "6685f8bc46b3581550a305112fb574d5f9203bc6" have entirely different histories.
7cf2b60803
...
6685f8bc46
34
main.go
34
main.go
@ -29,7 +29,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const VERSION = "v41"
|
const VERSION = "v40"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
@ -711,15 +711,8 @@ 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, err = DatapointRetrieve(id, "")
|
dp.ID = 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"))
|
||||||
@ -731,29 +724,6 @@ 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,8 +4,6 @@ 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
|
||||||
@ -24,17 +22,6 @@ 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
|
||||||
@ -140,14 +127,6 @@ 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")
|
||||||
@ -233,6 +212,14 @@ 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 {
|
||||||
@ -261,14 +248,3 @@ 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