Skip to content

Commit

Permalink
Backup
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanIvanoff committed Oct 21, 2024
1 parent 053c7c0 commit 54c13ae
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lib/sanbase/metric/registry/registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Sanbase.Metric.Registry do
import Ecto.Changeset

alias __MODULE__.Validation
alias Sanbase.TemplateEngine

@timestamps_opts [type: :utc_datetime]
schema "metric_registry" do
Expand Down Expand Up @@ -80,4 +81,45 @@ defmodule Sanbase.Metric.Registry do
name: :metric_registry_composite_unique_index
)
end

def all(), do: Sanbase.Repo.all(__MODULE__)

def resolve(list) when is_list(list) do
Enum.map(list, &resolve/1)
end

def resolve(%__MODULE__{} = registry) do
registry
|> resolve_aliases()
|> Enum.flat_map(&apply_template_parameters/1)
end

defp resolve_aliases(%__MODULE__{} = registry) do
[registry] ++
Enum.map(registry.aliases, fn metric_alias ->
%{registry | metric: metric_alias}
end)
end

defp apply_template_parameters(%__MODULE__{} = registry)
when registry.is_template_metric == true do
%{
metric: metric,
internal_metric: internal_metric,
human_readable_name: human_readable_name,
parameters: parameters_list
} = registry

for parameters <- parameters_list do
%{
registry
| metric: TemplateEngine.run!(metric, params: parameters),
internal_metric: TemplateEngine.run!(internal_metric, params: parameters),
human_readable_name: TemplateEngine.run!(human_readable_name, params: parameters)
}
end
end

defp apply_template_parameters(registry) when registry.is_template_metric == false,
do: [registry]
end

0 comments on commit 54c13ae

Please sign in to comment.