Added datavalue filtering on date

This commit is contained in:
Magnus Åhall 2024-06-27 08:59:34 +02:00
parent b6e1139e8a
commit 43d8938459
6 changed files with 69 additions and 5 deletions

View File

@ -327,8 +327,21 @@ func DatapointDelete(id int) (err error) { // {{{
}
return
} // }}}
func DatapointValues(id int) (values []DatapointValue, err error) { // {{{
rows, err := service.Db.Conn.Queryx(`SELECT * FROM datapoint_value WHERE datapoint_id=$1 ORDER BY ts DESC LIMIT 500`, id)
func DatapointValues(id int, from, to time.Time) (values []DatapointValue, err error) { // {{{
rows, err := service.Db.Conn.Queryx(
`
SELECT *
FROM datapoint_value
WHERE
datapoint_id=$1 AND
ts >= $2 AND
ts <= $3
ORDER BY ts DESC
`,
id,
from,
to,
)
if err != nil {
err = werr.Wrap(err).WithData(id)
return

37
main.go
View File

@ -680,13 +680,44 @@ func pageDatapointValues(w http.ResponseWriter, r *http.Request, _ *session.T) {
return
}
// GET parameters.
display := r.URL.Query().Get("display")
if display == "" && datapoint.Datatype == INT {
display = "graph"
}
var timeFrom, timeTo time.Time
timeFromParam := r.URL.Query().Get("f")
if timeFromParam == "" {
timeFrom = time.Now().Add(time.Duration(-(24 * 7) * time.Hour))
} else {
timeFrom, err = time.Parse("2006-01-02T15:04:05", timeFromParam)
if err != nil {
httpError(w, werr.Wrap(err).Log())
return
}
}
timeToParam := r.URL.Query().Get("t")
if timeToParam == "" {
timeTo = time.Now()
} else {
timeTo, err = time.Parse("2006-01-02T15:04:05", timeToParam)
if err != nil {
httpError(w, werr.Wrap(err).Log())
return
}
}
// Fetch data point values according to the times.
var values []DatapointValue
values, err = DatapointValues(id)
values, err = DatapointValues(id, timeFrom, timeTo)
if err != nil {
httpError(w, werr.Wrap(err).Log())
return
}
logger.Info("FOO", "from", timeFrom, "to", timeTo, "t", timeToParam)
page := Page{
LAYOUT: "main",
PAGE: "datapoint_values",
@ -699,7 +730,9 @@ func pageDatapointValues(w http.ResponseWriter, r *http.Request, _ *session.T) {
page.Data = map[string]any{
"Datapoint": datapoint,
"Values": values,
"Display": r.URL.Query().Get("display"),
"TimeFrom": timeFrom.Format("2006-01-02T15:04:05"),
"TimeTo": timeTo.Format("2006-01-02T15:04:05"),
"Display": display,
}
page.Render(w)
return

View File

@ -68,6 +68,7 @@
grid-gap: 8px;
}
.graph {
width: 99%;
height: 600px;
border: 1px solid #fff;
}

View File

@ -68,6 +68,7 @@
grid-gap: 8px;
}
.graph {
width: 99%;
height: 600px;
border: 1px solid #fff;
}

View File

@ -83,6 +83,7 @@
}
.graph {
width: 99%;
height: 600px;
border: 1px solid #fff;
}

View File

@ -1,11 +1,26 @@
{{ define "page" }}
{{ $version := .VERSION }}
{{ $graph := and (eq .Data.Display "graph") (eq .Data.Datapoint.Datatype "INT") }}
<link rel="stylesheet" type="text/css" href="/css/{{ .VERSION }}/{{ .CONFIG.THEME }}/datapoints.css">
<script src="/js/{{ .VERSION }}/lib/plotly-2.32.0.min.js" charset="utf-8"></script>
{{ block "page_label" . }}{{end}}
{{ if and (eq .Data.Display "graph") (eq .Data.Datapoint.Datatype "INT") }}
<form action="/datapoint/values/{{ .Data.Datapoint.ID }}" method="get">
{{ if eq .Data.Datapoint.Datatype "INT" }}
<div>
<input name="display" value="graph" type="radio" id="display-graph" {{ if $graph }} checked {{ end}}> <label for="display-graph">Graph</label>
<input name="display" value="list" type="radio" id="display-list" {{ if not $graph }} checked {{ end }}> <label for="display-list">List</label>
</div>
{{ end }}
<input name="f" type="datetime-local" value="{{ .Data.TimeFrom }}">
<input name="t" type="datetime-local" value="{{ .Data.TimeTo }}">
<button>OK</button>
</form>
<br><br>
{{ if $graph }}
<div class="graph" id="tester"></div>
<script type="text/javascript">
const tester = document.getElementById('tester');