Make count function names more explicit

pull/256/head
jimmay5469 7 years ago
parent 7166ce4216
commit 5644e68deb
  1. 43
      apps/explorer/lib/explorer/chain.ex
  2. 4
      apps/explorer_web/lib/explorer_web/controllers/pending_transaction_controller.ex
  3. 4
      apps/explorer_web/lib/explorer_web/controllers/transaction_controller.ex
  4. 2
      apps/explorer_web/lib/explorer_web/templates/pending_transaction/index.html.eex
  5. 2
      apps/explorer_web/lib/explorer_web/templates/transaction/index.html.eex
  6. 2
      apps/explorer_web/test/explorer_web/controllers/pending_transaction_controller_test.exs
  7. 6
      apps/explorer_web/test/explorer_web/controllers/transaction_controller_test.exs

@ -1302,6 +1302,24 @@ defmodule Explorer.Chain do
end end
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 """ @doc """
Returns the paged list of collated transactions that occurred recently from newest to oldest using `block_number` Returns the paged list of collated transactions that occurred recently from newest to oldest using `block_number`
and `index`. and `index`.
@ -1494,35 +1512,18 @@ defmodule Explorer.Chain do
end end
@doc """ @doc """
Count of `t:Explorer.Chain.Transaction.t/0`. Estimated 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 both collated and pending transactions using the transactions table statistics.
""" """
@spec transaction_count(:all | :pending) :: non_neg_integer() @spec transaction_estimated_count() :: non_neg_integer()
def transaction_count(:all) do def transaction_estimated_count do
%Postgrex.Result{rows: [[rows]]} = %Postgrex.Result{rows: [[rows]]} =
SQL.query!(Repo, "SELECT reltuples::BIGINT AS estimate FROM pg_class WHERE relname='transactions'") SQL.query!(Repo, "SELECT reltuples::BIGINT AS estimate FROM pg_class WHERE relname='transactions'")
rows rows
end end
def transaction_count(:pending) do
Transaction
|> where([transaction], is_nil(transaction.block_hash))
|> Repo.aggregate(:count, :hash)
end
@doc """ @doc """
`t:Explorer.Chain.InternalTransaction/0`s in `t:Explorer.Chain.Transaction.t/0` with `hash`. `t:Explorer.Chain.InternalTransaction/0`s in `t:Explorer.Chain.Transaction.t/0` with `hash`.

@ -16,13 +16,13 @@ defmodule ExplorerWeb.PendingTransactionController do
full_options = Keyword.merge([necessity_by_association: %{from_address: :optional, to_address: :optional}], options) full_options = Keyword.merge([necessity_by_association: %{from_address: :optional, to_address: :optional}], options)
transactions = Chain.recent_pending_transactions(full_options) transactions = Chain.recent_pending_transactions(full_options)
last_seen_pending_inserted_at = last_seen_pending_inserted_at(transactions.entries) 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( render(
conn, conn,
"index.html", "index.html",
last_seen_pending_inserted_at: last_seen_pending_inserted_at, last_seen_pending_inserted_at: last_seen_pending_inserted_at,
transaction_count: transaction_count, pending_transaction_count: pending_transaction_count,
transactions: transactions transactions: transactions
) )
end end

@ -36,13 +36,13 @@ defmodule ExplorerWeb.TransactionController do
) )
transactions = Chain.recent_collated_transactions(full_options) transactions = Chain.recent_collated_transactions(full_options)
transaction_count = Chain.transaction_count(:all) transaction_estimated_count = Chain.transaction_estimated_count()
render( render(
conn, conn,
"index.html", "index.html",
earliest: earliest(transactions), earliest: earliest(transactions),
transaction_count: transaction_count, transaction_estimated_count: transaction_estimated_count,
transactions: transactions transactions: transactions
) )
end end

@ -3,7 +3,7 @@
Transactions Transactions
</h1> </h1>
<p> <p>
<%= gettext("Showing %{count} Pending Transactions", count: @transaction_count) %> <%= gettext("Showing %{count} Pending Transactions", count: @pending_transaction_count) %>
</p> </p>
<div class="card"> <div class="card">

@ -3,7 +3,7 @@
Transactions Transactions
</h1> </h1>
<p> <p>
<%= gettext("Showing %{count} Validated Transactions", count: @transaction_count) %> <%= gettext("Showing %{count} Validated Transactions", count: @transaction_estimated_count) %>
</p> </p>
<div class="card"> <div class="card">

@ -45,7 +45,7 @@ defmodule ExplorerWeb.PendingTransactionControllerTest do
conn = get(conn, pending_transaction_path(ExplorerWeb.Endpoint, :index, :en)) conn = get(conn, pending_transaction_path(ExplorerWeb.Endpoint, :index, :en))
assert html_response(conn, 200) assert html_response(conn, 200)
assert 1 == conn.assigns.transaction_count assert 1 == conn.assigns.pending_transaction_count
end end
test "paginates transactions using the last seen transaction", %{conn: conn} do test "paginates transactions using the last seen transaction", %{conn: conn} do

@ -23,7 +23,7 @@ defmodule ExplorerWeb.TransactionControllerTest do
conn = get(conn, "/en/transactions") conn = get(conn, "/en/transactions")
assert is_integer(conn.assigns.transaction_count) assert is_integer(conn.assigns.transaction_estimated_count)
end end
test "excludes pending transactions", %{conn: conn} do test "excludes pending transactions", %{conn: conn} do
@ -81,13 +81,13 @@ defmodule ExplorerWeb.TransactionControllerTest do
conn = get(conn, "/en/transactions") conn = get(conn, "/en/transactions")
refute conn.assigns.transaction_count == nil refute conn.assigns.transaction_estimated_count == nil
end end
test "works when there are no transactions", %{conn: conn} do test "works when there are no transactions", %{conn: conn} do
conn = get(conn, "/en/transactions") conn = get(conn, "/en/transactions")
assert conn.assigns.transaction_count == 0 assert conn.assigns.transaction_estimated_count == 0
assert conn.assigns.transactions == [] assert conn.assigns.transactions == []
end end
end end

Loading…
Cancel
Save