Gas usage at main page

pull/3385/head
Victor Baranov 4 years ago
parent 143a0cb4ad
commit 011ee7e46e
  1. 20
      apps/block_scout_web/assets/css/components/_custom_tooltips.scss
  2. 10
      apps/block_scout_web/assets/js/pages/chain.js
  3. 2
      apps/block_scout_web/lib/block_scout_web/controllers/chain_controller.ex
  4. 21
      apps/block_scout_web/lib/block_scout_web/templates/chain/show.html.eex
  5. 4
      apps/block_scout_web/lib/block_scout_web/views/bridged_tokens_view.ex
  6. 18
      apps/block_scout_web/priv/gettext/default.pot
  7. 18
      apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po
  8. 2
      apps/explorer/lib/explorer/application.ex
  9. 25
      apps/explorer/lib/explorer/chain.ex
  10. 65
      apps/explorer/lib/explorer/chain/cache/gas_usage.ex

@ -52,11 +52,25 @@ $tooltip-background-color: $btn-line-color !default;
} }
} }
.token-bridge-market-cap-header { .tooltip-gas-usage {
.tooltip-inner {
@media (min-width: 576px) {
max-width: 150px !important;
}
@media (min-width: 768px) {
max-width: 150px !important;
}
width: 150px !important;
}
}
.custom-tooltip-header {
font-size: 1.2em; font-size: 1.2em;
margin-bottom: 10px; margin-bottom: 10px;
} }
.token-bridge-market-cap-description { .custom-tooltip-description {
text-align: left; .left {
text-align: left;
}
} }

