Split API fetch into separate requests per price area
One request per area (DK1, DK2) avoids hitting the 200-record limit that was truncating tomorrow's data when both areas were fetched together. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -15,11 +15,11 @@
|
|||||||
|
|
||||||
(defn- build-url
|
(defn- build-url
|
||||||
"Build the Energi Data Service API URL for day-ahead prices."
|
"Build the Energi Data Service API URL for day-ahead prices."
|
||||||
[start-date end-date]
|
[start-date end-date price-area]
|
||||||
(str "https://api.energidataservice.dk/dataset/DayAheadPrices"
|
(str "https://api.energidataservice.dk/dataset/DayAheadPrices"
|
||||||
"?start=" start-date
|
"?start=" start-date
|
||||||
"&end=" end-date
|
"&end=" end-date
|
||||||
"&filter=" (URLEncoder/encode (json/write-str {"PriceArea" ["DK1" "DK2"]}) "UTF-8")
|
"&filter=" (URLEncoder/encode (json/write-str {"PriceArea" [price-area]}) "UTF-8")
|
||||||
"&sort=TimeDK%20asc"
|
"&sort=TimeDK%20asc"
|
||||||
"&limit=200"))
|
"&limit=200"))
|
||||||
|
|
||||||
@@ -36,19 +36,23 @@
|
|||||||
(when (= 200 (.statusCode response))
|
(when (= 200 (.statusCode response))
|
||||||
(json/read-str (.body response) :key-fn keyword))))
|
(json/read-str (.body response) :key-fn keyword))))
|
||||||
|
|
||||||
(defn fetch-prices-from-api
|
(defn- fetch-area
|
||||||
"Fetch today's and tomorrow's prices from Energi Data Service."
|
"Fetch today's and tomorrow's prices for a single price area."
|
||||||
[]
|
[start end price-area]
|
||||||
(let [today (LocalDate/now dk-zone)
|
(let [url (build-url start end price-area)]
|
||||||
tomorrow (.plusDays today 1)
|
(log/info "Fetching day-ahead prices for" price-area "from" url)
|
||||||
;; Fetch from start of today to end of tomorrow
|
|
||||||
start (.format today date-fmt)
|
|
||||||
end (.format (.plusDays tomorrow 1) date-fmt)
|
|
||||||
url (build-url start end)]
|
|
||||||
(log/info "Fetching day-ahead prices from" url)
|
|
||||||
(when-let [data (fetch-json url)]
|
(when-let [data (fetch-json url)]
|
||||||
(:records data))))
|
(:records data))))
|
||||||
|
|
||||||
|
(defn fetch-prices-from-api
|
||||||
|
"Fetch today's and tomorrow's prices from Energi Data Service for DK1 and DK2."
|
||||||
|
[]
|
||||||
|
(let [today (LocalDate/now dk-zone)
|
||||||
|
start (.format today date-fmt)
|
||||||
|
end (.format (.plusDays today 2) date-fmt)]
|
||||||
|
(concat (fetch-area start end "DK1")
|
||||||
|
(fetch-area start end "DK2"))))
|
||||||
|
|
||||||
(defn- parse-timestamp
|
(defn- parse-timestamp
|
||||||
"Parse an ISO timestamp string to java.sql.Timestamp."
|
"Parse an ISO timestamp string to java.sql.Timestamp."
|
||||||
[s]
|
[s]
|
||||||
|
|||||||
Reference in New Issue
Block a user