Merge pull request #3449 from poanetwork/vb-avg-time-improvement

Correct avg time calculation
pull/3450/head
Victor Baranov 4 years ago committed by GitHub
commit 93198f2acc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 47
      apps/explorer/lib/explorer/counters/average_block_time.ex
  3. 18
      apps/explorer/test/explorer/counters/average_block_time_test.exs

@ -17,6 +17,7 @@
### Fixes ### Fixes
- [#3449](https://github.com/poanetwork/blockscout/pull/3449) - Correct avg time calculation
- [#3440](https://github.com/poanetwork/blockscout/pull/3440) - Rewrite missing blocks range query - [#3440](https://github.com/poanetwork/blockscout/pull/3440) - Rewrite missing blocks range query
- [#3439](https://github.com/poanetwork/blockscout/pull/3439) - Dark mode color fixes (search, charts) - [#3439](https://github.com/poanetwork/blockscout/pull/3439) - Dark mode color fixes (search, charts)
- [#3437](https://github.com/poanetwork/blockscout/pull/3437) - Fix Postgres Docker container - [#3437](https://github.com/poanetwork/blockscout/pull/3437) - Fix Postgres Docker container

@ -5,7 +5,7 @@ defmodule Explorer.Counters.AverageBlockTime do
Caches the number of token holders of a token. Caches the number of token holders of a token.
""" """
import Ecto.Query, only: [from: 2] import Ecto.Query, only: [from: 2, where: 2]
alias Explorer.Chain.Block alias Explorer.Chain.Block
alias Explorer.Repo alias Explorer.Repo
@ -62,27 +62,28 @@ defmodule Explorer.Counters.AverageBlockTime do
end end
defp refresh_timestamps do defp refresh_timestamps do
base_query =
from(block in Block,
limit: 100,
offset: 100,
order_by: [desc: block.number],
select: {block.number, block.timestamp}
)
timestamps_query = timestamps_query =
if Application.get_env(:explorer, :include_uncles_in_average_block_time) do if Application.get_env(:explorer, :include_uncles_in_average_block_time) do
from(block in Block, base_query
limit: 100,
offset: 100,
order_by: [desc: block.number],
select: {block.number, block.timestamp}
)
else else
from(block in Block, base_query
limit: 100, |> where(consensus: true)
offset: 100,
order_by: [desc: block.number],
where: block.consensus == true,
select: {block.number, block.timestamp}
)
end end
timestamps = timestamps_row =
timestamps_query timestamps_query
|> Repo.all() |> Repo.all()
timestamps =
timestamps_row
|> Enum.sort_by(fn {_, timestamp} -> timestamp end, &>=/2) |> Enum.sort_by(fn {_, timestamp} -> timestamp end, &>=/2)
|> Enum.map(fn {number, timestamp} -> |> Enum.map(fn {number, timestamp} ->
{number, DateTime.to_unix(timestamp, :millisecond)} {number, DateTime.to_unix(timestamp, :millisecond)}
@ -102,7 +103,7 @@ defmodule Explorer.Counters.AverageBlockTime do
{sum + duration, count + 1} {sum + duration, count + 1}
end) end)
average = sum / count average = if count == 0, do: 0, else: sum / count
average average
|> round() |> round()
@ -111,12 +112,18 @@ defmodule Explorer.Counters.AverageBlockTime do
defp durations(timestamps) do defp durations(timestamps) do
timestamps timestamps
|> Enum.reduce({[], nil}, fn {_, timestamp}, {durations, last_timestamp} -> |> Enum.reduce({[], nil, nil}, fn {block_number, timestamp}, {durations, last_block_number, last_timestamp} ->
if last_timestamp do if last_timestamp do
duration = last_timestamp - timestamp block_numbers_range = last_block_number - block_number
{[duration | durations], timestamp}
if block_numbers_range == 0 do
{durations, block_number, timestamp}
else
duration = (last_timestamp - timestamp) / block_numbers_range
{[duration | durations], block_number, timestamp}
end
else else
{durations, timestamp} {durations, block_number, timestamp}
end end
end) end)
|> elem(0) |> elem(0)

@ -34,19 +34,27 @@ defmodule Explorer.Counters.AverageBlockTimeTest do
first_timestamp = Timex.now() first_timestamp = Timex.now()
insert(:block, number: block_number, consensus: true, timestamp: Timex.shift(first_timestamp, seconds: -100 - 3)) insert(:block, number: block_number, consensus: true, timestamp: Timex.shift(first_timestamp, seconds: -100 - 6))
insert(:block, number: block_number, consensus: false, timestamp: Timex.shift(first_timestamp, seconds: -100 - 12))
insert(:block, number: block_number, consensus: false, timestamp: Timex.shift(first_timestamp, seconds: -100 - 9)) insert(:block, number: block_number, consensus: false, timestamp: Timex.shift(first_timestamp, seconds: -100 - 9))
insert(:block, number: block_number, consensus: false, timestamp: Timex.shift(first_timestamp, seconds: -100 - 6))
insert(:block,
number: block_number + 1,
consensus: true,
timestamp: Timex.shift(first_timestamp, seconds: -100 - 3)
)
Enum.each(1..100, fn i -> Enum.each(1..100, fn i ->
insert(:block, insert(:block,
number: block_number + i, number: block_number + 1 + i,
consensus: true, consensus: true,
timestamp: Timex.shift(first_timestamp, seconds: -(101 - i) - 9) timestamp: Timex.shift(first_timestamp, seconds: -(101 - i) - 12)
) )
end) end)
assert Repo.aggregate(Block, :count, :hash) == 103 assert Repo.aggregate(Block, :count, :hash) == 104
AverageBlockTime.refresh() AverageBlockTime.refresh()

Loading…
Cancel
Save