stats server combined totals update
This commit is contained in:
parent
c4057cdacc
commit
ffcba21f74
|
|
@ -1,5 +1,7 @@
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import sys
|
import sys
|
||||||
|
import datetime
|
||||||
|
import time
|
||||||
from flask import Flask, jsonify, request
|
from flask import Flask, jsonify, request
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -73,6 +75,36 @@ def query_total_profit(pair=None):
|
||||||
return 0
|
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):
|
def query_daily_totals(pair=None):
|
||||||
'''
|
'''
|
||||||
Returns a dictionary of daily totals of the trading pair.
|
Returns a dictionary of daily totals of the trading pair.
|
||||||
|
|
@ -311,6 +343,14 @@ def fetch_log():
|
||||||
return jsonify({'Error': 'API key invalid'}), 401
|
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")
|
@stats_api.route("/daily_totals")
|
||||||
def get_daily_totals():
|
def get_daily_totals():
|
||||||
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in valid_keys:
|
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in valid_keys:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue