GoodAnalytics.Core.Sessions.Backfill (GoodAnalytics v0.1.1)

Copy Markdown View Source

Runtime API for backfilling historical sessions.

Walks ga_events rows with no session_id, ordered by {visitor_id, inserted_at, id}, builds ga_sessions, and stamps each event. It is resumable because each batch only selects unstamped events.

For large historical tables, create a temporary partial index before the rollout and drop it after the backfill finishes:

CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_ga_events_session_backfill
  ON good_analytics.ga_events (visitor_id, inserted_at, id)
  WHERE session_id IS NULL;

This module does not depend on Mix, so compiled production releases can call it from application code, a release task, or a remote console.

Summary

Functions

Runs batches until no eligible events remain.

Processes one batch of unstamped historical events.

Functions

run(opts \\ [])

@spec run(keyword()) ::
  {:ok, %{events: non_neg_integer(), sessions: non_neg_integer()}}

Runs batches until no eligible events remain.

Options:

  • :batch_size - maximum events per batch. Defaults to 5000.
  • :since - optional DateTime lower bound for inserted_at.

Returns {:ok, %{events: total_stamped, sessions: total_created}}.

run_batch(opts \\ [])

@spec run_batch(keyword()) ::
  {:ok, %{events: non_neg_integer(), sessions: non_neg_integer()}}

Processes one batch of unstamped historical events.