Faster datapoint latest value display

This commit is contained in:
Magnus Åhall 2024-07-24 15:39:04 +02:00
parent f8a64e4dfd
commit 570ea064aa
2 changed files with 18 additions and 25 deletions

View File

@ -162,8 +162,6 @@ func DatapointAdd[T any](name string, value T) (err error) { // {{{
return return
} }
service.Db.Conn.Exec(`UPDATE datapoint SET last_value = NOW(), nodata_is_problem = false WHERE id=$1`, dpID)
return return
} // }}} } // }}}
@ -172,30 +170,19 @@ func DatapointsRetrieve() (dps []Datapoint, err error) { // {{{
var rows *sqlx.Rows var rows *sqlx.Rows
rows, err = service.Db.Conn.Queryx(` rows, err = service.Db.Conn.Queryx(`
SELECT SELECT
dp.id, id, name, datatype, last_value, "group", comment, nodata_problem_seconds,
dp.name, last_value_id AS v_id,
dp.datatype, CASE
dp.last_value, WHEN last_value_id IS NULL THEN null
dp.group, ELSE last_value
dp.comment, END AS ts,
dp.nodata_problem_seconds, last_value_int AS value_int,
last_value_string AS value_string,
dpv.id AS v_id, last_value_datetime AS value_datetime
dpv.ts, FROM datapoint
dpv.value_int,
dpv.value_string,
dpv.value_datetime
FROM public.datapoint dp
LEFT JOIN (
SELECT
*,
row_number() OVER (PARTITION BY "datapoint_id" ORDER BY ts DESC) AS rn
FROM datapoint_value
) dpv ON dpv.datapoint_id = dp.id AND rn = 1
ORDER BY ORDER BY
dp.group ASC, "group" ASC,
dp.name ASC name ASC
`) `)
if err != nil { if err != nil {
err = werr.Wrap(err) err = werr.Wrap(err)

View File

@ -1,4 +1,5 @@
/* Adding last values to the datapoint table since they are a regularly used value. */ /* Adding last values to the datapoint table since they are a regularly used value. */
ALTER TABLE public.datapoint ADD COLUMN last_value_id int4 NULL;
ALTER TABLE public.datapoint ADD COLUMN last_value_int int8 NULL; ALTER TABLE public.datapoint ADD COLUMN last_value_int int8 NULL;
ALTER TABLE public.datapoint ADD COLUMN last_value_string varchar NULL; ALTER TABLE public.datapoint ADD COLUMN last_value_string varchar NULL;
ALTER TABLE public.datapoint ADD COLUMN last_value_datetime timestamptz NULL; ALTER TABLE public.datapoint ADD COLUMN last_value_datetime timestamptz NULL;
@ -8,12 +9,14 @@ ALTER TABLE public.datapoint ADD COLUMN last_value_datetime timestamptz NULL;
/* Once-run query to update it to the latest, to avoid user having to wait for the next entry. */ /* Once-run query to update it to the latest, to avoid user having to wait for the next entry. */
UPDATE public.datapoint AS dp UPDATE public.datapoint AS dp
SET SET
last_value_id = dpv.id,
last_value_int = dpv.value_int, last_value_int = dpv.value_int,
last_value_string = dpv.value_string, last_value_string = dpv.value_string,
last_value_datetime = dpv.value_datetime last_value_datetime = dpv.value_datetime
FROM ( FROM (
SELECT SELECT
dp.id AS datapoint_id, dp.id AS datapoint_id,
dpv.id,
dpv.value_int, dpv.value_int,
dpv.value_string, dpv.value_string,
dpv.value_datetime dpv.value_datetime
@ -39,6 +42,9 @@ $$
BEGIN BEGIN
UPDATE public.datapoint UPDATE public.datapoint
SET SET
nodata_is_problem = false,
last_value = NEW.ts,
last_value_id = NEW.id,
last_value_int = NEW.value_int, last_value_int = NEW.value_int,
last_value_string = NEW.value_string, last_value_string = NEW.value_string,
last_value_datetime = NEW.value_datetime last_value_datetime = NEW.value_datetime