load address counters async

pull/2787/head
Ayrat Badykov 5 years ago
parent a57cda266d
commit a32f4936c7
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 39
      apps/block_scout_web/assets/js/pages/address.js
  2. 36
      apps/block_scout_web/lib/block_scout_web/controllers/address_controller.ex
  3. 16
      apps/block_scout_web/lib/block_scout_web/templates/address/overview.html.eex
  4. 6
      apps/block_scout_web/lib/block_scout_web/views/address_view.ex

@ -34,6 +34,13 @@ export function reducer (state = initialState, action) {
channelDisconnected: true channelDisconnected: true
}) })
} }
case 'COUNTERS_FETCHED': {
console.log(action)
return Object.assign({}, state, {
transactionCount: action.transactionCount,
validationCount: action.validationCount
})
}
case 'RECEIVED_NEW_BLOCK': { case 'RECEIVED_NEW_BLOCK': {
if (state.channelDisconnected) return state if (state.channelDisconnected) return state
@ -81,8 +88,13 @@ const elements = {
return { transactionCount: numeral($el.text()).value() } return { transactionCount: numeral($el.text()).value() }
}, },
render ($el, state, oldState) { render ($el, state, oldState) {
if (oldState.transactionCount === state.transactionCount) return if (state.transactionCount) {
$el.empty().append(numeral(state.transactionCount).format()) if (oldState.transactionCount === state.transactionCount) return
$el.empty().append('>= ' + numeral(state.transactionCount).format() + ' Transactions')
$el.show()
} else {
$el.hide()
}
} }
}, },
'[data-selector="fetched-coin-balance-block-number"]': { '[data-selector="fetched-coin-balance-block-number"]': {
@ -99,12 +111,29 @@ const elements = {
return { validationCount: numeral($el.text()).value() } return { validationCount: numeral($el.text()).value() }
}, },
render ($el, state, oldState) { render ($el, state, oldState) {
if (oldState.validationCount === state.validationCount) return if (state.validationCount) {
$el.empty().append(numeral(state.validationCount).format()) if (oldState.validationCount === state.validationCount) return
$el.empty().append(numeral(state.validationCount).format() + ' Blocks Validated')
$el.show()
} else {
$el.hide()
}
} }
} }
} }
function loadCounters (store) {
const $element = $('[data-async-counters]')
const path = $element.data().asyncCounters
function fetchCounters () {
$.getJSON(path)
.done(response => store.dispatch(Object.assign({type: 'COUNTERS_FETCHED'}, humps.camelizeKeys(response))))
}
fetchCounters()
}
const $addressDetailsPage = $('[data-page="address-details"]') const $addressDetailsPage = $('[data-page="address-details"]')
if ($addressDetailsPage.length) { if ($addressDetailsPage.length) {
const store = createStore(reducer) const store = createStore(reducer)
@ -149,4 +178,6 @@ if ($addressDetailsPage.length) {
type: 'RECEIVED_UPDATED_BALANCE', type: 'RECEIVED_UPDATED_BALANCE',
msg: humps.camelizeKeys(msg) msg: humps.camelizeKeys(msg)
})) }))
loadCounters(store)
} }

