fix js chart

pull/2538/head
Ayrat Badykov 5 years ago
parent 6fb93c0cdf
commit b6ba44d1a1
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 10
      apps/block_scout_web/assets/js/lib/coin_balance_history_chart.js
  2. 9
      apps/explorer/lib/explorer/chain.ex
  3. 21
      apps/explorer/lib/explorer/chain/address/coin_balance.ex
  4. 12
      apps/explorer/test/explorer/chain/address/coin_balance_test.exs

@ -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: [{

@ -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

@ -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)

@ -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)

Loading…
Cancel
Save