filter token to update by updated_at field

Currently, we're updating all tokens periodically
without considering that token is updated.
pull/2748/head
Ayrat Badykov 5 years ago
parent 0d778d2435
commit e8bdfd79f9
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 6
      apps/explorer/lib/explorer/chain.ex
  2. 7
      apps/explorer/lib/explorer/chain/token.ex
  3. 19
      apps/explorer/test/explorer/chain/token_test.exs
  4. 13
      apps/explorer/test/explorer/chain_test.exs

@ -2916,8 +2916,10 @@ defmodule Explorer.Chain do
reducer :: (entry :: Hash.Address.t(), accumulator -> accumulator)
) :: {:ok, accumulator}
when accumulator: term()
def stream_cataloged_token_contract_address_hashes(initial, reducer) when is_function(reducer, 2) do
Token.cataloged_tokens()
def stream_cataloged_token_contract_address_hashes(initial, reducer, hours_ago_updated \\ 48)
when is_function(reducer, 2) do
hours_ago_updated
|> Token.cataloged_tokens()
|> order_by(asc: :updated_at)
|> Repo.stream_reduce(initial, reducer)
end

@ -105,11 +105,14 @@ defmodule Explorer.Chain.Token do
These are tokens with cataloged field set to true.
"""
def cataloged_tokens do
def cataloged_tokens(hours \\ 48) do
date_now = DateTime.utc_now()
hours_ago_date = DateTime.add(date_now, -:timer.hours(hours), :millisecond)
from(
token in __MODULE__,
select: token.contract_address_hash,
where: token.cataloged == true
where: token.cataloged == true and token.updated_at <= ^hours_ago_date
)
end
end

@ -3,14 +3,27 @@ defmodule Explorer.Chain.TokenTest do
import Explorer.Factory
alias Explorer.Chain
alias Explorer.Chain.Token
alias Explorer.Repo
describe "cataloged_tokens/0" do
test "filters only cataloged tokens" do
token = insert(:token, cataloged: true)
{:ok, date} = DateTime.now("Etc/UTC")
hours_ago_date = DateTime.add(date, -:timer.hours(60), :millisecond)
token = insert(:token, cataloged: true, updated_at: hours_ago_date)
insert(:token, cataloged: false)
assert Repo.all(Chain.Token.cataloged_tokens()) == [token.contract_address_hash]
assert Repo.all(Token.cataloged_tokens()) == [token.contract_address_hash]
end
test "filter tokens by updated_at filed" do
{:ok, date} = DateTime.now("Etc/UTC")
hours_ago_date = DateTime.add(date, -:timer.hours(60), :millisecond)
token = insert(:token, cataloged: true, updated_at: hours_ago_date)
insert(:token, cataloged: true)
assert Repo.all(Token.cataloged_tokens()) == [token.contract_address_hash]
end
end
end

@ -3522,24 +3522,27 @@ defmodule Explorer.ChainTest do
describe "stream_cataloged_token_contract_address_hashes/2" do
test "reduces with given reducer and accumulator" do
%Token{contract_address_hash: catalog_address} = insert(:token, cataloged: true)
today = DateTime.utc_now()
yesterday = Timex.shift(today, days: -1)
%Token{contract_address_hash: catalog_address} = insert(:token, cataloged: true, updated_at: yesterday)
insert(:token, cataloged: false)
assert Chain.stream_cataloged_token_contract_address_hashes([], &[&1 | &2]) == {:ok, [catalog_address]}
assert Chain.stream_cataloged_token_contract_address_hashes([], &[&1 | &2], 1) == {:ok, [catalog_address]}
end
test "sorts the tokens by updated_at in ascending order" do
today = DateTime.utc_now()
yesterday = Timex.shift(today, days: -1)
two_days_ago = Timex.shift(today, days: -2)
token1 = insert(:token, %{cataloged: true, updated_at: today})
token2 = insert(:token, %{cataloged: true, updated_at: yesterday})
token1 = insert(:token, %{cataloged: true, updated_at: yesterday})
token2 = insert(:token, %{cataloged: true, updated_at: two_days_ago})
expected_response =
[token1, token2]
|> Enum.sort(&(Timex.to_unix(&1.updated_at) < Timex.to_unix(&2.updated_at)))
|> Enum.map(& &1.contract_address_hash)
assert Chain.stream_cataloged_token_contract_address_hashes([], &(&2 ++ [&1])) == {:ok, expected_response}
assert Chain.stream_cataloged_token_contract_address_hashes([], &(&2 ++ [&1]), 12) == {:ok, expected_response}
end
end

Loading…
Cancel
Save