Fix divide-by-zero error in indexed_ratio

Dividing by `max_block_number` assumes it is 1-based, but it is 0-based,
so add 1.
pull/985/head
Luke Imhoff 6 years ago
parent e690c3219b
commit 21ace095ba
  1. 24
      apps/block_scout_web/test/block_scout_web/features/viewing_app_test.exs
  2. 4
      apps/explorer/lib/explorer/chain.ex
  3. 4
      apps/explorer/test/explorer/chain_test.exs

@ -7,50 +7,58 @@ defmodule BlockScoutWeb.ViewingAppTest do
describe "loading bar when indexing" do describe "loading bar when indexing" do
test "shows blocks indexed percentage", %{session: session} do test "shows blocks indexed percentage", %{session: session} do
for index <- 6..10 do for index <- 5..9 do
insert(:block, number: index) insert(:block, number: index)
end end
assert Explorer.Chain.indexed_ratio() == 0.5
session session
|> AppPage.visit_page() |> AppPage.visit_page()
|> assert_has(AppPage.indexed_status("50% Blocks Indexed")) |> assert_has(AppPage.indexed_status("50% Blocks Indexed"))
end end
test "shows tokens loading", %{session: session} do test "shows tokens loading", %{session: session} do
for index <- 1..10 do for index <- 0..9 do
insert(:block, number: index) insert(:block, number: index)
end end
assert Explorer.Chain.indexed_ratio() == 1.0
session session
|> AppPage.visit_page() |> AppPage.visit_page()
|> assert_has(AppPage.indexed_status("Indexing Tokens")) |> assert_has(AppPage.indexed_status("Indexing Tokens"))
end end
test "live updates blocks indexed percentage", %{session: session} do test "live updates blocks indexed percentage", %{session: session} do
for index <- 6..10 do for index <- 5..9 do
insert(:block, number: index) insert(:block, number: index)
end end
assert Explorer.Chain.indexed_ratio() == 0.5
session session
|> AppPage.visit_page() |> AppPage.visit_page()
|> assert_has(AppPage.indexed_status("50% Blocks Indexed")) |> assert_has(AppPage.indexed_status("50% Blocks Indexed"))
insert(:block, number: 5) insert(:block, number: 4)
Notifier.handle_event({:chain_event, :blocks, :catchup, []}) Notifier.handle_event({:chain_event, :blocks, :catchup, []})
assert_has(session, AppPage.indexed_status("60% Blocks Indexed")) assert_has(session, AppPage.indexed_status("60% Blocks Indexed"))
end end
test "live updates when blocks are fully indexed", %{session: session} do test "live updates when blocks are fully indexed", %{session: session} do
for index <- 2..10 do for index <- 1..9 do
insert(:block, number: index) insert(:block, number: index)
end end
assert Explorer.Chain.indexed_ratio() == 0.9
session session
|> AppPage.visit_page() |> AppPage.visit_page()
|> assert_has(AppPage.indexed_status("90% Blocks Indexed")) |> assert_has(AppPage.indexed_status("90% Blocks Indexed"))
insert(:block, number: 1) insert(:block, number: 0)
Notifier.handle_event({:chain_event, :blocks, :catchup, []}) Notifier.handle_event({:chain_event, :blocks, :catchup, []})
assert_has(session, AppPage.indexed_status("Indexing Tokens")) assert_has(session, AppPage.indexed_status("Indexing Tokens"))
@ -58,10 +66,12 @@ defmodule BlockScoutWeb.ViewingAppTest do
test "live removes message when chain is indexed", %{session: session} do test "live removes message when chain is indexed", %{session: session} do
[block | _] = [block | _] =
for index <- 1..10 do for index <- 0..9 do
insert(:block, number: index) insert(:block, number: index)
end end
assert Explorer.Chain.indexed_ratio() == 1.0
session session
|> AppPage.visit_page() |> AppPage.visit_page()
|> assert_has(AppPage.indexed_status("Indexing Tokens")) |> assert_has(AppPage.indexed_status("Indexing Tokens"))

@ -842,7 +842,7 @@ defmodule Explorer.Chain do
@doc """ @doc """
The percentage of indexed blocks on the chain. The percentage of indexed blocks on the chain.
iex> for index <- 6..10 do iex> for index <- 5..9 do
...> insert(:block, number: index) ...> insert(:block, number: index)
...> end ...> end
iex> Explorer.Chain.indexed_ratio() iex> Explorer.Chain.indexed_ratio()
@ -859,7 +859,7 @@ defmodule Explorer.Chain do
with {:ok, min_block_number} <- min_block_number(), with {:ok, min_block_number} <- min_block_number(),
{:ok, max_block_number} <- max_block_number() do {:ok, max_block_number} <- max_block_number() do
indexed_blocks = max_block_number - min_block_number + 1 indexed_blocks = max_block_number - min_block_number + 1
indexed_blocks / max_block_number indexed_blocks / (max_block_number + 1)
else else
{:error, _} -> 0 {:error, _} -> 0
end end

@ -820,7 +820,7 @@ defmodule Explorer.ChainTest do
describe "indexed_ratio/0" do describe "indexed_ratio/0" do
test "returns indexed ratio" do test "returns indexed ratio" do
for index <- 6..10 do for index <- 5..9 do
insert(:block, number: index) insert(:block, number: index)
end end
@ -832,7 +832,7 @@ defmodule Explorer.ChainTest do
end end
test "returns 1.0 if fully indexed blocks" do test "returns 1.0 if fully indexed blocks" do
for index <- 1..10 do for index <- 0..9 do
insert(:block, number: index) insert(:block, number: index)
end end

Loading…
Cancel
Save