Refactoring indexing ratio related functions

pull/7096/head
Viktor Baranov 2 years ago
parent b5b6f553c8
commit 8bfd6324d1
  1. 4
      apps/block_scout_web/lib/block_scout_web/controllers/api/v2/main_page_controller.ex
  2. 16
      apps/block_scout_web/lib/block_scout_web/counters/blocks_indexed_counter.ex
  3. 6
      apps/block_scout_web/lib/block_scout_web/counters/internal_transactions_indexed_counter.ex
  4. 20
      apps/block_scout_web/lib/block_scout_web/notifier.ex
  5. 14
      apps/block_scout_web/lib/block_scout_web/templates/layout/app.html.eex
  6. 106
      apps/explorer/lib/explorer/chain.ex
  7. 8
      apps/explorer/test/explorer/chain_test.exs

@ -41,11 +41,11 @@ defmodule BlockScoutWeb.API.V2.MainPageController do
def indexing_status(conn, _params) do
indexed_ratio_blocks = Chain.indexed_ratio_blocks()
finished_indexing_blocks = Chain.finished_blocks_indexing?(indexed_ratio_blocks)
finished_indexing_blocks = Chain.finished_indexing_from_ratio?(indexed_ratio_blocks)
json(conn, %{
finished_indexing_blocks: finished_indexing_blocks,
finished_indexing: Chain.finished_indexing?(indexed_ratio_blocks, api?: true),
finished_indexing: Chain.finished_indexing?(api?: true),
indexed_blocks_ratio: indexed_ratio_blocks,
indexed_internal_transactions_ratio: if(finished_indexing_blocks, do: Chain.indexed_ratio_internal_transactions())
})

@ -27,7 +27,7 @@ defmodule BlockScoutWeb.Counters.BlocksIndexedCounter do
@impl true
def init(args) do
if @enabled do
Task.start_link(&calculate_blocks_indexed/0)
Task.start_link(&calculate_blocks_indexed_and_broadcast/0)
schedule_next_consolidation()
end
@ -35,21 +35,19 @@ defmodule BlockScoutWeb.Counters.BlocksIndexedCounter do
{:ok, args}
end
def calculate_blocks_indexed do
indexed_ratio_blocks = Chain.indexed_ratio_blocks()
def calculate_blocks_indexed_and_broadcast do
ratio = Chain.indexed_ratio_blocks()
finished? = Chain.finished_indexing?(indexed_ratio_blocks)
Notifier.broadcast_blocks_indexed_ratio(indexed_ratio_blocks, finished?)
Notifier.broadcast_indexed_ratio("blocks:indexing", ratio)
end
defp schedule_next_consolidation do
Process.send_after(self(), :calculate_blocks_indexed, :timer.minutes(5))
Process.send_after(self(), :calculate_blocks_indexed_and_broadcast, :timer.minutes(5))
end
@impl true
def handle_info(:calculate_blocks_indexed, state) do
calculate_blocks_indexed()
def handle_info(:calculate_blocks_indexed_and_broadcast, state) do
calculate_blocks_indexed_and_broadcast()
schedule_next_consolidation()

@ -36,11 +36,9 @@ defmodule BlockScoutWeb.Counters.InternalTransactionsIndexedCounter do
end
def calculate_internal_transactions_indexed do
indexed_ratio_internal_transactions = Chain.indexed_ratio_internal_transactions()
ratio = Chain.indexed_ratio_internal_transactions()
finished? = Chain.finished_indexing?(indexed_ratio_internal_transactions)
Notifier.broadcast_internal_transactions_indexed_ratio(indexed_ratio_internal_transactions, finished?)
Notifier.broadcast_indexed_ratio("blocks:indexing_internal_transactions", ratio)
end
defp schedule_next_consolidation do

