From 1d5608d627966d4fae47ba11b5aff416f5961f83 Mon Sep 17 00:00:00 2001 From: ASU Date: Mon, 27 Feb 2023 12:14:38 +0200 Subject: [PATCH] Fix last_process related bug in RPC.health --- freqtrade/freqtradebot.py | 2 +- freqtrade/rpc/api_server/api_schemas.py | 4 ++-- freqtrade/rpc/api_server/api_v1.py | 2 +- freqtrade/rpc/rpc.py | 17 ++++++++++++----- freqtrade/rpc/telegram.py | 2 +- tests/rpc/test_rpc.py | 6 +++--- tests/rpc/test_rpc_apiserver.py | 4 ++-- 7 files changed, 22 insertions(+), 15 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 82be6f3b5..6529037e8 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -127,7 +127,7 @@ class FreqtradeBot(LoggingMixin): for minutes in [0, 15, 30, 45]: t = str(time(time_slot, minutes, 2)) self._schedule.every().day.at(t).do(update) - self.last_process = datetime(1970, 1, 1, tzinfo=timezone.utc) + self.last_process: Optional[datetime] = None self.strategy.ft_bot_start() # Initialize protections AFTER bot start - otherwise parameters are not loaded. diff --git a/freqtrade/rpc/api_server/api_schemas.py b/freqtrade/rpc/api_server/api_schemas.py index 58f6ad583..b9595a3dd 100644 --- a/freqtrade/rpc/api_server/api_schemas.py +++ b/freqtrade/rpc/api_server/api_schemas.py @@ -456,5 +456,5 @@ class SysInfo(BaseModel): class Health(BaseModel): - last_process: datetime - last_process_ts: int + last_process: Optional[datetime] + last_process_ts: Optional[int] diff --git a/freqtrade/rpc/api_server/api_v1.py b/freqtrade/rpc/api_server/api_v1.py index 73bdde86b..f6bab3624 100644 --- a/freqtrade/rpc/api_server/api_v1.py +++ b/freqtrade/rpc/api_server/api_v1.py @@ -346,4 +346,4 @@ def sysinfo(): @router.get('/health', response_model=Health, tags=['info']) def health(rpc: RPC = Depends(get_rpc)): - return rpc._health() + return rpc.health() diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index 83bffb779..08bf8d5c8 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -89,7 +89,7 @@ class RPC: # Bind _fiat_converter if needed _fiat_converter: Optional[CryptoToFiatConverter] = None - def __init__(self, freqtrade) -> None: + def __init__(self, freqtrade: "FreqtradeBot") -> None: """ Initializes all enabled rpc modules :param freqtrade: Instance of a freqtrade bot @@ -1198,10 +1198,17 @@ class RPC: "ram_pct": psutil.virtual_memory().percent } - def _health(self) -> Dict[str, Union[str, int]]: + def health(self) -> Dict[str, Optional[Union[str, int]]]: last_p = self._freqtrade.last_process + if last_p is None: + return { + "last_process": None, + "last_process_loc": None, + "last_process_ts": None, + } + return { - 'last_process': str(last_p), - 'last_process_loc': last_p.astimezone(tzlocal()).strftime(DATETIME_PRINT_FORMAT), - 'last_process_ts': int(last_p.timestamp()), + "last_process": str(last_p), + "last_process_loc": last_p.astimezone(tzlocal()).strftime(DATETIME_PRINT_FORMAT), + "last_process_ts": int(last_p.timestamp()), } diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py index fbd675d02..09032f10d 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -1527,7 +1527,7 @@ class Telegram(RPCHandler): Handler for /health Shows the last process timestamp """ - health = self._rpc._health() + health = self._rpc.health() message = f"Last process: `{health['last_process_loc']}`" self._send_msg(message) diff --git a/tests/rpc/test_rpc.py b/tests/rpc/test_rpc.py index 31e19ce3f..d9b7c764a 100644 --- a/tests/rpc/test_rpc.py +++ b/tests/rpc/test_rpc.py @@ -1252,6 +1252,6 @@ def test_rpc_health(mocker, default_conf) -> None: freqtradebot = get_patched_freqtradebot(mocker, default_conf) rpc = RPC(freqtradebot) - result = rpc._health() - assert result['last_process'] == '1970-01-01 00:00:00+00:00' - assert result['last_process_ts'] == 0 + result = rpc.health() + assert result['last_process'] is None + assert result['last_process_ts'] is None diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index 43d9abb78..9c6f33046 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -1801,8 +1801,8 @@ def test_health(botclient): assert_response(rc) ret = rc.json() - assert ret['last_process_ts'] == 0 - assert ret['last_process'] == '1970-01-01T00:00:00+00:00' + assert ret["last_process_ts"] is None + assert ret["last_process"] is None def test_api_ws_subscribe(botclient, mocker):