Skip to content

Commit

Permalink
Add origin to clickhouse functions (#3892)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanIvanoff authored Aug 21, 2023
1 parent 77e773b commit 18420b8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
25 changes: 20 additions & 5 deletions lib/sanbase/dashboard/autocomplete.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule Sanbase.Dashboard.Autocomplete do
alias Sanbase.ClickhouseRepo

def get_data() do
def get_data(opts \\ []) do
Sanbase.ClickhouseRepo.put_dynamic_repo(Sanbase.ClickhouseRepo.ReadOnly)

result = %{
columns: get_columns(),
functions: get_functions(),
functions: get_functions(opts),
tables: get_tables()
}

Expand Down Expand Up @@ -79,14 +79,29 @@ defmodule Sanbase.Dashboard.Autocomplete do
result
end

defp get_functions() do
defp get_functions(opts) do
filter_functions =
case Keyword.get(opts, :functions_filter) do
:user_defined -> "origin = 'SQLUserDefined'"
:system -> "origin = 'System'"
_ -> nil
end

sql = """
SELECT name
SELECT name, origin
FROM system.functions
#{if filter_functions, do: "WHERE #{filter_functions}"}
"""

query_struct = Sanbase.Clickhouse.Query.new(sql, %{})
{:ok, result} = ClickhouseRepo.query_transform(query_struct, fn [name] -> %{name: name} end)

{:ok, result} =
ClickhouseRepo.query_transform(query_struct, fn [name, origin] ->
%{name: name, origin: origin}
end)

result
# TODO: Remove after cleanup
|> Enum.reject(&(&1.name =~ ~r"boris|tzanko"))
end
end
5 changes: 3 additions & 2 deletions lib/sanbase_web/graphql/resolvers/dashboard_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ defmodule SanbaseWeb.Graphql.Resolvers.DashboardResolver do

require Logger

def get_clickhouse_database_metadata(_root, _args, _resolution) do
Dashboard.Autocomplete.get_data()
def get_clickhouse_database_metadata(_root, args, _resolution) do
opts = [functions_filter: args[:functions_filter]]
Dashboard.Autocomplete.get_data(opts)
end

def user_public_dashboards(%Sanbase.Accounts.User{} = user, _args, _resolution) do
Expand Down
2 changes: 2 additions & 0 deletions lib/sanbase_web/graphql/schema/queries/dashboard_queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ defmodule SanbaseWeb.Graphql.Schema.DashboardQueries do
field :get_clickhouse_database_metadata, :clickhouse_database_metadata do
meta(access: :free)

arg(:functions_filter, :clickhouse_metadata_function_filter_enum)

cache_resolve(&DashboardResolver.get_clickhouse_database_metadata/3)
end

Expand Down
6 changes: 6 additions & 0 deletions lib/sanbase_web/graphql/schema/types/dashboard_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ defmodule SanbaseWeb.Graphql.DashboardTypes do
"""
object :clickhouse_database_function_metadata do
field(:name, non_null(:string))
field(:origin, non_null(:string))
end

enum :clickhouse_metadata_function_filter_enum do
value(:system)
value(:user_defined)
end

@desc ~s"""
Expand Down
9 changes: 6 additions & 3 deletions test/sanbase_web/graphql/dashboard/dashboard_api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ defmodule SanbaseWeb.Graphql.DashboardApiTest do
getClickhouseDatabaseMetadata{
columns{ name isInSortingKey isInPartitionKey }
tables{ name partitionKey sortingKey primaryKey }
functions{ name }
functions{ name origin }
}
}
"""
Expand All @@ -876,7 +876,7 @@ defmodule SanbaseWeb.Graphql.DashboardApiTest do
}}
end,
# mock functions response
fn -> {:ok, %{rows: [["logTrace"], ["aes_decrypt_mysql"]]}} end,
fn -> {:ok, %{rows: [["logTrace", "System"], ["get_asset_id", "SQLUserDefined"]]}} end,
# mock tables response
fn ->
{:ok,
Expand Down Expand Up @@ -912,7 +912,10 @@ defmodule SanbaseWeb.Graphql.DashboardApiTest do
"name" => "computed_at"
}
],
"functions" => [%{"name" => "logTrace"}, %{"name" => "aes_decrypt_mysql"}],
"functions" => [
%{"name" => "logTrace", "origin" => "System"},
%{"name" => "get_asset_id", "origin" => "SQLUserDefined"}
],
"tables" => [
%{
"name" => "asset_metadata",
Expand Down

0 comments on commit 18420b8

Please sign in to comment.