From a8920e9480dcbfdc76d5b4c2dd37300cf0690bf7 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Thu, 2 Apr 2026 16:31:34 -0500 Subject: [PATCH] Fix position computation, grid truncation, and status resets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Truncate odd-length grids to nearest valid even length (FN03c → FN03) - Handle lng/lon key inconsistency in haversine distance calculation - Reset unavailable statuses to pending when positions are computed - Rename backfill queue column to "Pending" --- lib/microwaveprop/radio.ex | 42 +++++++++++++++++++-- lib/microwaveprop_web/live/backfill_live.ex | 2 +- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/lib/microwaveprop/radio.ex b/lib/microwaveprop/radio.ex index 84f61b32..eb0199f5 100644 --- a/lib/microwaveprop/radio.ex +++ b/lib/microwaveprop/radio.ex @@ -228,8 +228,11 @@ defmodule Microwaveprop.Radio do distance = if pos1 && pos2 do + lon1 = pos1["lon"] || pos1["lng"] + lon2 = pos2["lon"] || pos2["lng"] + pos1["lat"] - |> haversine_km(pos1["lon"], pos2["lat"], pos2["lon"]) + |> haversine_km(lon1, pos2["lat"], lon2) |> round() |> Decimal.new() else @@ -245,6 +248,27 @@ defmodule Microwaveprop.Radio do if changes == %{} do contact else + # Reset unavailable statuses to pending now that positions exist + new_pos1 = Map.get(changes, :pos1) + new_pos2 = Map.get(changes, :pos2) + + changes = + if new_pos1 do + changes + |> maybe_reset_status(:hrrr_status, contact.hrrr_status) + |> maybe_reset_status(:weather_status, contact.weather_status) + |> maybe_reset_status(:iemre_status, contact.iemre_status) + else + changes + end + + changes = + if new_pos1 && (new_pos2 || contact.pos2) do + maybe_reset_status(changes, :terrain_status, contact.terrain_status) + else + changes + end + contact |> Ecto.Changeset.change(changes) |> Repo.update!() @@ -254,12 +278,24 @@ defmodule Microwaveprop.Radio do end end + defp maybe_reset_status(changes, field, :unavailable), do: Map.put(changes, field, :pending) + defp maybe_reset_status(changes, _field, _status), do: changes + defp latlon_from_grid(nil), do: nil defp latlon_from_grid(grid) do case Maidenhead.to_latlon(grid) do - {:ok, {lat, lon}} -> %{"lat" => lat, "lon" => lon} - _ -> nil + {:ok, {lat, lon}} -> + %{"lat" => lat, "lon" => lon} + + _ -> + # Try truncating to nearest valid even length (e.g. "FN03c" → "FN03") + truncated = String.slice(grid, 0, div(String.length(grid), 2) * 2) + + case Maidenhead.to_latlon(truncated) do + {:ok, {lat, lon}} -> %{"lat" => lat, "lon" => lon} + _ -> nil + end end end diff --git a/lib/microwaveprop_web/live/backfill_live.ex b/lib/microwaveprop_web/live/backfill_live.ex index 022123d9..6bc3004d 100644 --- a/lib/microwaveprop_web/live/backfill_live.ex +++ b/lib/microwaveprop_web/live/backfill_live.ex @@ -343,7 +343,7 @@ defmodule MicrowavepropWeb.BackfillLive do Worker Executing - Available + Pending Retryable Total