From e088d673086805251d09927b2494991094760d08 Mon Sep 17 00:00:00 2001 From: Per Magnus Petersen Date: Sat, 16 May 2026 11:33:16 +0200 Subject: [PATCH] Use eur if not present --- resources/queries.sql | 18 ++++++++++++++++-- .../elprice/web/controllers/eloverblik.clj | 15 +++++++++++---- .../pmagnus/elprice/web/controllers/prices.clj | 12 +++++++++++- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/resources/queries.sql b/resources/queries.sql index c87f38f..29a3ef0 100644 --- a/resources/queries.sql +++ b/resources/queries.sql @@ -1,8 +1,10 @@ -- :name insert-price! :! :n --- :doc Insert a day-ahead price record, skip if already exists +-- :doc Insert a day-ahead price record, update DKK if it was null INSERT INTO day_ahead_prices (time_utc, time_dk, price_area, price_dkk, price_eur) VALUES (:time-utc, :time-dk, :price-area, :price-dkk, :price-eur) -ON CONFLICT (time_dk, price_area) DO NOTHING; +ON CONFLICT (time_dk, price_area) DO UPDATE SET + price_dkk = COALESCE(day_ahead_prices.price_dkk, EXCLUDED.price_dkk), + price_eur = COALESCE(day_ahead_prices.price_eur, EXCLUDED.price_eur); -- :name get-prices-for-date :? :* -- :doc Get all prices for a given date (time_dk local date) and price area @@ -54,6 +56,18 @@ SELECT count(*) AS cnt FROM consumption WHERE time_start = :time-start; +-- :name count-production-for-date :? :1 +-- :doc Count production records for a given UTC time_start +SELECT count(*) AS cnt +FROM production +WHERE time_start = :time-start; + +-- :name count-meter-readings-for-date :? :1 +-- :doc Count meter readings for a given UTC time_start (converted to DK date range) +SELECT count(*) AS cnt +FROM meter_readings +WHERE time_dk >= :from-dk AND time_dk < :to-dk; + -- :name insert-production! :! :n -- :doc Insert a production record, skip if already exists INSERT INTO production (time_start, hour, kwh) diff --git a/src/clj/pmagnus/elprice/web/controllers/eloverblik.clj b/src/clj/pmagnus/elprice/web/controllers/eloverblik.clj index 1940c93..74c5ac9 100644 --- a/src/clj/pmagnus/elprice/web/controllers/eloverblik.clj +++ b/src/clj/pmagnus/elprice/web/controllers/eloverblik.clj @@ -286,15 +286,22 @@ [query-fn ^LocalDate date] (let [yesterday (.minusDays (LocalDate/now dk-zone) 1) time-start (utc-start-for-date date) - cnt (:cnt (query-fn :count-consumption-for-date - {:time-start time-start}))] + from-dk (Timestamp/valueOf (.atStartOfDay date)) + to-dk (Timestamp/valueOf (.atStartOfDay (.plusDays date 1))) + cons-cnt (:cnt (query-fn :count-consumption-for-date + {:time-start time-start})) + prod-cnt (:cnt (query-fn :count-production-for-date + {:time-start time-start})) + read-cnt (:cnt (query-fn :count-meter-readings-for-date + {:from-dk from-dk :to-dk to-dk}))] (cond (not (.isBefore date yesterday)) (do (log/info "No data to fetch for" (.format date date-fmt) "(too recent)") {:no-data true :date (.format date date-fmt)}) - (and cnt (pos? cnt)) - (do (log/info "Data already exists for" (.format date date-fmt)) + (and (some-> cons-cnt pos?) (some-> prod-cnt pos?) (some-> read-cnt pos?)) + (do (log/info "Data already exists for" (.format date date-fmt) + "- consumption:" cons-cnt "production:" prod-cnt "readings:" read-cnt) {:exists true :date (.format date date-fmt)}) :else (do diff --git a/src/clj/pmagnus/elprice/web/controllers/prices.clj b/src/clj/pmagnus/elprice/web/controllers/prices.clj index 72e5b16..d93fecf 100644 --- a/src/clj/pmagnus/elprice/web/controllers/prices.clj +++ b/src/clj/pmagnus/elprice/web/controllers/prices.clj @@ -76,6 +76,16 @@ (.replace ^String s "T" " ")))) +(def ^:private eur-dkk-rate 7.46) + + +(defn- dkk-price + "Return DKK price, converting from EUR if DKK is nil." + [price-dkk price-eur] + (or price-dkk + (when price-eur (* price-eur eur-dkk-rate)))) + + (defn save-records! "Insert price records into the database, skipping existing ones." [query-fn records] @@ -84,7 +94,7 @@ {:time-utc (parse-timestamp (:TimeUTC rec)) :time-dk (parse-timestamp (:TimeDK rec)) :price-area (:PriceArea rec) - :price-dkk (:DayAheadPriceDKK rec) + :price-dkk (dkk-price (:DayAheadPriceDKK rec) (:DayAheadPriceEUR rec)) :price-eur (:DayAheadPriceEUR rec)})))