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
@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`.

@ -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

@ -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

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

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

@ -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

@ -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

Loading…
Cancel
Save