From e1f913ac83f0aeab67699074f3938e27b79c9401 Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Thu, 5 Dec 2024 16:48:57 +0200 Subject: [PATCH] Move Metric.Registry embedded schemas to a different file --- .../metric/registry/embedded_schemas.ex | 75 ++++++++++++++++++ lib/sanbase/metric/registry/registry.ex | 79 +++---------------- 2 files changed, 85 insertions(+), 69 deletions(-) create mode 100644 lib/sanbase/metric/registry/embedded_schemas.ex diff --git a/lib/sanbase/metric/registry/embedded_schemas.ex b/lib/sanbase/metric/registry/embedded_schemas.ex new file mode 100644 index 000000000..6622e4aa3 --- /dev/null +++ b/lib/sanbase/metric/registry/embedded_schemas.ex @@ -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 diff --git a/lib/sanbase/metric/registry/registry.ex b/lib/sanbase/metric/registry/registry.ex index 46af29260..b7ec04b8f 100644 --- a/lib/sanbase/metric/registry/registry.ex +++ b/lib/sanbase/metric/registry/registry.ex @@ -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 @@ -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(), @@ -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