Skip to content

Commit

Permalink
Migrate to Clickhouse labeling framework v2 dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanIvanoff committed Nov 27, 2023
1 parent 337e556 commit e5f7bd0
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Sanbase.BlockchainAddress.BlockchainAddressLabelChange.SqlQuery do
SELECT
toUnixTimestamp(dt),
address,
dictGetString('default.labels_dict', 'fqn', label_id) AS label_fqn,
dictGetString('default.labels', 'fqn', label_id) AS label_fqn,
sign
FROM address_label_changes
PREWHERE
Expand Down
1 change: 1 addition & 0 deletions lib/sanbase/clickhouse/exchange_address.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ defmodule Sanbase.Clickhouse.ExchangeAddress do
WHERE #{exchange_type_filter(:both)}
AND blockchain = {{blockchain}}
) USING address
LIMIT {{limit}}
"""

params = %{
Expand Down
8 changes: 4 additions & 4 deletions lib/sanbase/clickhouse/label/label.ex
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ defmodule Sanbase.Clickhouse.Label do

def addresses_by_label_fqns_query(label_fqns, nil = _blockchain) do
sql = """
SELECT address, blockchain, dictGetString('default.labels_dict', 'fqn', label_id) AS label_fqn
SELECT address, blockchain, dictGetString('default.labels', 'fqn', label_id) AS label_fqn
FROM label_addresses
PREWHERE
#{label_id_by_label_fqn_filter(label_fqns, argument_name: "label_fqns")}
Expand All @@ -173,7 +173,7 @@ defmodule Sanbase.Clickhouse.Label do

def addresses_by_label_fqns_query(label_fqns, blockchain) do
sql = """
SELECT address, blockchain, dictGetString('default.labels_dict', 'fqn', label_id) AS label_fqn
SELECT address, blockchain, dictGetString('default.labels', 'fqn', label_id) AS label_fqn
FROM label_addresses
PREWHERE
#{label_id_by_label_fqn_filter(label_fqns, argument_name: "label_fqns")} AND
Expand All @@ -188,7 +188,7 @@ defmodule Sanbase.Clickhouse.Label do

def addresses_by_label_keys_query(label_keys, nil = _blockchain) do
sql = """
SELECT address, blockchain, dictGetString('default.labels_dict', 'fqn', label_id) AS label_fqn
SELECT address, blockchain, dictGetString('default.labels', 'fqn', label_id) AS label_fqn
FROM label_addresses
PREWHERE
#{label_id_by_label_key_filter(label_keys, argument_name: "label_keys")}
Expand All @@ -202,7 +202,7 @@ defmodule Sanbase.Clickhouse.Label do

