kalibrace per pole
Some checks failed
CI and deploy / migration-check (push) Failing after 9s
CI and deploy / deploy (push) Has been skipped

This commit is contained in:
Dusan Vojacek
2026-04-22 22:17:28 +02:00
parent 3cd8e44d37
commit 568b584748
12 changed files with 705 additions and 267 deletions

View File

@@ -44,7 +44,28 @@ declare
begin
drop table if exists _ems_plan_slot_wk;
create temp table _ems_plan_slot_wk on commit drop as
with slot_spine as (
with prof as (
select ems.fn_pv_forecast_delta_profile(
p_site_id,
greatest(p_from, now() - interval '120 days'),
now()
) as j
),
delta_unnest as (
select (kv.key)::int as pv_array_id,
(x->>'slot_of_day')::int as slot_of_day,
(x->>'delta_w')::int as delta_w
from prof
cross join lateral jsonb_each((prof.j)->'deltas_by_array') kv(key, value)
cross join lateral jsonb_array_elements(kv.value->'deltas') x
),
legacy_slot_delta as (
select (x->>'slot_of_day')::int as slot_of_day,
(x->>'delta_w')::int as delta_w
from prof
cross join lateral jsonb_array_elements((prof.j)->'deltas') x
),
slot_spine as (
select gs as interval_start
from generate_series(
p_from,
@@ -108,9 +129,9 @@ begin
left join ems.vw_site_effective_price ep
on ep.site_id = p_site_id and ep.interval_start = s.interval_start
left join lateral (
select coalesce(sum(u.power_w), 0)::int as power_w
from (
with uq as (
select distinct on (apa.id)
apa.id as pv_array_id,
fpi.power_w
from ems.asset_pv_array apa
join ems.forecast_pv_run fpr
@@ -124,12 +145,42 @@ begin
where apa.site_id = p_site_id
and apa.controllable is true
order by apa.id, fpr.created_at desc
) u
),
slot_of as (
select (
(extract(hour from (s.interval_start at time zone 'Europe/Prague'))::int * 60)
+ extract(minute from (s.interval_start at time zone 'Europe/Prague'))::int
) / 15 as slot_of_day
),
tot as (select coalesce(sum(uq.power_w), 0)::numeric as w from uq)
select coalesce(sum(
greatest(
0,
uq.power_w - coalesce(
du.delta_w,
case
when exists (select 1 from delta_unnest limit 1) then null
else round(
ld.delta_w::numeric * uq.power_w::numeric / nullif((select w from tot), 0)
)::int
end,
0
)
)
), 0)::int as power_w
from uq
cross join slot_of
cross join tot
left join delta_unnest du
on du.pv_array_id = uq.pv_array_id
and du.slot_of_day = slot_of.slot_of_day
left join legacy_slot_delta ld
on ld.slot_of_day = slot_of.slot_of_day
) fpi_a on true
left join lateral (
select coalesce(sum(u.power_w), 0)::int as power_w
from (
with uq as (
select distinct on (apa.id)
apa.id as pv_array_id,
fpi.power_w
from ems.asset_pv_array apa
join ems.forecast_pv_run fpr
@@ -143,7 +194,37 @@ begin
where apa.site_id = p_site_id
and apa.controllable is false
order by apa.id, fpr.created_at desc
) u
),
slot_of as (
select (
(extract(hour from (s.interval_start at time zone 'Europe/Prague'))::int * 60)
+ extract(minute from (s.interval_start at time zone 'Europe/Prague'))::int
) / 15 as slot_of_day
),
tot as (select coalesce(sum(uq.power_w), 0)::numeric as w from uq)
select coalesce(sum(
greatest(
0,
uq.power_w - coalesce(
du.delta_w,
case
when exists (select 1 from delta_unnest limit 1) then null
else round(
ld.delta_w::numeric * uq.power_w::numeric / nullif((select w from tot), 0)
)::int
end,
0
)
)
), 0)::int as power_w
from uq
cross join slot_of
cross join tot
left join delta_unnest du
on du.pv_array_id = uq.pv_array_id
and du.slot_of_day = slot_of.slot_of_day
left join legacy_slot_delta ld
on ld.slot_of_day = slot_of.slot_of_day
) fpi_b on true
left join lateral (
select t.status