diff --git a/lib/sanbase/balances/balance.ex b/lib/sanbase/balances/balance.ex index b66108efd..e0e0919a0 100644 --- a/lib/sanbase/balances/balance.ex +++ b/lib/sanbase/balances/balance.ex @@ -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 @@ -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, diff --git a/lib/sanbase/balances/balance_sql_query.ex b/lib/sanbase/balances/balance_sql_query.ex index 573452d23..8203f0fe3 100644 --- a/lib/sanbase/balances/balance_sql_query.ex +++ b/lib/sanbase/balances/balance_sql_query.ex @@ -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 diff --git a/lib/sanbase/metric/registry/populate.ex b/lib/sanbase/metric/registry/populate.ex index e37494cfd..620e56124 100644 --- a/lib/sanbase/metric/registry/populate.ex +++ b/lib/sanbase/metric/registry/populate.ex @@ -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)