Skip to content

Commit

Permalink
Add deprecation-related fields to Metric Registry show and form
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanIvanoff committed Nov 8, 2024
1 parent 042ead8 commit 06a45ff
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,35 @@ defmodule SanbaseWeb.AvailableMetricsDescription do
</pre>
"""
end

def get_popover_text(%{key: "Is Deprecated"} = assigns) do
~H"""
<pre>
A boolean that indicates whether the metric is deprecated.
Deprecated metrics are discouraged to be used as they could be
removed in the near future.
</pre>
"""
end

def get_popover_text(%{key: "Hard Deprecate After"} = assigns) do
~H"""
<pre>
Specifies a datetime after which the metric will no longer be
accessible.
After the date passes, the metric is not shown in lists of available
metrics and querying it will return an error that the metric is
deprecated since the specified date.
</pre>
"""
end

def get_popover_text(%{key: "Deprecation Note"} = assigns) do
~H"""
<pre>
Freeform text explaining why the metric is deprecated.
This can include suggestion what other metric can be used instead and how.
</pre>
"""
end
end
78 changes: 63 additions & 15 deletions lib/sanbase_web/live/metric_registry/metric_registry_form_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ defmodule SanbaseWeb.MetricRegistryFormLive do
type="select"
id="input-data-type"
field={@form[:data_type]}
label="Access"
label="Data Type"
options={["timeseries", "histogram", "table"]}
/>
<.docs_input form={@form} />
Expand Down Expand Up @@ -132,6 +132,8 @@ defmodule SanbaseWeb.MetricRegistryFormLive do
value={Jason.encode!(@metric_registry.parameters)}
label="Parameters"
/>
<.deprecation_input form={@form} />
<.button phx-disable-with="Saving...">Save</.button>
<.error :for={{field, [msg]} <- @save_errors}><%= to_string(field) <> ": " <> msg %></.error>
Expand All @@ -140,29 +142,63 @@ defmodule SanbaseWeb.MetricRegistryFormLive do
"""
end

def min_plan_input(assigns) do
def deprecation_input(assigns) do
assigns = assign(assigns, disabled: assigns.form[:is_deprecated].value in [false, "false"])

~H"""
<div class="border border-gray-200 rounded-lg px-3 py-6">
<span class="text-sm font-semibold leading-6 text-zinc-800 mb-3">Min Plan per product</span>
<.input
type="select"
id="input-sanbase-min-plan"
field={@form[:sanbase_min_plan]}
label="Sanbase"
options={["free", "pro", "max"]}
id="input-is-deprecated"
label="Is Deprecated"
field={@form[:is_deprecated]}
options={[true, false]}
/>
<div class={["rounded-b-lg px-3 py-6", if(@disabled, do: "bg-gray-100")]}>
<.input
type="datetime-local"
id="input-hard-deprecate-after"
label="Hard Deprecate After"
field={@form[:hard_deprecate_after]}
disabled={@disabled}
title={if @disabled, do: "Disabled until `Is Deprecated` is set to true"}
/>
<.input
type="select"
id="input-sanapi-min-plan"
field={@form[:sanapi_min_plan]}
label="Sanapi"
options={["free", "pro", "max"]}
/>
<.input
type="textarea"
id="input-deprecation-note"
label="Deprecation Note"
field={@form[:deprecation_note]}
disabled={@disabled}
placeholder={if @disabled, do: "Disabled until `Is Deprecated` is set to true"}
/>
</div>
</div>
"""
end

attr :form, :map, required: true

def min_plan_input(assigns) do
~H"""
<.input
type="select"
id="input-sanbase-min-plan"
field={@form[:sanbase_min_plan]}
label="Sanbase Min Plan"
options={["free", "pro", "max"]}
/>
<.input
type="select"
id="input-sanapi-min-plan"
field={@form[:sanapi_min_plan]}
label="Sanapi Min Plan"
options={["free", "pro", "max"]}
/>
"""
end

attr :name, :string, required: true
attr :text, :string, required: true
attr :ef, :map, required: false, default: nil
Expand Down Expand Up @@ -380,6 +416,7 @@ defmodule SanbaseWeb.MetricRegistryFormLive do
defp process_params(params) do
params
|> maybe_update_if_present("parameters")
|> maybe_update_if_present("fixed_parameters")
end

defp maybe_update_if_present(%{"parameters" => json} = params, "parameters") do
Expand All @@ -388,7 +425,18 @@ defmodule SanbaseWeb.MetricRegistryFormLive do
# valid JSON, replace the string with the decoded JSON and let the Ecto changeset
# validate it further
case Jason.decode(json) do
{:ok, decoded} -> %{params | "parameters" => decoded}
{:ok, decoded} when is_list(decoded) -> %{params | "parameters" => decoded}
{:error, _} -> params
end
end

defp maybe_update_if_present(%{"fixed_parameters" => json} = params, "fixed_parameters") do
# If the fixed_parameters are not valid JSON, we will keep the old fixed_parameters.
# It will fail from the changeset validation with `invalid format`. If it is
# valid JSON, replace the string with the decoded JSON and let the Ecto changeset
# validate it further
case Jason.decode(json) do
{:ok, decoded} when is_map(decoded) -> %{params | "fixed_parameters" => decoded}
{:error, _} -> params
end
end
Expand Down
18 changes: 18 additions & 0 deletions lib/sanbase_web/live/metric_registry/metric_registry_show_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,24 @@ defmodule SanbaseWeb.MetricRegistryShowLive do
value: metric_registry.data_type,
popover_target: "popover-data-type",
popover_target_text: get_popover_text(%{key: "Data Type"})
},
%{
key: "Is Deprecated",
value: metric_registry.is_deprecated,
popover_target: "popover-is-deprecated",
popover_target_text: get_popover_text(%{key: "Is Deprecated"})
},
%{
key: "Hard Deprecate After",
value: metric_registry.hard_deprecate_after,
popover_target: "popover-hard-deprecate-after",
popover_target_text: get_popover_text(%{key: "Hard Deprecate After"})
},
%{
key: "Deprecation Note",
value: metric_registry.deprecation_note,
popover_target: "popover-deprecation-note",
popover_target_text: get_popover_text(%{key: "Deprecation Note"})
}
]
end
Expand Down

0 comments on commit 06a45ff

Please sign in to comment.