log cache

This commit is contained in:
Nicolás Sánchez 2024-11-25 17:04:07 -03:00
parent e9c84c3ed1
commit 598c29fc90
4 changed files with 62 additions and 1 deletions

View File

@ -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.

View File

@ -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)

34
main.py
View File

@ -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.

View File

@ -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: