Compare commits
No commits in common. "6909b223a781dfce246f9c1309c01a18640856a3" and "a8bdeae3a9491c4fa17a1a40ba63ef4a95c4d228" have entirely different histories.
6909b223a7
...
a8bdeae3a9
37
datapoint.go
37
datapoint.go
@ -162,6 +162,8 @@ func DatapointAdd[T any](name string, value T) (err error) { // {{{
|
||||
return
|
||||
}
|
||||
|
||||
service.Db.Conn.Exec(`UPDATE datapoint SET last_value = NOW(), nodata_is_problem = false WHERE id=$1`, dpID)
|
||||
|
||||
return
|
||||
} // }}}
|
||||
|
||||
@ -170,19 +172,30 @@ func DatapointsRetrieve() (dps []Datapoint, err error) { // {{{
|
||||
var rows *sqlx.Rows
|
||||
rows, err = service.Db.Conn.Queryx(`
|
||||
SELECT
|
||||
id, name, datatype, last_value, "group", comment, nodata_problem_seconds,
|
||||
last_value_id AS v_id,
|
||||
CASE
|
||||
WHEN last_value_id IS NULL THEN null
|
||||
ELSE last_value
|
||||
END AS ts,
|
||||
last_value_int AS value_int,
|
||||
last_value_string AS value_string,
|
||||
last_value_datetime AS value_datetime
|
||||
FROM datapoint
|
||||
dp.id,
|
||||
dp.name,
|
||||
dp.datatype,
|
||||
dp.last_value,
|
||||
dp.group,
|
||||
dp.comment,
|
||||
dp.nodata_problem_seconds,
|
||||
|
||||
dpv.id AS v_id,
|
||||
dpv.ts,
|
||||
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
|
||||
"group" ASC,
|
||||
name ASC
|
||||
dp.group ASC,
|
||||
dp.name ASC
|
||||
`)
|
||||
if err != nil {
|
||||
err = werr.Wrap(err)
|
||||
|
2
main.go
2
main.go
@ -29,7 +29,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const VERSION = "v38"
|
||||
const VERSION = "v37"
|
||||
|
||||
var (
|
||||
logger *slog.Logger
|
||||
|
@ -1,62 +0,0 @@
|
||||
/* 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_string varchar NULL;
|
||||
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. */
|
||||
UPDATE public.datapoint AS dp
|
||||
SET
|
||||
last_value_id = dpv.id,
|
||||
last_value_int = dpv.value_int,
|
||||
last_value_string = dpv.value_string,
|
||||
last_value_datetime = dpv.value_datetime
|
||||
FROM (
|
||||
SELECT
|
||||
dp.id AS datapoint_id,
|
||||
dpv.id,
|
||||
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
|
||||
) AS dpv
|
||||
WHERE
|
||||
dpv.datapoint_id = dp.id;
|
||||
|
||||
|
||||
|
||||
/* A trigger keeps the value current without bugs introduced in software missing the entry. */
|
||||
CREATE OR REPLACE FUNCTION datapoint_entry()
|
||||
RETURNS TRIGGER
|
||||
LANGUAGE PLPGSQL
|
||||
AS
|
||||
$$
|
||||
BEGIN
|
||||
UPDATE public.datapoint
|
||||
SET
|
||||
nodata_is_problem = false,
|
||||
last_value = NEW.ts,
|
||||
last_value_id = NEW.id,
|
||||
last_value_int = NEW.value_int,
|
||||
last_value_string = NEW.value_string,
|
||||
last_value_datetime = NEW.value_datetime
|
||||
WHERE
|
||||
id = NEW.datapoint_id;
|
||||
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
|
||||
CREATE TRIGGER datapoint_entry
|
||||
AFTER INSERT
|
||||
ON public.datapoint_value
|
||||
FOR EACH ROW
|
||||
EXECUTE PROCEDURE datapoint_entry();
|
Loading…
Reference in New Issue
Block a user