More home page query optimizations
pull/342/head
Tim Mecklem 7 years ago committed by GitHub
commit 09633bde64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 26
      apps/explorer/lib/explorer/chain/statistics.ex
  2. 18
      apps/explorer/test/explorer/chain/statistics_test.exs

@ -19,24 +19,8 @@ defmodule Explorer.Chain.Statistics do
) t ) t
""" """
@lag_query """
SELECT coalesce(avg(lag), interval '0 seconds')
FROM (
SELECT inserted_at - timestamp AS lag
FROM blocks
WHERE blocks.inserted_at > NOW() - interval '1 hour'
AND blocks.timestamp > NOW() - interval '1 hour'
) t
"""
@block_velocity_query """
SELECT count(blocks.hash)
FROM blocks
WHERE blocks.inserted_at > NOW() - interval '1 minute'
"""
@transaction_velocity_query """ @transaction_velocity_query """
SELECT count(transactions.hash) SELECT count(transactions.inserted_at)
FROM transactions FROM transactions
WHERE transactions.inserted_at > NOW() - interval '1 minute' WHERE transactions.inserted_at > NOW() - interval '1 minute'
""" """
@ -53,9 +37,7 @@ defmodule Explorer.Chain.Statistics do
@typedoc """ @typedoc """
* `average_time` - the average time it took to mine/validate the last <= 100 `t:Explorer.Chain.Block.t/0` * `average_time` - the average time it took to mine/validate the last <= 100 `t:Explorer.Chain.Block.t/0`
* `block_velocity` - the number of `t:Explorer.Chain.Block.t/0` mined/validated in the last minute
* `blocks` - the last <= 5 `t:Explorer.Chain.Block.t/0` * `blocks` - the last <= 5 `t:Explorer.Chain.Block.t/0`
* `lag` - the average time over the last hour between when the block was mined/validated
(`t:Explorer.Chain.Block.t/0` `timestamp`) and when it was inserted into the databasse (`t:Explorer.Chain.Block.t/0` `timestamp`) and when it was inserted into the databasse
(`t:Explorer.Chain.Block.t/0` `inserted_at`) (`t:Explorer.Chain.Block.t/0` `inserted_at`)
* `number` - the latest `t:Explorer.Chain.Block.t/0` `number` * `number` - the latest `t:Explorer.Chain.Block.t/0` `number`
@ -66,9 +48,7 @@ defmodule Explorer.Chain.Statistics do
""" """
@type t :: %__MODULE__{ @type t :: %__MODULE__{
average_time: Duration.t(), average_time: Duration.t(),
block_velocity: blocks_per_minute(),
blocks: [Block.t()], blocks: [Block.t()],
lag: Duration.t(),
number: Block.block_number(), number: Block.block_number(),
timestamp: :calendar.datetime(), timestamp: :calendar.datetime(),
transaction_velocity: transactions_per_minute(), transaction_velocity: transactions_per_minute(),
@ -76,9 +56,7 @@ defmodule Explorer.Chain.Statistics do
} }
defstruct average_time: %Duration{seconds: 0, megaseconds: 0, microseconds: 0}, defstruct average_time: %Duration{seconds: 0, megaseconds: 0, microseconds: 0},
block_velocity: 0,
blocks: [], blocks: [],
lag: %Duration{seconds: 0, megaseconds: 0, microseconds: 0},
number: -1, number: -1,
timestamp: nil, timestamp: nil,
transaction_velocity: 0, transaction_velocity: 0,
@ -105,9 +83,7 @@ defmodule Explorer.Chain.Statistics do
%__MODULE__{ %__MODULE__{
average_time: query_duration(@average_time_query), average_time: query_duration(@average_time_query),
block_velocity: query_value(@block_velocity_query),
blocks: Repo.all(blocks), blocks: Repo.all(blocks),
lag: query_duration(@lag_query),
transaction_velocity: query_value(@transaction_velocity_query), transaction_velocity: query_value(@transaction_velocity_query),
transactions: transactions transactions: transactions
} }

@ -45,24 +45,6 @@ defmodule Explorer.Chain.StatisticsTest do
} = Statistics.fetch() } = Statistics.fetch()
end end
test "returns the lag between validation and insertion time" do
validation_time = DateTime.utc_now()
inserted_at = validation_time |> Timex.shift(seconds: 5)
insert(:block, timestamp: validation_time, inserted_at: inserted_at)
assert %Statistics{lag: %Duration{seconds: 5, megaseconds: 0, microseconds: 0}} = Statistics.fetch()
end
test "returns the number of blocks inserted in the last minute" do
old_inserted_at = Timex.shift(DateTime.utc_now(), days: -1)
insert(:block, inserted_at: old_inserted_at)
insert(:block)
statistics = Statistics.fetch()
assert statistics.block_velocity == 1
end
test "returns the number of transactions inserted in the last minute" do test "returns the number of transactions inserted in the last minute" do
old_inserted_at = Timex.shift(DateTime.utc_now(), days: -1) old_inserted_at = Timex.shift(DateTime.utc_now(), days: -1)
insert(:transaction, inserted_at: old_inserted_at) insert(:transaction, inserted_at: old_inserted_at)

Loading…
Cancel
Save