# `AshHooks.EndpointHealth`
[🔗](https://github.com/baselabs/ash_hooks/blob/v3.0.0/lib/ash_hooks/endpoint_health.ex#L1)

Endpoint health over the attempt history, and the sustained-failure
auto-disable policy.

`stats/2` computes one endpoint's trailing-window health from its
attempt rows: the success rate and nearest-rank latency percentiles
(p50/p90/p99, in milliseconds, over the attempts that reached the
wire), with FIRST-ATTEMPT success reported separately from EVENTUAL
success (a delivery whose first attempt succeeded vs. one that
succeeded after retries). `sweep/3` applies the endpoint resource's `auto_disable` policy
(a configurable duration and failure share, beside the existing 410
rule): an endpoint whose window meets the failure share with the
minimum evidence is DISABLED through the same durable switch the 410
rule uses, after a warning fired first at the warning thresholds.
Re-enabling is an explicit application action — the package never
re-enables.

Schedule both alongside your retention sweeps (an Oban cron, a mix
task): `stats/2` per endpoint page, `sweep/3` per endpoint resource.

# `stats`

```elixir
@type stats() :: %{
  attempts: non_neg_integer(),
  succeeded: non_neg_integer(),
  failed: non_neg_integer(),
  success_rate: float() | nil,
  first_attempt_successes: non_neg_integer(),
  eventual_successes: non_neg_integer(),
  deliveries: non_neg_integer(),
  latency_p50_ms: non_neg_integer() | nil,
  latency_p90_ms: non_neg_integer() | nil,
  latency_p99_ms: non_neg_integer() | nil
}
```

One endpoint's trailing-window health.

# `stats`

```elixir
@spec stats(module(), keyword()) :: {:ok, stats()} | {:error, term()}
```

Computes one endpoint's health over the trailing window of its attempt
rows (requires an `AshHooks.OutboundAttempt` resource).

Options: `:endpoint_id` (required), `:window_seconds` (default 86,400),
`:now` (the application clock), `:from`/`:to` (explicit `DateTime`
bounds instead of the trailing window), and `:tenant` (the tenancy
pre-flight applies).

    {:ok, %{attempts: 40, success_rate: 0.925, latency_p50_ms: 120, ...}} =
      AshHooks.EndpointHealth.stats(MyApp.Attempt, endpoint_id: endpoint.id)

# `sweep`

```elixir
@spec sweep(module(), module(), keyword()) ::
  {:ok, %{disabled: non_neg_integer(), warned: non_neg_integer()}}
  | {:error, term()}
```

Applies the endpoint resource's `auto_disable` policy to every enabled
endpoint, from the attempt history:

  * the FIRST sweep that sees an endpoint meeting `:failure_share`
    with at least `:min_attempts` of evidence WARNED: it emits
    `[:ash_hooks, :endpoint, :disable_warning]` (`failure_share` and
    `attempts` in the metadata), durably stamps `disable_warned_at`
    (the marker column the `auto_disable` declaration injects), and
    leaves the endpoint enabled — the notify surface gets at least one
    full sweep interval before the breaker can open;
  * the NEXT sweep that still sees qualifying failure disables through
    the same durable switch the 410 rule uses, emits
    `[:ash_hooks, :endpoint, :disable]` with
    `reason: :sustained_failure`, and clears the stamp;
  * an endpoint that meets the share with less evidence (the warning
    thresholds) warns the same way and stays enabled;
  * an endpoint that drops below the warning band has its stamp
    cleared — a later degradation warns afresh.

Resources without an `auto_disable` section are skipped wholesale.
Options: `:now`, `:tenant`. Returns `{:ok, %{disabled: n, warned: n}}`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
