|
|
|
@ -1,6 +1,6 @@ |
|
|
|
|
defmodule Explorer.Counters.NewVerifiedContractsCounter do |
|
|
|
|
defmodule Explorer.Chain.Cache.NewVerifiedContractsCounter do |
|
|
|
|
@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. |
|
|
|
|
""" |
|
|
|
@ -8,24 +8,22 @@ defmodule Explorer.Counters.NewVerifiedContractsCounter do |
|
|
|
|
use GenServer |
|
|
|
|
|
|
|
|
|
alias Explorer.Chain |
|
|
|
|
alias Explorer.Counters.Helper |
|
|
|
|
|
|
|
|
|
@table :new_verified_contracts_counter |
|
|
|
|
|
|
|
|
|
@cache_key "new_verified" |
|
|
|
|
@counter_type "new_verified_contracts_counter" |
|
|
|
|
|
|
|
|
|
# It is undesirable to automatically start the consolidation in all environments. |
|
|
|
|
# Consider the test environment: if the consolidation initiates but does not |
|
|
|
|
# finish before a test ends, that test will fail. This way, hundreds of |
|
|
|
|
# tests were failing before disabling the consolidation and the scheduler in |
|
|
|
|
# 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) |
|
|
|
|
|
|
|
|
|
@update_interval_in_seconds Keyword.get(config, :update_interval_in_seconds) |
|
|
|
|
|
|
|
|
|
@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() |
|
|
|
|
def start_link(_) do |
|
|
|
@ -34,26 +32,13 @@ defmodule Explorer.Counters.NewVerifiedContractsCounter do |
|
|
|
|
|
|
|
|
|
@impl true |
|
|
|
|
def init(_args) do |
|
|
|
|
create_table() |
|
|
|
|
|
|
|
|
|
{:ok, %{consolidate?: enable_consolidation?()}, {:continue, :ok}} |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def create_table do |
|
|
|
|
Helper.create_cache_table(@table) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
defp schedule_next_consolidation do |
|
|
|
|
Process.send_after(self(), :consolidate, :timer.seconds(@update_interval_in_seconds)) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
|
Inserts new items into the `:ets` table. |
|
|
|
|
""" |
|
|
|
|
def insert_counter({key, info}) do |
|
|
|
|
:ets.insert(@table, {key, info}) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
@impl true |
|
|
|
|
def handle_continue(:ok, %{consolidate?: true} = state) do |
|
|
|
|
consolidate() |
|
|
|
@ -76,19 +61,24 @@ defmodule Explorer.Counters.NewVerifiedContractsCounter do |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
@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 |
|
|
|
|
Helper.fetch_from_cache(@cache_key, @table) |
|
|
|
|
Chain.get_last_fetched_counter(@counter_type) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
@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 |
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
@doc """ |
|
|
|
@ -97,11 +87,11 @@ defmodule Explorer.Counters.NewVerifiedContractsCounter do |
|
|
|
|
In order to choose whether or not to enable the scheduler and the initial |
|
|
|
|
consolidation, change the following Explorer config: |
|
|
|
|
|
|
|
|
|
`config :explorer, Explorer.Counters.NewVerifiedContractsCounter, enable_consolidation: true` |
|
|
|
|
`config :explorer, Explorer.Chain.Cache.NewVerifiedContractsCounter, enable_consolidation: true` |
|
|
|
|
|
|
|
|
|
to: |
|
|
|
|
|
|
|
|
|
`config :explorer, Explorer.Counters.NewVerifiedContractsCounter, enable_consolidation: false` |
|
|
|
|
`config :explorer, Explorer.Chain.Cache.NewVerifiedContractsCounter, enable_consolidation: false` |
|
|
|
|
""" |
|
|
|
|
def enable_consolidation?, do: @enable_consolidation |
|
|
|
|
end |