prop/lib/microwaveprop/propagation/run_timing.ex
Graham McIntire fc91f204fd
feat(propagation): record per-forecast-hour chain step timings
Add a propagation_run_timings table so the wall-clock duration of each
(run_time, forecast_hour) step is queryable long after the run is over.
Keyed by (run_time, forecast_hour) with a status column that captures
whether the step succeeded or bailed out, and an error string on
failure.

PropagationGridWorker stamps every step (ok and failed) via
Propagation.record_run_timing/1. Timing inserts are wrapped in rescue +
changeset-error handling so the instrumentation can never brick the
chain.
2026-04-18 09:36:52 -05:00

39 lines
1.3 KiB
Elixir

defmodule Microwaveprop.Propagation.RunTiming do
@moduledoc """
One row per forecast hour of a `PropagationGridWorker` chain, recording
how long that hour's fetch + score + persist cycle took.
Rows are keyed by `(run_time, forecast_hour)`. `run_time` is the HRRR
model cycle and `forecast_hour` ∈ 0..18.
"""
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "propagation_run_timings" do
field :run_time, :utc_datetime
field :forecast_hour, :integer
field :valid_time, :utc_datetime
field :started_at, :utc_datetime_usec
field :finished_at, :utc_datetime_usec
field :duration_ms, :integer
field :status, Ecto.Enum, values: [:ok, :failed]
field :error, :string
timestamps(type: :utc_datetime, updated_at: false)
end
@required ~w(run_time forecast_hour valid_time started_at finished_at duration_ms status)a
@optional ~w(error)a
def changeset(row, attrs) do
row
|> cast(attrs, @required ++ @optional)
|> validate_required(@required)
|> validate_number(:forecast_hour, greater_than_or_equal_to: 0, less_than_or_equal_to: 18)
|> validate_number(:duration_ms, greater_than_or_equal_to: 0)
|> unique_constraint([:run_time, :forecast_hour])
end
end