prop/lib/microwaveprop/workers/space_weather_fetch_worker.ex
Graham McIntire fd976b0cd5
fix: resolve 391 Credo issues across codebase
- Add jump_credo_checks ~> 0.4 with all 20 checks enabled
- Fix all standard Credo issues: 139 @spec (113 done, 26 remain),
  4 refactoring, 3 alias usage, 9 System.cmd env, 5 unsafe_to_atom,
  2 max line length, 9 assert_receive timeout
- Fix 170+ jump_credo_checks warnings:
  - 117 TopLevelAliasImportRequire: move nested alias/import to module top
  - 32 UseObanProWorker: switch to Oban.Pro.Worker
  - 4 DoctestIExExamples: add doctests / create test file
  - ~20 WeakAssertion: strengthen type-check assertions
  - Various ConditionalAssertion, AssertReceiveTimeout fixes
- Exclude vendor/ from Credo analysis
- Remaining: 175 warnings (mostly opinionated WeakAssertion,
  AvoidSocketAssignsInTest), 26 @spec annotations
2026-06-12 13:51:32 -05:00

43 lines
1.5 KiB
Elixir

defmodule Microwaveprop.Workers.SpaceWeatherFetchWorker do
@moduledoc """
Polls NOAA SWPC's free public JSON services for the three space-weather
products we use for HF / Es scoring:
- Planetary K-index (1-min cadence) — geomagnetic disturbance level
- 10.7 cm solar flux (hourly) — SSN proxy for HF MUF prediction
- GOES X-ray flux (1-min, 0.1-0.8nm band) — short-wave fade from flares
Runs on a 5-minute cron. SWPC publishes Kp and X-ray at 1-min cadence;
5 minutes strikes a balance between freshness and request volume. Each
fetch is independent: a failure in one product doesn't block the others.
"""
use Oban.Pro.Worker,
queue: :space_weather,
max_attempts: 3,
unique: [period: 120, states: :incomplete]
alias Microwaveprop.SpaceWeather
alias Microwaveprop.SpaceWeather.SwpcClient
require Logger
@impl Oban.Worker
def perform(%Oban.Job{}) do
fetch_and_upsert(:kp, &SwpcClient.fetch_kp/0, &SpaceWeather.upsert_kp/1)
fetch_and_upsert(:f107, &SwpcClient.fetch_f107/0, &SpaceWeather.upsert_solar_flux/1)
fetch_and_upsert(:xrays, &SwpcClient.fetch_xrays/0, &SpaceWeather.upsert_xray/1)
:ok
end
defp fetch_and_upsert(name, fetcher, upserter) do
case fetcher.() do
{:ok, rows} ->
{:ok, count} = upserter.(rows)
Logger.info("SpaceWeatherFetch: #{name} upserted #{count} rows")
{:error, reason} ->
Logger.warning("SpaceWeatherFetch: #{name} failed: #{inspect(reason)}")
end
end
end