Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#538 - check network main subgraph query fails #539

Merged
merged 15 commits into from
Jan 16, 2024
6 changes: 3 additions & 3 deletions pdr_backend/analytics/check_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pdr_backend.util.constants_opf_addrs import get_opf_addresses
from pdr_backend.util.contract import get_address
from pdr_backend.util.mathutil import from_wei
from pdr_backend.util.timeutil import current_ut
from pdr_backend.util.timeutil import current_ut_s

_N_FEEDS = 20 # magic number alert. FIX ME, shouldn't be hardcoded

Expand Down Expand Up @@ -44,7 +44,7 @@ def check_dfbuyer(
subgraph_url: str,
token_amt: int,
):
cur_ut = current_ut() # accounts for timezone
cur_ut = current_ut_s()
start_ut = int((cur_ut // S_PER_WEEK) * S_PER_WEEK)

contracts_sg_dict = contract_query_result["data"]["predictContracts"]
Expand Down Expand Up @@ -92,7 +92,7 @@ def get_expected_consume(for_ut: int, token_amt: int) -> Union[float, int]:
def check_network_main(ppss: PPSS, lookback_hours: int):
web3_pp = ppss.web3_pp

cur_ut = current_ut() # accounts for timezone
cur_ut = current_ut_s()
start_ut = cur_ut - lookback_hours * 60 * 60
query = """
{
Expand Down
24 changes: 21 additions & 3 deletions pdr_backend/analytics/test/test_check_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

PATH = "pdr_backend.analytics.check_network"

MOCK_CUR_UT = 1702826080982
MOCK_CUR_UT = 1702826080


@enforce_types
Expand All @@ -27,11 +27,11 @@
side_effect=Mock(return_value=100),
)
@patch(
f"{PATH}.current_ut",
f"{PATH}.current_ut_s",
side_effect=Mock(return_value=MOCK_CUR_UT),
)
def test_check_dfbuyer( # pylint: disable=unused-argument
mock_current_ut,
mock_current_ut_ms,
mock_get_expected_consume_,
mock_get_consume_so_far_per_contract_,
capsys,
Expand Down Expand Up @@ -150,3 +150,21 @@ def test_check_network_others( # pylint: disable=unused-argument
check_network_main(ppss, lookback_hours=24)
assert mock_query_subgraph.call_count == 1
assert mock_check_dfbuyer.call_count == 1


@enforce_types
@patch(f"{PATH}.check_dfbuyer")
@patch(f"{PATH}.get_opf_addresses")
@patch(f"{PATH}.Token")
def test_check_network_without_mock( # pylint: disable=unused-argument
mock_token,
mock_get_opf_addresses,
mock_check_dfbuyer,
tmpdir,
monkeypatch,
):
mock_token.balanceOf.return_value = 1000e18
ppss = mock_ppss(["binance BTC/USDT c 5m"], "sapphire-mainnet", str(tmpdir))

check_network_main(ppss, lookback_hours=1)
assert mock_check_dfbuyer.call_count == 1
4 changes: 2 additions & 2 deletions pdr_backend/lake/gql_data_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pdr_backend.ppss.ppss import PPSS
from pdr_backend.subgraph.subgraph_predictions import get_all_contract_ids_by_owner
from pdr_backend.util.networkutil import get_sapphire_postfix
from pdr_backend.util.timeutil import current_ut, pretty_timestr
from pdr_backend.util.timeutil import current_ut_ms, pretty_timestr


@enforce_types
Expand Down Expand Up @@ -113,7 +113,7 @@ def _update(self, fin_ut: int):

st_ut = self._calc_start_ut(filename)
print(f" Aim to fetch data from start time: {pretty_timestr(st_ut)}")
if st_ut > min(current_ut(), fin_ut):
if st_ut > min(current_ut_ms(), fin_ut):
print(" Given start time, no data to gather. Exit.")
continue

Expand Down
4 changes: 2 additions & 2 deletions pdr_backend/lake/ohlcv_data_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
save_rawohlcv_file,
)
from pdr_backend.ppss.lake_ss import LakeSS
from pdr_backend.util.timeutil import current_ut, pretty_timestr
from pdr_backend.util.timeutil import current_ut_ms, pretty_timestr


@enforce_types
Expand Down Expand Up @@ -114,7 +114,7 @@ def _update_rawohlcv_files_at_feed(self, feed: ArgFeed, fin_ut: int):
assert feed.timeframe
st_ut = self._calc_start_ut_maybe_delete(feed.timeframe, filename)
print(f" Aim to fetch data from start time: {pretty_timestr(st_ut)}")
if st_ut > min(current_ut(), fin_ut):
if st_ut > min(current_ut_ms(), fin_ut):
print(" Given start time, no data to gather. Exit.")
return

Expand Down
4 changes: 2 additions & 2 deletions pdr_backend/lake/test/test_ohlcv_data_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from pdr_backend.lake.test.resources import _lake_ss_1feed, _lake_ss
from pdr_backend.util.constants import S_PER_MIN
from pdr_backend.util.mathutil import all_nan, has_nan
from pdr_backend.util.timeutil import current_ut, ut_to_timestr
from pdr_backend.util.timeutil import current_ut_ms, ut_to_timestr

MS_PER_5M_EPOCH = 300000

Expand Down Expand Up @@ -60,7 +60,7 @@ def _uts_in_range(st_ut: int, fin_ut: int, limit_N=100000) -> List[int]:
# setup: exchange
class FakeExchange:
def __init__(self):
self.cur_ut: int = current_ut() # fixed value, for easier testing
self.cur_ut: int = current_ut_ms() # fixed value, for easier testing

# pylint: disable=unused-argument
def fetch_ohlcv(self, since, limit, *args, **kwargs) -> list:
Expand Down
4 changes: 2 additions & 2 deletions pdr_backend/sim/sim_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pdr_backend.lake.ohlcv_data_factory import OhlcvDataFactory
from pdr_backend.ppss.ppss import PPSS
from pdr_backend.util.mathutil import nmse
from pdr_backend.util.timeutil import current_ut, pretty_timestr
from pdr_backend.util.timeutil import current_ut_ms, pretty_timestr

FONTSIZE = 12

Expand Down Expand Up @@ -70,7 +70,7 @@ def usdcoin(self) -> str:

@enforce_types
def _init_loop_attributes(self):
filebase = f"out_{current_ut()}.txt"
filebase = f"out_{current_ut_ms()}.txt"
self.logfile = os.path.join(self.ppss.sim_ss.log_dir, filebase)
with open(self.logfile, "w") as f:
f.write("\n")
Expand Down
6 changes: 3 additions & 3 deletions pdr_backend/util/test_noganache/test_timeutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from enforce_typing import enforce_types

from pdr_backend.util.timeutil import (
current_ut,
current_ut_ms,
dt_to_ut,
ms_to_seconds,
pretty_timestr,
Expand All @@ -25,8 +25,8 @@ def test_pretty_timestr():


@enforce_types
def test_current_ut():
ut = current_ut()
def test_current_ut_ms():
ut = current_ut_ms()
assert isinstance(ut, int)
assert ut > 1648576500000

Expand Down
10 changes: 8 additions & 2 deletions pdr_backend/util/timeutil.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
import datetime
from datetime import timezone

Expand All @@ -11,12 +12,17 @@ def pretty_timestr(ut: int) -> str:


@enforce_types
def current_ut() -> int:
def current_ut_ms() -> int:
"""Return the current date/time as a unix time (int in # ms)"""
dt = datetime.datetime.now(timezone.utc)
return dt_to_ut(dt)


def current_ut_s() -> int:
"""Returns the current UTC unix time in seconds"""
return int(time.time())


@enforce_types
def timestr_to_ut(timestr: str) -> int:
"""
Expand All @@ -33,7 +39,7 @@ def timestr_to_ut(timestr: str) -> int:
Does not use local time, rather always uses UTC
"""
if timestr.lower() == "now":
return current_ut()
return current_ut_ms()

ncolon = timestr.count(":")
if ncolon == 0:
Expand Down
Loading