@ -264,22 +264,14 @@ defmodule BlockScoutWeb.Notifier do
do: Map.has_key?(params, "verification_type") && Map.get(params, "verification_type") == type
@doc """
Broadcast the percentage of blocks indexed so far.
Broadcast the percentage of blocks or pending block operations indexed so far.
"""
def broadcast_blocks_indexed_ratio(ratio, finished?) do
Endpoint.broadcast("blocks:indexing", "index_status", %{
@spec broadcast_indexed_ratio(String.t(), Decimal.t()) ::
:ok | {:error, term()}
def broadcast_indexed_ratio(msg, ratio) do
Endpoint.broadcast(msg, "index_status", %{
ratio: Decimal.to_string(ratio),
finished: finished?
})
end
@doc """
Broadcast the percentage of pending block operations indexed so far.
"""
def broadcast_internal_transactions_indexed_ratio(ratio, finished?) do
Endpoint.broadcast("blocks:indexing_internal_transactions", "index_status", %{
ratio: Decimal.to_string(ratio),
finished: finished?
finished: Chain.finished_indexing_from_ratio?(ratio)
})
end

@ -72,13 +72,13 @@
<%= raw(System.get_env("MAINTENANCE_ALERT_MESSAGE")) %>
</div>
<% end %>
<% indexed_ratio_blocks = Explorer.Chain.indexed_ratio_blocks() %>
<% indexed_ratio =
case Chain.finished_blocks_indexing?(indexed_ratio_blocks) do
false -> indexed_ratio_blocks
_ -> Explorer.Chain.indexed_ratio_internal_transactions()
end %>
<%= if not Explorer.Chain.finished_indexing?(indexed_ratio_blocks) do %>
<%= if not Chain.finished_indexing?() do %>
<% indexed_ratio_blocks = Chain.indexed_ratio_blocks() %>
<% indexed_ratio =
case Chain.finished_indexing_from_ratio?(indexed_ratio_blocks) do
false -> indexed_ratio_blocks
_ -> Chain.indexed_ratio_internal_transactions()
end %>
<div class="alert alert-warning text-center mb-0 p-3" data-seindexed_ratiolector="indexed-status">
<%= render BlockScoutWeb.CommonComponentsView, "_loading_spinner.html" %>
<span data-indexed-ratio-blocks="<%= indexed_ratio_blocks %>" data-indexed-ratio="<%= indexed_ratio %>"></span>

@ -1275,8 +1275,8 @@ defmodule Explorer.Chain do
Checks to see if the chain is down indexing based on the transaction from the
oldest block and the pending operation
"""
@spec finished_internal_transactions_indexing?([api?]) :: boolean()
def finished_internal_transactions_indexing?(options \\ []) do
@spec finished_indexing_internal_transactions?([api?]) :: boolean()
def finished_indexing_internal_transactions?(options \\ []) do
internal_transactions_disabled? = System.get_env("INDEXER_DISABLE_INTERNAL_TRANSACTIONS_FETCHER", "false") == "true"
if internal_transactions_disabled? do
@ -1312,19 +1312,21 @@ defmodule Explorer.Chain do
end
end
def finished_blocks_indexing?(indexed_ratio_blocks) do
Decimal.compare(indexed_ratio_blocks, 1) !== :lt
def finished_indexing_from_ratio?(ratio) do
Decimal.compare(ratio, 1) !== :lt
end
@doc """
Checks if indexing of blocks and internal transactions finished aka full indexing
"""
@spec finished_indexing?(Decimal.t(), [api?]) :: boolean()
def finished_indexing?(indexed_ratio_blocks, options \\ []) do
@spec finished_indexing?([api?]) :: boolean()
def finished_indexing?(options \\ []) do
if Application.get_env(:indexer, Indexer.Supervisor)[:enabled] do
case finished_blocks_indexing?(indexed_ratio_blocks) do
indexed_ratio = indexed_ratio_blocks()
case finished_indexing_from_ratio?(indexed_ratio) do
false -> false
_ -> finished_internal_transactions_indexing?(options)
_ -> finished_indexing_internal_transactions?(options)
end
else
true
@ -2185,58 +2187,66 @@ defmodule Explorer.Chain do
"""
@spec indexed_ratio_blocks() :: Decimal.t()
def indexed_ratio_blocks do
%{min: min, max: max} = BlockNumber.get_all()
if Application.get_env(:indexer, Indexer.Supervisor)[:enabled] do
%{min: min, max: max} = BlockNumber.get_all()
min_blockchain_block_number =
case Integer.parse(Application.get_env(:indexer, :first_block)) do
{block_number, _} -> block_number
_ -> 0
end
min_blockchain_block_number =
case Integer.parse(Application.get_env(:indexer, :first_block)) do
{block_number, _} -> block_number
_ -> 0
end
case {min, max} do
{0, 0} ->
Decimal.new(0)
case {min, max} do
{0, 0} ->
Decimal.new(0)
_ ->
result =
BlockCache.estimated_count()
|> Decimal.div(max - min_blockchain_block_number + 1)
|> (&if(
(Decimal.compare(&1, Decimal.from_float(0.99)) == :gt ||
Decimal.compare(&1, Decimal.from_float(0.99)) == :eq) &&
min <= min_blockchain_block_number,
do: Decimal.new(1),
else: &1
)).()
result
|> Decimal.round(2, :down)
|> Decimal.min(Decimal.new(1))
_ ->
result =
BlockCache.estimated_count()
|> Decimal.div(max - min_blockchain_block_number + 1)
|> (&if(
(Decimal.compare(&1, Decimal.from_float(0.99)) == :gt ||
Decimal.compare(&1, Decimal.from_float(0.99)) == :eq) &&
min <= min_blockchain_block_number,
do: Decimal.new(1),
else: &1
)).()
result
|> Decimal.round(2, :down)
|> Decimal.min(Decimal.new(1))
end
else
Decimal.new(1)
end
end
@spec indexed_ratio_internal_transactions() :: Decimal.t()
def indexed_ratio_internal_transactions do
%{max: max} = BlockNumber.get_all()
count = Repo.aggregate(PendingBlockOperation, :count, timeout: :infinity)
if Application.get_env(:indexer, Indexer.Supervisor)[:enabled] do
%{max: max} = BlockNumber.get_all()
count = Repo.aggregate(PendingBlockOperation, :count, timeout: :infinity)
min_blockchain_trace_block_number =
case Integer.parse(Application.get_env(:indexer, :trace_first_block)) do
{block_number, _} -> block_number
_ -> 0
end
min_blockchain_trace_block_number =
case Integer.parse(Application.get_env(:indexer, :trace_first_block)) do
{block_number, _} -> block_number
_ -> 0
end
case max do
0 ->
Decimal.new(0)
case max do
0 ->
Decimal.new(0)
_ ->
full_blocks_range = max - min_blockchain_trace_block_number + 1
result = Decimal.div(full_blocks_range - count, full_blocks_range)
_ ->
full_blocks_range = max - min_blockchain_trace_block_number + 1
result = Decimal.div(full_blocks_range - count, full_blocks_range)
result
|> Decimal.round(2, :down)
|> Decimal.min(Decimal.new(1))
result
|> Decimal.round(2, :down)
|> Decimal.min(Decimal.new(1))
end
else
Decimal.new(1)
end
end

@ -1206,7 +1206,7 @@ defmodule Explorer.ChainTest do
end
end
describe "finished_internal_transactions_indexing?/0" do
describe "finished_indexing_internal_transactions?/0" do
test "finished indexing" do
block = insert(:block, number: 1)
@ -1214,11 +1214,11 @@ defmodule Explorer.ChainTest do
|> insert()
|> with_block(block)
assert Chain.finished_internal_transactions_indexing?()
assert Chain.finished_indexing_internal_transactions?()
end
test "finished indexing (no txs)" do
assert Chain.finished_internal_transactions_indexing?()
assert Chain.finished_indexing_internal_transactions?()
end
test "not finished indexing" do
@ -1230,7 +1230,7 @@ defmodule Explorer.ChainTest do
insert(:pending_block_operation, block: block, block_number: block.number)
refute Chain.finished_internal_transactions_indexing?()
refute Chain.finished_indexing_internal_transactions?()
end
end

Loading…
Cancel
Save