@ -5,7 +5,6 @@ defmodule BlockScoutWeb.AddressController do
alias BlockScoutWeb.AddressView alias BlockScoutWeb.AddressView
alias Explorer.{Chain, Market} alias Explorer.{Chain, Market}
alias Explorer.Chain.Hash
alias Explorer.ExchangeRates.Token alias Explorer.ExchangeRates.Token
alias Phoenix.View alias Phoenix.View
@ -69,26 +68,25 @@ defmodule BlockScoutWeb.AddressController do
end end
def address_counters(conn, %{"id" => address_hash_string}) do def address_counters(conn, %{"id" => address_hash_string}) do
case Chain.string_to_address_hash(address_hash_string) do with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, address_hash} -> {:ok, address} <- Chain.hash_to_address(address_hash) do
{transaction_count, validation_count} = transaction_and_validation_count(address_hash) {transaction_count, validation_count} = transaction_and_validation_count(address)
json(conn, %{transaction_count: transaction_count, validation_count: validation_count}) json(conn, %{transaction_count: transaction_count, validation_count: validation_count})
else
_ -> _ -> not_found(conn)
not_found(conn)
end end
end end
defp transaction_and_validation_count(%Hash{byte_count: unquote(Hash.Address.byte_count())} = address_hash) do defp transaction_and_validation_count(address) do
transaction_count_task = transaction_count_task =
Task.async(fn -> Task.async(fn ->
transaction_count(address_hash) transaction_count(address)
end) end)
validation_count_task = validation_count_task =
Task.async(fn -> Task.async(fn ->
validation_count(address_hash) validation_count(address)
end) end)
[transaction_count_task, validation_count_task] [transaction_count_task, validation_count_task]
@ -108,11 +106,19 @@ defmodule BlockScoutWeb.AddressController do
|> List.to_tuple() |> List.to_tuple()
end end
defp transaction_count(%Hash{byte_count: unquote(Hash.Address.byte_count())} = address_hash) do defp transaction_count(address) do
Chain.total_transactions_sent_by_address(address_hash) if contract?(address) do
Chain.address_to_incoming_transaction_count(address.hash)
else
Chain.total_transactions_sent_by_address(address.hash)
end
end end
defp validation_count(%Hash{byte_count: unquote(Hash.Address.byte_count())} = address_hash) do defp validation_count(address) do
Chain.address_to_validation_count(address_hash) Chain.address_to_validation_count(address.hash)
end end
def contract?(%{contract_code: nil}), do: false
def contract?(%{contract_code: _}), do: true
end end

@ -71,18 +71,8 @@
<% end %> <% end %>
<span> <span>
<span class="address-detail-item"> <span class="address-detail-item">
<%= if contract?(@address) do %> <span data-selector="transaction-count">
<%= gettext(">=") %> </span>
<span data-selector="transaction-count">
<%= incoming_transaction_count(@address.hash) %>
</span>
<%= gettext("Incoming Transactions") %>
<% else %>
<span data-selector="transaction-count">
<%= BlockScoutWeb.Cldr.Number.to_string!(0, format: "#,###") %>
</span>
<%= gettext("Transactions Sent") %>
<% end %>
</span> </span>
<%= if @address.fetched_coin_balance_block_number do %> <%= if @address.fetched_coin_balance_block_number do %>
<span class="address-detail-item"> <span class="address-detail-item">
@ -92,9 +82,7 @@
<span class="address-detail-item"> <span class="address-detail-item">
<span data-selector="validation-count"> <span data-selector="validation-count">
<%= BlockScoutWeb.Cldr.Number.to_string!(0, format: "#,###") %>
</span> </span>
<%= gettext("Blocks Validated") %>
</span> </span>
</span> </span>
</div> </div>

@ -219,12 +219,6 @@ defmodule BlockScoutWeb.AddressView do
def token_title(%Token{name: name, symbol: symbol}), do: "#{name} (#{symbol})" def token_title(%Token{name: name, symbol: symbol}), do: "#{name} (#{symbol})"
def incoming_transaction_count(address_hash) do
address_hash
|> Chain.address_to_incoming_transaction_count()
|> BlockScoutWeb.Cldr.Number.to_string!(format: "#,###")
end
def trimmed_hash(%Hash{} = hash) do def trimmed_hash(%Hash{} = hash) do
string_hash = to_string(hash) string_hash = to_string(hash)
"#{String.slice(string_hash, 0..5)}#{String.slice(string_hash, -6..-1)}" "#{String.slice(string_hash, 0..5)}#{String.slice(string_hash, -6..-1)}"

Loading…
Cancel
Save