|
|
@ -1,6 +1,6 @@ |
|
|
|
defmodule Explorer.Counters.NewVerifiedContractsCounter do |
|
|
|
defmodule Explorer.Chain.Cache.NewVerifiedContractsCounter do |
|
|
|
@moduledoc """ |
|
|
|
@moduledoc """ |
|
|
|
Caches the number of contracts, new and verified. |
|
|
|
Caches the number of new verfied contracts (verified in last 24 hours). |
|
|
|
|
|
|
|
|
|
|
|
It loads the count asynchronously and in a time interval of 30 minutes. |
|
|
|
It loads the count asynchronously and in a time interval of 30 minutes. |
|
|
|
""" |
|
|
|
""" |
|
|
@ -8,24 +8,22 @@ defmodule Explorer.Counters.NewVerifiedContractsCounter do |
|
|
|
use GenServer |
|
|
|
use GenServer |
|
|
|
|
|
|
|
|
|
|
|
alias Explorer.Chain |
|
|
|
alias Explorer.Chain |
|
|
|
alias Explorer.Counters.Helper |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@table :new_verified_contracts_counter |
|
|
|
@counter_type "new_verified_contracts_counter" |
|
|
|
|
|
|
|
|
|
|
|
@cache_key "new_verified" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# It is undesirable to automatically start the consolidation in all environments. |
|
|
|
# It is undesirable to automatically start the consolidation in all environments. |
|
|
|
# Consider the test environment: if the consolidation initiates but does not |
|
|
|
# Consider the test environment: if the consolidation initiates but does not |
|
|
|
# finish before a test ends, that test will fail. This way, hundreds of |
|
|
|
# finish before a test ends, that test will fail. This way, hundreds of |
|
|
|
# tests were failing before disabling the consolidation and the scheduler in |
|
|
|
# tests were failing before disabling the consolidation and the scheduler in |
|
|
|
# the test env. |
|
|
|
# the test env. |
|
|
|
config = Application.compile_env(:explorer, Explorer.Counters.NewVerifiedContractsCounter) |
|
|
|
config = Application.compile_env(:explorer, Explorer.Chain.Cache.NewVerifiedContractsCounter) |
|
|
|
@enable_consolidation Keyword.get(config, :enable_consolidation) |
|
|
|
@enable_consolidation Keyword.get(config, :enable_consolidation) |
|
|
|
|
|
|
|
|
|
|
|
@update_interval_in_seconds Keyword.get(config, :update_interval_in_seconds) |
|
|
|
@update_interval_in_seconds Keyword.get(config, :update_interval_in_seconds) |
|
|
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
@doc """ |
|
|
|
Starts a process to periodically update the counter of the . |
|
|
|
Starts a process to periodically update the counter of new verified |
|
|
|
|
|
|
|
contracts (verified in last 24 hours). |
|
|
|
""" |
|
|
|
""" |
|
|
|
@spec start_link(term()) :: GenServer.on_start() |
|
|
|
@spec start_link(term()) :: GenServer.on_start() |
|
|
|
def start_link(_) do |
|
|
|
def start_link(_) do |
|
|
@ -34,26 +32,13 @@ defmodule Explorer.Counters.NewVerifiedContractsCounter do |
|
|
|
|
|
|
|
|
|
|
|
@impl true |
|
|
|
@impl true |
|
|
|
def init(_args) do |
|
|
|
def init(_args) do |
|
|
|
create_table() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{:ok, %{consolidate?: enable_consolidation?()}, {:continue, :ok}} |
|
|
|
{:ok, %{consolidate?: enable_consolidation?()}, {:continue, :ok}} |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def create_table do |
|
|
|
|
|
|
|
Helper.create_cache_table(@table) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defp schedule_next_consolidation do |
|
|
|
defp schedule_next_consolidation do |
|
|
|
Process.send_after(self(), :consolidate, :timer.seconds(@update_interval_in_seconds)) |
|
|
|
Process.send_after(self(), :consolidate, :timer.seconds(@update_interval_in_seconds)) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
|
|
|
|
Inserts new items into the `:ets` table. |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
def insert_counter({key, info}) do |
|
|
|
|
|
|
|
:ets.insert(@table, {key, info}) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@impl true |
|
|
|
@impl true |
|
|
|
def handle_continue(:ok, %{consolidate?: true} = state) do |
|
|
|
def handle_continue(:ok, %{consolidate?: true} = state) do |
|
|
|
consolidate() |
|
|
|
consolidate() |
|
|
@ -76,19 +61,24 @@ defmodule Explorer.Counters.NewVerifiedContractsCounter do |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
@doc """ |
|
|
|
Fetches the info for a specific item from the `:ets` table. |
|
|
|
Fetches the value for a `#{@counter_type}` counter type from the `last_fetched_counters` table. |
|
|
|
""" |
|
|
|
""" |
|
|
|
def fetch do |
|
|
|
def fetch do |
|
|
|
Helper.fetch_from_cache(@cache_key, @table) |
|
|
|
Chain.get_last_fetched_counter(@counter_type) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
@doc """ |
|
|
|
Consolidates the info by populating the `:ets` table with the current database information. |
|
|
|
Consolidates the info by populating the `last_fetched_counters` table with the current database information. |
|
|
|
""" |
|
|
|
""" |
|
|
|
def consolidate do |
|
|
|
def consolidate do |
|
|
|
new_verified_counter = Chain.count_new_verified_contracts() |
|
|
|
new_verified_counter = Chain.count_new_verified_contracts() |
|
|
|
|
|
|
|
|
|
|
|
insert_counter({@cache_key, new_verified_counter}) |
|
|
|
params = %{ |
|
|
|
|
|
|
|
counter_type: @counter_type, |
|
|
|
|
|
|
|
value: new_verified_counter |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Chain.upsert_last_fetched_counter(params) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
@doc """ |
|
|
@ -97,11 +87,11 @@ defmodule Explorer.Counters.NewVerifiedContractsCounter do |
|
|
|
In order to choose whether or not to enable the scheduler and the initial |
|
|
|
In order to choose whether or not to enable the scheduler and the initial |
|
|
|
consolidation, change the following Explorer config: |
|
|
|
consolidation, change the following Explorer config: |
|
|
|
|
|
|
|
|
|
|
|
`config :explorer, Explorer.Counters.NewVerifiedContractsCounter, enable_consolidation: true` |
|
|
|
`config :explorer, Explorer.Chain.Cache.NewVerifiedContractsCounter, enable_consolidation: true` |
|
|
|
|
|
|
|
|
|
|
|
to: |
|
|
|
to: |
|
|
|
|
|
|
|
|
|
|
|
`config :explorer, Explorer.Counters.NewVerifiedContractsCounter, enable_consolidation: false` |
|
|
|
`config :explorer, Explorer.Chain.Cache.NewVerifiedContractsCounter, enable_consolidation: false` |
|
|
|
""" |
|
|
|
""" |
|
|
|
def enable_consolidation?, do: @enable_consolidation |
|
|
|
def enable_consolidation?, do: @enable_consolidation |
|
|
|
end |
|
|
|
end |