Refactored timeparsing from HTML input

This commit is contained in:
Magnus Åhall 2024-06-27 13:45:01 +02:00
parent ab87da256c
commit 530204c1a5
6 changed files with 45 additions and 22 deletions

View File

@ -1,6 +1,9 @@
package main package main
import ( import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
// Standard // Standard
"time" "time"
) )
@ -9,3 +12,18 @@ func stringToTime(strTime string) (t time.Time, err error) {// {{{
t, err = time.Parse(time.RFC3339, strTime) t, err = time.Parse(time.RFC3339, strTime)
return return
}// }}} }// }}}
func parseHTMLDateTime(str string, dflt time.Time) (t time.Time, err error) {
// Browser sending 2024-06-27T10:43 (16 characters) when seconds is 00.
if len(str) == 16 {
str += ":00"
}
if str == "" {
return dflt, nil
} else {
t, err = time.ParseInLocation("2006-01-02T15:04:05", str, smonConfig.Timezone())
if err != nil {
err = werr.Wrap(err)
}
}
return
}

18
main.go
View File

@ -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.In(smonConfig.Timezone()).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:05<span class="seconds">:05</span></span>`),
) )
}, },
} }
@ -709,27 +709,19 @@ func pageDatapointValues(w http.ResponseWriter, r *http.Request, _ *session.T) {
} }
var timeFrom, timeTo time.Time var timeFrom, timeTo time.Time
timeFromParam := r.URL.Query().Get("f") yesterday := time.Now().Add(time.Duration(-24 * time.Hour))
if timeFromParam == "" {
timeFrom = time.Now().Add(time.Duration(-(24 * 7) * time.Hour)) timeFrom, err = parseHTMLDateTime(r.URL.Query().Get("f"), yesterday)
} else {
timeFrom, err = time.Parse("2006-01-02T15:04:05", timeFromParam)
if err != nil { if err != nil {
httpError(w, werr.Wrap(err).Log()) httpError(w, werr.Wrap(err).Log())
return return
} }
}
timeToParam := r.URL.Query().Get("t") timeTo, err = parseHTMLDateTime(r.URL.Query().Get("t"), time.Now())
if timeToParam == "" {
timeTo = time.Now()
} else {
timeTo, err = time.Parse("2006-01-02T15:04:05", timeToParam)
if err != nil { if err != nil {
httpError(w, werr.Wrap(err).Log()) httpError(w, werr.Wrap(err).Log())
return return
} }
}
// Fetch data point values according to the times. // Fetch data point values according to the times.
var values []DatapointValue var values []DatapointValue

View File

@ -42,6 +42,9 @@
gap: 16px; gap: 16px;
white-space: nowrap; white-space: nowrap;
} }
#values .header {
font-weight: bold;
}
.widgets { .widgets {
display: grid; display: grid;
grid-template-columns: min-content 1fr; grid-template-columns: min-content 1fr;

View File

@ -42,6 +42,9 @@
gap: 16px; gap: 16px;
white-space: nowrap; white-space: nowrap;
} }
#values .header {
font-weight: bold;
}
.widgets { .widgets {
display: grid; display: grid;
grid-template-columns: min-content 1fr; grid-template-columns: min-content 1fr;

View File

@ -51,6 +51,10 @@
grid-template-columns: repeat(2, min-content); grid-template-columns: repeat(2, min-content);
gap: 16px; gap: 16px;
white-space: nowrap; white-space: nowrap;
.header {
font-weight: bold;
}
} }
.widgets { .widgets {

View File

@ -14,8 +14,8 @@
</div> </div>
{{ end }} {{ end }}
<input name="f" type="datetime-local" value="{{ .Data.TimeFrom }}"> <input name="f" type="datetime-local" step="1" value="{{ .Data.TimeFrom }}">
<input name="t" type="datetime-local" value="{{ .Data.TimeTo }}"> <input name="t" type="datetime-local" step="1" value="{{ .Data.TimeTo }}">
<button>OK</button> <button>OK</button>
</form> </form>
<br><br> <br><br>
@ -43,9 +43,12 @@
</script> </script>
{{ else }} {{ else }}
<div id="values"> <div id="values">
<div class="header">Value added</div>
<div class="header">Value</div>
<div class="line"></div>
{{ range .Data.Values }} {{ range .Data.Values }}
<div class="value">{{ format_time .Ts }}</div> <div class="value">{{ format_time .Ts }}</div>
<div class="value">{{ .Value }}</div> <div class="value">{{ format_time .Value }}</div>
{{ end }} {{ end }}
</div> </div>
{{ end }} {{ end }}