create window for abg

This commit is contained in:
Ryan Voots 2025-05-11 13:50:39 -04:00
parent dfd2238dfa
commit 96a36317bb

View file

@ -1,51 +1,64 @@
WITH range_ts AS ( WITH range_ts AS (
SELECT DATE_TRUNC('minute', ts) as ts SELECT date_trunc('minute'::text, ts.ts) AS ts
FROM generate_series($__timeFrom(), $__timeTo(), '1 minute'::interval) AS ts FROM generate_series(( SELECT min("glucose-import"."time") AS min
), coallated AS FROM "glucose-import"), ( SELECT max("glucose-import"."time") AS max
FROM "glucose-import"), '00:01:00'::interval) ts(ts)
(SELECT ts, ), coallated AS (
gi_prev.time as stime, SELECT rts.ts,
gi_next.time as etime, gi_prev."time" AS stime,
gi_prev.value as svalue, gi_next."time" AS etime,
gi_next.value as evalue, gi_prev.value AS svalue,
EXTRACT(EPOCH FROM ts-gi_prev.time)/EXTRACT(EPOCH FROM gi_next.time-gi_prev.time) * gi_prev.value + gi_next.value AS evalue,
EXTRACT(EPOCH FROM gi_next.time-ts)/EXTRACT(EPOCH FROM gi_next.time-gi_prev.time) * gi_next.value AS samp_value (EXTRACT(epoch FROM rts.ts - gi_prev."time") / EXTRACT(epoch FROM gi_next."time" - gi_prev."time"))::double precision * gi_prev.value + (EXTRACT(epoch FROM gi_next."time" - rts.ts) / EXTRACT(epoch FROM gi_next."time" - gi_prev."time"))::double precision * gi_next.value AS samp_value
FROM range_ts rts FROM range_ts rts
LEFT JOIN LATERAL ( LEFT JOIN LATERAL ( SELECT "glucose-import".id,
SELECT * "glucose-import".serialnum,
FROM "glucose-import" "glucose-import".devicetype,
WHERE "time" <= rts.ts "glucose-import".units,
ORDER BY "time" DESC "glucose-import".value,
LIMIT 1 "glucose-import"."time"
) AS gi_prev ON true FROM "glucose-import"
LEFT JOIN LATERAL ( WHERE "glucose-import"."time" <= rts.ts
SELECT * ORDER BY "glucose-import"."time" DESC
FROM "glucose-import" LIMIT 1) gi_prev ON true
WHERE "time" >= rts.ts LEFT JOIN LATERAL ( SELECT "glucose-import".id,
ORDER BY "time" ASC "glucose-import".serialnum,
LIMIT 1 "glucose-import".devicetype,
) AS gi_next ON true "glucose-import".units,
WHERE gi_prev.time IS NOT NULL and gi_next.time IS NOT NULL AND gi_next.time <> gi_prev.time "glucose-import".value,
), collected AS ( "glucose-import"."time"
SELECT ts as time, FROM "glucose-import"
ema(samp_value ::real, 0.33) over (order by ts asc) as smoothval, WHERE "glucose-import"."time" >= rts.ts
samp_value as absvalue ORDER BY "glucose-import"."time"
FROM coallated), firstorder AS ( LIMIT 1) gi_next ON true
SELECT time, smoothval, absvalue, WHERE gi_prev."time" IS NOT NULL AND gi_next."time" IS NOT NULL AND gi_next."time" <> gi_prev."time"
smoothval - LAG (smoothval, 1) OVER (ORDER BY time ASC) as delta1st ), collected AS (
FROM collected), secondorder AS ( SELECT coallated.ts AS "time",
SELECT ema(coallated.samp_value::real, 0.33::real) OVER (ORDER BY coallated.ts) AS smoothval,
time, coallated.samp_value AS absvalue
smoothval, FROM coallated
absvalue, ), firstorder AS (
delta1st, SELECT collected."time",
delta1st - LAG(delta1st, 1) OVER (ORDER BY time ASC) as delta2nd collected.smoothval,
FROM firstorder) collected.absvalue,
SELECT *, collected.smoothval - lag(collected.smoothval, 1) OVER (ORDER BY collected."time") AS delta1st
ema(delta1st, 0.25) OVER (ORDER BY time ASC) as smoothdelta1st, FROM collected
ema(delta2nd, 0.25) OVER (ORDER BY time ASC) as smoothdelta2nd, ), secondorder AS (
STDDEV(delta1st) OVER ( SELECT firstorder."time",
ORDER BY time firstorder.smoothval,
RANGE BETWEEN INTERVAL '6 hours' PRECEDING AND CURRENT ROW firstorder.absvalue,
) AS std_dev_delta1st firstorder.delta1st,
FROM secondorder; firstorder.delta1st - lag(firstorder.delta1st, 1) OVER (ORDER BY firstorder."time") AS delta2nd
FROM firstorder
)
SELECT "time",
smoothval,
absvalue,
delta1st,
delta2nd,
ema(delta1st, 0.25::real) OVER (ORDER BY "time") AS smoothdelta1st,
ema(delta2nd, 0.25::real) OVER (ORDER BY "time") AS smoothdelta2nd,
stddev(delta1st) OVER (ORDER BY "time" RANGE BETWEEN '02:00:00'::interval PRECEDING AND CURRENT ROW) AS std_dev_delta1st,
avg(absvalue) OVER (ORDER BY "time" RANGE BETWEEN '3 mons'::interval PRECEDING AND CURRENT ROW) AS "3mo_eabg",
avg(absvalue) OVER (ORDER BY "time" RANGE BETWEEN '7 days'::interval PRECEDING AND CURRENT ROW) AS week_eabg
FROM secondorder;