Skip to content

Commit

Permalink
Move Metric.Registry embedded schemas to a different file
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanIvanoff committed Jan 8, 2025
1 parent cc39eee commit e1f913a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 69 deletions.
75 changes: 75 additions & 0 deletions lib/sanbase/metric/registry/embedded_schemas.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
defmodule Sanbase.Metric.Registry.Selector do
use Ecto.Schema
import Ecto.Changeset

@derive {Jason.Encoder, only: [:type]}

@primary_key false
embedded_schema do
field(:type, :string)
end

def changeset(%__MODULE__{} = struct, attrs) do
struct
|> cast(attrs, [:type])
|> validate_required([:type])
end
end

defmodule Sanbase.Metric.Registry.Table do
use Ecto.Schema
import Ecto.Changeset

@derive {Jason.Encoder, only: [:name]}

@primary_key false
embedded_schema do
field(:name, :string)
end

def changeset(%__MODULE__{} = struct, attrs) do
struct
|> cast(attrs, [:name])
|> validate_required([:name])
|> validate_format(:name, ~r/[a-z0-9_\-]/)
end
end

defmodule Sanbase.Metric.Registry.Alias do
use Ecto.Schema
import Ecto.Changeset

@derive {Jason.Encoder, only: [:name]}

@primary_key false
embedded_schema do
field(:name, :string)
end

def changeset(%__MODULE__{} = struct, attrs) do
struct
|> cast(attrs, [:name])
|> validate_required(:name)
|> validate_format(:name, Sanbase.Metric.Registry.metric_regex())
|> validate_length(:name, min: 3, max: 100)
end
end

defmodule Sanbase.Metric.Registry.Doc do
use Ecto.Schema
import Ecto.Changeset

@derive {Jason.Encoder, only: [:link]}

@primary_key false
embedded_schema do
field(:link, :string)
end

def changeset(%__MODULE__{} = struct, attrs) do
struct
|> cast(attrs, [:link])
|> validate_required([:link])
|> validate_format(:link, ~r|https://academy.santiment.net|)
end
end
79 changes: 10 additions & 69 deletions lib/sanbase/metric/registry/registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ defmodule Sanbase.Metric.Registry do
import Ecto.Changeset
import Sanbase.Metric.Registry.EventEmitter, only: [emit_event: 3]

alias Sanbase.Repo
alias __MODULE__.Validation
alias __MODULE__.ChangeSuggestion
alias __MODULE__.Doc
alias __MODULE__.Alias
alias __MODULE__.Selector
alias __MODULE__.Table

alias Sanbase.Repo
alias Sanbase.TemplateEngine

# Matches letters, digits, _, -, :, ., {, }, (, ), \, / and space
Expand All @@ -18,74 +23,6 @@ defmodule Sanbase.Metric.Registry do
@metric_regex ~r/^[a-z0-9_{}:]+$/
def metric_regex(), do: @metric_regex

defmodule Selector do
use Ecto.Schema
import Ecto.Changeset

@primary_key false
embedded_schema do
field(:type, :string)
end

def changeset(%__MODULE__{} = struct, attrs) do
struct
|> cast(attrs, [:type])
|> validate_required([:type])
end
end

defmodule Table do
use Ecto.Schema
import Ecto.Changeset

@primary_key false
embedded_schema do
field(:name, :string)
end

def changeset(%__MODULE__{} = struct, attrs) do
struct
|> cast(attrs, [:name])
|> validate_required([:name])
|> validate_format(:name, ~r/[a-z0-9_\-]/)
end
end

defmodule Alias do
use Ecto.Schema
import Ecto.Changeset

@primary_key false
embedded_schema do
field(:name, :string)
end

def changeset(%__MODULE__{} = struct, attrs) do
struct
|> cast(attrs, [:name])
|> validate_required(:name)
|> validate_format(:name, Sanbase.Metric.Registry.metric_regex())
|> validate_length(:name, min: 3, max: 100)
end
end

defmodule Doc do
use Ecto.Schema
import Ecto.Changeset

@primary_key false
embedded_schema do
field(:link, :string)
end

def changeset(%__MODULE__{} = struct, attrs) do
struct
|> cast(attrs, [:link])
|> validate_required([:link])
|> validate_format(:link, ~r|https://academy.santiment.net|)
end
end

@type t :: %__MODULE__{
id: integer(),
metric: String.t(),
Expand Down Expand Up @@ -117,6 +54,10 @@ defmodule Sanbase.Metric.Registry do
inserted_at: DateTime.t(),
updated_at: DateTime.t()
}

@derive {Jason.Encoder,
except: [:__struct__, :__meta__, :id, :inserted_at, :updated_at, :change_suggestions]}

@timestamps_opts [type: :utc_datetime]
schema "metric_registry" do
# How the metric is exposed to external users
Expand Down

0 comments on commit e1f913a

Please sign in to comment.