perf: Fix performance of Explorer.Counters.Transactions24hStats.consolidate/0 function (#11082)

pull/11094/head
Victor Baranov 3 weeks ago committed by GitHub
parent e566d99c67
commit b7f15f8472
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 17
      apps/explorer/lib/explorer/counters/transactions_24h_stats.ex

@ -10,7 +10,7 @@ defmodule Explorer.Counters.Transactions24hStats do
import Ecto.Query import Ecto.Query
alias Explorer.{Chain, Repo} alias Explorer.{Chain, Repo}
alias Explorer.Chain.Transaction alias Explorer.Chain.{DenormalizationHelper, Transaction}
@transaction_count_name "transaction_count_24h" @transaction_count_name "transaction_count_24h"
@transaction_fee_sum_name "transaction_fee_sum_24h" @transaction_fee_sum_name "transaction_fee_sum_24h"
@ -94,15 +94,18 @@ defmodule Explorer.Counters.Transactions24hStats do
sum_query = dynamic([_, _], sum(^fee_query)) sum_query = dynamic([_, _], sum(^fee_query))
avg_query = dynamic([_, _], avg(^fee_query)) avg_query = dynamic([_, _], avg(^fee_query))
query = base_query =
from(transaction in Transaction, from(transaction in Transaction,
join: block in assoc(transaction, :block), join: block in assoc(transaction, :block),
where: block.timestamp >= ago(24, "hour"),
select: %{count: count(transaction.hash)}, select: %{count: count(transaction.hash)},
select_merge: ^%{fee_sum: sum_query}, select_merge: ^%{fee_sum: sum_query},
select_merge: ^%{fee_average: avg_query} select_merge: ^%{fee_average: avg_query}
) )
query =
base_query
|> where_block_timestamp_in_last_24_hours()
%{ %{
count: count, count: count,
fee_sum: fee_sum, fee_sum: fee_sum,
@ -125,6 +128,14 @@ defmodule Explorer.Counters.Transactions24hStats do
}) })
end end
defp where_block_timestamp_in_last_24_hours(query) do
if DenormalizationHelper.transactions_denormalization_finished?() do
where(query, [transaction, _block], transaction.block_timestamp >= ago(24, "hour"))
else
where(query, [_transaction, block], block.timestamp >= ago(24, "hour"))
end
end
@doc """ @doc """
Returns a boolean that indicates whether consolidation is enabled Returns a boolean that indicates whether consolidation is enabled

Loading…
Cancel
Save