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 threading
import os
import functools
from contextlib import contextmanager
from flask import Flask, jsonify, request
from waitress import serve
profits_database = "../profits/profits_database.db"
_local_storage = threading.local()
def get_db_connection():
@ -40,8 +40,6 @@ def db_cursor():
raise
def load_keys_from_db(file_name):
#valid_keys = []
connection = sqlite3.connect(file_name)
cursor = connection.cursor()
cursor.execute("SELECT * FROM credentials_table")
@ -49,17 +47,16 @@ def load_keys_from_db(file_name):
connection.close()
valid_keys = [line[1] for line in data]
#for line in data:
# valid_keys.append(line[1])
return valid_keys
def get_valid_keys():
if not hasattr(get_valid_keys, '_keys'):
get_valid_keys._keys = load_keys_from_db("api_credentials.db")
return get_valid_keys._keys
def profit_report():
##Queries
#Last 60 days query
@ -72,7 +69,6 @@ def profit_report():
ORDER BY day_utc3;""")
last_60_days_rows = cursor.fetchall()
#Last 30 days query
#cursor.execute("""SELECT strftime('%Y-%m-%d', timestamp, 'unixepoch', '-3 hours') AS day_utc3,
with db_cursor() as cursor:
cursor.execute("""SELECT strftime('%Y-%m-%d', timestamp, 'unixepoch', '-3 hours') AS day_utc3,
SUM(amount) AS total_amount
@ -123,9 +119,6 @@ def profit_report():
exchange_name, month_group;""")
per_exchange = cursor.fetchall()
#Close db
#cursor.close()
#Projection calculation
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}
def query_total_profit(pair=None):
'''
Returns total profit of the trading pair.
@ -208,12 +200,11 @@ def query_total_profit(pair=None):
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.
'''
now = datetime.datetime.now()
# Create a datetime object for the start of the day
@ -223,16 +214,26 @@ def daily_and_monthly_totals():
# Convert the start of the day to Unix time
start_of_day_unix = int(time.mktime(start_of_day.timetuple()))
start_of_month_unix = int(time.mktime(start_of_month.timetuple()))
query = """SELECT
COALESCE(SUM(CASE WHEN timestamp >= :day THEN amount END),0) AS daily_total,
COALESCE(SUM(CASE WHEN timestamp >= :month THEN amount END),0) AS monthly_total
FROM profits_table;
"""
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()
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])
daily_total = sum([item[2] for item in query_result if item[0]>=start_of_day_unix])
# 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])
return (daily_total, monthly_total)
@ -386,29 +387,6 @@ def fetch_profit_report():
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")
def fetch_last_n_deals():
'''