Skip to content

Commit

Permalink
Reuse historical_balance to compute historical_balance_changes
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanIvanoff committed Jan 28, 2025
1 parent 327bea3 commit 44f636f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 92 deletions.
57 changes: 20 additions & 37 deletions lib/sanbase/balances/balance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,26 @@ defmodule Sanbase.Balance do
|> List.wrap()
|> Sanbase.BlockchainAddress.to_internal_format()

do_historical_balance_changes(
addresses,
slug,
decimals,
blockchain,
from,
to,
interval
)
# Shift `from` with one interval back, as the computation of balance change
# for `from` datetime requires knowing the balance at `from` minus `interval`
# datetime. Furthermore, the consecutive differences computation reduces the
# number of elements by 1, so the extension by 1 interval also addresses this.
from = DateTime.add(from, -Sanbase.DateTimeUtils.str_to_sec(interval))

case do_historical_balance(addresses, slug, decimals, blockchain, from, to, interval) do
{:ok, balances} ->
changes =
balances
|> Enum.chunk_every(2, 1, :discard)
|> Enum.map(fn [%{balance: previous}, %{balance: current, datetime: dt}] ->
%{datetime: dt, balance: current - previous}
end)

{:ok, changes}

{:error, error} ->
{:error, error}
end
end
end

Expand Down Expand Up @@ -373,34 +384,6 @@ defmodule Sanbase.Balance do
end)
end

defp do_historical_balance_changes(
addresses,
slug,
decimals,
blockchain,
from,
to,
interval
) do
query_struct =
historical_balance_changes_query(
addresses,
slug,
decimals,
blockchain,
from,
to,
interval
)

ClickhouseRepo.query_transform(query_struct, fn [unix, balance_change] ->
%{
datetime: DateTime.from_unix!(unix),
balance_change_amount: balance_change
}
end)
end

defp do_last_balance_before(
address_or_addresses,
slug,
Expand Down
54 changes: 0 additions & 54 deletions lib/sanbase/balances/balance_sql_query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,60 +26,6 @@ defmodule Sanbase.Balance.SqlQuery do
def decimals("dogecoin", _), do: 1
def decimals(_, decimals), do: Integer.pow(10, decimals)

def historical_balance_changes_query(
addresses,
slug,
decimals,
blockchain,
from,
to,
interval
) do
sql = """
SELECT time, SUM(change) / {{decimals}}
FROM (
SELECT
toUnixTimestamp(intDiv(toUInt32({{from}} + number * {{interval}}), {{interval}}) * {{interval}}) AS time,
toFloat64(0) AS change
FROM numbers({{span}})
UNION ALL
SELECT
toUnixTimestamp(intDiv(toUInt32(dt), {{interval}}) * {{interval}}) AS time,
SUM(change) AS change
FROM (
SELECT dt, address, (argMax(balance, (dt, txID, computedAt)) - argMax(oldBalance, (dt, txID, computedAt))) AS change
FROM {{table}}
WHERE
#{maybe_selector_clause(blockchain, slug, "slug")}
#{address_clause(addresses, argument_name: "addresses")} AND
dt >= toDateTime({{from}}) AND
dt <= toDateTime({{to}})
GROUP BY address, dt
)
GROUP BY address, dt
)
GROUP BY time
ORDER BY time
"""

{from, to, interval, span} = timerange_parameters(from, to, interval)

params = %{
addresses: addresses,
slug: slug,
decimals: decimals(blockchain, decimals),
from: from,
to: to,
interval: interval,
span: span,
table: blockchain_to_table(blockchain, slug)
}

Sanbase.Clickhouse.Query.new(sql, params)
end

def balance_change_query(addresses, slug, decimals, blockchain, from, to) do
sql = """
SELECT
Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase/metric/registry/populate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ defmodule Sanbase.Metric.Registry.Populate do
|> Sanbase.Metric.Registry.changeset(params)
end

def populate(opts \\ []) do
defp populate(opts \\ []) do
case process_metrics() do
list when is_list(list) ->
{:ok, list, summary} = summarize_results(list)
Expand Down

0 comments on commit 44f636f

Please sign in to comment.