Add tariff_dkk + total_dkk to elprice
Each hourly row now carries the N1 time-of-use tariff (øre/kWh × 10 to match the DKK/MWh unit of price_dkk) and the combined consumer price (spot + tariff). Existing rows are backfilled in the migration with a CASE expression mirroring tariffs/tariff-for-hour; new writes compute it in the controller. API response exposes both fields. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
(ns pmagnus.elprice.web.controllers.elprice
|
||||
(:require
|
||||
[clojure.data.json :as json]
|
||||
[clojure.tools.logging :as log])
|
||||
[clojure.tools.logging :as log]
|
||||
[pmagnus.elprice.web.controllers.tariffs :as tariffs])
|
||||
(:import
|
||||
(java.net
|
||||
URI
|
||||
@@ -14,6 +15,7 @@
|
||||
Timestamp)
|
||||
(java.time
|
||||
LocalDate
|
||||
LocalDateTime
|
||||
ZoneId)
|
||||
(java.time.format
|
||||
DateTimeFormatter)))
|
||||
@@ -68,14 +70,25 @@
|
||||
(:records (fetch-json url))))
|
||||
|
||||
|
||||
(defn- tariff-dkk-mwh
|
||||
"N1 time-of-use tariff converted from øre/kWh to DKK/MWh (×10)."
|
||||
[^LocalDateTime dt]
|
||||
(* 10.0 (tariffs/tariff-for-hour (.getMonthValue dt) (.getHour dt))))
|
||||
|
||||
|
||||
(defn- save-records!
|
||||
[query-fn records]
|
||||
(doseq [rec records]
|
||||
(when-let [dkk (dkk-price (:DayAheadPriceDKK rec) (:DayAheadPriceEUR rec))]
|
||||
(query-fn :insert-elprice!
|
||||
{:time-dk (parse-timestamp (:TimeDK rec))
|
||||
:price-area (:PriceArea rec)
|
||||
:price-dkk dkk}))))
|
||||
(let [ts (parse-timestamp (:TimeDK rec))
|
||||
tariff (tariff-dkk-mwh (.toLocalDateTime ^Timestamp ts))
|
||||
total (+ (double dkk) tariff)]
|
||||
(query-fn :insert-elprice!
|
||||
{:time-dk ts
|
||||
:price-area (:PriceArea rec)
|
||||
:price-dkk dkk
|
||||
:tariff-dkk tariff
|
||||
:total-dkk total})))))
|
||||
|
||||
|
||||
(defn get-or-fetch!
|
||||
|
||||
@@ -49,7 +49,9 @@
|
||||
{:date date
|
||||
:prices (mapv (fn [r] {:time_dk (str (:time_dk r))
|
||||
:price_area (:price_area r)
|
||||
:price_dkk (:price_dkk r)})
|
||||
:price_dkk (:price_dkk r)
|
||||
:tariff_dkk (:tariff_dkk r)
|
||||
:total_dkk (:total_dkk r)})
|
||||
rows)}))
|
||||
(catch DateTimeParseException _
|
||||
(http-response/bad-request {:error "date must be YYYY-MM-DD"}))))
|
||||
|
||||
Reference in New Issue
Block a user