Adding datapoints to triggers

This commit is contained in:
Magnus Åhall 2024-05-01 10:02:33 +02:00
parent b0a0f9290e
commit c746343dc0
15 changed files with 269 additions and 90 deletions

View File

@ -36,6 +36,7 @@ type DatapointValue struct {
ValueInt sql.NullInt64 `db:"value_int"`
ValueString sql.NullString `db:"value_string"`
ValueDateTime sql.NullTime `db:"value_datetime"`
TemplateValue any
}
func (dp DatapointValue) Value() any { // {{{

61
main.go
View File

@ -103,13 +103,17 @@ func main() { // {{{
service.Register("/", false, false, staticHandler)
service.Register("/problems", false, false, pageProblems)
service.Register("/datapoints", false, false, pageDatapoints)
service.Register("/datapoint/edit/{id}", false, false, pageDatapointEdit)
service.Register("/datapoint/update/{id}", false, false, pageDatapointUpdate)
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/update/{id}", false, false, pageTriggerUpdate)
service.Register("/trigger/run/{id}", false, false, pageTriggerRun)
service.Register("/configuration", false, false, pageConfiguration)
service.Register("/entry/{datapoint}", false, false, entryDatapoint)
@ -187,7 +191,7 @@ func entryDatapoint(w http.ResponseWriter, r *http.Request, sess *session.T) { /
switch v := out.(type) {
case bool:
// Trigger returning true - a problem occured
// Trigger returning true - a problem occurred
if v {
err = ProblemStart(trigger)
if err != nil {
@ -272,6 +276,7 @@ func pageIndex(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{
}
page.Render(w)
} // }}}
func pageProblems(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{
page := Page{
LAYOUT: "main",
@ -297,7 +302,8 @@ func pageProblems(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{
page.Render(w)
return
} // }}}
func pageDatapoints(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{
func pageDatapoints(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
page := Page{
LAYOUT: "main",
PAGE: "datapoints",
@ -308,7 +314,13 @@ func pageDatapoints(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {
httpError(w, we.Wrap(err).Log())
return
}
logger.Info("FOO", "dps", datapoints)
// The datapoint selector in trigger edit wants the raw data in JSON.
if r.URL.Query().Get("format") == "json" {
j, _ := json.Marshal(datapoints)
w.Write(j)
return
}
page.Data = map[string]any{
"Datapoints": datapoints,
@ -371,6 +383,7 @@ func pageDatapointUpdate(w http.ResponseWriter, r *http.Request, _ *session.T) {
w.Header().Add("Location", "/datapoints")
w.WriteHeader(302)
} // }}}
func pageTriggers(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{
areas, err := TriggersRetrieve()
if err != nil {
@ -400,11 +413,26 @@ func pageTriggerEdit(w http.ResponseWriter, r *http.Request, _ *session.T) { //
return
}
// Creating a new trigger uses the edit function.
// ID == 0 - create a new trigger.
// ID > 0 - edit existing trigger.
var trigger Trigger
trigger, err = TriggerRetrieve(id)
if err != nil {
httpError(w, we.Wrap(err).Log())
return
if id > 0 {
trigger, err = TriggerRetrieve(id)
if err != nil {
httpError(w, we.Wrap(err).Log())
return
}
} else {
// A new trigger needs to know which section it belongs to.
sectionIDStr := r.PathValue("sectionID")
if sectionIDStr != "" {
trigger.SectionID, err = strconv.Atoi(sectionIDStr)
if err != nil {
httpError(w, we.Wrap(err).Log())
return
}
}
}
datapoints := make(map[string]Datapoint)
@ -414,6 +442,7 @@ func pageTriggerEdit(w http.ResponseWriter, r *http.Request, _ *session.T) { //
httpError(w, we.Wrap(err).Log())
return
}
dp.LastDatapointValue.TemplateValue = dp.LastDatapointValue.Value()
datapoints[dpname] = dp
}
@ -440,14 +469,23 @@ func pageTriggerUpdate(w http.ResponseWriter, r *http.Request, _ *session.T) { /
}
var trigger Trigger
trigger, err = TriggerRetrieve(id)
if err != nil {
httpError(w, we.Wrap(err).Log())
return
if id > 0 {
trigger, err = TriggerRetrieve(id)
if err != nil {
httpError(w, we.Wrap(err).Log())
return
}
} else {
trigger.SectionID, err = strconv.Atoi(r.FormValue("sectionID"))
if err != nil {
httpError(w, we.Wrap(err).Log())
return
}
}
trigger.Name = r.FormValue("name")
trigger.Expression = r.FormValue("expression")
trigger.Datapoints = r.Form["datapoints[]"]
err = trigger.Update()
if err != nil {
httpError(w, we.Wrap(err).Log())
@ -492,6 +530,7 @@ func pageTriggerRun(w http.ResponseWriter, r *http.Request, _ *session.T) { // {
w.Header().Add("Content-Type", "application/json")
w.Write(j)
} // }}}
func pageConfiguration(w http.ResponseWriter, _ *http.Request, _ *session.T) { // {{{
areas, err := AreaRetrieve()
if err != nil {

View File

@ -52,7 +52,6 @@ func (p *Page) Render(w http.ResponseWriter) {
"Data": p.Data,
}
logger.Info("foo", "tmpl", tmpl)
err = tmpl.Execute(w, data)
if err != nil {
httpError(w, we.Wrap(err).Log())

1
sql/00008.sql Normal file
View File

@ -0,0 +1 @@
ALTER TABLE public."trigger" ADD CONSTRAINT trigger_sectionname_unique UNIQUE (section_id,"name");

View File

@ -37,7 +37,7 @@ h2 {
font-size: 1.25em;
}
a {
color: #3f9da1;
color: #fabd2f;
text-decoration: none;
}
a:hover {
@ -70,7 +70,7 @@ button {
background: #202020;
color: #d5c4a1;
padding: 8px 32px;
border: 1px solid #3a3a3a;
border: 1px solid #535353;
font-size: 1em;
height: 3em;
}

View File

@ -37,7 +37,7 @@ h2 {
font-size: 1.25em;
}
a {
color: #3f9da1;
color: #fabd2f;
text-decoration: none;
}
a:hover {
@ -70,7 +70,7 @@ button {
background: #202020;
color: #d5c4a1;
padding: 8px 32px;
border: 1px solid #3a3a3a;
border: 1px solid #535353;
font-size: 1em;
height: 3em;
}
@ -137,12 +137,17 @@ button:focus {
#areas .area .section {
margin: 8px 16px;
}
#areas .area .section > .name {
#areas .area .section .create {
display: grid;
grid-template-columns: min-content min-content;
grid-gap: 8px;
white-space: nowrap;
}
#areas .area .section .create .new {
font-weight: 500;
}
#areas .area .section .triggers a {
color: inherit;
text-decoration: none;
#areas .area .section > .name {
font-weight: 500;
}
#areas .area .section .triggers .trigger {
display: grid;
@ -155,5 +160,11 @@ button:focus {
height: 16px;
}
#areas .area .section .triggers .trigger .label {
color: #3f9da1;
color: inherit;
}
dialog {
background: #202020;
border: 1px solid #606060;
color: #d5c4a1;
box-shadow: 10px 10px 15px 0px rgba(0, 0, 0, 0.25);
}

View File

@ -37,7 +37,7 @@ h2 {
font-size: 1.25em;
}
a {
color: #3f9da1;
color: #fabd2f;
text-decoration: none;
}
a:hover {
@ -70,7 +70,7 @@ button {
background: #202020;
color: #d5c4a1;
padding: 8px 32px;
border: 1px solid #3a3a3a;
border: 1px solid #535353;
font-size: 1em;
height: 3em;
}

View File

@ -37,7 +37,7 @@ h2 {
font-size: 1.25em;
}
a {
color: #3f9da1;
color: #fabd2f;
text-decoration: none;
}
a:hover {
@ -70,7 +70,7 @@ button {
background: #202020;
color: #d5c4a1;
padding: 8px 32px;
border: 1px solid #3a3a3a;
border: 1px solid #535353;
font-size: 1em;
height: 3em;
}

View File

@ -1,17 +1,35 @@
export class UI {
constructor() {
constructor() {//{{{
document.getElementById('button-run').
addEventListener('click', evt=>evt.preventDefault())
addEventListener('click', evt => evt.preventDefault())
document.addEventListener('keydown', evt=>this.keyHandler(evt))
}
setTrigger(t) {
document.addEventListener('keydown', evt => this.keyHandler(evt))
document.querySelector('input[name="name"]').focus()
this.datapoints = []
}//}}}
render() {//{{{
document.querySelectorAll('.datapoints .datapoint').forEach(el => el.remove());
const datapoints = document.querySelector('.datapoints')
let html = Object.keys(this.trigger.datapoints).sort().map(dpName => {
const dp = this.trigger.datapoints[dpName]
return `
<div class="datapoint name"><b>${dp.Name}</b></div>
<div class="datapoint value">${dp.LastDatapointValue.TemplateValue}</div>
`
}).join('')
datapoints.innerHTML += html
}//}}}
setTrigger(t) {//{{{
this.trigger = t
}
run() {
}//}}}
run() {//{{{
this.trigger.run()
}
keyHandler(evt) {
}//}}}
keyHandler(evt) {//{{{
if (!(evt.altKey && evt.shiftKey))
return
@ -21,21 +39,75 @@ export class UI {
switch (evt.key) {
case 'T':
this.run()
break
break
case 'S':
document.getElementById('form-trigger').submit()
break
this.update()
break
}
}
}//}}}
addDatapoint() {//{{{
const dlg = document.getElementById('dlg-datapoints')
const datalist = document.getElementById('list-datapoints')
dlg.showModal()
fetch('/datapoints?format=json')
.then(data => data.json())
.then(json => {
this.datapoints = json
let html = ''
this.datapoints.forEach(dp => {
html += `<option value="${dp.Name}">`
})
datalist.innerHTML = html
})
.catch(err => alert(err))
}//}}}
chooseDatapoint() {//{{{
const dlg = document.getElementById('dlg-datapoints')
const datapoint = document.getElementById('datapoint').value
const dp = this.datapoints.find(dp => dp.Name == datapoint)
if (dp === undefined) {
alert('Invalid datapoint')
return
}
this.trigger.addDatapoint(dp)
dlg.close()
this.render()
}//}}}
update() {//{{{
const form = document.getElementById('form-trigger')
var formData = new FormData(form)
Object.keys(this.trigger.datapoints).forEach(name => formData.append("datapoints[]", name))
fetch(form.action, {
method: 'POST',
body: formData,
})
.then(resp => {
if (resp.redirected) {
location.href = resp.url
return
}
return resp.json()
})
.then(json => {
if (json)
alert(json.Error)
})
.catch(err => alert(err))
}//}}}
}
export class Trigger {
constructor(id, name) {
constructor(id, name, datapoints) {//{{{
this.id = id
this.name = name
}
run() {
this.datapoints = datapoints
}//}}}
run() {//{{{
const result = document.getElementById('run-result')
const classes = result.classList
const expr = document.getElementById('expr').value
@ -59,5 +131,8 @@ export class Trigger {
result.innerText = json.Output
})
.catch(err => alert(err))
}
}//}}}
addDatapoint(dp) {//{{{
this.datapoints[dp.Name] = dp
}//}}}
}

View File

@ -71,17 +71,22 @@
.section {
margin: 8px 16px;
.create {
display: grid;
grid-template-columns: min-content min-content;
grid-gap: 8px;
white-space: nowrap;
.new {
font-weight: @bold;
}
}
&>.name {
font-weight: 500;
font-weight: @bold;
}
.triggers {
a {
color: inherit;
text-decoration: none;
}
.trigger {
display: grid;
grid-template-columns: min-content 1fr;
@ -94,10 +99,17 @@
}
.label {
color: @color4;
color: inherit;
}
}
}
}
}
}
dialog {
background: @bg2;
border: 1px solid lighten(@bg2, 25%);
color: @text1;
box-shadow: 10px 10px 15px 0px rgba(0, 0, 0, 0.25);
}

View File

@ -59,7 +59,7 @@ h2 {
}
a {
color: @color4;
color: @color2;
text-decoration: none;
&:hover {
@ -96,7 +96,7 @@ button {
background: @bg2;
color: @text1;
padding: 8px 32px;
border: 1px solid lighten(@bg2, 10%);
border: 1px solid lighten(@bg2, 20%);
font-size: 1em;
height: 3em;

View File

@ -1,5 +1,8 @@
@import "theme.less";
#dlg-datapoints {
}
.widgets {
display: grid;
grid-template-columns: min-content 1fr;

View File

@ -4,6 +4,7 @@ import (
// External
we "git.gibonuddevalla.se/go/wrappederror"
"github.com/expr-lang/expr"
"github.com/lib/pq"
// Standard
"encoding/json"
@ -19,9 +20,6 @@ type Trigger struct {
Datapoints []string
}
func Foo() {
}
func TriggersRetrieve() (areas []Area, err error) { // {{{
areas = []Area{}
@ -130,21 +128,50 @@ func (t *Trigger) Update() (err error) { // {{{
return
}
logger.Info("FOO", "trigger", t)
_, err = service.Db.Conn.Exec(`
UPDATE "trigger"
SET
name=$2,
expression=$3
WHERE
id=$1
`,
t.ID,
t.Name,
t.Expression,
)
if err != nil {
err = we.Wrap(err)
if t.Datapoints == nil {
t.Datapoints = []string{}
}
jsonDatapoints, _ := json.Marshal(t.Datapoints)
if t.ID == 0 {
_, err = service.Db.Conn.Exec(`
INSERT INTO "trigger"(name, section_id, expression, datapoints)
VALUES($1, $2, $3, $4)
`,
t.Name,
t.SectionID,
t.Expression,
jsonDatapoints,
)
} else {
_, err = service.Db.Conn.Exec(`
UPDATE "trigger"
SET
name=$2,
expression=$3,
datapoints=$4
WHERE
id=$1
`,
t.ID,
t.Name,
t.Expression,
jsonDatapoints,
)
}
if pqErr, ok := err.(*pq.Error); ok {
err = we.Wrap(err).WithData(
struct {
Trigger *Trigger
PostgresCode pq.ErrorCode
PostgresMsg string
}{
t,
pqErr.Code,
pqErr.Code.Name(),
})
} else if err != nil {
err = we.Wrap(err).WithData(t)
}
return
} // }}}

View File

@ -6,34 +6,42 @@
let trigger = new Trigger(
{{ .Data.Trigger.ID }},
'{{ .Data.Trigger.Name }}',
{{ .Data.Datapoints }},
)
_ui.setTrigger(trigger)
_ui.render()
</script>
<link rel="stylesheet" type="text/css" href="/css/{{ .VERSION }}/trigger_edit.css">
{{ block "page_label" . }}{{end}}
<dialog id="dlg-datapoints">
<input list="list-datapoints" id="datapoint">
<datalist id="list-datapoints"></datalist>
<button onclick="_ui.chooseDatapoint()">OK</button>
</dialog>
<form id="form-trigger" action="/trigger/update/{{ .Data.Trigger.ID }}" method="post">
<div id="widgets" class="widgets">
<div class="label">Name</div>
<div><input type="text" name="name" value="{{ .Data.Trigger.Name }}"></div>
<input type="hidden" name="sectionID" value="{{ .Data.Trigger.SectionID }}">
<div id="widgets" class="widgets">
<div class="label">Name</div>
<div><input type="text" name="name" value="{{ .Data.Trigger.Name }}"></div>
<div class="label">Datapoints</div>
<div class="datapoints" style="margin-top: 4px">
{{ range .Data.Datapoints }}
<div class="datapoint name"><b>{{ .Name }}</b></div>
<div class="datapoint value">{{ .LastDatapointValue.Value }}</div>
{{ end }}
</div>
<div class="label">Datapoints</div>
<div class="datapoints" style="margin-top: 4px">
<div><a onclick="_ui.addDatapoint()">Add</a></div>
<div></div>
</div>
<div class="label">Expression</div>
<div><textarea id="expr" name="expression" rows=8>{{ .Data.Trigger.Expression }}</textarea></div>
<div class="label">Expression</div>
<div><textarea id="expr" name="expression" rows=8>{{ .Data.Trigger.Expression }}</textarea></div>
<div></div>
<div class="action">
<button id="button-update">Update</button>
<button id="button-run" onclick="window._ui.run(); return false">Test</button>
<div id="run-result"></div>
<div></div>
<div class="action">
<button id="button-update" onclick="_ui.update(); return false">{{ if eq .Data.Trigger.ID 0 }}Create{{ else }}Update{{ end }}</button>
<button id="button-run" onclick="window._ui.run(); return false">Test</button>
<div id="run-result"></div>
</div>
</div>
</form>

View File

@ -9,7 +9,10 @@
<div class="name">{{ .Name }}</div>
{{ range .SortedSections }}
<div class="section">
<div class="name">{{ .Name }}</div>
<div class="create">
<div class="name">{{ .Name }}</div>
<div class="new"><a href="/trigger/edit/0/{{ .ID }}">+</a></div>
</div>
<div class="triggers">
{{ range .SortedTriggers }}