From 5644e68debaa9237783606f14389f637d7c98c9e Mon Sep 17 00:00:00 2001 From: jimmay5469 Date: Thu, 7 Jun 2018 15:33:30 -0400 Subject: [PATCH] Make count function names more explicit --- apps/explorer/lib/explorer/chain.ex | 43 ++++++++++--------- .../pending_transaction_controller.ex | 4 +- .../controllers/transaction_controller.ex | 4 +- .../pending_transaction/index.html.eex | 2 +- .../templates/transaction/index.html.eex | 2 +- .../pending_transaction_controller_test.exs | 2 +- .../transaction_controller_test.exs | 6 +-- 7 files changed, 32 insertions(+), 31 deletions(-) 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) %>

diff --git a/apps/explorer_web/lib/explorer_web/templates/transaction/index.html.eex b/apps/explorer_web/lib/explorer_web/templates/transaction/index.html.eex index 44ea0a3436..aea640d2c7 100644 --- a/apps/explorer_web/lib/explorer_web/templates/transaction/index.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/transaction/index.html.eex @@ -3,7 +3,7 @@ Transactions

- <%= gettext("Showing %{count} Validated Transactions", count: @transaction_count) %> + <%= gettext("Showing %{count} Validated Transactions", count: @transaction_estimated_count) %>

diff --git a/apps/explorer_web/test/explorer_web/controllers/pending_transaction_controller_test.exs b/apps/explorer_web/test/explorer_web/controllers/pending_transaction_controller_test.exs index 8640090bd3..0fdbe4edaa 100644 --- a/apps/explorer_web/test/explorer_web/controllers/pending_transaction_controller_test.exs +++ b/apps/explorer_web/test/explorer_web/controllers/pending_transaction_controller_test.exs @@ -45,7 +45,7 @@ defmodule ExplorerWeb.PendingTransactionControllerTest do conn = get(conn, pending_transaction_path(ExplorerWeb.Endpoint, :index, :en)) assert html_response(conn, 200) - assert 1 == conn.assigns.transaction_count + assert 1 == conn.assigns.pending_transaction_count end test "paginates transactions using the last seen transaction", %{conn: conn} do diff --git a/apps/explorer_web/test/explorer_web/controllers/transaction_controller_test.exs b/apps/explorer_web/test/explorer_web/controllers/transaction_controller_test.exs index cf3d7d5f6e..eec2512a95 100644 --- a/apps/explorer_web/test/explorer_web/controllers/transaction_controller_test.exs +++ b/apps/explorer_web/test/explorer_web/controllers/transaction_controller_test.exs @@ -23,7 +23,7 @@ defmodule ExplorerWeb.TransactionControllerTest do conn = get(conn, "/en/transactions") - assert is_integer(conn.assigns.transaction_count) + assert is_integer(conn.assigns.transaction_estimated_count) end test "excludes pending transactions", %{conn: conn} do @@ -81,13 +81,13 @@ defmodule ExplorerWeb.TransactionControllerTest do conn = get(conn, "/en/transactions") - refute conn.assigns.transaction_count == nil + refute conn.assigns.transaction_estimated_count == nil end test "works when there are no transactions", %{conn: conn} do conn = get(conn, "/en/transactions") - assert conn.assigns.transaction_count == 0 + assert conn.assigns.transaction_estimated_count == 0 assert conn.assigns.transactions == [] end end