Merge pull request #7096 from blockscout/vb-hide-alert

Hide indexing alert, if indexer disabled
pull/7113/head
Victor Baranov 2 years ago committed by GitHub
commit 85de713799
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 4
      apps/block_scout_web/lib/block_scout_web/controllers/api/v2/main_page_controller.ex
  3. 16
      apps/block_scout_web/lib/block_scout_web/counters/blocks_indexed_counter.ex
  4. 6
      apps/block_scout_web/lib/block_scout_web/counters/internal_transactions_indexed_counter.ex
  5. 20
      apps/block_scout_web/lib/block_scout_web/notifier.ex
  6. 8
      apps/block_scout_web/lib/block_scout_web/templates/layout/app.html.eex
  7. 118
      apps/explorer/lib/explorer/chain.ex
  8. 8
      apps/explorer/test/explorer/chain_test.exs

@ -9,6 +9,7 @@
### Fixes ### Fixes
- [#7096](https://github.com/blockscout/blockscout/pull/7096) - Hide indexing alert, if indexer disabled
- [#7091](https://github.com/blockscout/blockscout/pull/7091) - Fix custom ABI - [#7091](https://github.com/blockscout/blockscout/pull/7091) - Fix custom ABI
- [#7062](https://github.com/blockscout/blockscout/pull/7062) - Save block count in the DB when calculated in Cache module - [#7062](https://github.com/blockscout/blockscout/pull/7062) - Save block count in the DB when calculated in Cache module
- [#7008](https://github.com/blockscout/blockscout/pull/7008) - Fetch image/video content from IPFS link - [#7008](https://github.com/blockscout/blockscout/pull/7008) - Fetch image/video content from IPFS link

@ -41,11 +41,11 @@ defmodule BlockScoutWeb.API.V2.MainPageController do
def indexing_status(conn, _params) do def indexing_status(conn, _params) do
indexed_ratio_blocks = Chain.indexed_ratio_blocks() 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, %{ json(conn, %{
finished_indexing_blocks: finished_indexing_blocks, 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_blocks_ratio: indexed_ratio_blocks,
indexed_internal_transactions_ratio: if(finished_indexing_blocks, do: Chain.indexed_ratio_internal_transactions()) 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 @impl true
def init(args) do def init(args) do
if @enabled do if @enabled do
Task.start_link(&calculate_blocks_indexed/0) Task.start_link(&calculate_blocks_indexed_and_broadcast/0)
schedule_next_consolidation() schedule_next_consolidation()
end end
@ -35,21 +35,19 @@ defmodule BlockScoutWeb.Counters.BlocksIndexedCounter do
{:ok, args} {:ok, args}
end end
def calculate_blocks_indexed do def calculate_blocks_indexed_and_broadcast do
indexed_ratio_blocks = Chain.indexed_ratio_blocks() ratio = Chain.indexed_ratio_blocks()
finished? = Chain.finished_indexing?(indexed_ratio_blocks) Notifier.broadcast_indexed_ratio("blocks:indexing", ratio)
Notifier.broadcast_blocks_indexed_ratio(indexed_ratio_blocks, finished?)
end end
defp schedule_next_consolidation do 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 end
@impl true @impl true
def handle_info(:calculate_blocks_indexed, state) do def handle_info(:calculate_blocks_indexed_and_broadcast, state) do
calculate_blocks_indexed() calculate_blocks_indexed_and_broadcast()
schedule_next_consolidation() schedule_next_consolidation()

@ -36,11 +36,9 @@ defmodule BlockScoutWeb.Counters.InternalTransactionsIndexedCounter do
end end
def calculate_internal_transactions_indexed do 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_indexed_ratio("blocks:indexing_internal_transactions", ratio)
Notifier.broadcast_internal_transactions_indexed_ratio(indexed_ratio_internal_transactions, finished?)
end end
defp schedule_next_consolidation do 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 do: Map.has_key?(params, "verification_type") && Map.get(params, "verification_type") == type
@doc """ @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 @spec broadcast_indexed_ratio(String.t(), Decimal.t()) ::
Endpoint.broadcast("blocks:indexing", "index_status", %{ :ok | {:error, term()}
def broadcast_indexed_ratio(msg, ratio) do
Endpoint.broadcast(msg, "index_status", %{
ratio: Decimal.to_string(ratio), ratio: Decimal.to_string(ratio),
finished: finished? finished: Chain.finished_indexing_from_ratio?(ratio)
})
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?
}) })
end end

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

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

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

Loading…
Cancel
Save