statistics server optimizations

This commit is contained in:
Nicolás Sánchez 2025-08-18 13:58:36 -03:00
parent 74e24e6249
commit 29c3f37a65
1 changed files with 23 additions and 45 deletions

View File

@ -5,13 +5,13 @@ import calendar
import logging import logging
import threading import threading
import os import os
import functools
from contextlib import contextmanager from contextlib import contextmanager
from flask import Flask, jsonify, request from flask import Flask, jsonify, request
from waitress import serve from waitress import serve
profits_database = "../profits/profits_database.db" profits_database = "../profits/profits_database.db"
_local_storage = threading.local() _local_storage = threading.local()
def get_db_connection(): def get_db_connection():
@ -40,8 +40,6 @@ def db_cursor():
raise raise
def load_keys_from_db(file_name): def load_keys_from_db(file_name):
#valid_keys = []
connection = sqlite3.connect(file_name) connection = sqlite3.connect(file_name)
cursor = connection.cursor() cursor = connection.cursor()
cursor.execute("SELECT * FROM credentials_table") cursor.execute("SELECT * FROM credentials_table")
@ -49,11 +47,10 @@ def load_keys_from_db(file_name):
connection.close() connection.close()
valid_keys = [line[1] for line in data] valid_keys = [line[1] for line in data]
#for line in data:
# valid_keys.append(line[1])
return valid_keys return valid_keys
def get_valid_keys(): def get_valid_keys():
if not hasattr(get_valid_keys, '_keys'): if not hasattr(get_valid_keys, '_keys'):
get_valid_keys._keys = load_keys_from_db("api_credentials.db") get_valid_keys._keys = load_keys_from_db("api_credentials.db")
@ -72,7 +69,6 @@ def profit_report():
ORDER BY day_utc3;""") ORDER BY day_utc3;""")
last_60_days_rows = cursor.fetchall() last_60_days_rows = cursor.fetchall()
#Last 30 days query #Last 30 days query
#cursor.execute("""SELECT strftime('%Y-%m-%d', timestamp, 'unixepoch', '-3 hours') AS day_utc3,
with db_cursor() as cursor: with db_cursor() as cursor:
cursor.execute("""SELECT strftime('%Y-%m-%d', timestamp, 'unixepoch', '-3 hours') AS day_utc3, cursor.execute("""SELECT strftime('%Y-%m-%d', timestamp, 'unixepoch', '-3 hours') AS day_utc3,
SUM(amount) AS total_amount SUM(amount) AS total_amount
@ -123,9 +119,6 @@ def profit_report():
exchange_name, month_group;""") exchange_name, month_group;""")
per_exchange = cursor.fetchall() per_exchange = cursor.fetchall()
#Close db
#cursor.close()
#Projection calculation #Projection calculation
days_in_month = calendar.monthrange(datetime.date.today().year, datetime.date.today().month)[1] days_in_month = calendar.monthrange(datetime.date.today().year, datetime.date.today().month)[1]
@ -182,7 +175,6 @@ def profit_report():
"Total profit": total_amount} "Total profit": total_amount}
def query_total_profit(pair=None): def query_total_profit(pair=None):
''' '''
Returns total profit of the trading pair. Returns total profit of the trading pair.
@ -208,12 +200,11 @@ def query_total_profit(pair=None):
return 0 return 0
def daily_and_monthly_totals(): def daily_and_monthly_totals() -> tuple[float, float]:
''' '''
Returns a tuple with the current day and the current month's total profit. Returns a tuple with the current day and the current month's total profit.
''' '''
now = datetime.datetime.now() now = datetime.datetime.now()
# Create a datetime object for the start of the day # Create a datetime object for the start of the day
@ -224,15 +215,25 @@ def daily_and_monthly_totals():
start_of_day_unix = int(time.mktime(start_of_day.timetuple())) start_of_day_unix = int(time.mktime(start_of_day.timetuple()))
start_of_month_unix = int(time.mktime(start_of_month.timetuple())) start_of_month_unix = int(time.mktime(start_of_month.timetuple()))
query = """SELECT * FROM profits_table query = """SELECT
WHERE timestamp >= ? COALESCE(SUM(CASE WHEN timestamp >= :day THEN amount END),0) AS daily_total,
ORDER BY timestamp DESC;""" COALESCE(SUM(CASE WHEN timestamp >= :month THEN amount END),0) AS monthly_total
with db_cursor() as cursor: FROM profits_table;
cursor.execute(query, (start_of_month_unix,)) """
query_result = cursor.fetchall() with db_cursor() as cur:
cur.execute(query, {"day": start_of_day_unix, "month": start_of_month_unix})
row = cur.fetchone()
daily_total = float(row["daily_total"])
monthly_total = float(row["monthly_total"])
# query = """SELECT * FROM profits_table
# WHERE timestamp >= ?
# ORDER BY timestamp DESC;"""
# with db_cursor() as cursor:
# cursor.execute(query, (start_of_month_unix,))
# query_result = cursor.fetchall()
monthly_total = sum([item[2] for item in query_result]) # monthly_total = sum([item[2] for item in query_result])
daily_total = sum([item[2] for item in query_result if item[0]>=start_of_day_unix]) # daily_total = sum([item[2] for item in query_result if item[0]>=start_of_day_unix])
return (daily_total, monthly_total) return (daily_total, monthly_total)
@ -386,29 +387,6 @@ def fetch_profit_report():
return jsonify({'Error': 'API key invalid'}), 401 return jsonify({'Error': 'API key invalid'}), 401
@stats_api.route("/clear_caches")
def clear_hashes():
global hashes_db
'''
GET request
'''
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in get_valid_keys():
hashes_db = {"fetch_last_n_deals":0,
"fetch_last_n_deals_without_history":0,
"fetch_full_log":0,
"fetch_log":0,
"daily_totals":0,
"daily_totals_by_pair":0,
"monthly_totals":0,
"monthly_totals_by_pair":0,
"get_averages":0,
"total_profit":0,
"total_profit_by_pair":0}
return jsonify({"Done":0})
return jsonify({'Error': 'API key invalid'}), 401
@stats_api.route("/fetch_last_n_deals") @stats_api.route("/fetch_last_n_deals")
def fetch_last_n_deals(): def fetch_last_n_deals():
''' '''