Blockchain explorer for Ethereum based network and a tool for inspecting and analyzing EVM based blockchains.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
blockscout/lib/explorer/servers/chain_statistics.ex

33 lines
809 B

defmodule Explorer.Servers.ChainStatistics do
@moduledoc "Stores the latest chain statistics."
use GenServer
alias Explorer.Chain
@interval 1_000
def fetch, do: GenServer.call(__MODULE__, :fetch)
def start_link, do: start_link(%Chain{})
def start_link(%Chain{} = chain) do
GenServer.start_link(__MODULE__, chain, name: __MODULE__)
end
def init(chain) do
refresh()
{:ok, chain}
end
def refresh, do: refresh(@interval)
def refresh(interval), do: Process.send_after(self(), :refresh, interval)
def handle_info(:refresh, _) do
refresh()
chain = Chain.fetch()
{:noreply, chain}
end
def handle_info(_, tasks), do: {:noreply, tasks}
def handle_call(:fetch, _, chain), do: {:reply, chain, chain}
def handle_call(_, _, chain), do: {:noreply, chain}
end