Use eur if not present
This commit is contained in:
+16
-2
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)})))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user