From 15d5389564c5aa5297ed9ce6d123f63ba290747b Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 28 Jan 2022 07:57:43 +0100 Subject: [PATCH] Update /health endpoint to be in local timezone --- freqtrade/freqtradebot.py | 4 ++-- freqtrade/rpc/api_server/api_schemas.py | 1 + freqtrade/rpc/rpc.py | 8 ++++++-- freqtrade/rpc/telegram.py | 3 +-- tests/rpc/test_rpc.py | 3 ++- tests/rpc/test_rpc_apiserver.py | 3 ++- 6 files changed, 14 insertions(+), 8 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 17d345f48..c98c9a804 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -100,7 +100,7 @@ class FreqtradeBot(LoggingMixin): self._exit_lock = Lock() LoggingMixin.__init__(self, logger, timeframe_to_seconds(self.strategy.timeframe)) - self.last_process = datetime.utcfromtimestamp(0.0) + self.last_process = datetime(1970, 1, 1, tzinfo=timezone.utc) def notify_status(self, msg: str) -> None: """ @@ -189,7 +189,7 @@ class FreqtradeBot(LoggingMixin): self.enter_positions() Trade.commit() - self.last_process = datetime.utcnow() + self.last_process = datetime.now(timezone.utc) def process_stopped(self) -> None: """ diff --git a/freqtrade/rpc/api_server/api_schemas.py b/freqtrade/rpc/api_server/api_schemas.py index 5709639b3..d3a0f3e7d 100644 --- a/freqtrade/rpc/api_server/api_schemas.py +++ b/freqtrade/rpc/api_server/api_schemas.py @@ -386,3 +386,4 @@ class SysInfo(BaseModel): class Health(BaseModel): last_process: datetime + last_process_ts: int diff --git a/freqtrade/rpc/rpc.py b/freqtrade/rpc/rpc.py index fa301ed60..f57253562 100644 --- a/freqtrade/rpc/rpc.py +++ b/freqtrade/rpc/rpc.py @@ -10,6 +10,7 @@ from typing import Any, Dict, List, Optional, Tuple, Union import arrow import psutil from dateutil.relativedelta import relativedelta +from dateutil.tz import tzlocal from numpy import NAN, inf, int64, mean from pandas import DataFrame @@ -1031,7 +1032,10 @@ class RPC: "ram_pct": psutil.virtual_memory().percent } - def _health(self) -> Dict[str, str]: + def _health(self) -> Dict[str, Union[str, int]]: + last_p = self._freqtrade.last_process return { - 'last_process': str(self._freqtrade.last_process) + '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 13ad98e0e..74f56b4c1 100644 --- a/freqtrade/rpc/telegram.py +++ b/freqtrade/rpc/telegram.py @@ -1319,8 +1319,7 @@ class Telegram(RPCHandler): """ try: health = self._rpc._health() - message = f"Last process: `{health['last_process']}`" - logger.debug(message) + message = f"Last process: `{health['last_process_loc']}`" self._send_msg(message) except RPCException as e: self._send_msg(str(e)) diff --git a/tests/rpc/test_rpc.py b/tests/rpc/test_rpc.py index 9d8db06f2..03c068966 100644 --- a/tests/rpc/test_rpc.py +++ b/tests/rpc/test_rpc.py @@ -1280,4 +1280,5 @@ 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' + assert result['last_process'] == '1970-01-01 00:00:00+00:00' + assert result['last_process_ts'] == 0 diff --git a/tests/rpc/test_rpc_apiserver.py b/tests/rpc/test_rpc_apiserver.py index 3e891baa8..1c33dd928 100644 --- a/tests/rpc/test_rpc_apiserver.py +++ b/tests/rpc/test_rpc_apiserver.py @@ -1451,4 +1451,5 @@ def test_health(botclient): assert_response(rc) ret = rc.json() - assert ret['last_process'] == '1970-01-01T00:00:00' + assert ret['last_process_ts'] == 0 + assert ret['last_process'] == '1970-01-01T00:00:00+00:00'