From 598c29fc90fc5c76604fa75dc644e848868bd7bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20S=C3=A1nchez?= Date: Mon, 25 Nov 2024 17:04:07 -0300 Subject: [PATCH] log cache --- changelog.txt | 4 ++++ exchange_wrapper.py | 24 ++++++++++++++++++++++++ main.py | 34 +++++++++++++++++++++++++++++++++- todo.txt | 1 + 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/changelog.txt b/changelog.txt index 4b44cea..d3dd01a 100755 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,7 @@ +2024.11.25: +. Implemented a short log list: In order to avoid constant log file queries, a list of the last few log entries is stored in memory and it's returned + via /get_log_list API endpoint. + 2024.11.17: . The trader is supplied with a complete open order list, instead of only the ids. . Removed sum_filled_amounts, it was only called once. diff --git a/exchange_wrapper.py b/exchange_wrapper.py index 0098dae..d314a6c 100755 --- a/exchange_wrapper.py +++ b/exchange_wrapper.py @@ -950,6 +950,22 @@ class logger: self.broker_config = broker_config self.exchange_name = self.broker_config["exchange"] self.tg_credentials = credentials.get_credentials("telegram") + self.log_list_max_length = 20 + self.log_list = self.preload_logs() + + + def preload_logs(self): + try: + with open(f"logs/{self.exchange_name}.log","r") as f: + self.log_list = f.readlines() + return self.log_list[-self.log_list_max_length:] + except Exception as e: + print(e) + return [] + + + def get_log_list(self): + return self.log_list def set_telegram_notifications(self, toggle): @@ -990,9 +1006,17 @@ class logger: if level<2: try: + #Write to log file with open(f"logs/{self.exchange_name}.log","a") as log_file: log_file.write(text+"\n") log_file.close() + + #Append to log list + self.log_list.append(text) + + #Trim log list + self.log_list = self.log_list[-self.log_list_max_length:] + except Exception as e: print("Can't write log file") print(e) diff --git a/main.py b/main.py index c3c14de..099febc 100644 --- a/main.py +++ b/main.py @@ -22,7 +22,7 @@ In case the permissions of the certificate changes, reset them this way: # ll /etc/letsencrypt/ ''' -version = "2024.11.17" +version = "2024.11.25" ''' Color definitions. If you want to change them, check the reference at https://en.wikipedia.org/wiki/ANSI_escape_code#Colors @@ -1073,6 +1073,7 @@ def toggle_telegram(): return unwrapped_toggle_telegram() return jsonify({'Error': 'API key invalid'}), 401 + @base_api.route("/server_time", methods=['GET']) def server_time(): ''' @@ -1086,6 +1087,21 @@ def server_time(): return unwrapped_server_time() return jsonify({'Error': 'API key invalid'}), 401 + +@base_api.route("/get_log_list", methods=['GET']) +def get_log_list(): + ''' + GET request + + Parameters: + None + ''' + + if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in valid_keys: + return unwrapped_get_log_list() + return jsonify({'Error': 'API key invalid'}), 401 + + @base_api.route("/trader_time", methods=['GET']) def trader_time(): ''' @@ -1120,6 +1136,8 @@ def loop_wait_time(): return jsonify({'Error': 'Halp'}) return jsonify({'Error': 'API key invalid'}), 401 + + @base_api.route("/edit_call_wait_time", methods=['POST']) def call_wait_time(): ''' @@ -1988,6 +2006,20 @@ def unwrapped_loop_wait_time(wait_time): return jsonify({"Success": "Lap time modified successfully"}) +def unwrapped_get_log_list(): + ''' + Retrieves the last n entries from the broker's logger. + This list is kept on memory, to avoid having to read the log file every time. + + Parameters: + None + + Returns: + jsonify: A jsonified dictionary containing the last n entries from the log file. + ''' + return jsonify({"Logs": broker.logger.get_log_list()}) + + def unwrapped_call_wait_time(wait_time): ''' Modifies the time between some API calls and retries. diff --git a/todo.txt b/todo.txt index c645f87..42a4e3f 100755 --- a/todo.txt +++ b/todo.txt @@ -8,6 +8,7 @@ Mandatory: 4. Base add for short traders. 5. Proper handling of order price too high/low in OKX (rare, it happens when under heavy volatility). 6. Keep a copy of the instance's last n log entries on RAM, to speed up querying. +7. Do the same for the last n deals. Load a few from the db at instance initialization. Would be nice to have: