|
|
|
@ -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 |
|
|
|
|
timestamps_query = |
|
|
|
|
if Application.get_env(:explorer, :include_uncles_in_average_block_time) 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 |
|
|
|
|
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)} |
|
|
|
@ -102,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() |
|
|
|
@ -111,12 +112,18 @@ 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 |
|
|
|
|
|
|
|
|
|
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, timestamp} |
|
|
|
|
{durations, block_number, timestamp} |
|
|
|
|
end |
|
|
|
|
end) |
|
|
|
|
|> elem(0) |
|
|
|
|