Switched to purer libraries

This commit is contained in:
Magnus Åhall 2026-08-19 17:53:52 +02:00
parent 8fa8bb4c3a
commit 9496947f01
21 changed files with 390 additions and 316 deletions

33
area.go
View file

@ -2,10 +2,11 @@ package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
werr "git.ahall.se/go/wrappederror"
"github.com/jackc/pgx/v5"
// Standard
"database/sql"
"context"
"encoding/json"
"sort"
)
@ -19,7 +20,7 @@ type Area struct {
func AreaRetrieve() (areas []Area, err error) { // {{{
areas = []Area{}
row := service.Db.Conn.QueryRow(`
row := db.QueryRow(context.Background(), `
SELECT
jsonb_agg(jsonsections)
FROM (
@ -59,21 +60,23 @@ func AreaRetrieve() (areas []Area, err error) { // {{{
return
} // }}}
func AreaCreate(name string) (err error) { // {{{
_, err = service.Db.Conn.Exec(`INSERT INTO area(name) VALUES($1)`, name)
_, err = db.Exec(context.Background(), `INSERT INTO area(name) VALUES($1)`, name)
return
} // }}}
func AreaRename(id int, name string) (err error) { // {{{
_, err = service.Db.Conn.Exec(`UPDATE area SET name=$2 WHERE id=$1`, id, name)
_, err = db.Exec(context.Background(), `UPDATE area SET name=$2 WHERE id=$1`, id, name)
return
} // }}}
func AreaDelete(id int) (err error) { // {{{
var trx *sql.Tx
trx, err = service.Db.Conn.Begin()
ctx := context.Background()
var trx pgx.Tx
trx, err = db.Begin(ctx)
if err != nil {
err = werr.Wrap(err).WithData(id)
}
_, err = trx.Exec(`
_, err = trx.Exec(context.Background(), `
DELETE
FROM trigger t
USING section s
@ -84,34 +87,34 @@ func AreaDelete(id int) (err error) { // {{{
id,
)
if err != nil {
err2 := trx.Rollback()
err2 := trx.Rollback(ctx)
if err2 != nil {
return werr.Wrap(err2).WithData(err)
}
return werr.Wrap(err).WithData(id)
}
_, err = trx.Exec(`DELETE FROM public.section WHERE area_id = $1`, id)
_, err = trx.Exec(ctx, `DELETE FROM public.section WHERE area_id = $1`, id)
if err != nil {
err2 := trx.Rollback()
err2 := trx.Rollback(ctx)
if err2 != nil {
return werr.Wrap(err2).WithData(err)
}
return werr.Wrap(err).WithData(id)
}
_, err = trx.Exec(`DELETE FROM public.area WHERE id = $1`, id)
_, err = trx.Exec(ctx, `DELETE FROM public.area WHERE id = $1`, id)
if err != nil {
err2 := trx.Rollback()
err2 := trx.Rollback(ctx)
if err2 != nil {
return werr.Wrap(err2).WithData(err)
}
return werr.Wrap(err).WithData(id)
}
err = trx.Commit()
err = trx.Commit(ctx)
if err != nil {
err2 := trx.Rollback()
err2 := trx.Rollback(ctx)
if err2 != nil {
return werr.Wrap(err2).WithData(err)
}

View file

@ -1,6 +1,42 @@
package main
type FileConfiguration struct {
LogFile string
import (
// Standard
"encoding/json"
"os"
)
type DatabaseDetails struct {
Host string
Port int
Name string
Username string
Password string
}
type Config struct {
Network struct {
Address string
Port int
}
Websocket struct {
Domains []string
}
Database DatabaseDetails
NodataInterval int `json:"nodata_interval"` // in seconds
}
func initConfig(fname string) (cfg Config, err error) {// {{{
var data []byte
data, err = os.ReadFile(fname)
if err != nil {
return
}
err = json.Unmarshal(data, &cfg)
return
}// }}}

View file

@ -2,16 +2,17 @@ package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
werr "git.ahall.se/go/wrappederror"
"github.com/jackc/pgx/v5"
// Standard
"database/sql"
"context"
"time"
)
type Configuration struct {
timezoneLocation *time.Location
Settings map[string]string
Settings map[string]string
}
var smonConfig Configuration
@ -19,8 +20,8 @@ var smonConfig Configuration
func SmonConfigInit() (cfg Configuration, err error) {
cfg.Settings = make(map[string]string, 8)
var rows *sql.Rows
rows, err = service.Db.Conn.Query(`SELECT * FROM public.configuration`)
var rows pgx.Rows
rows, err = db.Query(context.Background(), `SELECT * FROM public.configuration`)
if err != nil {
err = werr.Wrap(err)
return
@ -63,7 +64,7 @@ func (cfg *Configuration) Timezone() *time.Location {
func (cfg *Configuration) SetTheme(theme string) (err error) {
cfg.Settings["THEME"] = theme
_, err = service.Db.Conn.Exec(`UPDATE public.configuration SET value=$1 WHERE setting='THEME'`, theme)
_, err = db.Exec(context.Background(), `UPDATE public.configuration SET value=$1 WHERE setting='THEME'`, theme)
return
}
@ -74,7 +75,7 @@ func (cfg *Configuration) SetTimezone(tz string) (err error) {
return werr.Wrap(err).WithData(tz)
}
_, err = service.Db.Conn.Exec(`UPDATE public.configuration SET value=$1 WHERE setting='TIMEZONE'`, tz)
_, err = db.Exec(context.Background(), `UPDATE public.configuration SET value=$1 WHERE setting='TIMEZONE'`, tz)
if err != nil {
return werr.Wrap(err).WithData(tz)
}

View file

@ -2,10 +2,11 @@ package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
"github.com/jmoiron/sqlx"
werr "git.ahall.se/go/wrappederror"
"github.com/jackc/pgx/v5"
// Standard
"context"
"database/sql"
"errors"
"strings"
@ -73,7 +74,8 @@ func (dp Datapoint) Update() (err error) { // {{{
}
if dp.ID == 0 {
_, err = service.Db.Conn.Exec(
_, err = db.Exec(
context.Background(),
`INSERT INTO datapoint("group", name, datatype, nodata_problem_seconds, comment) VALUES($1, $2, $3, $4, $5)`,
dp.Group,
name,
@ -85,7 +87,8 @@ func (dp Datapoint) Update() (err error) { // {{{
/* Keep nodata_is_problem as is unless the nodata_problem_seconds is changed.
* Otherwise unnecessary nodata problems could be notified when updating unrelated
* datapoint properties. */
_, err = service.Db.Conn.Exec(
_, err = db.Exec(
context.Background(),
`
UPDATE datapoint
SET
@ -125,7 +128,7 @@ func DatapointAdd[T any](name string, value T) (err error) { // {{{
value any
}
row := service.Db.Conn.QueryRow(`SELECT id, datatype FROM datapoint WHERE name=$1`, name)
row := db.QueryRow(context.Background(), `SELECT id, datatype FROM datapoint WHERE name=$1`, name)
var dpID int
var dpType DatapointType
@ -140,9 +143,9 @@ func DatapointAdd[T any](name string, value T) (err error) { // {{{
switch dpType {
case INT:
_, err = service.Db.Conn.Exec(`INSERT INTO datapoint_value(datapoint_id, value_int) VALUES($1, $2)`, dpID, value)
_, err = db.Exec(context.Background(), `INSERT INTO datapoint_value(datapoint_id, value_int) VALUES($1, $2)`, dpID, value)
case STRING:
_, err = service.Db.Conn.Exec(`INSERT INTO datapoint_value(datapoint_id, value_string) VALUES($1, $2)`, dpID, value)
_, err = db.Exec(context.Background(), `INSERT INTO datapoint_value(datapoint_id, value_string) VALUES($1, $2)`, dpID, value)
case DATETIME:
// Time value is required to be a RFC 3339 formatted time string
var t time.Time
@ -155,7 +158,7 @@ func DatapointAdd[T any](name string, value T) (err error) { // {{{
return werr.Wrap(err).WithData(dpRequest{dpID, value}).Log()
}
_, err = service.Db.Conn.Exec(`INSERT INTO datapoint_value(datapoint_id, value_datetime) VALUES($1, $2)`, dpID, t)
_, err = db.Exec(context.Background(), `INSERT INTO datapoint_value(datapoint_id, value_datetime) VALUES($1, $2)`, dpID, t)
}
if err != nil {
err = werr.Wrap(err).WithData(dpRequest{dpID, value})
@ -167,8 +170,10 @@ func DatapointAdd[T any](name string, value T) (err error) { // {{{
func DatapointsRetrieve() (dps []Datapoint, err error) { // {{{
dps = []Datapoint{}
var rows *sqlx.Rows
rows, err = service.Db.Conn.Queryx(`
var rows pgx.Rows
rows, err = db.Query(
context.Background(),
`
SELECT
id, name, datatype, last_value, "group", comment, nodata_problem_seconds,
last_value_id AS v_id,
@ -205,37 +210,43 @@ func DatapointsRetrieve() (dps []Datapoint, err error) { // {{{
ValueDateTime sql.NullTime `db:"value_datetime"`
}
for rows.Next() {
dp := Datapoint{}
dpv := DatapointValue{}
res := DbRes{}
err = rows.StructScan(&res)
if err != nil {
err = werr.Wrap(err)
dps, err = pgx.CollectRows(
rows,
func(row pgx.CollectableRow) (dp Datapoint, err error) {
var dpv DatapointValue
var res DbRes
res, err = pgx.RowToStructByName[DbRes](row)
if err != nil {
err = werr.Wrap(err)
return
}
dp.ID = res.ID
dp.Name = res.Name
dp.Group = res.Group
dp.Datatype = res.Datatype
dp.Comment = res.Comment
dp.LastValue = res.LastValue
dp.Found = true
dp.NodataProblemSeconds = res.NodataProblemSeconds
if res.VID.Valid {
dpv.ID = int(res.VID.Int64)
dpv.Ts = res.Ts.Time
dpv.ValueInt = res.ValueInt
dpv.ValueString = res.ValueString
dpv.ValueDateTime = res.ValueDateTime
dp.LastDatapointValue = dpv
}
return
}
})
dp.ID = res.ID
dp.Name = res.Name
dp.Group = res.Group
dp.Datatype = res.Datatype
dp.Comment = res.Comment
dp.LastValue = res.LastValue
dp.Found = true
dp.NodataProblemSeconds = res.NodataProblemSeconds
if res.VID.Valid {
dpv.ID = int(res.VID.Int64)
dpv.Ts = res.Ts.Time
dpv.ValueInt = res.ValueInt
dpv.ValueString = res.ValueString
dpv.ValueDateTime = res.ValueDateTime
dp.LastDatapointValue = dpv
}
dps = append(dps, dp)
if err != nil {
err = werr.Wrap(err)
return
}
return
} // }}}
func DatapointRetrieve(id int, name string) (dp Datapoint, err error) { // {{{
@ -250,8 +261,12 @@ func DatapointRetrieve(id int, name string) (dp Datapoint, err error) { // {{{
param = name
}
row := service.Db.Conn.QueryRowx(query, param)
err = row.StructScan(&dp)
rows, _ := db.Query(context.Background(), query, param)
dp, err = pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Datapoint])
if err != nil {
err = werr.Wrap(err)
return
}
if err == sql.ErrNoRows {
dp = Datapoint{
@ -266,7 +281,9 @@ func DatapointRetrieve(id int, name string) (dp Datapoint, err error) { // {{{
return
}
row = service.Db.Conn.QueryRowx(`
rows, _ = db.Query(
context.Background(),
`
SELECT *
FROM datapoint_value
WHERE datapoint_id = $1
@ -275,7 +292,7 @@ func DatapointRetrieve(id int, name string) (dp Datapoint, err error) { // {{{
`,
dp.ID,
)
err = row.StructScan(&dp.LastDatapointValue)
dp, err = pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Datapoint])
if err == sql.ErrNoRows {
err = nil
return
@ -290,50 +307,41 @@ func DatapointRetrieve(id int, name string) (dp Datapoint, err error) { // {{{
} // }}}
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)
err = db.QueryRow(context.Background(), `SELECT name FROM public.datapoint WHERE id = $1`, id).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)
rows, _ := db.Query(context.Background(), `SELECT name FROM public.trigger WHERE datapoints ? $1`, dpName)
triggerNames, err = pgx.CollectRows(rows, pgx.RowTo[string])
if err != nil {
err = werr.Wrap(err).WithData(id)
return
}
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)
_, err = db.Exec(context.Background(), `DELETE FROM datapoint WHERE id=$1`, id)
if err != nil {
err = werr.Wrap(err).WithData(id)
return
}
return
} // }}}
func DatapointValues(id int, from, to time.Time) (values []DatapointValue, err error) { // {{{
_, err = service.Db.Conn.Exec(`SELECT set_config('timezone', $1, false)`, smonConfig.Timezone().String())
_, err = db.Exec(context.Background(), `SELECT set_config('timezone', $1, false)`, smonConfig.Timezone().String())
if err != nil {
err = werr.Wrap(err).WithData(smonConfig.Timezone().String())
return
}
rows, err := service.Db.Conn.Queryx(
rows, _ := db.Query(
context.Background(),
`
SELECT
id,
@ -354,20 +362,11 @@ func DatapointValues(id int, from, to time.Time) (values []DatapointValue, err e
from,
to,
)
values, err = pgx.CollectRows(rows, pgx.RowToStructByName[DatapointValue])
if err != nil {
err = werr.Wrap(err).WithData(id)
return
}
defer rows.Close()
for rows.Next() {
dpv := DatapointValue{}
err = rows.StructScan(&dpv)
if err != nil {
err = werr.Wrap(err).WithData(id)
return
}
values = append(values, dpv)
}
return
} // }}}

19
go.mod
View file

@ -1,19 +1,18 @@
module smon
go 1.22.0
go 1.25.0
require (
git.gibonuddevalla.se/go/webservice v0.2.16
git.gibonuddevalla.se/go/wrappederror v0.3.4
git.ahall.se/go/html_template v0.1.1
git.ahall.se/go/wrappederror v1.0.0
github.com/expr-lang/expr v1.16.5
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.9
github.com/jackc/pgx/v5 v5.9.1
)
require (
git.gibonuddevalla.se/go/dbschema v1.3.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
golang.org/x/net v0.17.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
golang.org/x/sync v0.17.0 // indirect
golang.org/x/text v0.29.0 // indirect
)

47
go.sum
View file

@ -1,33 +1,32 @@
git.gibonuddevalla.se/go/dbschema v1.3.0 h1:HzFMR29tWfy/ibIjltTbIMI4inVktj/rh8bESALibgM=
git.gibonuddevalla.se/go/dbschema v1.3.0/go.mod h1:BNw3q/574nXbGoeWyK+tLhRfggVkw2j2aXZzrBKC3ig=
git.gibonuddevalla.se/go/webservice v0.2.15 h1:ECe63fRDSrg3RJcgYV2pG+WsAQLVG8wvfHennz7aHsY=
git.gibonuddevalla.se/go/webservice v0.2.15/go.mod h1:3uBS6nLbK9qbuGzDls8MZD5Xr9ORY1Srbj6v06BIhws=
git.gibonuddevalla.se/go/wrappederror v0.3.4 h1:dcKp9/+QrZSO3S4fVnq7yG2p7DUZVmlztBAb/OzoZNY=
git.gibonuddevalla.se/go/wrappederror v0.3.4/go.mod h1:j4w320Hk1wvhOPjUaK4GgLvmtnjUUM5yVu6JFO1OCSc=
git.ahall.se/go/html_template v0.1.1 h1:GrSNvs/ZAWKkuxQ4OEOTqHBTtYnx8C88JBxVERDxv18=
git.ahall.se/go/html_template v0.1.1/go.mod h1:bBpM0K1HXZadhZg4VeT39IgyGwwhWoZet99nw8QeZKg=
git.ahall.se/go/wrappederror v1.0.0 h1:TxiAUTZUnmkVpaPKQjhTGSFsvc0qLkULsIj4Rwh4UsY=
git.ahall.se/go/wrappederror v1.0.0/go.mod h1:trMBDaw5SdUrsZu3MCh1q+INsrmB4T9L+3DuV7JM+c8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/expr-lang/expr v1.16.5 h1:m2hvtguFeVaVNTHj8L7BoAyt7O0PAIBaSVbjdHgRXMs=
github.com/expr-lang/expr v1.16.5/go.mod h1:uCkhfG+x7fcZ5A5sXHKuQ07jGZRl6J0FCAaf2k4PtVQ=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.9.1 h1:uwrxJXBnx76nyISkhr33kQLlUqjv7et7b9FjCen/tdc=
github.com/jackc/pgx/v5 v5.9.1/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View file

@ -2,7 +2,7 @@ package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
werr "git.ahall.se/go/wrappederror"
// Standard
"regexp"

211
main.go
View file

@ -2,14 +2,14 @@ package main
import (
// External
ws "git.gibonuddevalla.se/go/webservice"
"git.gibonuddevalla.se/go/webservice/session"
werr "git.gibonuddevalla.se/go/wrappederror"
"git.ahall.se/go/html_template"
werr "git.ahall.se/go/wrappederror"
// Internal
"smon/notification"
// Standard
"context"
"embed"
"encoding/json"
"flag"
@ -36,12 +36,11 @@ var (
flagConfigFile string
flagDev bool
flagVersion bool
service *ws.Service
logFile *os.File
parsedTemplates map[string]*template.Template
componentFilenames []string
notificationManager notification.Manager
fileConf FileConfiguration
config Config
htmlEngine HTMLTemplate.Engine
//go:embed sql
sqlFS embed.FS
@ -61,8 +60,8 @@ func init() { // {{{
if err != nil {
logger.Error("application", "error", werr.Wrap(err))
}
cfgPath := path.Join(confDir, "smon.yaml")
flag.StringVar(&flagConfigFile, "config", cfgPath, "Path and filename of the YAML configuration file")
cfgPath := path.Join(confDir, "smon.json")
flag.StringVar(&flagConfigFile, "config", cfgPath, "Path and filename of the JSON configuration file")
flag.BoolVar(&flagDev, "dev", false, "reload templates on each request")
flag.BoolVar(&flagVersion, "version", false, "Display version and exit")
flag.Parse()
@ -81,84 +80,85 @@ func main() { // {{{
os.Exit(0)
}
logger.Info("application", "version", VERSION)
var err error
werr.Init()
werr.SetLogCallback(logHandler)
service, err = ws.New(flagConfigFile, VERSION, logger)
logger.Info("application", "config", flagConfigFile)
config, err = initConfig(flagConfigFile)
if err != nil {
logger.Error("application", "error", err)
logger.Error("application", "op", "read_config", "error", err)
os.Exit(1)
}
if config.NodataInterval < 10 {
logger.Error("application → nodata_interval has to be larger or equal to 10.")
os.Exit(1)
}
j, _ := json.Marshal(service.Config.Application)
json.Unmarshal(j, &fileConf)
logFile, err = os.OpenFile(fileConf.LogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
err = initDb(
config.Database.Host,
config.Database.Port,
config.Database.Name,
config.Database.Username,
config.Database.Password,
)
if err != nil {
logger.Error("application", "error", err)
return
}
if fileConf.NodataInterval < 10 {
logger.Error("application → nodata_interval has to be larger or equal to 10.")
return
logger.Error("database", "error", err)
os.Exit(1)
}
service.SetDatabase(sqlProvider)
service.SetStaticFS(staticFS, "static")
service.SetStaticDirectory("static", flagDev)
service.InitDatabaseConnection()
err = service.Db.Connect()
htmlEngine, err = initHTMLTemplate()
if err != nil {
logger.Error("application", "error", err)
return
}
notificationManager, err = InitNotificationManager()
notificationManager, err = initNotificationManager()
if err != nil {
err = werr.Wrap(err).Log()
logger.Error("notification", "error", err)
}
service.Register("/", false, false, staticHandler)
http.HandleFunc("/", staticHandler)
service.Register("/area/new/{name}", false, false, actionAreaNew)
service.Register("/area/rename/{id}/{name}", false, false, actionAreaRename)
service.Register("/area/delete/{id}", false, false, actionAreaDelete)
http.HandleFunc("/area/new/{name}", actionAreaNew)
http.HandleFunc("/area/rename/{id}/{name}", actionAreaRename)
http.HandleFunc("/area/delete/{id}", actionAreaDelete)
service.Register("/section/new/{areaID}/{name}", false, false, actionSectionNew)
service.Register("/section/rename/{id}/{name}", false, false, actionSectionRename)
service.Register("/section/delete/{id}", false, false, actionSectionDelete)
http.HandleFunc("/section/new/{areaID}/{name}", actionSectionNew)
http.HandleFunc("/section/rename/{id}/{name}", actionSectionRename)
http.HandleFunc("/section/delete/{id}", actionSectionDelete)
service.Register("/problems", false, false, pageProblems)
service.Register("/problem/acknowledge/{id}", false, false, actionProblemAcknowledge)
service.Register("/problem/unacknowledge/{id}", false, false, actionProblemUnacknowledge)
http.HandleFunc("/problems", pageProblems)
http.HandleFunc("/problem/acknowledge/{id}", actionProblemAcknowledge)
http.HandleFunc("/problem/unacknowledge/{id}", actionProblemUnacknowledge)
service.Register("/datapoints", false, false, pageDatapoints)
service.Register("/datapoint/edit/{id}", false, false, pageDatapointEdit)
service.Register("/datapoint/update/{id}", false, false, actionDatapointUpdate)
service.Register("/datapoint/delete/{id}", false, false, actionDatapointDelete)
service.Register("/datapoint/values/{id}", false, false, pageDatapointValues)
service.Register("/datapoint/json/{id}", false, false, actionDatapointJson)
http.HandleFunc("/datapoints", pageDatapoints)
http.HandleFunc("/datapoint/edit/{id}", pageDatapointEdit)
http.HandleFunc("/datapoint/update/{id}", actionDatapointUpdate)
http.HandleFunc("/datapoint/delete/{id}", actionDatapointDelete)
http.HandleFunc("/datapoint/values/{id}", pageDatapointValues)
http.HandleFunc("/datapoint/json/{id}", actionDatapointJson)
service.Register("/triggers", false, false, pageTriggers)
service.Register("/trigger/create/{sectionID}/{name}", false, false, actionTriggerCreate)
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, actionTriggerDatapointAdd)
service.Register("/trigger/update/{id}", false, false, actionTriggerUpdate)
service.Register("/trigger/run/{id}", false, false, actionTriggerRun)
service.Register("/trigger/delete/{id}", false, false, actionTriggerDelete)
http.HandleFunc("/triggers", pageTriggers)
http.HandleFunc("/trigger/create/{sectionID}/{name}", actionTriggerCreate)
http.HandleFunc("/trigger/edit/{id}", pageTriggerEdit)
http.HandleFunc("/trigger/edit/{id}/{sectionID}", pageTriggerEdit)
http.HandleFunc("/trigger/addDatapoint/{id}/{datapointName}", actionTriggerDatapointAdd)
http.HandleFunc("/trigger/update/{id}", actionTriggerUpdate)
http.HandleFunc("/trigger/run/{id}", actionTriggerRun)
http.HandleFunc("/trigger/delete/{id}", actionTriggerDelete)
service.Register("/notifications", false, false, pageNotifications)
http.HandleFunc("/notifications", pageNotifications)
service.Register("/configuration", false, false, pageConfiguration)
service.Register("/configuration/theme", false, false, actionConfigurationTheme)
service.Register("/configuration/timezone", false, false, actionConfigurationTimezone)
service.Register("/configuration/notification", false, false, pageConfigurationNotification)
service.Register("/configuration/notification/update/{prio}", false, false, actionConfigurationNotificationUpdate)
service.Register("/configuration/notification/delete/{prio}", false, false, actionConfigurationNotificationDelete)
service.Register("/entry/{datapoint}", false, false, actionEntryDatapoint)
http.HandleFunc("/configuration", pageConfiguration)
http.HandleFunc("/configuration/theme", actionConfigurationTheme)
http.HandleFunc("/configuration/timezone", actionConfigurationTimezone)
http.HandleFunc("/configuration/notification", pageConfigurationNotification)
http.HandleFunc("/configuration/notification/update/{prio}", actionConfigurationNotificationUpdate)
http.HandleFunc("/configuration/notification/delete/{prio}", actionConfigurationNotificationDelete)
http.HandleFunc("/entry/{datapoint}", actionEntryDatapoint)
go nodataLoop()
@ -174,7 +174,9 @@ func main() { // {{{
os.Exit(1)
}
err = service.Start()
listen := fmt.Sprintf("%s:%d", config.Network.Address, config.Network.Port)
logger.Info("webserver", "listen", listen)
err = http.ListenAndServe(listen, nil)
if err != nil {
logger.Error("webserver", "error", werr.Wrap(err))
os.Exit(1)
@ -191,10 +193,8 @@ func sqlProvider(dbname string, version int) (sql []byte, found bool) { // {{{
found = true
return
} // }}}
func logHandler(err werr.Error) { // {{{
j, _ := json.Marshal(err)
logFile.Write(j)
logFile.Write([]byte("\n"))
func initHTMLTemplate() (HTMLTemplate.Engine, error) { // {{{
return HTMLTemplate.NewEngine(viewFS, staticFS, flagDev)
} // }}}
func httpError(w http.ResponseWriter, err error) { // {{{
@ -223,19 +223,19 @@ func pageError(w http.ResponseWriter, redirectURL string, pageErr error) { // {{
w.WriteHeader(302)
} // }}}
func staticHandler(w http.ResponseWriter, r *http.Request, sess *session.T) { // {{{
func staticHandler(w http.ResponseWriter, r *http.Request) { // {{{
if flagDev && !reloadTemplates(w) {
return
}
if r.URL.Path == "/" {
pageIndex(w, r, sess)
pageIndex(w, r)
return
}
service.StaticHandler(w, r, sess)
htmlEngine.StaticResource(w, r)
} // }}}
func actionEntryDatapoint(w http.ResponseWriter, r *http.Request, sess *session.T) { // {{{
func actionEntryDatapoint(w http.ResponseWriter, r *http.Request) { // {{{
dpoint := r.PathValue("datapoint")
value, _ := io.ReadAll(r.Body)
@ -307,15 +307,16 @@ func actionEntryDatapoint(w http.ResponseWriter, r *http.Request, sess *session.
} else {
errBody = nil
}
_, err = service.Db.Conn.Exec(
_, err = db.Exec(
context.Background(),
`
INSERT INTO notification_send(notification_id, problem_id, uuid, ok, error)
SELECT
id, $3, '', $4, $5
FROM notification
WHERE
service=$1 AND
prio=$2
INSERT INTO notification_send(notification_id, problem_id, uuid, ok, error)
SELECT
id, $3, '', $4, $5
FROM notification
WHERE
service=$1 AND
prio=$2
`,
(*notificationService).GetType(),
(*notificationService).GetPrio(),
@ -398,7 +399,7 @@ func getPage(layout, page string) (tmpl *template.Template, err error) { // {{{
return
} // }}}
func pageIndex(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func pageIndex(w http.ResponseWriter, r *http.Request) { // {{{
page := Page{
LAYOUT: "main",
PAGE: "index",
@ -407,7 +408,7 @@ func pageIndex(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
page.Render(w, r)
} // }}}
func actionAreaNew(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionAreaNew(w http.ResponseWriter, r *http.Request) { // {{{
name := r.PathValue("name")
err := AreaCreate(name)
if err != nil {
@ -419,7 +420,7 @@ func actionAreaNew(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{
w.WriteHeader(302)
return
} // }}}
func actionAreaRename(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionAreaRename(w http.ResponseWriter, r *http.Request) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
@ -438,7 +439,7 @@ func actionAreaRename(w http.ResponseWriter, r *http.Request, _ *session.T) { //
w.WriteHeader(302)
return
} // }}}
func actionAreaDelete(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionAreaDelete(w http.ResponseWriter, r *http.Request) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
@ -457,7 +458,7 @@ func actionAreaDelete(w http.ResponseWriter, r *http.Request, _ *session.T) { //
return
} // }}}
func actionSectionNew(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionSectionNew(w http.ResponseWriter, r *http.Request) { // {{{
idStr := r.PathValue("areaID")
areaID, err := strconv.Atoi(idStr)
if err != nil {
@ -476,7 +477,7 @@ func actionSectionNew(w http.ResponseWriter, r *http.Request, _ *session.T) { //
w.WriteHeader(302)
return
} // }}}
func actionSectionRename(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionSectionRename(w http.ResponseWriter, r *http.Request) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
@ -495,7 +496,7 @@ func actionSectionRename(w http.ResponseWriter, r *http.Request, _ *session.T) {
w.WriteHeader(302)
return
} // }}}
func actionSectionDelete(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionSectionDelete(w http.ResponseWriter, r *http.Request) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
@ -514,7 +515,7 @@ func actionSectionDelete(w http.ResponseWriter, r *http.Request, _ *session.T) {
return
} // }}}
func pageProblems(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func pageProblems(w http.ResponseWriter, r *http.Request) { // {{{
page := Page{
LAYOUT: "main",
PAGE: "problems",
@ -578,7 +579,7 @@ func pageProblems(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
page.Render(w, r)
return
} // }}}
func actionProblemAcknowledge(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionProblemAcknowledge(w http.ResponseWriter, r *http.Request) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
@ -597,7 +598,7 @@ func actionProblemAcknowledge(w http.ResponseWriter, r *http.Request, _ *session
return
} // }}}
func actionProblemUnacknowledge(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionProblemUnacknowledge(w http.ResponseWriter, r *http.Request) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
@ -617,7 +618,7 @@ func actionProblemUnacknowledge(w http.ResponseWriter, r *http.Request, _ *sessi
return
} // }}}
func pageDatapoints(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func pageDatapoints(w http.ResponseWriter, r *http.Request) { // {{{
page := Page{
LAYOUT: "main",
PAGE: "datapoints",
@ -643,7 +644,7 @@ func pageDatapoints(w http.ResponseWriter, r *http.Request, _ *session.T) { // {
page.Render(w, r)
return
} // }}}
func pageDatapointEdit(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func pageDatapointEdit(w http.ResponseWriter, r *http.Request) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
@ -700,7 +701,7 @@ func pageDatapointEdit(w http.ResponseWriter, r *http.Request, _ *session.T) { /
page.Render(w, r)
return
} // }}}
func actionDatapointUpdate(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionDatapointUpdate(w http.ResponseWriter, r *http.Request) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
@ -757,7 +758,7 @@ func actionDatapointUpdate(w http.ResponseWriter, r *http.Request, _ *session.T)
w.Header().Add("Location", "/datapoints")
w.WriteHeader(302)
} // }}}
func actionDatapointDelete(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionDatapointDelete(w http.ResponseWriter, r *http.Request) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
@ -774,7 +775,7 @@ func actionDatapointDelete(w http.ResponseWriter, r *http.Request, _ *session.T)
w.Header().Add("Location", "/datapoints")
w.WriteHeader(302)
} // }}}
func pageDatapointValues(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func pageDatapointValues(w http.ResponseWriter, r *http.Request) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
@ -833,7 +834,7 @@ func pageDatapointValues(w http.ResponseWriter, r *http.Request, _ *session.T) {
page.Render(w, r)
return
} // }}}
func actionDatapointJson(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionDatapointJson(w http.ResponseWriter, r *http.Request) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
@ -865,7 +866,7 @@ func actionDatapointJson(w http.ResponseWriter, r *http.Request, _ *session.T) {
w.Write(j)
} // }}}
func pageTriggers(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func pageTriggers(w http.ResponseWriter, r *http.Request) { // {{{
areas, err := TriggersRetrieve()
if err != nil {
httpError(w, werr.Wrap(err).Log())
@ -887,7 +888,7 @@ func pageTriggers(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
page.Render(w, r)
} // }}}
func actionTriggerCreate(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionTriggerCreate(w http.ResponseWriter, r *http.Request) { // {{{
name := r.PathValue("name")
sectionIDStr := r.PathValue("sectionID")
sectionID, err := strconv.Atoi(sectionIDStr)
@ -919,7 +920,7 @@ func actionTriggerCreate(w http.ResponseWriter, r *http.Request, _ *session.T) {
w.Header().Add("Content-Type", "application/json")
w.Write(j)
} // }}}
func pageTriggerEdit(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func pageTriggerEdit(w http.ResponseWriter, r *http.Request) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
@ -975,7 +976,7 @@ func pageTriggerEdit(w http.ResponseWriter, r *http.Request, _ *session.T) { //
page.Render(w, r)
} // }}}
func actionTriggerDatapointAdd(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionTriggerDatapointAdd(w http.ResponseWriter, r *http.Request) { // {{{
triggerID := r.PathValue("id")
dpName := r.PathValue("datapointName")
@ -1022,7 +1023,7 @@ func actionTriggerDatapointAdd(w http.ResponseWriter, r *http.Request, _ *sessio
w.Header().Add("Content-Type", "application/json")
w.Write(j)
} // }}}
func actionTriggerUpdate(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionTriggerUpdate(w http.ResponseWriter, r *http.Request) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
@ -1057,7 +1058,7 @@ func actionTriggerUpdate(w http.ResponseWriter, r *http.Request, _ *session.T) {
w.Header().Add("Location", "/triggers")
w.WriteHeader(302)
} // }}}
func actionTriggerRun(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionTriggerRun(w http.ResponseWriter, r *http.Request) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
@ -1092,7 +1093,7 @@ func actionTriggerRun(w http.ResponseWriter, r *http.Request, _ *session.T) { //
w.Header().Add("Content-Type", "application/json")
w.Write(j)
} // }}}
func actionTriggerDelete(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionTriggerDelete(w http.ResponseWriter, r *http.Request) { // {{{
idStr := r.PathValue("id")
id, err := strconv.Atoi(idStr)
if err != nil {
@ -1110,7 +1111,7 @@ func actionTriggerDelete(w http.ResponseWriter, r *http.Request, _ *session.T) {
w.WriteHeader(302)
} // }}}
func pageConfiguration(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func pageConfiguration(w http.ResponseWriter, r *http.Request) { // {{{
areas, err := AreaRetrieve()
if err != nil {
httpError(w, werr.Wrap(err).Log())
@ -1134,7 +1135,7 @@ func pageConfiguration(w http.ResponseWriter, r *http.Request, _ *session.T) { /
page.Render(w, r)
} // }}}
func actionConfigurationTheme(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionConfigurationTheme(w http.ResponseWriter, r *http.Request) { // {{{
theme := r.FormValue("theme")
err := smonConfig.SetTheme(theme)
if err != nil {
@ -1145,7 +1146,7 @@ func actionConfigurationTheme(w http.ResponseWriter, r *http.Request, _ *session
w.Header().Add("Location", "/configuration")
w.WriteHeader(302)
} // }}}
func actionConfigurationTimezone(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionConfigurationTimezone(w http.ResponseWriter, r *http.Request) { // {{{
timezone := r.FormValue("timezone")
_, err := time.LoadLocation(timezone)
@ -1163,7 +1164,7 @@ func actionConfigurationTimezone(w http.ResponseWriter, r *http.Request, _ *sess
w.Header().Add("Location", "/configuration")
w.WriteHeader(302)
} // }}}
func pageConfigurationNotification(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func pageConfigurationNotification(w http.ResponseWriter, r *http.Request) { // {{{
// This function is either receiving a type when creating a new service,
// or a prio when editing an existing.
notificationType := r.URL.Query().Get("type")
@ -1215,7 +1216,7 @@ func pageConfigurationNotification(w http.ResponseWriter, r *http.Request, _ *se
page.Render(w, r)
} // }}}
func actionConfigurationNotificationUpdate(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionConfigurationNotificationUpdate(w http.ResponseWriter, r *http.Request) { // {{{
prioStr := r.PathValue("prio")
prio, err := strconv.Atoi(prioStr)
if err != nil {
@ -1268,7 +1269,7 @@ func actionConfigurationNotificationUpdate(w http.ResponseWriter, r *http.Reques
w.Header().Add("Location", "/configuration")
w.WriteHeader(302)
} // }}}
func actionConfigurationNotificationDelete(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func actionConfigurationNotificationDelete(w http.ResponseWriter, r *http.Request) { // {{{
prioStr := r.PathValue("prio")
prio, err := strconv.Atoi(prioStr)
if err != nil {
@ -1283,7 +1284,7 @@ func actionConfigurationNotificationDelete(w http.ResponseWriter, r *http.Reques
w.WriteHeader(302)
} // }}}
func pageNotifications(w http.ResponseWriter, r *http.Request, _ *session.T) { // {{{
func pageNotifications(w http.ResponseWriter, r *http.Request) { // {{{
var err error
// Manage the values from the timefilter component

View file

@ -2,13 +2,14 @@ package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
werr "git.ahall.se/go/wrappederror"
"github.com/jackc/pgx/v5"
// Internal
"smon/notification"
// Standard
"database/sql"
"context"
"encoding/json"
"time"
)
@ -24,7 +25,7 @@ func nodataLoop() {
var datapoints []Datapoint
var err error
ticker := time.NewTicker(time.Second * time.Duration(fileConf.NodataInterval))
ticker := time.NewTicker(time.Second * time.Duration(config.NodataInterval))
for {
<-ticker.C
datapoints, err = nodataDatapoints()
@ -62,7 +63,8 @@ func nodataNotify(datapoint Datapoint, state string) (err error) {
} else {
errBody = nil
}
_, err = service.Db.Conn.Exec(
_, err = db.Exec(
context.Background(),
`
INSERT INTO notification_send(notification_id, datapoint_nodata_id, uuid, ok, error)
SELECT
@ -93,8 +95,9 @@ func nodataNotify(datapoint Datapoint, state string) (err error) {
func nodataDatapoints() (datapoints []Datapoint, err error) {
datapoints = []Datapoint{}
var rows *sql.Rows
rows, err = service.Db.Conn.Query(`
rows, _ := db.Query(
context.Background(),
`
UPDATE datapoint
SET
nodata_is_problem = true
@ -114,20 +117,12 @@ func nodataDatapoints() (datapoints []Datapoint, err error) {
datapoint.id,
datapoint.name
`)
`)
datapoints, err = pgx.CollectRows(rows, pgx.RowToStructByName[Datapoint])
if err != nil {
err = werr.Wrap(err)
return
}
defer rows.Close()
var dp Datapoint
for rows.Next() {
if err = rows.Scan(&dp.ID, &dp.Name); err != nil {
err = werr.Wrap(err)
return
}
datapoints = append(datapoints, dp)
}
return
}

View file

@ -2,7 +2,7 @@ package notification
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
werr "git.ahall.se/go/wrappederror"
// Standard
"log/slog"

View file

@ -2,7 +2,7 @@ package notification
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
werr "git.ahall.se/go/wrappederror"
// Standard
"bytes"

View file

@ -2,7 +2,7 @@ package notification
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
werr "git.ahall.se/go/wrappederror"
// Standard
"log/slog"

View file

@ -2,7 +2,7 @@ package notification
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
werr "git.ahall.se/go/wrappederror"
// Standard
"bytes"

View file

@ -2,7 +2,7 @@ package notification
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
werr "git.ahall.se/go/wrappederror"
// Standard
"encoding/json"

View file

@ -2,13 +2,14 @@ package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
"github.com/jmoiron/sqlx"
werr "git.ahall.se/go/wrappederror"
"github.com/jackc/pgx/v5"
// Internal
"smon/notification"
// Standard
"context"
"database/sql"
"encoding/json"
"time"
@ -30,19 +31,24 @@ type NotificationSend struct {
func notificationLog(notificationService *notification.Service, problemID int, err error) {
if err == nil {
logger.Info("notification", "service", (*notificationService).GetType(), "problemID", problemID, "prio", (*notificationService).GetPrio(), "ok", true)
service.Db.Conn.Query(
_, dbErr := db.Exec(
context.Background(),
`
INSERT INTO notification_send()
`,
)
if dbErr != nil {
logger.Error("notification", "op", "notification_send", "error", werr.Wrap(err))
}
} else {
logger.Error("notification", "service", (*notificationService).GetType(), "problemID", problemID, "prio", (*notificationService).GetPrio(), "ok", false, "error", err)
}
}
func notificationsSent(from, to time.Time) (nss []NotificationSend, err error) {
var rows *sqlx.Rows
rows, err = service.Db.Conn.Queryx(
rows, _ := db.Query(
context.Background(),
`
SELECT
n.prio,
@ -71,15 +77,8 @@ func notificationsSent(from, to time.Time) (nss []NotificationSend, err error) {
from,
to,
)
if err != nil {
err = werr.Wrap(err)
return
}
defer rows.Close()
for rows.Next() {
ns := NotificationSend{}
err = rows.StructScan(&ns)
nss, err = pgx.CollectRows(rows, func(row pgx.CollectableRow) (ns NotificationSend, err error) {
ns, err = pgx.RowToStructByName[NotificationSend](row)
if err != nil {
err = werr.Wrap(err)
return
@ -93,7 +92,12 @@ func notificationsSent(from, to time.Time) (nss []NotificationSend, err error) {
j, err = json.MarshalIndent(foo, "", " ")
ns.ErrorIndented = string(j)
nss = append(nss, ns)
return
})
if err != nil {
err = werr.Wrap(err)
return
}
return
}

View file

@ -2,14 +2,16 @@ package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
"github.com/lib/pq"
werr "git.ahall.se/go/wrappederror"
"github.com/jackc/pgx/v5/pgconn"
// Internal
"smon/notification"
// Standard
"context"
"encoding/json"
"errors"
)
type DbNotificationService struct {
@ -19,9 +21,11 @@ type DbNotificationService struct {
Prio int
}
func InitNotificationManager() (nm notification.Manager, err error) { // {{{
func initNotificationManager() (nm notification.Manager, err error) { // {{{
var dbServices []DbNotificationService
row := service.Db.Conn.QueryRow(`
row := db.QueryRow(
context.Background(),
`
WITH services AS (
SELECT
id,
@ -36,6 +40,7 @@ func InitNotificationManager() (nm notification.Manager, err error) { // {{{
FROM services s
`,
)
var dbData []byte
err = row.Scan(&dbData)
if err != nil {
@ -70,7 +75,8 @@ func InitNotificationManager() (nm notification.Manager, err error) { // {{{
} // }}}
func UpdateNotificationService(svc notification.Service) (created bool, err error) { // {{{
if svc.Exists() {
_, err = service.Db.Conn.Exec(
_, err = db.Exec(
context.Background(),
`
UPDATE public.notification
SET
@ -84,11 +90,11 @@ func UpdateNotificationService(svc notification.Service) (created bool, err erro
svc.Updated().JSON(),
)
} else {
_, err = service.Db.Conn.Exec(
_, err = db.Exec(
context.Background(),
`
INSERT INTO public.notification(prio, configuration, service)
VALUES($1, $2, $3)
`,
svc.Updated().GetPrio(),
svc.Updated().JSON(),
@ -99,8 +105,8 @@ func UpdateNotificationService(svc notification.Service) (created bool, err erro
if err != nil {
// Check if this is just a duplicated prio, which isn't allowed.
pgErr, isPgErr := err.(*pq.Error)
if isPgErr && pgErr.Code == "23505" {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr); pgErr.Code == "23505" {
return false, werr.New("Prio %d is already used by another service", svc.Updated().GetPrio())
}
@ -117,12 +123,12 @@ func UpdateNotificationService(svc notification.Service) (created bool, err erro
return
} // }}}
func DeleteNotificationService(prio int) (err error) { // {{{
_, err = service.Db.Conn.Exec(
_, err = db.Exec(
context.Background(),
`
DELETE FROM public.notification
WHERE
prio = $1
`,
prio,
)
@ -135,7 +141,7 @@ func DeleteNotificationService(prio int) (err error) { // {{{
func AcknowledgeNotification(uuid string) (err error) { // {{{
/*
_, err = service.Db.Conn.Exec(`UPDATE schedule SET acknowledged=true WHERE schedule_uuid=$1`, uuid)
_, err = db.Exec(context.Background(), `UPDATE schedule SET acknowledged=true WHERE schedule_uuid=$1`, uuid)
if err != nil {
err = werr.Wrap(err).WithData(uuid)
}

View file

@ -2,7 +2,7 @@ package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
werr "git.ahall.se/go/wrappederror"
// Standard
"fmt"

View file

@ -2,9 +2,10 @@ package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
werr "git.ahall.se/go/wrappederror"
// Standard
"context"
"database/sql"
"encoding/json"
"fmt"
@ -28,7 +29,9 @@ type Problem struct { // {{{
func ProblemsRetrieve(showCurrent bool, from, to time.Time) (problems []Problem, err error) { // {{{
problems = []Problem{}
row := service.Db.Conn.QueryRow(`
row := db.QueryRow(
context.Background(),
`
SELECT
jsonb_agg(problems.*)
FROM (
@ -104,7 +107,9 @@ func ProblemsRetrieve(showCurrent bool, from, to time.Time) (problems []Problem,
return
} // }}}
func ProblemStart(trigger Trigger) (problemID int, err error) { // {{{
row := service.Db.Conn.QueryRow(`
row := db.QueryRow(
context.Background(),
`
SELECT COUNT(id)
FROM problem
WHERE
@ -124,7 +129,8 @@ func ProblemStart(trigger Trigger) (problemID int, err error) { // {{{
// Open up a new problem if no open exists.
if openProblems == 0 {
datapointValuesJson, _ := json.Marshal(trigger.DatapointValues)
row = service.Db.Conn.QueryRow(
row = db.QueryRow(
context.Background(),
`INSERT INTO problem(trigger_id, trigger_name, datapoints, trigger_expression) VALUES($1, $2, $3, $4) RETURNING id`,
trigger.ID,
trigger.Name,
@ -139,9 +145,13 @@ func ProblemStart(trigger Trigger) (problemID int, err error) { // {{{
return
} // }}}
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)
row := db.QueryRow(
context.Background(),
`UPDATE problem SET "end"=NOW() WHERE trigger_id=$1 AND "end" IS NULL RETURNING id`,
trigger.ID,
)
err = row.Scan(&problemID)
if err == sql.ErrNoRows {
err = nil
return
@ -154,7 +164,7 @@ func ProblemClose(trigger Trigger) (problemID int, err error) { // {{{
return
} // }}}
func ProblemAcknowledge(id int, state bool) (err error) { // {{{
_, err = service.Db.Conn.Exec(`UPDATE problem SET "acknowledged"=$2 WHERE id=$1`, id, state)
_, err = db.Exec(context.Background(), `UPDATE problem SET "acknowledged"=$2 WHERE id=$1`, id, state)
if err != nil {
err = werr.Wrap(err).WithData(id)
return

View file

@ -2,9 +2,10 @@ package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
werr "git.ahall.se/go/wrappederror"
// Standard
"context"
"sort"
)
@ -26,20 +27,26 @@ func (s *Section) SortedTriggers() []Trigger {
}
func SectionCreate(areaID int, name string) (err error) { // {{{
_, err = service.Db.Conn.Exec(`INSERT INTO section(area_id, name) VALUES($1, $2)`, areaID, name)
_, err = db.Exec(context.Background(), `INSERT INTO section(area_id, name) VALUES($1, $2)`, areaID, name)
if err != nil {
err = werr.Wrap(err)
}
return
} // }}}
func SectionRename(id int, name string) (err error) { // {{{
_, err = service.Db.Conn.Exec(`UPDATE section SET name=$2 WHERE id=$1`, id, name)
_, err = db.Exec(context.Background(), `UPDATE section SET name=$2 WHERE id=$1`, id, name)
if err != nil {
err = werr.Wrap(err)
}
return
} // }}}
func SectionDelete(id int) (err error) { // {{{
_, err = service.Db.Conn.Exec(`DELETE FROM public.trigger WHERE section_id = $1`, id)
_, err = db.Exec(context.Background(), `DELETE FROM public.trigger WHERE section_id = $1`, id)
if err != nil {
return werr.Wrap(err).WithData(id)
}
_, err = service.Db.Conn.Exec(`DELETE FROM public.section WHERE id = $1`, id)
_, err = db.Exec(context.Background(), `DELETE FROM public.section WHERE id = $1`, id)
if err != nil {
return werr.Wrap(err).WithData(id)
}

View file

@ -2,7 +2,7 @@ package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
werr "git.ahall.se/go/wrappederror"
// Standard
"time"

View file

@ -2,15 +2,16 @@ package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
werr "git.ahall.se/go/wrappederror"
"github.com/expr-lang/expr"
"github.com/expr-lang/expr/ast"
"github.com/expr-lang/expr/parser"
"github.com/lib/pq"
"github.com/jackc/pgx/v5/pgconn"
// Standard
"database/sql"
"context"
"encoding/json"
"errors"
"fmt"
"strings"
)
@ -45,7 +46,9 @@ func TriggerCreate(sectionID int, name string) (t Trigger, err error) { // {{{
func TriggersRetrieve() (areas []Area, err error) { // {{{
areas = []Area{}
row := service.Db.Conn.QueryRow(`
row := db.QueryRow(
context.Background(),
`
WITH section_triggers AS (
SELECT
s.id AS id,
@ -99,7 +102,9 @@ func TriggersRetrieve() (areas []Area, err error) { // {{{
} // }}}
func TriggersRetrieveByDatapoint(datapointName string) (triggers []Trigger, err error) { // {{{
triggers = []Trigger{}
row := service.Db.Conn.QueryRow(`
row := db.QueryRow(
context.Background(),
`
SELECT jsonb_agg(t.*)
FROM public."trigger" t
WHERE
@ -129,7 +134,7 @@ func TriggersRetrieveByDatapoint(datapointName string) (triggers []Trigger, err
return
} // }}}
func TriggerRetrieve(id int) (trigger Trigger, err error) { // {{{
row := service.Db.Conn.QueryRow(`SELECT to_jsonb(t.*) FROM "trigger" t WHERE id=$1`, id)
row := db.QueryRow(context.Background(), `SELECT to_jsonb(t.*) FROM "trigger" t WHERE id=$1`, id)
var jsonData []byte
err = row.Scan(&jsonData)
if err != nil {
@ -141,7 +146,7 @@ func TriggerRetrieve(id int) (trigger Trigger, err error) { // {{{
return
} // }}}
func TriggerDelete(id int) (err error) { // {{{
_, err = service.Db.Conn.Exec(`DELETE FROM public.trigger WHERE id=$1`, id)
_, err = db.Exec(context.Background(), `DELETE FROM public.trigger WHERE id=$1`, id)
if err != nil {
return werr.Wrap(err).WithData(id)
}
@ -172,8 +177,9 @@ func (t *Trigger) Update() (err error) { // {{{
}
jsonDatapoints, _ := json.Marshal(t.Datapoints)
if t.ID == 0 {
var row *sql.Row
row = service.Db.Conn.QueryRow(`
row := db.QueryRow(
context.Background(),
`
INSERT INTO "trigger"(name, section_id, expression, datapoints)
VALUES($1, $2, $3, $4)
RETURNING id
@ -201,7 +207,9 @@ func (t *Trigger) Update() (err error) { // {{{
return
}
} else {
_, err = service.Db.Conn.Exec(`
_, err = db.Exec(
context.Background(),
`
UPDATE "trigger"
SET
name=$2,
@ -217,20 +225,26 @@ func (t *Trigger) Update() (err error) { // {{{
)
}
if pqErr, ok := err.(*pq.Error); ok {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
err = werr.Wrap(err).WithData(
struct {
Trigger *Trigger
PostgresCode pq.ErrorCode
PostgresCode string
PostgresMsg string
}{
t,
pqErr.Code,
pqErr.Code.Name(),
pgErr.Code,
pgErr.Message,
})
} else if err != nil {
err = werr.Wrap(err).WithData(t)
return
}
if err != nil {
err = werr.Wrap(err).WithData(t)
return
}
return
} // }}}
func (t *Trigger) Run() (output any, err error) { // {{{