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. 35
      apps/block_scout_web/assets/js/pages/address.js
  2. 34
      apps/block_scout_web/lib/block_scout_web/controllers/address_controller.ex
  3. 12
      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
})
}
case 'COUNTERS_FETCHED': {
console.log(action)
return Object.assign({}, state, {
transactionCount: action.transactionCount,
validationCount: action.validationCount
})
}
case 'RECEIVED_NEW_BLOCK': {
if (state.channelDisconnected) return state
@ -81,8 +88,13 @@ const elements = {
return { transactionCount: numeral($el.text()).value() }
},
render ($el, state, oldState) {
if (state.transactionCount) {
if (oldState.transactionCount === state.transactionCount) return
$el.empty().append(numeral(state.transactionCount).format())
$el.empty().append('>= ' + numeral(state.transactionCount).format() + ' Transactions')
$el.show()
} else {
$el.hide()
}
}
},
'[data-selector="fetched-coin-balance-block-number"]': {
@ -99,12 +111,29 @@ const elements = {
return { validationCount: numeral($el.text()).value() }
},
render ($el, state, oldState) {
if (state.validationCount) {
if (oldState.validationCount === state.validationCount) return
$el.empty().append(numeral(state.validationCount).format())
$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"]')
if ($addressDetailsPage.length) {
const store = createStore(reducer)
@ -149,4 +178,6 @@ if ($addressDetailsPage.length) {
type: 'RECEIVED_UPDATED_BALANCE',
msg: humps.camelizeKeys(msg)
}))
loadCounters(store)
}

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

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

@ -219,12 +219,6 @@ defmodule BlockScoutWeb.AddressView do
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
string_hash = to_string(hash)
"#{String.slice(string_hash, 0..5)}#{String.slice(string_hash, -6..-1)}"

Loading…
Cancel
Save