From ffcba21f748e84ca4e7bac30e2b27f570ba2a299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20S=C3=A1nchez?= Date: Tue, 26 Nov 2024 18:43:17 -0300 Subject: [PATCH] stats server combined totals update --- utils/statistics_server_v3.py | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/utils/statistics_server_v3.py b/utils/statistics_server_v3.py index a04d478..d6fbd36 100644 --- a/utils/statistics_server_v3.py +++ b/utils/statistics_server_v3.py @@ -1,5 +1,7 @@ import sqlite3 import sys +import datetime +import time from flask import Flask, jsonify, request @@ -73,6 +75,36 @@ def query_total_profit(pair=None): return 0 +def daily_and_monthly_totals(): + ''' + Returns a tuple with the current day and the current month's total profit. + ''' + #Connect to db + connection = sqlite3.connect(profits_database) + cursor = connection.cursor() + now = datetime.datetime.now() + + # Create a datetime object for the start of the day + start_of_day = datetime.datetime(now.year, now.month, now.day) + start_of_month = datetime.datetime(now.year, now.month, 1) + + # 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 = f"""SELECT * FROM profits_table + WHERE timestamp >= {start_of_month_unix} + ORDER BY timestamp DESC;""" + cursor.execute(query) + query_result = cursor.fetchall() + connection.close() + + 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) + + def query_daily_totals(pair=None): ''' Returns a dictionary of daily totals of the trading pair. @@ -311,6 +343,14 @@ def fetch_log(): return jsonify({'Error': 'API key invalid'}), 401 +@stats_api.route("/combined_totals") +def combined_totals(): + if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in valid_keys: + daily_totals = daily_and_monthly_totals() + return jsonify({"combined": daily_totals}) + return jsonify({'Error': 'API key invalid'}), 401 + + @stats_api.route("/daily_totals") def get_daily_totals(): if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in valid_keys: