diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index c80931e18d..47b8037148 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -1302,6 +1302,24 @@ defmodule Explorer.Chain do end end + @doc """ + Count of pending `t:Explorer.Chain.Transaction.t/0`. + + A count of all pending transactions. + + iex> insert(:transaction) + iex> :transaction |> insert() |> with_block() + iex> Explorer.Chain.pending_transaction_count() + 1 + + """ + @spec pending_transaction_count() :: non_neg_integer() + def pending_transaction_count do + Transaction + |> where([transaction], is_nil(transaction.block_hash)) + |> Repo.aggregate(:count, :hash) + end + @doc """ Returns the paged list of collated transactions that occurred recently from newest to oldest using `block_number` and `index`. @@ -1494,35 +1512,18 @@ defmodule Explorer.Chain do end @doc """ - Count of `t:Explorer.Chain.Transaction.t/0`. - - With :all both collated and pending transactions will be counted using estimates from table statistics. - - iex> insert(:transaction) - iex> :transaction |> insert() |> with_block() - iex> Explorer.Chain.transaction_count(:pending) - 1 - - ## Arguments - - * `:all` - returns an estimated count of all collated and pending transactions using table statistics - * `:pending` - returns a count of all pending transactions + Estimated count of `t:Explorer.Chain.Transaction.t/0`. + Estimated count of both collated and pending transactions using the transactions table statistics. """ - @spec transaction_count(:all | :pending) :: non_neg_integer() - def transaction_count(:all) do + @spec transaction_estimated_count() :: non_neg_integer() + def transaction_estimated_count do %Postgrex.Result{rows: [[rows]]} = SQL.query!(Repo, "SELECT reltuples::BIGINT AS estimate FROM pg_class WHERE relname='transactions'") rows end - def transaction_count(:pending) do - Transaction - |> where([transaction], is_nil(transaction.block_hash)) - |> Repo.aggregate(:count, :hash) - end - @doc """ `t:Explorer.Chain.InternalTransaction/0`s in `t:Explorer.Chain.Transaction.t/0` with `hash`. diff --git a/apps/explorer_web/lib/explorer_web/controllers/pending_transaction_controller.ex b/apps/explorer_web/lib/explorer_web/controllers/pending_transaction_controller.ex index 9fa35f82fe..19fc582cb6 100644 --- a/apps/explorer_web/lib/explorer_web/controllers/pending_transaction_controller.ex +++ b/apps/explorer_web/lib/explorer_web/controllers/pending_transaction_controller.ex @@ -16,13 +16,13 @@ defmodule ExplorerWeb.PendingTransactionController do full_options = Keyword.merge([necessity_by_association: %{from_address: :optional, to_address: :optional}], options) transactions = Chain.recent_pending_transactions(full_options) last_seen_pending_inserted_at = last_seen_pending_inserted_at(transactions.entries) - transaction_count = Chain.transaction_count(:pending) + pending_transaction_count = Chain.pending_transaction_count() render( conn, "index.html", last_seen_pending_inserted_at: last_seen_pending_inserted_at, - transaction_count: transaction_count, + pending_transaction_count: pending_transaction_count, transactions: transactions ) end diff --git a/apps/explorer_web/lib/explorer_web/controllers/transaction_controller.ex b/apps/explorer_web/lib/explorer_web/controllers/transaction_controller.ex index 4019d4a072..4a61846923 100644 --- a/apps/explorer_web/lib/explorer_web/controllers/transaction_controller.ex +++ b/apps/explorer_web/lib/explorer_web/controllers/transaction_controller.ex @@ -36,13 +36,13 @@ defmodule ExplorerWeb.TransactionController do ) transactions = Chain.recent_collated_transactions(full_options) - transaction_count = Chain.transaction_count(:all) + transaction_estimated_count = Chain.transaction_estimated_count() render( conn, "index.html", earliest: earliest(transactions), - transaction_count: transaction_count, + transaction_estimated_count: transaction_estimated_count, transactions: transactions ) end diff --git a/apps/explorer_web/lib/explorer_web/templates/pending_transaction/index.html.eex b/apps/explorer_web/lib/explorer_web/templates/pending_transaction/index.html.eex index 650c41ace2..59feb1cebe 100644 --- a/apps/explorer_web/lib/explorer_web/templates/pending_transaction/index.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/pending_transaction/index.html.eex @@ -3,7 +3,7 @@ Transactions
- <%= gettext("Showing %{count} Pending Transactions", count: @transaction_count) %> + <%= gettext("Showing %{count} Pending Transactions", count: @pending_transaction_count) %>
- <%= gettext("Showing %{count} Validated Transactions", count: @transaction_count) %> + <%= gettext("Showing %{count} Validated Transactions", count: @transaction_estimated_count) %>