@ -29,6 +29,7 @@ export const initialState = {
transactionsError: false, transactionsError: false,
transactionsLoading: true, transactionsLoading: true,
transactionCount: null, transactionCount: null,
totalGasUsageCount: null,
usdMarketCap: null, usdMarketCap: null,
blockCount: null blockCount: null
} }
@ -185,6 +186,15 @@ const elements = {
$el.empty().append(numeral(state.transactionCount).format()) $el.empty().append(numeral(state.transactionCount).format())
} }
}, },
'[data-selector="total-gas-usage"]': {
load ($el) {
return { totalGasUsageCount: numeral($el.text()).value() }
},
render ($el, state, oldState) {
if (oldState.totalGasUsageCount === state.totalGasUsageCount) return
$el.empty().append(numeral(state.totalGasUsageCount).format())
}
},
'[data-selector="block-count"]': { '[data-selector="block-count"]': {
load ($el) { load ($el) {
return { blockCount: numeral($el.text()).value() } return { blockCount: numeral($el.text()).value() }

@ -13,6 +13,7 @@ defmodule BlockScoutWeb.ChainController do
def show(conn, _params) do def show(conn, _params) do
transaction_estimated_count = Chain.transaction_estimated_count() transaction_estimated_count = Chain.transaction_estimated_count()
total_gas_usage = Chain.total_gas_usage()
block_count = Chain.block_estimated_count() block_count = Chain.block_estimated_count()
address_count = Chain.address_estimated_count() address_count = Chain.address_estimated_count()
@ -50,6 +51,7 @@ defmodule BlockScoutWeb.ChainController do
chart_data_paths: chart_data_paths, chart_data_paths: chart_data_paths,
market_cap_calculation: market_cap_calculation, market_cap_calculation: market_cap_calculation,
transaction_estimated_count: transaction_estimated_count, transaction_estimated_count: transaction_estimated_count,
total_gas_usage: total_gas_usage,
transactions_path: recent_transactions_path(conn, :index), transactions_path: recent_transactions_path(conn, :index),
transaction_stats: transaction_stats, transaction_stats: transaction_stats,
block_count: block_count, block_count: block_count,

@ -74,7 +74,7 @@
data-placement="top" data-placement="top"
data-html="true" data-html="true"
data-template="<div class='tooltip tooltip-inversed-color tooltip-market-cap' role='tooltip'><div class='arrow'></div><div class='tooltip-inner'></div></div>" data-template="<div class='tooltip tooltip-inversed-color tooltip-market-cap' role='tooltip'><div class='arrow'></div><div class='tooltip-inner'></div></div>"
title="<div class='token-bridge-market-cap-header'><b><%= format_usd_value(total_market_cap) %></b> is a sum of assets locked in TokenBridge and OmniBridge</div><div class='token-bridge-market-cap-description'><b><%= format_usd_value(token_bridge_market_cap) %></b> locked in Dai in TokenBridge <br/><b><%= format_usd_value(omni_bridge_market_cap) %></b> locked in different assets in OmniBridge</div>"> title="<div class='custom-tooltip-header'><b><%= format_usd_value(total_market_cap) %></b> is a sum of assets locked in TokenBridge and OmniBridge</div><div class='custom-tooltip-description left'><b><%= format_usd_value(token_bridge_market_cap) %></b> locked in Dai in TokenBridge <br/><b><%= format_usd_value(omni_bridge_market_cap) %></b> locked in different assets in OmniBridge</div>">
<i style="color: #ffffff;" class="fa fa-info-circle ml-1" data-test="token-bridge-supply"></i> <i style="color: #ffffff;" class="fa fa-info-circle ml-1" data-test="token-bridge-supply"></i>
</span> </span>
<% end %> <% end %>
@ -118,9 +118,22 @@
<span class="dashboard-banner-network-stats-label"> <span class="dashboard-banner-network-stats-label">
<%= gettext "Total transactions" %> <%= gettext "Total transactions" %>
</span> </span>
<span class="dashboard-banner-network-stats-value" data-selector="transaction-count"> <div style="display: flex;">
<%= BlockScoutWeb.Cldr.Number.to_string!(@transaction_estimated_count, format: "#,###") %> <span class="dashboard-banner-network-stats-value" data-selector="transaction-count">
</span> <%= BlockScoutWeb.Cldr.Number.to_string!(@transaction_estimated_count, format: "#,###") %>
</span>
<%= if @total_gas_usage > 0 do %>
<span
data-toggle="tooltip"
data-placement="top"
data-html="true"
data-template="<div class='tooltip tooltip-inversed-color tooltip-gas-usage' role='tooltip'><div class='arrow'></div><div class='tooltip-inner'></div></div>"
title="<div class='custom-tooltip-header'>Total gas used</div><div class='custom-tooltip-description'><b><%= BlockScoutWeb.Cldr.Number.to_string!(@total_gas_usage, format: "#,###") %><b></div>"
style="margin-top:8px;">
<i style="color: #ffffff;" class="fa fa-info-circle ml-2"></i>
</span>
<% end %>
</div>
</div> </div>
<div class="dashboard-banner-network-stats-item dashboard-banner-network-stats-item-3"> <div class="dashboard-banner-network-stats-item dashboard-banner-network-stats-item-3">
<span class="dashboard-banner-network-stats-label"> <span class="dashboard-banner-network-stats-label">

@ -22,10 +22,10 @@ defmodule BlockScoutWeb.BridgedTokensView do
end end
def owl_token_amb_info do def owl_token_amb_info do
"<div class='token-bridge-market-cap-header'>OWL token bridged through AMB extension with support of <i>burnOWL</i> method. It is recommended to use.</div>" "<div class='custom-tooltip-header'>OWL token bridged through AMB extension with support of <i>burnOWL</i> method. It is recommended to use.</div>"
end end
def owl_token_omni_info do def owl_token_omni_info do
"<div class='token-bridge-market-cap-header'>OWL token bridged through OmniBridge without support of <i>burnOWL</i> method. It is not recommended to use.</div>" "<div class='custom-tooltip-header'>OWL token bridged through OmniBridge without support of <i>burnOWL</i> method. It is not recommended to use.</div>"
end end
end end

@ -236,7 +236,7 @@ msgid "BlockScout provides analytics data, API, and Smart Contract tools for the
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:152 #: lib/block_scout_web/templates/chain/show.html.eex:165
#: lib/block_scout_web/templates/layout/_topnav.html.eex:34 #: lib/block_scout_web/templates/layout/_topnav.html.eex:34
#: lib/block_scout_web/templates/layout/_topnav.html.eex:38 #: lib/block_scout_web/templates/layout/_topnav.html.eex:38
msgid "Blocks" msgid "Blocks"
@ -1065,7 +1065,7 @@ msgid "More internal transactions have come in"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:213 #: lib/block_scout_web/templates/chain/show.html.eex:226
#: lib/block_scout_web/templates/pending_transaction/index.html.eex:12 #: lib/block_scout_web/templates/pending_transaction/index.html.eex:12
#: lib/block_scout_web/templates/transaction/index.html.eex:18 #: lib/block_scout_web/templates/transaction/index.html.eex:18
msgid "More transactions have come in" msgid "More transactions have come in"
@ -1309,7 +1309,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_transaction/index.html.eex:48 #: lib/block_scout_web/templates/address_transaction/index.html.eex:48
#: lib/block_scout_web/templates/address_validation/index.html.eex:22 #: lib/block_scout_web/templates/address_validation/index.html.eex:22
#: lib/block_scout_web/templates/block_transaction/index.html.eex:23 #: lib/block_scout_web/templates/block_transaction/index.html.eex:23
#: lib/block_scout_web/templates/chain/show.html.eex:156 #: lib/block_scout_web/templates/chain/show.html.eex:169
#: lib/block_scout_web/templates/pending_transaction/index.html.eex:21 #: lib/block_scout_web/templates/pending_transaction/index.html.eex:21
#: lib/block_scout_web/templates/stakes/_table.html.eex:44 #: lib/block_scout_web/templates/stakes/_table.html.eex:44
#: lib/block_scout_web/templates/tokens/holder/index.html.eex:22 #: lib/block_scout_web/templates/tokens/holder/index.html.eex:22
@ -1324,7 +1324,7 @@ msgid "Something went wrong, click to reload."
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:219 #: lib/block_scout_web/templates/chain/show.html.eex:232
msgid "Something went wrong, click to retry." msgid "Something went wrong, click to retry."
msgstr "" msgstr ""
@ -1453,7 +1453,7 @@ msgid "Total Supply"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:127 #: lib/block_scout_web/templates/chain/show.html.eex:140
msgid "Total blocks" msgid "Total blocks"
msgstr "" msgstr ""
@ -1593,12 +1593,12 @@ msgid "Version"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:151 #: lib/block_scout_web/templates/chain/show.html.eex:164
msgid "View All Blocks" msgid "View All Blocks"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:209 #: lib/block_scout_web/templates/chain/show.html.eex:222
msgid "View All Transactions" msgid "View All Transactions"
msgstr "" msgstr ""
@ -1640,7 +1640,7 @@ msgid "WEI"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:135 #: lib/block_scout_web/templates/chain/show.html.eex:148
msgid "Wallet addresses" msgid "Wallet addresses"
msgstr "" msgstr ""
@ -1867,7 +1867,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_transaction/index.html.eex:15 #: lib/block_scout_web/templates/address_transaction/index.html.eex:15
#: lib/block_scout_web/templates/block_transaction/index.html.eex:10 #: lib/block_scout_web/templates/block_transaction/index.html.eex:10
#: lib/block_scout_web/templates/block_transaction/index.html.eex:18 #: lib/block_scout_web/templates/block_transaction/index.html.eex:18
#: lib/block_scout_web/templates/chain/show.html.eex:210 #: lib/block_scout_web/templates/chain/show.html.eex:223
#: lib/block_scout_web/templates/layout/_topnav.html.eex:53 #: lib/block_scout_web/templates/layout/_topnav.html.eex:53
#: lib/block_scout_web/views/address_view.ex:349 #: lib/block_scout_web/views/address_view.ex:349
msgid "Transactions" msgid "Transactions"

@ -236,7 +236,7 @@ msgid "BlockScout provides analytics data, API, and Smart Contract tools for the
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:152 #: lib/block_scout_web/templates/chain/show.html.eex:165
#: lib/block_scout_web/templates/layout/_topnav.html.eex:34 #: lib/block_scout_web/templates/layout/_topnav.html.eex:34
#: lib/block_scout_web/templates/layout/_topnav.html.eex:38 #: lib/block_scout_web/templates/layout/_topnav.html.eex:38
msgid "Blocks" msgid "Blocks"
@ -1065,7 +1065,7 @@ msgid "More internal transactions have come in"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:213 #: lib/block_scout_web/templates/chain/show.html.eex:226
#: lib/block_scout_web/templates/pending_transaction/index.html.eex:12 #: lib/block_scout_web/templates/pending_transaction/index.html.eex:12
#: lib/block_scout_web/templates/transaction/index.html.eex:18 #: lib/block_scout_web/templates/transaction/index.html.eex:18
msgid "More transactions have come in" msgid "More transactions have come in"
@ -1309,7 +1309,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_transaction/index.html.eex:48 #: lib/block_scout_web/templates/address_transaction/index.html.eex:48
#: lib/block_scout_web/templates/address_validation/index.html.eex:22 #: lib/block_scout_web/templates/address_validation/index.html.eex:22
#: lib/block_scout_web/templates/block_transaction/index.html.eex:23 #: lib/block_scout_web/templates/block_transaction/index.html.eex:23
#: lib/block_scout_web/templates/chain/show.html.eex:156 #: lib/block_scout_web/templates/chain/show.html.eex:169
#: lib/block_scout_web/templates/pending_transaction/index.html.eex:21 #: lib/block_scout_web/templates/pending_transaction/index.html.eex:21
#: lib/block_scout_web/templates/stakes/_table.html.eex:44 #: lib/block_scout_web/templates/stakes/_table.html.eex:44
#: lib/block_scout_web/templates/tokens/holder/index.html.eex:22 #: lib/block_scout_web/templates/tokens/holder/index.html.eex:22
@ -1324,7 +1324,7 @@ msgid "Something went wrong, click to reload."
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:219 #: lib/block_scout_web/templates/chain/show.html.eex:232
msgid "Something went wrong, click to retry." msgid "Something went wrong, click to retry."
msgstr "" msgstr ""
@ -1453,7 +1453,7 @@ msgid "Total Supply"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:127 #: lib/block_scout_web/templates/chain/show.html.eex:140
msgid "Total blocks" msgid "Total blocks"
msgstr "" msgstr ""
@ -1593,12 +1593,12 @@ msgid "Version"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:151 #: lib/block_scout_web/templates/chain/show.html.eex:164
msgid "View All Blocks" msgid "View All Blocks"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:209 #: lib/block_scout_web/templates/chain/show.html.eex:222
msgid "View All Transactions" msgid "View All Transactions"
msgstr "" msgstr ""
@ -1640,7 +1640,7 @@ msgid "WEI"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:135 #: lib/block_scout_web/templates/chain/show.html.eex:148
msgid "Wallet addresses" msgid "Wallet addresses"
msgstr "" msgstr ""
@ -1867,7 +1867,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_transaction/index.html.eex:15 #: lib/block_scout_web/templates/address_transaction/index.html.eex:15
#: lib/block_scout_web/templates/block_transaction/index.html.eex:10 #: lib/block_scout_web/templates/block_transaction/index.html.eex:10
#: lib/block_scout_web/templates/block_transaction/index.html.eex:18 #: lib/block_scout_web/templates/block_transaction/index.html.eex:18
#: lib/block_scout_web/templates/chain/show.html.eex:210 #: lib/block_scout_web/templates/chain/show.html.eex:223
#: lib/block_scout_web/templates/layout/_topnav.html.eex:53 #: lib/block_scout_web/templates/layout/_topnav.html.eex:53
#: lib/block_scout_web/views/address_view.ex:349 #: lib/block_scout_web/views/address_view.ex:349
msgid "Transactions" msgid "Transactions"

@ -14,6 +14,7 @@ defmodule Explorer.Application do
BlockCount, BlockCount,
BlockNumber, BlockNumber,
Blocks, Blocks,
GasUsage,
NetVersion, NetVersion,
TransactionCount, TransactionCount,
Transactions, Transactions,
@ -52,6 +53,7 @@ defmodule Explorer.Application do
AddressSumMinusBurnt, AddressSumMinusBurnt,
BlockCount, BlockCount,
Blocks, Blocks,
GasUsage,
NetVersion, NetVersion,
BlockNumber, BlockNumber,
con_cache_child_spec(MarketHistoryCache.cache_name()), con_cache_child_spec(MarketHistoryCache.cache_name()),

@ -69,6 +69,7 @@ defmodule Explorer.Chain do
BlockCount, BlockCount,
BlockNumber, BlockNumber,
Blocks, Blocks,
GasUsage,
TransactionCount, TransactionCount,
Transactions, Transactions,
Uncles Uncles
@ -1590,7 +1591,7 @@ defmodule Explorer.Chain do
where: block.consensus == true where: block.consensus == true
) )
Repo.one!(query) Repo.one!(query) || 0
end end
@spec fetch_sum_coin_total_supply_minus_burnt() :: non_neg_integer @spec fetch_sum_coin_total_supply_minus_burnt() :: non_neg_integer
@ -1620,6 +1621,17 @@ defmodule Explorer.Chain do
Repo.one!(query) || 0 Repo.one!(query) || 0
end end
@spec fetch_sum_gas_used() :: non_neg_integer
def fetch_sum_gas_used do
query =
from(
t0 in Transaction,
select: fragment("SUM(t0.gas_used)")
)
Repo.one!(query) || 0
end
@doc """ @doc """
The number of `t:Explorer.Chain.InternalTransaction.t/0`. The number of `t:Explorer.Chain.InternalTransaction.t/0`.
@ -2702,6 +2714,17 @@ defmodule Explorer.Chain do
end end
end end
@spec total_gas_usage() :: non_neg_integer()
def total_gas_usage do
cached_value = GasUsage.get_sum()
if is_nil(cached_value) do
0
else
cached_value
end
end
@doc """ @doc """
Estimated count of `t:Explorer.Chain.Block.t/0`. Estimated count of `t:Explorer.Chain.Block.t/0`.

@ -0,0 +1,65 @@
defmodule Explorer.Chain.Cache.GasUsage do
@moduledoc """
Cache for total gas usage.
"""
require Logger
@default_cache_period :timer.minutes(10)
use Explorer.Chain.MapCache,
name: :gas_usage,
key: :sum,
key: :async_task,
global_ttl: cache_period(),
ttl_check_interval: :timer.minutes(1),
callback: &async_task_on_deletion(&1)
alias Explorer.Chain
defp handle_fallback(:sum) do
# This will get the task PID if one exists and launch a new task if not
# See next `handle_fallback` definition
get_async_task()
{:return, nil}
end
defp handle_fallback(:async_task) do
# If this gets called it means an async task was requested, but none exists
# so a new one needs to be launched
{:ok, task} =
Task.start(fn ->
try do
result = Chain.fetch_sum_gas_used()
set_sum(result)
rescue
e ->
Logger.debug([
"Coudn't update gas used sum test #{inspect(e)}"
])
end
set_async_task(nil)
end)
{:update, task}
end
# By setting this as a `callback` an async task will be started each time the
# `sum` expires (unless there is one already running)
defp async_task_on_deletion({:delete, _, :sum}), do: get_async_task()
defp async_task_on_deletion(_data), do: nil
defp cache_period do
"TOTAL_GAS_USAGE_CACHE_PERIOD"
|> System.get_env("")
|> Integer.parse()
|> case do
{integer, ""} -> :timer.seconds(integer)
_ -> @default_cache_period
end
end
end
Loading…
Cancel
Save