AuroraMeter.Storage behaviour (Aurora Meter v0.1.0)

View Source

Behaviour for persisting Aurora Meter state, plus dispatch helpers that route to the configured adapter (AuroraMeter.Config.storage/0, default AuroraMeter.Storage.Ecto).

Only a Postgres/Ecto adapter ships in v1; this behaviour keeps the door open for others without building through it now.

Summary

Types

A counter snapshot to persist. feature may be an atom or string.

A raw usage event to persist.

Functions

Fetches a tenant's subscription, or nil.

Appends raw usage events (durable mode / audit).

Loads a single flushed counter value, or nil if absent.

Inserts or updates a tenant's subscription (upsert on tenant_key).

Returns all counter snapshots for a period (used by Pro rollups).

Upserts counter snapshots (absolute values) by {tenant_key, feature, period_start}.

Types

counter_row()

@type counter_row() :: %{
  tenant_key: String.t(),
  feature: atom() | String.t(),
  period_start: DateTime.t(),
  value: integer()
}

A counter snapshot to persist. feature may be an atom or string.

event_row()

@type event_row() :: %{
  :tenant_key => String.t(),
  :feature => atom() | String.t(),
  optional(:quantity) => integer(),
  optional(:metadata) => map()
}

A raw usage event to persist.

Callbacks

get_subscription(t)

@callback get_subscription(String.t()) :: AuroraMeter.Schema.Subscription.t() | nil

insert_events(list)

@callback insert_events([event_row()]) :: :ok

load_counter(t, arg2, t)

@callback load_counter(String.t(), atom() | String.t(), DateTime.t()) :: integer() | nil

put_subscription(map)

@callback put_subscription(map()) ::
  {:ok, AuroraMeter.Schema.Subscription.t()} | {:error, Ecto.Changeset.t()}

stream_counters(t)

@callback stream_counters(DateTime.t()) :: [AuroraMeter.Schema.Counter.t()]

upsert_counters(list)

@callback upsert_counters([counter_row()]) :: :ok

Functions

get_subscription(tenant_key)

@spec get_subscription(String.t()) :: AuroraMeter.Schema.Subscription.t() | nil

Fetches a tenant's subscription, or nil.

insert_events(rows)

@spec insert_events([event_row()]) :: :ok

Appends raw usage events (durable mode / audit).

load_counter(tenant_key, feature, period_start)

@spec load_counter(String.t(), atom() | String.t(), DateTime.t()) :: integer() | nil

Loads a single flushed counter value, or nil if absent.

put_subscription(attrs)

@spec put_subscription(map()) ::
  {:ok, AuroraMeter.Schema.Subscription.t()} | {:error, Ecto.Changeset.t()}

Inserts or updates a tenant's subscription (upsert on tenant_key).

stream_counters(period_start)

@spec stream_counters(DateTime.t()) :: [AuroraMeter.Schema.Counter.t()]

Returns all counter snapshots for a period (used by Pro rollups).

upsert_counters(rows)

@spec upsert_counters([counter_row()]) :: :ok

Upserts counter snapshots (absolute values) by {tenant_key, feature, period_start}.