Add hourly price rank (#1 = cheapest) based on total price

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-14 14:35:39 +01:00
co-authored by Claude Opus 4.6
parent 2000154c8a
commit 83d9f22bbd
+19 -3
View File
@@ -54,7 +54,7 @@
(when (seq vals) (when (seq vals)
(/ (reduce + 0.0 (map double vals)) (count vals) 10.0)))) (/ (reduce + 0.0 (map double vals)) (count vals) 10.0))))
(defn- hour-block [hour quarters] (defn- hour-block [hour quarters rank]
(let [sorted (sort-by #(.getMinute (->ldt (:time_dk %))) quarters) (let [sorted (sort-by #(.getMinute (->ldt (:time_dk %))) quarters)
avg (avg-ore quarters) avg (avg-ore quarters)
month (.getMonthValue (->ldt (:time_dk (first quarters)))) month (.getMonthValue (->ldt (:time_dk (first quarters))))
@@ -63,6 +63,7 @@
[:summary {:class "flex justify-between py-2 px-3 cursor-pointer hover:bg-gray-50 [:summary {:class "flex justify-between py-2 px-3 cursor-pointer hover:bg-gray-50
font-medium text-gray-900"} font-medium text-gray-900"}
[:span (format "%02d:00" hour)] [:span (format "%02d:00" hour)]
[:span {:class "text-gray-500 text-sm"} (str "#" rank)]
[:span {:class "font-mono"} [:span {:class "font-mono"}
(if avg (format "%.1f(%.1f)" (+ avg tariff) avg) "-")]] (if avg (format "%.1f(%.1f)" (+ avg tariff) avg) "-")]]
[:div {:class "border-t-2 border-blue-400 px-3 pb-2"} [:div {:class "border-t-2 border-blue-400 px-3 pb-2"}
@@ -71,12 +72,27 @@
[:span (format "%02d:%02d" hour (.getMinute (->ldt (:time_dk q))))] [:span (format "%02d:%02d" hour (.getMinute (->ldt (:time_dk q))))]
[:span {:class "font-mono"} (format-ore (:price_dkk q) tariff)]])]])) [:span {:class "font-mono"} (format-ore (:price_dkk q) tariff)]])]]))
(defn- hour-ranks
"Compute a map of hour -> rank (1 = cheapest) based on total price (avg + tariff)."
[grouped-hours]
(let [totals (for [[hour quarters] grouped-hours]
(let [avg (avg-ore quarters)
month (.getMonthValue (->ldt (:time_dk (first quarters))))
tariff (tariffs/tariff-for-hour month hour)]
[hour (if avg (+ avg tariff) Double/MAX_VALUE)]))]
(->> totals
(sort-by second)
(map-indexed (fn [i [hour _]] [hour (inc i)]))
(into {}))))
(defn- price-table [prices] (defn- price-table [prices]
[:div [:div
(if (seq prices) (if (seq prices)
(let [grouped (group-by-hour prices)
ranks (hour-ranks grouped)]
[:div [:div
(for [[hour quarters] (group-by-hour prices)] (for [[hour quarters] grouped]
(hour-block hour quarters))] (hour-block hour quarters (get ranks hour)))])
[:p {:class "mt-2 text-sm text-gray-400"} "Not available yet"])]) [:p {:class "mt-2 text-sm text-gray-400"} "Not available yet"])])
(def ^:private tab-base (def ^:private tab-base