Skip to content

Commit

Permalink
Rework Metric.Registry table. Improve edit/new form
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanIvanoff committed Nov 7, 2024
1 parent a28af82 commit de50a06
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 124 deletions.
4 changes: 4 additions & 0 deletions lib/sanbase/metric/registry/event_emitter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ defmodule Sanbase.Metric.Registry.EventEmitter do
|> notify()
end

def handle_event({:error, _changeset}, _event_type, _args) do
:ok
end

defp notify(data) do
Sanbase.EventBus.notify(%{topic: @topic, data: data})
:ok
Expand Down
3 changes: 2 additions & 1 deletion lib/sanbase/metric/registry/populate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ defmodule Sanbase.Metric.Registry.Populate do
is_timebound: Map.get(map, "is_timebound", false),
metric: map["name"],
min_interval: map["min_interval"],
min_plan: Map.get(map, "min_plan", %{}),
sanbase_min_plan: get_in(map, ["min_plan", "SANBASE"]) || "free",
sanapi_min_plan: get_in(map, ["min_plan", "SANAPI"]) || "free",
parameters: Map.get(map, "parameters", []),
required_selectors: Map.get(map, "required_selectors", []) |> Enum.map(&%{type: &1}),
selectors: Map.get(map, "selectors", []) |> Enum.map(&%{type: &1}),
Expand Down
26 changes: 16 additions & 10 deletions lib/sanbase/metric/registry/registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ defmodule Sanbase.Metric.Registry do
@human_readable_name_regex ~r|^[a-zA-Z0-9_\.\-{}():/\\ ]+$|
@aggregations ["sum", "last", "count", "avg", "max", "min", "first"]
def aggregations(), do: @aggregations
@metric_regex ~r/^[a-z0-9_{}:]+$/
def metric_regex(), do: @metric_regex

@type t :: %__MODULE__{
id: integer(),
Expand All @@ -24,15 +26,15 @@ defmodule Sanbase.Metric.Registry do
default_aggregation: String.t(),
min_interval: String.t(),
access: String.t(),
min_plan: map(),
sanbase_min_plan: String.t(),
sanapi_min_plan: String.t(),
selectors: [String.t()],
required_selectors: [String.t()],
is_template: boolean(),
parameters: [map()],
fixed_parameters: map(),
is_timebound: boolean(),
has_incomplete_data: boolean(),
is_exposed: boolean(),
exposed_environments: String.t(),
is_hidden: boolean(),
is_deprecated: boolean(),
Expand Down Expand Up @@ -71,6 +73,7 @@ defmodule Sanbase.Metric.Registry do
struct
|> cast(attrs, [:name])
|> validate_required([:name])
|> validate_format(:name, ~r/[a-z0-9_\-]/)
end
end

Expand All @@ -85,7 +88,9 @@ defmodule Sanbase.Metric.Registry do
def changeset(%__MODULE__{} = struct, attrs) do
struct
|> cast(attrs, [:name])
|> validate_required([:name])
|> validate_required(:name)
|> validate_format(:name, Sanbase.Metric.Registry.metric_regex())
|> validate_length(:name, min: 3, max: 100)
end
end

Expand Down Expand Up @@ -118,7 +123,8 @@ defmodule Sanbase.Metric.Registry do
field(:default_aggregation, :string)
field(:min_interval, :string)
field(:access, :string)
field(:min_plan, :map)
field(:sanbase_min_plan, :string)
field(:sanapi_min_plan, :string)
embeds_many(:selectors, Selector, on_replace: :delete)
embeds_many(:required_selectors, Selector, on_replace: :delete)

Expand All @@ -131,7 +137,6 @@ defmodule Sanbase.Metric.Registry do
field(:is_timebound, :boolean, default: false)
field(:has_incomplete_data, :boolean, default: false)

field(:is_exposed, :boolean, default: true)
field(:exposed_environments, :string, default: "all")

field(:is_hidden, :boolean, default: false)
Expand Down Expand Up @@ -159,13 +164,13 @@ defmodule Sanbase.Metric.Registry do
:human_readable_name,
:internal_metric,
:is_deprecated,
:is_exposed,
:is_hidden,
:is_template,
:is_timebound,
:metric,
:min_interval,
:min_plan,
:sanbase_min_plan,
:sanapi_min_plan,
:parameters
])
|> cast_embed(:selectors,
Expand Down Expand Up @@ -201,8 +206,8 @@ defmodule Sanbase.Metric.Registry do
:metric,
:min_interval
])
|> validate_format(:metric, ~r/^[a-z0-9_{}:]+$/)
|> validate_format(:internal_metric, ~r/^[a-z0-9_{}:]+$/)
|> validate_format(:metric, @metric_regex)
|> validate_format(:internal_metric, @metric_regex)
|> validate_format(:human_readable_name, @human_readable_name_regex)
|> validate_length(:metric, min: 3, max: 100)
|> validate_length(:internal_metric, min: 3, max: 100)
Expand All @@ -212,7 +217,8 @@ defmodule Sanbase.Metric.Registry do
|> validate_inclusion(:exposed_environments, ["all", "none", "stage", "prod"])
|> validate_inclusion(:access, ["free", "restricted"])
|> validate_change(:min_interval, &Validation.validate_min_interval/2)
|> validate_change(:min_plan, &Validation.validate_min_plan/2)
|> validate_inclusion(:sanbase_min_plan, ["free", "pro", "max"])
|> validate_inclusion(:sanapi_min_plan, ["free", "pro", "max"])
|> Validation.validate_template_fields()
|> unique_constraint([:metric, :data_type, :fixed_parameters],
name: :metric_registry_composite_unique_index
Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase/metric/registry/validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ defmodule Sanbase.Metric.Registry.Validation do
:parameters,
"""
The provided parameters do not match the captures in the metric #{metric}.
Captures: #{Enum.join(captures, ", ")}
Captures: #{Enum.join(captures, ", ")},
Parameters: #{Enum.join(parameter_keys, ", ")}
"""
)
Expand Down
Loading

0 comments on commit de50a06

Please sign in to comment.