Default forecast timeline selection to closest hour to now

This commit is contained in:
Graham McIntire 2026-03-31 17:45:54 -05:00
parent 2b1fd6129d
commit 6dff6372ab
No known key found for this signature in database
GPG key ID: F4ABF488E6029E59

View file

@ -20,8 +20,7 @@ defmodule MicrowavepropWeb.MapLive do
def mount(_params, _session, socket) do
bands = BandConfig.all_bands()
valid_times = Propagation.available_valid_times(@default_band)
# Default to the earliest (current analysis) hour
selected_time = List.first(valid_times)
selected_time = closest_to_now(valid_times)
initial_scores = Propagation.scores_at(@default_band, selected_time, @initial_bounds)
if connected?(socket) do
@ -46,7 +45,7 @@ defmodule MicrowavepropWeb.MapLive do
def handle_event("select_band", %{"value" => band}, socket) do
band = if is_binary(band), do: String.to_integer(band), else: band
valid_times = Propagation.available_valid_times(band)
selected_time = List.first(valid_times)
selected_time = closest_to_now(valid_times)
scores = Propagation.scores_at(band, selected_time, socket.assigns.bounds)
socket =
@ -158,7 +157,7 @@ defmodule MicrowavepropWeb.MapLive do
if socket.assigns.selected_time in valid_times do
socket.assigns.selected_time
else
List.first(valid_times)
closest_to_now(valid_times)
end
scores = Propagation.scores_at(band, selected_time, socket.assigns.bounds)
@ -209,6 +208,13 @@ defmodule MicrowavepropWeb.MapLive do
Calendar.strftime(dt, "%H:%M")
end
defp closest_to_now([]), do: nil
defp closest_to_now(times) do
now = DateTime.utc_now()
Enum.min_by(times, fn t -> abs(DateTime.diff(t, now)) end)
end
defp score_range_km(score, config) do
cond do
score >= 80 -> config.exceptional_range_km