3.4 KiB · text History 28e6625
defmodule Vigil.Repo.Migrations.CreateMonitors do
  use Ecto.Migration

  # Slice 7 (ROADMAP.md): cron monitors, their check-ins and incidents.
  # Alert notifications can carry a plain message (a monitor incident, later
  # an uptime one) instead of issues.
  def change do
    create table(:monitors, 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 :slug, :string, null: false
      add :name, :string, null: false
      add :enabled, :boolean, null: false, default: true
      # crontab: `schedule` is the expression; interval: every
      # `interval_value` `interval_unit`s.
      add :schedule_type, :string, null: false
      add :schedule, :string
      add :interval_value, :integer
      add :interval_unit, :string
      add :timezone, :string, null: false, default: "Etc/UTC"
      # Minutes, as Sentry's monitor_config.
      add :checkin_margin, :integer, null: false, default: 1
      add :max_runtime, :integer, null: false, default: 30
      add :failure_threshold, :integer, null: false, default: 1
      add :recovery_threshold, :integer, null: false, default: 1

      # Health: ok, error (an open incident), or new (no check-in yet).
      add :health, :string, null: false, default: "new"
      add :next_expected_at, :utc_datetime
      add :last_checkin_at, :utc_datetime_usec
      add :last_status, :string
      add :consecutive_failures, :integer, null: false, default: 0
      add :consecutive_ok, :integer, null: false, default: 0

      timestamps(type: :utc_datetime)
    end

    create unique_index(:monitors, [:project_id, :slug])
    create index(:monitors, [:next_expected_at], where: "enabled")

    create table(:monitor_incidents, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :monitor_id, references(:monitors, type: :binary_id, on_delete: :delete_all),
        null: false

      # missed, error or timeout: what opened it.
      add :reason, :string, null: false
      add :started_at, :utc_datetime_usec, null: false
      add :resolved_at, :utc_datetime_usec
    end

    create index(:monitor_incidents, [:monitor_id, :started_at])

    create unique_index(:monitor_incidents, [:monitor_id],
             where: "resolved_at IS NULL",
             name: :monitor_incidents_one_open_index
           )

    create table(:monitor_checkins, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :monitor_id, references(:monitors, type: :binary_id, on_delete: :delete_all),
        null: false

      add :project_id, :binary_id, null: false
      # The SDK's check_in_id (missed ones have none).
      add :check_in_id, :uuid
      # in_progress, ok, error, missed, timeout
      add :status, :string, null: false
      add :duration_ms, :float
      add :expected_at, :utc_datetime
      add :started_at, :utc_datetime_usec
      add :finished_at, :utc_datetime_usec
      add :environment, :string
      add :release, :string

      timestamps(type: :utc_datetime_usec)
    end

    create index(:monitor_checkins, [:monitor_id, :id])
    create unique_index(:monitor_checkins, [:monitor_id, :check_in_id])
    create index(:monitor_checkins, [:status, :started_at], where: "status = 'in_progress'")

    alter table(:alert_notifications) do
      add :message, :map
    end
  end
end