From 44d1949050a91622c7707c095c74d6d6df89a758 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Thu, 12 Nov 2020 11:34:15 +0300 Subject: [PATCH 1/2] Correct avg time calculation --- CHANGELOG.md | 1 + .../explorer/counters/average_block_time.ex | 40 ++++++++++--------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d58e22b782..065a512002 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ ### 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 - [#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 diff --git a/apps/explorer/lib/explorer/counters/average_block_time.ex b/apps/explorer/lib/explorer/counters/average_block_time.ex index ab49650030..390a69398a 100644 --- a/apps/explorer/lib/explorer/counters/average_block_time.ex +++ b/apps/explorer/lib/explorer/counters/average_block_time.ex @@ -5,7 +5,7 @@ defmodule Explorer.Counters.AverageBlockTime do 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.Repo @@ -62,27 +62,28 @@ defmodule Explorer.Counters.AverageBlockTime do end 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 = if Application.get_env(:explorer, :include_uncles_in_average_block_time) do - from(block in Block, - limit: 100, - offset: 100, - order_by: [desc: block.number], - select: {block.number, block.timestamp} - ) + base_query else - from(block in Block, - limit: 100, - offset: 100, - order_by: [desc: block.number], - where: block.consensus == true, - select: {block.number, block.timestamp} - ) + base_query + |> where(consensus: true) end - timestamps = + timestamps_row = timestamps_query |> Repo.all() + + timestamps = + timestamps_row |> Enum.sort_by(fn {_, timestamp} -> timestamp end, &>=/2) |> Enum.map(fn {number, timestamp} -> {number, DateTime.to_unix(timestamp, :millisecond)} @@ -111,12 +112,13 @@ defmodule Explorer.Counters.AverageBlockTime do defp durations(timestamps) do 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 - duration = last_timestamp - timestamp - {[duration | durations], timestamp} + block_numbers_range = last_block_number - block_number + duration = (last_timestamp - timestamp) / block_numbers_range + {[duration | durations], block_number, timestamp} else - {durations, timestamp} + {durations, block_number, timestamp} end end) |> elem(0) From aab400dff004c79079e7d57487d473472a7b73e3 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Thu, 12 Nov 2020 13:15:55 +0300 Subject: [PATCH 2/2] Fix tests --- .../explorer/counters/average_block_time.ex | 11 ++++++++--- .../counters/average_block_time_test.exs | 18 +++++++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/apps/explorer/lib/explorer/counters/average_block_time.ex b/apps/explorer/lib/explorer/counters/average_block_time.ex index 390a69398a..74ab6d9ed4 100644 --- a/apps/explorer/lib/explorer/counters/average_block_time.ex +++ b/apps/explorer/lib/explorer/counters/average_block_time.ex @@ -103,7 +103,7 @@ defmodule Explorer.Counters.AverageBlockTime do {sum + duration, count + 1} end) - average = sum / count + average = if count == 0, do: 0, else: sum / count average |> round() @@ -115,8 +115,13 @@ defmodule Explorer.Counters.AverageBlockTime do |> Enum.reduce({[], nil, nil}, fn {block_number, timestamp}, {durations, last_block_number, last_timestamp} -> if last_timestamp do block_numbers_range = last_block_number - block_number - duration = (last_timestamp - timestamp) / block_numbers_range - {[duration | durations], block_number, 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 {durations, block_number, timestamp} end diff --git a/apps/explorer/test/explorer/counters/average_block_time_test.exs b/apps/explorer/test/explorer/counters/average_block_time_test.exs index 82db1f5af4..9d55664dcf 100644 --- a/apps/explorer/test/explorer/counters/average_block_time_test.exs +++ b/apps/explorer/test/explorer/counters/average_block_time_test.exs @@ -34,19 +34,27 @@ defmodule Explorer.Counters.AverageBlockTimeTest do 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 - 6)) + + insert(:block, + number: block_number + 1, + consensus: true, + timestamp: Timex.shift(first_timestamp, seconds: -100 - 3) + ) Enum.each(1..100, fn i -> insert(:block, - number: block_number + i, + number: block_number + 1 + i, consensus: true, - timestamp: Timex.shift(first_timestamp, seconds: -(101 - i) - 9) + timestamp: Timex.shift(first_timestamp, seconds: -(101 - i) - 12) ) end) - assert Repo.aggregate(Block, :count, :hash) == 103 + assert Repo.aggregate(Block, :count, :hash) == 104 AverageBlockTime.refresh()