def addresses_by_label_keys_query(label_keys, blockchain) do
sql = """
SELECT address, blockchain, dictGetString('default.labels_dict', 'fqn', label_id) AS label_fqn
SELECT address, blockchain, dictGetString('default.labels', 'fqn', label_id) AS label_fqn
FROM label_addresses
PREWHERE
#{label_id_by_label_key_filter(label_keys, argument_name: "label_keys")} AND
Expand Down
4 changes: 2 additions & 2 deletions lib/sanbase/clickhouse/nft/nft_trade.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ defmodule Sanbase.Clickhouse.NftTrade do

defp fetch_label_query(contract, blockchain, field) do
sql = """
SELECT dictGet('default.labels_dict', '#{field}', label_id)
SELECT dictGet('default.labels', '#{field}', label_id)
FROM
(
SELECT labels
FROM default.current_labels
WHERE (blockchain = {{blockchain}}) AND (address = lower({{contract}}))
)
ARRAY JOIN labels AS label_id
WHERE dictGet('default.labels_dict', 'key', label_id) = 'name'
WHERE dictGet('default.labels', 'key', label_id) = 'name'
"""

params = %{blockchain: blockchain, contract: contract}
Expand Down
8 changes: 4 additions & 4 deletions lib/sanbase/metric/sql_query_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,14 @@ defmodule Sanbase.Metric.SqlQuery.Helper do
def label_id_by_label_fqn_filter(label_fqn, opts) when is_binary(label_fqn) do
arg_name = Keyword.fetch!(opts, :argument_name)

"label_id = dictGetUInt64('default.label_ids_dict', 'label_id', tuple({{#{arg_name}}}))"
"label_id = dictGetUInt64('default.labels_by_fqn', 'label_id', tuple({{#{arg_name}}}))"
end

def label_id_by_label_fqn_filter(label_fqns, opts) when is_list(label_fqns) do
arg_name = Keyword.fetch!(opts, :argument_name)

"label_id IN (
SELECT dictGetUInt64('default.label_ids_dict', 'label_id', tuple(fqn)) AS label_id
SELECT dictGetUInt64('default.labels_by_fqn', 'label_id', tuple(fqn)) AS label_id
FROM system.one
ARRAY JOIN [{{#{arg_name}}}] AS fqn
)"
Expand Down Expand Up @@ -305,7 +305,7 @@ defmodule Sanbase.Metric.SqlQuery.Helper do
label_fqn_key = "label_fqn_#{pos}"

str = "label_id IN (
SELECT dictGetUInt64('default.label_ids_dict', 'label_id', tuple(fqn)) AS label_id
SELECT dictGetUInt64('default.labels_by_fqn', 'label_id', tuple(fqn)) AS label_id
FROM system.one
ARRAY JOIN [{{#{label_fqn_key}}}] AS fqn
)"
Expand All @@ -318,7 +318,7 @@ defmodule Sanbase.Metric.SqlQuery.Helper do
label_fqn_key = "label_fqn_#{pos}"

str =
"label_id = dictGetUInt64('default.label_ids_dict', 'label_id', tuple({{#{label_fqn_key}}}))"
"label_id = dictGetUInt64('default.labels_by_fqn', 'label_id', tuple({{#{label_fqn_key}}}))"

{str, Map.put(params, label_fqn_key, value)}
end
Expand Down
1 change: 0 additions & 1 deletion test/sanbase/billing/query_access_level_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ defmodule Sanbase.Billing.QueryAccessLevelTest do
:api_metric_distribution,
:assets_held_by_address,
:blockchain_address_label_changes,
:blockchain_address_labels,
:blockchain_address_transaction_volume_over_time,
:blockchain_address_user_pair,
:blockchain_address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,6 @@ defmodule SanbaseWeb.Graphql.BlockchainAddressApiTest do
}
end

test "fetch all blockchain address labels with blockchainAddressLabelsApi", context do
rows = [["miner"], ["centralized_exchange"], ["decentralized_exchange"]]

Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:ok, %{rows: rows}})
|> Sanbase.Mock.run_with_mocks(fn ->
result =
blockchain_address_labels(context.conn)
|> get_in(["data", "blockchainAddressLabels"])

assert result == ["miner", "centralized_exchange", "decentralized_exchange"]
end)
end

test "fetch blockchain address labels for a given blockchain with blockchainAddressLabelsApi",
context do
rows = [["centralized_exchange"], ["decentralized_exchange"]]

Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:ok, %{rows: rows}})
|> Sanbase.Mock.run_with_mocks(fn ->
result =
blockchain_address_labels(context.conn, blockchain: "ethereum")
|> get_in(["data", "blockchainAddressLabels"])

assert result == ["centralized_exchange", "decentralized_exchange"]
end)
end

test "fetch blockchain address labels with getBlockchainAddressLabels API", context do
rows = [["santiment/miner:v1", "Miner"], ["santiment/owner->Coinbase:v1", "owner->Coinbase"]]

Expand Down Expand Up @@ -266,18 +239,6 @@ defmodule SanbaseWeb.Graphql.BlockchainAddressApiTest do
|> json_response(200)
end

defp blockchain_address_labels(conn, opts \\ []) do
query =
case Keyword.get(opts, :blockchain) do
nil -> "{ blockchainAddressLabels }"
blockchain -> ~s/{ blockchainAddressLabels(blockchain: "#{blockchain}") }/
end

conn
|> post("/graphql", query_skeleton(query))
|> json_response(200)
end

defp add_blockchain_address_labels_mutation(selector, labels) do
"""
mutation {
Expand Down

0 comments on commit e5f7bd0

Please sign in to comment.