Use eur if not present

This commit is contained in:
2026-05-16 11:33:16 +02:00
parent 1958ac9c5a
commit e088d67308
3 changed files with 38 additions and 7 deletions
+16 -2
View File
@@ -1,8 +1,10 @@
-- :name insert-price! :! :n -- :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) 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) 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 :? :* -- :name get-prices-for-date :? :*
-- :doc Get all prices for a given date (time_dk local date) and price area -- :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 FROM consumption
WHERE time_start = :time-start; 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 -- :name insert-production! :! :n
-- :doc Insert a production record, skip if already exists -- :doc Insert a production record, skip if already exists
INSERT INTO production (time_start, hour, kwh) INSERT INTO production (time_start, hour, kwh)
@@ -286,15 +286,22 @@
[query-fn ^LocalDate date] [query-fn ^LocalDate date]
(let [yesterday (.minusDays (LocalDate/now dk-zone) 1) (let [yesterday (.minusDays (LocalDate/now dk-zone) 1)
time-start (utc-start-for-date date) time-start (utc-start-for-date date)
cnt (:cnt (query-fn :count-consumption-for-date from-dk (Timestamp/valueOf (.atStartOfDay date))
{:time-start time-start}))] 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 (cond
(not (.isBefore date yesterday)) (not (.isBefore date yesterday))
(do (log/info "No data to fetch for" (.format date date-fmt) "(too recent)") (do (log/info "No data to fetch for" (.format date date-fmt) "(too recent)")
{:no-data true :date (.format date date-fmt)}) {:no-data true :date (.format date date-fmt)})
(and cnt (pos? cnt)) (and (some-> cons-cnt pos?) (some-> prod-cnt pos?) (some-> read-cnt pos?))
(do (log/info "Data already exists for" (.format date date-fmt)) (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)}) {:exists true :date (.format date date-fmt)})
:else :else
(do (do
@@ -76,6 +76,16 @@
(.replace ^String s "T" " ")))) (.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! (defn save-records!
"Insert price records into the database, skipping existing ones." "Insert price records into the database, skipping existing ones."
[query-fn records] [query-fn records]
@@ -84,7 +94,7 @@
{:time-utc (parse-timestamp (:TimeUTC rec)) {:time-utc (parse-timestamp (:TimeUTC rec))
:time-dk (parse-timestamp (:TimeDK rec)) :time-dk (parse-timestamp (:TimeDK rec))
:price-area (:PriceArea rec) :price-area (:PriceArea rec)
:price-dkk (:DayAheadPriceDKK rec) :price-dkk (dkk-price (:DayAheadPriceDKK rec) (:DayAheadPriceEUR rec))
:price-eur (:DayAheadPriceEUR rec)}))) :price-eur (:DayAheadPriceEUR rec)})))