diff --git a/lib/microwaveprop/propagation/path_compute.ex b/lib/microwaveprop/propagation/path_compute.ex index c8b1e2b1..b62865f5 100644 --- a/lib/microwaveprop/propagation/path_compute.ex +++ b/lib/microwaveprop/propagation/path_compute.ex @@ -63,14 +63,45 @@ defmodule Microwaveprop.Propagation.PathCompute do sounding: map() | nil } + @type progress_fn :: (pos_integer(), pos_integer(), String.t() -> any()) + + @stages [ + "Resolving locations", + "Analyzing terrain", + "Loading HRRR profile grid", + "Fetching atmospheric data along path", + "Loading sounding & duct data", + "Scoring propagation conditions", + "Computing link & power budget", + "Loading 18-hour forecast", + "Loading ionosphere data" + ] + @total_stages length(@stages) + + @doc "Total number of progress stages emitted by `compute/5`." + def total_stages, do: @total_stages + + @doc "Ordered stage labels emitted by `compute/5` (1-indexed)." + def stage_labels, do: @stages + @doc """ Run the full path-calculator pipeline. Returns `{:ok, result}` on success, `{:error, reason}` when either endpoint fails to resolve (callsign / grid / coords). + + Options: + * `:on_progress` — `(step, total, label)` callback invoked at the + start of each stage. Defaults to a no-op. Use it to push live + progress updates to a `Phoenix.LiveView`. """ - @spec compute(String.t(), String.t(), integer(), station_params()) :: + @spec compute(String.t(), String.t(), integer(), station_params(), keyword()) :: {:ok, result()} | {:error, term()} - def compute(source, dest, band_mhz, station_params) do + def compute(source, dest, band_mhz, station_params, opts \\ []) do + on_progress = Keyword.get(opts, :on_progress, fn _step, _total, _label -> :ok end) + report = fn step -> on_progress.(step, @total_stages, Enum.at(@stages, step - 1)) end + + report.(1) + with {:ok, src} <- resolve_location(source), {:ok, dst} <- resolve_location(dest) do band_config = BandConfig.get(band_mhz) || BandConfig.get(10_000) @@ -79,22 +110,27 @@ defmodule Microwaveprop.Propagation.PathCompute do dist_km = haversine_km(src.lat, src.lon, dst.lat, dst.lon) bearing = bearing_deg(src.lat, src.lon, dst.lat, dst.lon) + report.(2) terrain_result = compute_terrain(src, dst, dist_km, freq_ghz, station_params) now = DateTime.utc_now() midlat = (src.lat + dst.lat) / 2 midlon = (src.lon + dst.lon) / 2 + report.(3) profile_valid_time = latest_profile_valid_time(now) profile_grid = profile_grid_for(profile_valid_time) sample_points = path_sample_points(src, dst, 9) {grid_hits, misses} = partition_grid_hits(sample_points, profile_grid, profile_valid_time) + + report.(4) fallback_hits = fallback_hits(misses, now) hrrr_points = Enum.reverse(grid_hits, fallback_hits) hrrr_profiles = Enum.map(hrrr_points, & &1.profile) + report.(5) sounding = build_sounding_readout(midlat, midlon, now) native_duct = @@ -103,14 +139,19 @@ defmodule Microwaveprop.Propagation.PathCompute do {:error, :not_found} -> %{best_duct_band_ghz: nil, bulk_richardson: nil} end + report.(6) + {conditions, scoring} = build_scoring(hrrr_profiles, src, dst, now, band_config, native_duct) + report.(7) loss_budget = compute_loss_budget(dist_km, freq_ghz, band_config, terrain_result, conditions) power_budget = compute_power_budget(station_params, loss_budget) + report.(8) forecast = Propagation.point_forecast(band_mhz, midlat, midlon) + report.(9) ionosphere = build_ionosphere_readout(band_mhz, midlat, midlon, dist_km) {:ok, diff --git a/lib/microwaveprop_web/live/path_live.ex b/lib/microwaveprop_web/live/path_live.ex index a2b91aa2..4f5a8e76 100644 --- a/lib/microwaveprop_web/live/path_live.ex +++ b/lib/microwaveprop_web/live/path_live.ex @@ -45,6 +45,9 @@ defmodule MicrowavepropWeb.PathLive do result: nil, error: nil, computing: false, + compute_step: 0, + compute_total: PathCompute.total_stages(), + compute_label: nil, source: "", destination: "", band: "10000", @@ -224,13 +227,45 @@ defmodule MicrowavepropWeb.PathLive do dst_gain_dbi: parse_float(p["dst_gain_dbi"], 30.0) } - send(self(), {:compute_path, p["source"], p["destination"], parse_int(p["band"], 10_000), station_params}) - {:noreply, assign(socket, computing: true)} + {:noreply, + start_compute_async( + socket, + p["source"], + p["destination"], + parse_int(p["band"], 10_000), + station_params + )} else {:noreply, socket} end end + # Kicks off the path compute in a Task so the LV can keep rendering + # progress messages while it runs. The Task captures the LV pid and + # sends `{:compute_progress, step, total, label}` messages from + # `PathCompute`'s `:on_progress` callback as each stage starts; + # `handle_async(:compute_path, ...)` picks up the final result. + defp start_compute_async(socket, source, dest, band_mhz, station_params) do + lv = self() + + on_progress = fn step, total, label -> + send(lv, {:compute_progress, step, total, label}) + end + + socket + |> assign( + computing: true, + error: nil, + result: nil, + compute_step: 0, + compute_total: PathCompute.total_stages(), + compute_label: "Starting…" + ) + |> start_async(:compute_path, fn -> + PathCompute.compute(source, dest, band_mhz, station_params, on_progress: on_progress) + end) + end + @impl true def handle_event("update_form", params, socket) do {:noreply, @@ -265,7 +300,8 @@ defmodule MicrowavepropWeb.PathLive do } socket = - assign(socket, + socket + |> assign( source: params["source"], destination: params["destination"], band: params["band"], @@ -273,16 +309,14 @@ defmodule MicrowavepropWeb.PathLive do dst_height_ft: params["dst_height_ft"], tx_power_dbm: params["tx_power_dbm"], src_gain_dbi: params["src_gain_dbi"], - dst_gain_dbi: params["dst_gain_dbi"], - error: nil, - computing: true, - result: nil + dst_gain_dbi: params["dst_gain_dbi"] + ) + |> start_compute_async( + params["source"], + params["destination"], + parse_int(params["band"], 10_000), + station_params ) - - send( - self(), - {:compute_path, params["source"], params["destination"], parse_int(params["band"], 10_000), station_params} - ) url_params = params @@ -351,13 +385,12 @@ defmodule MicrowavepropWeb.PathLive do end @impl true - def handle_info({:compute_path, source, dest, band_mhz, station_params}, socket) do - case compute_path(source, dest, band_mhz, station_params) do - {:ok, result} -> - {:noreply, assign(socket, result: result, computing: false)} - - {:error, reason} -> - {:noreply, assign(socket, error: reason, computing: false)} + def handle_info({:compute_progress, step, total, label}, socket) do + # Drop stale progress messages from any task we no longer care about. + if socket.assigns.computing do + {:noreply, assign(socket, compute_step: step, compute_total: total, compute_label: label)} + else + {:noreply, socket} end end @@ -374,8 +407,38 @@ defmodule MicrowavepropWeb.PathLive do end end - defp compute_path(source, dest, band_mhz, station_params) do - PathCompute.compute(source, dest, band_mhz, station_params) + @impl true + def handle_async(:compute_path, {:ok, {:ok, result}}, socket) do + {:noreply, + assign(socket, + result: result, + computing: false, + compute_label: nil, + compute_step: 0, + error: nil + )} + end + + def handle_async(:compute_path, {:ok, {:error, reason}}, socket) do + {:noreply, + assign(socket, + error: reason, + computing: false, + compute_label: nil, + compute_step: 0 + )} + end + + def handle_async(:compute_path, {:exit, reason}, socket) do + Logger.error("PathLive compute task crashed: #{inspect(reason)}") + + {:noreply, + assign(socket, + error: "Internal error computing path. Please try again.", + computing: false, + compute_label: nil, + compute_step: 0 + )} end # ── Score tier helpers ── @@ -524,14 +587,28 @@ defmodule MicrowavepropWeb.PathLive do /> -