From b6ba44d1a11d1fab7d8fab16c3d7873979788878 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 9 Aug 2019 15:17:02 +0300 Subject: [PATCH] fix js chart --- .../js/lib/coin_balance_history_chart.js | 10 ++++++++- apps/explorer/lib/explorer/chain.ex | 9 +++++--- .../explorer/chain/address/coin_balance.ex | 21 +++++++++++++++++-- .../chain/address/coin_balance_test.exs | 12 +++++++---- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/apps/block_scout_web/assets/js/lib/coin_balance_history_chart.js b/apps/block_scout_web/assets/js/lib/coin_balance_history_chart.js index ec41d57a4e..a3bd66b867 100644 --- a/apps/block_scout_web/assets/js/lib/coin_balance_history_chart.js +++ b/apps/block_scout_web/assets/js/lib/coin_balance_history_chart.js @@ -18,6 +18,14 @@ export function createCoinBalanceHistoryChart (el) { y: balance.value })) + var step_size = 3 + + if (data.length > 2) { + console.log(data[0].date) + var diff = Math.abs(new Date(data[0].date) - new Date(data[1].date)); + var period_in_days = diff / (1000 * 60 * 60 * 24) + step_size = period_in_days + } return new Chart(el, { type: 'line', data: { @@ -36,7 +44,7 @@ export function createCoinBalanceHistoryChart (el) { type: 'time', time: { unit: 'day', - stepSize: 3 + stepSize: step_size } }], yAxes: [{ diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index aa1ab8aae1..8cec885e03 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -66,8 +66,6 @@ defmodule Explorer.Chain do @max_incoming_transactions_count 10_000 - @coin_balance_records_count 90 - @typedoc """ The name of an association on the `t:Ecto.Schema.t/0` """ @@ -2977,8 +2975,13 @@ defmodule Explorer.Chain do @spec address_to_balances_by_day(Hash.Address.t()) :: [balance_by_day] def address_to_balances_by_day(address_hash) do + latest_block_timestamp = + address_hash + |> CoinBalance.last_coin_balance_timestamp() + |> Repo.one() + address_hash - |> CoinBalance.balances_by_day(@coin_balance_records_count) + |> CoinBalance.balances_by_day(latest_block_timestamp) |> Repo.all() |> normalize_balances_by_day() end diff --git a/apps/explorer/lib/explorer/chain/address/coin_balance.ex b/apps/explorer/lib/explorer/chain/address/coin_balance.ex index 6402b31b80..8d7b47e8da 100644 --- a/apps/explorer/lib/explorer/chain/address/coin_balance.ex +++ b/apps/explorer/lib/explorer/chain/address/coin_balance.ex @@ -89,16 +89,33 @@ defmodule Explorer.Chain.Address.CoinBalance do Builds an `Ecto.Query` to fetch a series of balances by day for the given account. Each element in the series corresponds to the maximum balance in that day. Only the last 90 days of data are used. """ - def balances_by_day(address_hash, number \\ 60) do + def balances_by_day(address_hash, block_timestamp \\ nil) do CoinBalance |> join(:inner, [cb], b in Block, on: cb.block_number == b.number) |> where([cb], cb.address_hash == ^address_hash) + |> limit_time_interval(block_timestamp) |> group_by([cb, b], fragment("date_trunc('day', ?)", b.timestamp)) |> order_by([cb, b], fragment("date_trunc('day', ?)", b.timestamp)) |> select([cb, b], %{date: type(fragment("date_trunc('day', ?)", b.timestamp), :date), value: max(cb.value)}) - |> limit(^number) end + def limit_time_interval(query, nil) do + query |> where([cb, b], b.timestamp >= fragment("date_trunc('day', now()) - interval '90 days'")) + end + + def limit_time_interval(query, %{timestamp: timestamp}) do + query |> where([cb, b], b.timestamp >= fragment("(? AT TIME ZONE ?) - interval '90 days'", ^timestamp, ^"Etc/UTC")) + end + + def last_coin_balance_timestamp(address_hash) do + CoinBalance + |> join(:inner, [cb], b in Block, on: cb.block_number == b.number) + |> where([cb], cb.address_hash == ^address_hash) + |> last(:block_number) + |> select([cb, b], %{timestamp: b.timestamp}) + end + + def changeset(%__MODULE__{} = balance, params) do balance |> cast(params, @allowed_fields) diff --git a/apps/explorer/test/explorer/chain/address/coin_balance_test.exs b/apps/explorer/test/explorer/chain/address/coin_balance_test.exs index a5ce968b39..171a2764c0 100644 --- a/apps/explorer/test/explorer/chain/address/coin_balance_test.exs +++ b/apps/explorer/test/explorer/chain/address/coin_balance_test.exs @@ -277,17 +277,21 @@ defmodule Explorer.Chain.Address.CoinBalanceTest do test "fetches old records" do address = insert(:address) noon = Timex.now() |> Timex.beginning_of_day() |> Timex.set(hour: 12) - block = insert(:block, timestamp: noon) + old_block = insert(:block, timestamp: Timex.shift(noon, days: -700)) - insert(:fetched_balance, address_hash: address.hash, value: 1000, block_number: block.number) insert(:fetched_balance, address_hash: address.hash, value: 2000, block_number: old_block.number) + latest_block_timestamp = + address.hash + |> CoinBalance.last_coin_balance_timestamp() + |> Repo.one() + result = address.hash - |> CoinBalance.balances_by_day() + |> CoinBalance.balances_by_day(latest_block_timestamp) |> Repo.all() - assert(length(result) == 2) + assert(length(result) == 1) value = result |> List.first() |> Map.get(:value)