Merge pull request #4836 from bzed/telegram-locks

Telegram rpc: split too long /locks messages
This commit is contained in:
Matthias 2021-05-05 20:15:13 +02:00 committed by GitHub
commit 32577cc0cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 13 deletions

View File

@ -6,7 +6,7 @@ import logging
import re import re
from datetime import datetime from datetime import datetime
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any, Iterator, List
from typing.io import IO from typing.io import IO
import rapidjson import rapidjson
@ -202,3 +202,14 @@ def render_template_with_fallback(templatefile: str, templatefallbackfile: str,
return render_template(templatefile, arguments) return render_template(templatefile, arguments)
except TemplateNotFound: except TemplateNotFound:
return render_template(templatefallbackfile, arguments) return render_template(templatefallbackfile, arguments)
def chunks(lst: List[Any], n: int) -> Iterator[List[Any]]:
"""
Split lst into chunks of the size n.
:param lst: list to split into chunks
:param n: number of max elements per chunk
:return: None
"""
for chunk in range(0, len(lst), n):
yield (lst[chunk:chunk + n])

View File

@ -20,7 +20,7 @@ from telegram.utils.helpers import escape_markdown
from freqtrade.__init__ import __version__ from freqtrade.__init__ import __version__
from freqtrade.constants import DUST_PER_COIN from freqtrade.constants import DUST_PER_COIN
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.misc import round_coin_value from freqtrade.misc import chunks, round_coin_value
from freqtrade.rpc import RPC, RPCException, RPCHandler, RPCMessageType from freqtrade.rpc import RPC, RPCException, RPCHandler, RPCMessageType
@ -750,17 +750,18 @@ class Telegram(RPCHandler):
Handler for /locks. Handler for /locks.
Returns the currently active locks Returns the currently active locks
""" """
locks = self._rpc._rpc_locks() rpc_locks = self._rpc._rpc_locks()
message = tabulate([[ for locks in chunks(rpc_locks['locks'], 25):
lock['id'], message = tabulate([[
lock['pair'], lock['id'],
lock['lock_end_time'], lock['pair'],
lock['reason']] for lock in locks['locks']], lock['lock_end_time'],
headers=['ID', 'Pair', 'Until', 'Reason'], lock['reason']] for lock in locks],
tablefmt='simple') headers=['ID', 'Pair', 'Until', 'Reason'],
message = f"<pre>{escape(message)}</pre>" tablefmt='simple')
logger.debug(message) message = f"<pre>{escape(message)}</pre>"
self._send_msg(message, parse_mode=ParseMode.HTML) logger.debug(message)
self._send_msg(message, parse_mode=ParseMode.HTML)
@authorized_only @authorized_only
def _delete_locks(self, update: Update, context: CallbackContext) -> None: def _delete_locks(self, update: Update, context: CallbackContext) -> None: