Mead.Ledger.Journal (mead v0.1.1)

Copy Markdown View Source

The pure model of the ledger journal: the counter rows, the ring of the last 100 turns and the dropped count that a sequence of journal events adds up to.

The events are {:turn, entry}, with the map the ring holds, {:forget, session_id}, :dropped, and {:checkpoint, rows, ring}, which replaces everything before it with the whole state: every counter row, plus {:dropped, count} when it is above zero, and the ring entries oldest first. Replaying the events of a file rebuilds the numbers the ETS tables held. It also serves the rewrite of the file, which is built from the file itself and not from the tables: the tables may already hold an event whose journal record has not been written, and a checkpoint taken from them would count it twice.

A counter row is {key, turns, failures, input_tokens, output_tokens, duration_ms}, where the key is {:session, id} or {:route, route, provider}.

Summary

Functions

Applies one event to the model.

The checkpoint event that replays to this model.

How many unreadable events the model has seen.

What one turn adds to a counter row: turns, failures, input tokens, output tokens, ms.

An empty model.

The :ets.update_counter/4 operations that add one turn to a counter row.

Adds the events up, or {:error, :corrupt} at the first one nobody writes.

The ring entries, oldest first.

The counter rows of the model.

Types

counter_row()

entry()

@type entry() :: map()

event()

@type event() ::
  {:turn, entry()}
  | {:forget, String.t()}
  | :dropped
  | {:checkpoint, [counter_row() | {:dropped, pos_integer()}], [entry()]}

t()

@type t() :: %Mead.Ledger.Journal{
  dropped: non_neg_integer(),
  ring: [entry()],
  rows: %{required(term()) => counter_row()}
}

Functions

apply_event(model, arg2)

@spec apply_event(t(), event()) :: {:ok, t()} | :error

Applies one event to the model.

checkpoint(model)

@spec checkpoint(t()) :: event()

The checkpoint event that replays to this model.

dropped(model)

@spec dropped(t()) :: non_neg_integer()

How many unreadable events the model has seen.

increments(entry)

@spec increments(entry()) ::
  {1, 0 | 1, non_neg_integer(), non_neg_integer(), non_neg_integer()}

What one turn adds to a counter row: turns, failures, input tokens, output tokens, ms.

new()

@spec new() :: t()

An empty model.

operations(entry)

@spec operations(entry()) :: [{pos_integer(), non_neg_integer()}]

The :ets.update_counter/4 operations that add one turn to a counter row.

replay(events)

@spec replay([event()]) :: {:ok, t()} | {:error, :corrupt}

Adds the events up, or {:error, :corrupt} at the first one nobody writes.

ring_oldest_first(model)

@spec ring_oldest_first(t()) :: [entry()]

The ring entries, oldest first.

rows(model)

@spec rows(t()) :: [counter_row()]

The counter rows of the model.