Compare commits
No commits in common. "adab2ab67dc60429fe3fa4bdf44cccfe6e73a2c9" and "a94559ab6cdd39ac4572170cb27e04bf0176e897" have entirely different histories.
adab2ab67d
...
a94559ab6c
@ -2,5 +2,4 @@ package main
|
||||
|
||||
type SmonConfiguration struct {
|
||||
LogFile string
|
||||
NodataInterval int `json:"nodata_interval"` // in seconds
|
||||
}
|
||||
|
33
datapoint.go
33
datapoint.go
@ -73,7 +73,7 @@ func (dp Datapoint) Update() (err error) { // {{{
|
||||
|
||||
if dp.ID == 0 {
|
||||
_, err = service.Db.Conn.Exec(
|
||||
`INSERT INTO datapoint("group", name, datatype, nodata_problem_seconds) VALUES($1, $2, $3, $4)`,
|
||||
`INSERT INTO datapoint("group", name, datatype) VALUES($1, $2, $3, $4)`,
|
||||
dp.Group,
|
||||
name,
|
||||
dp.Datatype,
|
||||
@ -283,37 +283,6 @@ func DatapointRetrieve(id int, name string) (dp Datapoint, err error) { // {{{
|
||||
return
|
||||
} // }}}
|
||||
func DatapointDelete(id int) (err error) { // {{{
|
||||
var dpName string
|
||||
row := service.Db.Conn.QueryRow(`SELECT name FROM public.datapoint WHERE id = $1`, id)
|
||||
err = row.Scan(&dpName)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err).WithData(id)
|
||||
return
|
||||
}
|
||||
|
||||
var rows *sql.Rows
|
||||
rows, err = service.Db.Conn.Query(`SELECT name FROM public.trigger WHERE datapoints ? $1`, dpName)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err).WithData(dpName)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var triggerNames []string
|
||||
var name string
|
||||
for rows.Next() {
|
||||
err = rows.Scan(&name)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err)
|
||||
return
|
||||
}
|
||||
triggerNames = append(triggerNames, name)
|
||||
}
|
||||
|
||||
if len(triggerNames) > 0 {
|
||||
return werr.New("Datapoint '%s' used in the following triggers: %s", dpName, strings.Join(triggerNames, ", "))
|
||||
}
|
||||
|
||||
_, err = service.Db.Conn.Exec(`DELETE FROM datapoint WHERE id=$1`, id)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err).WithData(id)
|
||||
|
40
main.go
40
main.go
@ -21,7 +21,6 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
@ -39,7 +38,6 @@ var (
|
||||
parsedTemplates map[string]*template.Template
|
||||
componentFilenames []string
|
||||
notificationManager notification.Manager
|
||||
smonConf SmonConfiguration
|
||||
|
||||
//go:embed sql
|
||||
sqlFS embed.FS
|
||||
@ -90,6 +88,7 @@ func main() { // {{{
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
smonConf := SmonConfiguration{}
|
||||
j, _ := json.Marshal(service.Config.Application)
|
||||
json.Unmarshal(j, &smonConf)
|
||||
logFile, err = os.OpenFile(smonConf.LogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
|
||||
@ -97,10 +96,6 @@ func main() { // {{{
|
||||
logger.Error("application", "error", err)
|
||||
return
|
||||
}
|
||||
if smonConf.NodataInterval < 10 {
|
||||
logger.Error("application → nodata_interval has to be larger or equal to 10.")
|
||||
return
|
||||
}
|
||||
|
||||
service.SetDatabase(sqlProvider)
|
||||
service.SetStaticFS(staticFS, "static")
|
||||
@ -139,7 +134,6 @@ func main() { // {{{
|
||||
service.Register("/triggers", false, false, pageTriggers)
|
||||
service.Register("/trigger/edit/{id}", false, false, pageTriggerEdit)
|
||||
service.Register("/trigger/edit/{id}/{sectionID}", false, false, pageTriggerEdit)
|
||||
service.Register("/trigger/addDatapoint/{id}/{datapointName}", false, false, pageTriggerDatapointAdd)
|
||||
service.Register("/trigger/update/{id}", false, false, pageTriggerUpdate)
|
||||
service.Register("/trigger/run/{id}", false, false, pageTriggerRun)
|
||||
|
||||
@ -727,38 +721,6 @@ func pageTriggerEdit(w http.ResponseWriter, r *http.Request, _ *session.T) { //
|
||||
|
||||
page.Render(w)
|
||||
} // }}}
|
||||
func pageTriggerDatapointAdd(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
|
||||
triggerID := r.PathValue("id")
|
||||
dpName := r.PathValue("datapointName")
|
||||
|
||||
id, err := strconv.Atoi(triggerID)
|
||||
if err != nil {
|
||||
httpError(w, werr.Wrap(err).Log())
|
||||
return
|
||||
}
|
||||
|
||||
var trigger Trigger
|
||||
if id > 0 {
|
||||
trigger, err = TriggerRetrieve(id)
|
||||
if err != nil {
|
||||
httpError(w, werr.Wrap(err).Log())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if !slices.Contains(trigger.Datapoints, dpName) {
|
||||
trigger.Datapoints = append(trigger.Datapoints, dpName)
|
||||
}
|
||||
err = trigger.Update()
|
||||
if err != nil {
|
||||
httpError(w, werr.Wrap(err).Log())
|
||||
return
|
||||
}
|
||||
|
||||
j, _ := json.Marshal(struct{ OK bool }{OK: true})
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.Write(j)
|
||||
} // }}}
|
||||
func pageTriggerUpdate(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
|
||||
idStr := r.PathValue("id")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
|
@ -25,7 +25,7 @@ func nodataLoop() {
|
||||
var err error
|
||||
|
||||
// TODO - should be configurable
|
||||
ticker := time.NewTicker(time.Second * time.Duration(smonConf.NodataInterval))
|
||||
ticker := time.NewTicker(time.Second * 5)
|
||||
for {
|
||||
<-ticker.C
|
||||
datapoints, err = nodataDatapoints()
|
||||
|
@ -66,7 +66,7 @@ export class UI {
|
||||
})
|
||||
.catch(err => alert(err))
|
||||
}//}}}
|
||||
async chooseDatapoint() {//{{{
|
||||
chooseDatapoint() {//{{{
|
||||
const dlg = document.getElementById('dlg-datapoints')
|
||||
const datapoint = document.getElementById('datapoint').value
|
||||
const dp = this.datapoints.find(dp => dp.Name == datapoint)
|
||||
@ -77,10 +77,8 @@ export class UI {
|
||||
}
|
||||
|
||||
this.trigger.addDatapoint(dp)
|
||||
.then(() => {
|
||||
dlg.close()
|
||||
this.render()
|
||||
})
|
||||
}//}}}
|
||||
deleteDatapoint(name) {//{{{
|
||||
if (!confirm(`Delete ${name}?`)) {
|
||||
@ -149,15 +147,7 @@ export class Trigger {
|
||||
})
|
||||
.catch(err => alert(err))
|
||||
}//}}}
|
||||
async addDatapoint(dp) {//{{{
|
||||
return fetch(`/trigger/addDatapoint/${this.id}/${dp.Name}`)
|
||||
.then(data => data.json())
|
||||
.then(json => {
|
||||
if (!json.OK) {
|
||||
alert(json.Error)
|
||||
return
|
||||
}
|
||||
addDatapoint(dp) {//{{{
|
||||
this.datapoints[dp.Name] = dp
|
||||
})
|
||||
}//}}}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user