diff --git a/apps/explorer/lib/explorer/chain/statistics.ex b/apps/explorer/lib/explorer/chain/statistics.ex index 5dc8d3ecee..cebe2975fe 100644 --- a/apps/explorer/lib/explorer/chain/statistics.ex +++ b/apps/explorer/lib/explorer/chain/statistics.ex @@ -13,10 +13,9 @@ defmodule Explorer.Chain.Statistics do @average_time_query """ SELECT coalesce(avg(difference), interval '0 seconds') FROM ( - SELECT timestamp - lag(timestamp) over (order by timestamp) as difference - FROM blocks - ORDER BY number DESC - LIMIT 100 + SELECT b.timestamp - lag(b.timestamp) over (order by b.timestamp) as difference + FROM (SELECT * FROM blocks ORDER BY number DESC LIMIT 101) b + LIMIT 100 OFFSET 1 ) t """ diff --git a/apps/explorer/test/explorer/chain/statistics_test.exs b/apps/explorer/test/explorer/chain/statistics_test.exs index 1837717604..78a5f90026 100644 --- a/apps/explorer/test/explorer/chain/statistics_test.exs +++ b/apps/explorer/test/explorer/chain/statistics_test.exs @@ -27,11 +27,14 @@ defmodule Explorer.Chain.StatisticsTest do assert Timex.diff(statistics.timestamp, time, :seconds) == 0 end - test "returns the average time between blocks" do + test "returns the average time between blocks for the last 100 blocks" do time = DateTime.utc_now() - next_time = Timex.shift(time, seconds: 5) - insert(:block, timestamp: time) - insert(:block, timestamp: next_time) + + insert(:block, timestamp: Timex.shift(time, seconds: -1000)) + + for x <- 100..0 do + insert(:block, timestamp: Timex.shift(time, seconds: -5 * x)) + end assert %Statistics{ average_time: %Duration{