Stricter datetime input
This commit is contained in:
parent
3109124a88
commit
ab87da256c
@ -6,9 +6,11 @@ import (
|
|||||||
|
|
||||||
// Standard
|
// Standard
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Configuration struct {
|
type Configuration struct {
|
||||||
|
timezoneLocation *time.Location
|
||||||
Settings map[string]string
|
Settings map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,6 +38,7 @@ func SmonConfigInit() (cfg Configuration, err error) {
|
|||||||
cfg.Settings[setting] = value
|
cfg.Settings[setting] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = cfg.LoadTimezone()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,6 +52,15 @@ func (cfg *Configuration) Validate() (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cfg *Configuration) LoadTimezone() (err error) {
|
||||||
|
cfg.timezoneLocation, err = time.LoadLocation(cfg.Settings["TIMEZONE"])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *Configuration) Timezone() *time.Location {
|
||||||
|
return cfg.timezoneLocation
|
||||||
|
}
|
||||||
|
|
||||||
func (cfg *Configuration) SetTheme(theme string) (err error) {
|
func (cfg *Configuration) SetTheme(theme string) (err error) {
|
||||||
cfg.Settings["THEME"] = theme
|
cfg.Settings["THEME"] = theme
|
||||||
_, err = service.Db.Conn.Exec(`UPDATE public.configuration SET value=$1 WHERE setting='THEME'`, theme)
|
_, err = service.Db.Conn.Exec(`UPDATE public.configuration SET value=$1 WHERE setting='THEME'`, theme)
|
||||||
@ -57,6 +69,15 @@ func (cfg *Configuration) SetTheme(theme string) (err error) {
|
|||||||
|
|
||||||
func (cfg *Configuration) SetTimezone(tz string) (err error) {
|
func (cfg *Configuration) SetTimezone(tz string) (err error) {
|
||||||
cfg.Settings["TIMEZONE"] = tz
|
cfg.Settings["TIMEZONE"] = tz
|
||||||
|
err = cfg.LoadTimezone()
|
||||||
|
if err != nil {
|
||||||
|
return werr.Wrap(err).WithData(tz)
|
||||||
|
}
|
||||||
|
|
||||||
_, err = service.Db.Conn.Exec(`UPDATE public.configuration SET value=$1 WHERE setting='TIMEZONE'`, tz)
|
_, err = service.Db.Conn.Exec(`UPDATE public.configuration SET value=$1 WHERE setting='TIMEZONE'`, tz)
|
||||||
|
if err != nil {
|
||||||
|
return werr.Wrap(err).WithData(tz)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
24
datapoint.go
24
datapoint.go
@ -120,8 +120,12 @@ func (dp Datapoint) Update() (err error) { // {{{
|
|||||||
} // }}}
|
} // }}}
|
||||||
|
|
||||||
func DatapointAdd[T any](name string, value T) (err error) { // {{{
|
func DatapointAdd[T any](name string, value T) (err error) { // {{{
|
||||||
row := service.Db.Conn.QueryRow(`SELECT id, datatype FROM datapoint WHERE name=$1`, name)
|
type dpRequest = struct {
|
||||||
|
ID int
|
||||||
|
value any
|
||||||
|
}
|
||||||
|
|
||||||
|
row := service.Db.Conn.QueryRow(`SELECT id, datatype FROM datapoint WHERE name=$1`, name)
|
||||||
var dpID int
|
var dpID int
|
||||||
var dpType DatapointType
|
var dpType DatapointType
|
||||||
|
|
||||||
@ -140,13 +144,21 @@ func DatapointAdd[T any](name string, value T) (err error) { // {{{
|
|||||||
case STRING:
|
case STRING:
|
||||||
_, err = service.Db.Conn.Exec(`INSERT INTO datapoint_value(datapoint_id, value_string) VALUES($1, $2)`, dpID, value)
|
_, err = service.Db.Conn.Exec(`INSERT INTO datapoint_value(datapoint_id, value_string) VALUES($1, $2)`, dpID, value)
|
||||||
case DATETIME:
|
case DATETIME:
|
||||||
_, err = service.Db.Conn.Exec(`INSERT INTO datapoint_value(datapoint_id, value_datetime) VALUES($1, $2)`, dpID, value)
|
// Time value is required to be a RFC 3339 formatted time string
|
||||||
|
var t time.Time
|
||||||
|
valueStr, ok := any(value).([]byte)
|
||||||
|
if !ok {
|
||||||
|
return werr.New("DATETIME value not a string").WithData(dpRequest{dpID, value})
|
||||||
|
}
|
||||||
|
t, err = stringToTime(string(valueStr))
|
||||||
|
if err != nil {
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = werr.Wrap(err).WithData(struct {
|
err = werr.Wrap(err).WithData(dpRequest{dpID, value})
|
||||||
ID int
|
|
||||||
value any
|
|
||||||
}{dpID, value})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
helper.go
Normal file
11
helper.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
// Standard
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func stringToTime(strTime string) (t time.Time, err error) {// {{{
|
||||||
|
t, err = time.Parse(time.RFC3339, strTime)
|
||||||
|
return
|
||||||
|
}// }}}
|
3
main.go
3
main.go
@ -369,7 +369,7 @@ func getPage(layout, page string) (tmpl *template.Template, err error) { // {{{
|
|||||||
funcMap := template.FuncMap{
|
funcMap := template.FuncMap{
|
||||||
"format_time": func(t time.Time) template.HTML {
|
"format_time": func(t time.Time) template.HTML {
|
||||||
return template.HTML(
|
return template.HTML(
|
||||||
t.Local().Format(`<span class="date">2006-01-02</span> <span class="time">15:04<span class="seconds">:05</span></span>`),
|
t.In(smonConfig.Timezone()).Format(`<span class="date">2006-01-02</span> <span class="time">15:04<span class="seconds">:05</span></span>`),
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -739,7 +739,6 @@ func pageDatapointValues(w http.ResponseWriter, r *http.Request, _ *session.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info("FOO", "from", timeFrom, "to", timeTo, "t", timeToParam)
|
|
||||||
page := Page{
|
page := Page{
|
||||||
LAYOUT: "main",
|
LAYOUT: "main",
|
||||||
PAGE: "datapoint_values",
|
PAGE: "datapoint_values",
|
||||||
|
Loading…
Reference in New Issue
Block a user