Skip to content

Commit

Permalink
Allow filtering availableFounders by slug
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanIvanoff committed Dec 16, 2024
1 parent de3d7c1 commit d7e93e1
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
20 changes: 15 additions & 5 deletions lib/sanbase/clickhouse/founders/founders.ex
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
defmodule Sanbase.Clickhouse.Founders do
def get_founders() do
query = get_founders_query()
def get_founders(slug \\ nil) do
query = get_founders_query(slug)

Sanbase.ClickhouseRepo.query_transform(query, fn [name, slug] ->
%{name: name, slug: slug}
Sanbase.ClickhouseRepo.query_transform(query, fn [name, project_slug] ->
%{name: name, slug: project_slug}
end)
end

defp get_founders_query() do
defp get_founders_query(nil) do
sql = """
SELECT name, slug
FROM founder_metadata
"""

Sanbase.Clickhouse.Query.new(sql, %{})
end

defp get_founders_query(slug) do
sql = """
SELECT name, slug
FROM founder_metadata
WHERE slug = {{slug}}
"""

Sanbase.Clickhouse.Query.new(sql, %{slug: slug})
end
end
6 changes: 4 additions & 2 deletions lib/sanbase_web/graphql/resolvers/metric/metric_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ defmodule SanbaseWeb.Graphql.Resolvers.MetricResolver do
end
end

def get_available_founders(_root, _args, %{source: %{metric: metric}}) do
def get_available_founders(_root, args, %{source: %{metric: metric}}) do
with {:ok, selectors} <- Metric.available_selectors(metric) do
case :founders in selectors do
true ->
with {:ok, data} <- Sanbase.Clickhouse.Founders.get_founders() do
slug = Map.get(args, :slug, nil)

with {:ok, data} <- Sanbase.Clickhouse.Founders.get_founders(slug) do
slugs = Enum.map(data, & &1.slug)
projects = Sanbase.Project.List.by_slugs(slugs)
slug_to_project_map = Map.new(projects, &{&1.slug, &1})
Expand Down
4 changes: 4 additions & 0 deletions lib/sanbase_web/graphql/schema/types/metric_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,10 @@ defmodule SanbaseWeb.Graphql.MetricTypes do
end

field :available_founders, list_of(:founder) do
@desc ~s"""
Filter the founders for which slug should be returned
"""
arg(:slug, :string, default_value: nil)
cache_resolve(&MetricResolver.get_available_founders/3)
end

Expand Down
44 changes: 43 additions & 1 deletion test/sanbase_web/graphql/metric/api_metric_metadata_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ defmodule SanbaseWeb.Graphql.ApiMetricMetadataTest do
|> json_response(200)
|> get_in(["data", "getMetric", "metadata", "availableFounders"])

assert %{"name" => "Vitalik Buterin", "project" => %{"name" => "Ethereum"}} in result
assert length(result) == 2
assert %{"name" => "Satoshi Nakamoto", "project" => %{"name" => "Bitcoin"}} in result
assert %{"name" => "Vitalik Buterin", "project" => %{"name" => "Ethereum"}} in result
end
end)

Expand All @@ -59,6 +60,47 @@ defmodule SanbaseWeb.Graphql.ApiMetricMetadataTest do
assert result == []
end

test "returns data for availableFounders with slug filter", %{conn: conn} do
metric_with_founders =
Metric.available_metrics()
|> Enum.filter(fn m ->
{:ok, selectors} = Metric.available_selectors(m)

:founders in selectors
end)
|> hd()

insert(:project, %{name: "Ethereum", ticker: "ETH", slug: "ethereum"})
insert(:project, %{name: "Bitcoin", ticker: "BTC", slug: "bitcoin"})

rows = [
["Satoshi Nakamoto", "bitcoin"]
]

query =
"""
{
getMetric(metric: "#{metric_with_founders}"){
metadata{
availableFounders(slug: "bitcoin"){ name project{ name } }
}
}
}
"""

Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:ok, %{rows: rows}})
|> Sanbase.Mock.run_with_mocks(fn ->
result =
conn
|> post("/graphql", query_skeleton(query))
|> json_response(200)
|> get_in(["data", "getMetric", "metadata", "availableFounders"])

assert length(result) == 1
assert %{"name" => "Satoshi Nakamoto", "project" => %{"name" => "Bitcoin"}} in result
end)
end

test "returns data for all available metric", %{conn: conn} do
metrics = Metric.available_metrics() |> Enum.shuffle()

Expand Down

0 comments on commit d7e93e1

Please sign in to comment.