Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/sanbase/clickhouse/metric/sql_query/metric_sql_query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ defmodule Sanbase.Clickhouse.MetricAdapter.SqlQuery do
metric_id_filter: 2,
versioned_metric_id_filter: 2,
additional_filters: 3,
dt_to_unix: 2
dt_to_unix: 2,
not_publicly_disabled_label_filter: 1
]

alias Sanbase.Clickhouse.MetricAdapter.Registry
Expand Down Expand Up @@ -358,7 +359,8 @@ defmodule Sanbase.Clickhouse.MetricAdapter.SqlQuery do
FROM labeled_balances_filtered
WHERE
#{metric_id_filter(metric, argument_name: "metric")} AND
#{where_clause}
#{where_clause} AND
#{not_publicly_disabled_label_filter(argument_name: "metric", filter_by: :fqn)}
"""

params =
Expand All @@ -381,7 +383,8 @@ defmodule Sanbase.Clickhouse.MetricAdapter.SqlQuery do
WHERE
#{metric_id_filter(metric, argument_name: "metric")} AND
#{where_clause} AND
asset_name = {{slug}}
asset_name = {{slug}} AND
#{not_publicly_disabled_label_filter(argument_name: "metric", filter_by: :fqn)}
"""

params =
Expand All @@ -400,7 +403,8 @@ defmodule Sanbase.Clickhouse.MetricAdapter.SqlQuery do
FROM labeled_intraday_metrics_v2
WHERE
dt >= now() - INTERVAL 365 DAY AND
#{metric_id_filter(metric, argument_name: "metric")}
#{metric_id_filter(metric, argument_name: "metric")} AND
#{not_publicly_disabled_label_filter(argument_name: "metric", filter_by: :label_id)}
"""

params =
Expand All @@ -418,7 +422,8 @@ defmodule Sanbase.Clickhouse.MetricAdapter.SqlQuery do
WHERE
dt >= now() - INTERVAL 365 DAY AND
#{metric_id_filter(metric, argument_name: "metric")} AND
#{asset_id_filter(%{slug: slug}, argument_name: "slug")}
#{asset_id_filter(%{slug: slug}, argument_name: "slug")} AND
#{not_publicly_disabled_label_filter(argument_name: "metric", filter_by: :label_id)}
"""

params =
Expand Down
36 changes: 36 additions & 0 deletions lib/sanbase/metric/sql_query_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,42 @@ defmodule Sanbase.Metric.SqlQuery.Helper do
end
end

@doc ~s"""
Generate a SQL filter that excludes labels present in the
`publicly_disabled_labeled_metrics` ClickHouse table.

The `filter_by` option controls which column to filter on:
- `:label_id` — used for tables that have a `label_id` column (e.g. labeled_intraday_metrics_v2)
- `:fqn` — used for tables that have a `fqn` column (e.g. labeled_balances_filtered);
converts the disabled label_ids to fqns via the `labels_dict` dictionary.
"""
def not_publicly_disabled_label_filter(opts) do
arg_name = Keyword.fetch!(opts, :argument_name)
filter_by = Keyword.fetch!(opts, :filter_by)

{column, select_expr} =
case filter_by do
:label_id ->
{"label_id", "label_id"}

:fqn ->
{"fqn", "dictGet('labels_dict', 'fqn', label_id)"}
end

"""
#{column} NOT IN (
SELECT #{select_expr}
FROM publicly_disabled_labeled_metrics
WHERE metric_id = (
SELECT metric_id
FROM metric_metadata_external FINAL
WHERE name = {{#{arg_name}}} AND version = '1.0'
LIMIT 1
)
)
"""
end
Comment thread
IvanIvanoff marked this conversation as resolved.

def metric_id_filter(metric, opts) when is_binary(metric) do
# In the spikes table the column is called calculated_on_metric_id
metric_id_column = Keyword.get(opts, :metric_id_column, "metric_id")
Expand Down