prop/lib/microwaveprop/workers/station_elevation_worker.ex
Graham McInitre 93b8f881e2 perf: batch fixes for site-wide audit — indexes, N+1, streams, atomicity
- Add index on weather_stations(station_type, lat, lon) for nearby_stations
- Add UPPER(station1/2) expression indexes for callsign search
- Batch sync_network ~3K individual Repo.insert calls into one insert_all
- Batch Oban.insert calls in insert_unique into Oban.insert_all
- Log warnings on station elevation Repo.update errors instead of discarding
- Wrap reconcile_mission_paths delete+insert in Repo.transaction
- BeaconLive.Index: targeted stream_insert/delete instead of full re-query+push_patch
- RoverPlanningLive.Show: re-fetch single path instead of re-querying all
- PskrSpotsLive: convert to LiveView streams, add 60s auto-refresh
- ImportConfetti: add missing phx-update=ignore
2026-07-16 07:31:19 -05:00

50 lines
1.4 KiB
Elixir

defmodule Microwaveprop.Workers.StationElevationWorker do
@moduledoc """
Oban worker that enriches a `FixedStation` row with its SRTM
elevation. Enqueued from `Microwaveprop.Rover.create_station/2`
whenever the user-supplied attrs leave `elevation_m` nil.
A missing tile or a void value is non-fatal — the row simply stays
with `elevation_m = nil`. The worker also no-ops if the station has
been deleted between enqueue and execution.
"""
use Oban.Pro.Worker, queue: :terrain, max_attempts: 3
alias Microwaveprop.Repo
alias Microwaveprop.Rover.FixedStation
alias Microwaveprop.Terrain.Srtm
require Logger
@impl Oban.Pro.Worker
def process(%Oban.Job{args: %{"id" => id}}) do
case Repo.get(FixedStation, id) do
nil ->
:ok
station ->
update_elevation(station)
end
end
defp update_elevation(station) do
tiles_dir = Application.get_env(:microwaveprop, :srtm_tiles_dir, "/data/srtm")
case Srtm.lookup(station.lat, station.lon, tiles_dir) do
{:ok, elev} ->
station
|> Ecto.Changeset.change(elevation_m: elev)
|> Repo.update()
|> case do
{:ok, _station} ->
:ok
{:error, _changeset} ->
Logger.warning("StationElevationWorker: failed to update elevation for station #{station.id}")
end
{:error, _} ->
:ok
end
end
end