2.1 KiB · text History bbb2a48
defmodule Vigil.Repo.Migrations.CreateIngestItemsAndOutcomes do
  use Ecto.Migration

  # Slice 1 (ROADMAP.md): the ingest buffer and SDK outcomes.
  def change do
    # Envelope items accepted at the edge and waiting for a processor
    # (Vigil.Ingest.Drain). Processed rows are deleted; items whose
    # processor arrives in a later slice wait as "deferred".
    create table(:ingest_items, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :project_id, references(:projects, type: :binary_id, on_delete: :delete_all),
        null: false

      add :organization_id, :binary_id, null: false
      add :type, :string, null: false
      add :event_id, :string
      add :envelope_headers, :map, null: false, default: %{}
      add :item_headers, :map, null: false, default: %{}
      add :payload, :binary, null: false
      add :client_ip, :string
      add :user_agent, :string
      add :received_at, :utc_datetime_usec, null: false
      # pending | processing (claimed by a drain) | deferred | failed
      add :state, :string, null: false, default: "pending"
      add :claimed_at, :utc_datetime_usec
      add :attempts, :integer, null: false, default: 0
      add :error, :text
    end

    create index(:ingest_items, [:project_id, :state, :id])
    create index(:ingest_items, [:state, :type])

    # What SDKs report having dropped (client_report items), and what the
    # edge refused, per hour: category, reason, how many.
    create table(:outcomes, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :organization_id, :binary_id, null: false

      add :project_id, references(:projects, type: :binary_id, on_delete: :delete_all),
        null: false

      add :hour, :utc_datetime, null: false
      # client (an SDK's client_report) | edge (refused here)
      add :source, :string, null: false
      add :category, :string, null: false
      add :reason, :string, null: false
      add :quantity, :bigint, null: false, default: 0
    end

    create unique_index(:outcomes, [:project_id, :hour, :source, :category, :reason])
    create index(:outcomes, [:organization_id, :hour])
  end
end