minor refactorings
This commit is contained in:
parent
29c3f37a65
commit
c667c70a64
|
|
@ -5,9 +5,10 @@ import calendar
|
|||
import logging
|
||||
import threading
|
||||
import os
|
||||
import functools
|
||||
from collections import deque
|
||||
from typing import Iterable, List, Tuple
|
||||
from contextlib import contextmanager
|
||||
from flask import Flask, jsonify, request
|
||||
from flask import Flask, jsonify, request, Response
|
||||
from waitress import serve
|
||||
|
||||
|
||||
|
|
@ -225,15 +226,6 @@ def daily_and_monthly_totals() -> tuple[float, float]:
|
|||
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])
|
||||
# daily_total = sum([item[2] for item in query_result if item[0]>=start_of_day_unix])
|
||||
|
||||
return (daily_total, monthly_total)
|
||||
|
||||
|
|
@ -378,13 +370,14 @@ def fetch_profit_report():
|
|||
Returns: JSON object with profit report data
|
||||
'''
|
||||
|
||||
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
try:
|
||||
return jsonify(profit_report())
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({"Error": f"{e}"})
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
if not "X-API-KEY" in request.headers or not request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
try:
|
||||
return jsonify(profit_report())
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({"Error": f"{e}"})
|
||||
|
||||
|
||||
|
||||
@stats_api.route("/fetch_last_n_deals")
|
||||
|
|
@ -393,15 +386,15 @@ def fetch_last_n_deals():
|
|||
GET request
|
||||
Parameter: 'amount_of_deals' -> int
|
||||
'''
|
||||
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
try:
|
||||
parameter = request.args.get("amount_of_deals")
|
||||
response_value = last_n_deals(parameter)
|
||||
return jsonify({"last_deals": response_value})
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({"last_deals":""})
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
if not "X-API-KEY" in request.headers or not request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
try:
|
||||
parameter = request.args.get("amount_of_deals")
|
||||
response_value = last_n_deals(parameter)
|
||||
return jsonify({"last_deals": response_value})
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({"last_deals":""})
|
||||
|
||||
|
||||
@stats_api.route("/fetch_last_n_deals_without_history")
|
||||
|
|
@ -410,16 +403,16 @@ def fetch_last_n_deals_without_history():
|
|||
GET request
|
||||
Parameter: 'amount_of_deals' -> int
|
||||
'''
|
||||
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
try:
|
||||
parameter = request.args.get("amount_of_deals")
|
||||
#return jsonify({"last_deals": last_n_deals_without_history(parameter)})
|
||||
response_value = last_n_deals_without_history(parameter)
|
||||
return jsonify({"last_deals": response_value})
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({"last_deals":""})
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
if not "X-API-KEY" in request.headers or not request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
try:
|
||||
parameter = request.args.get("amount_of_deals")
|
||||
#return jsonify({"last_deals": last_n_deals_without_history(parameter)})
|
||||
response_value = last_n_deals_without_history(parameter)
|
||||
return jsonify({"last_deals": response_value})
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({"last_deals":""})
|
||||
|
||||
|
||||
@stats_api.route("/fetch_full_log")
|
||||
|
|
@ -430,17 +423,16 @@ def fetch_full_log():
|
|||
|
||||
It trims the full log to 200 lines, to avoid sending too much data to the client.
|
||||
'''
|
||||
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
try:
|
||||
exchange_name = request.args.get("exchange_name")
|
||||
width = 0
|
||||
#last_lines,amount_of_lines = last_n_lines(f"../logs/{exchange_name}.log",width,0,full_log=True)
|
||||
last_lines, amount_of_lines = tail_log(f"../logs/{exchange_name}.log", 200)
|
||||
return jsonify({"line": last_lines[-200:], "amount_of_lines": amount_of_lines})
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return {"line": [""]*width,"amount_of_lines": 0}
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
if not "X-API-KEY" in request.headers or not request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
try:
|
||||
exchange_name = request.args.get("exchange_name")
|
||||
width = 0
|
||||
last_lines, amount_of_lines = tail_log(f"../logs/{exchange_name}.log", 200)
|
||||
return jsonify({"line": last_lines[-200:], "amount_of_lines": amount_of_lines})
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return {"line": [""]*width,"amount_of_lines": 0}
|
||||
|
||||
|
||||
@stats_api.route("/fetch_log")
|
||||
|
|
@ -451,33 +443,33 @@ def fetch_log():
|
|||
'width' -> int
|
||||
'amount' -> int
|
||||
'''
|
||||
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
try:
|
||||
exchange_name = request.args.get("exchange_name")
|
||||
width = int(request.args.get("width")) # type: ignore
|
||||
amount = int(request.args.get("amount")) # type: ignore
|
||||
last_lines,total_amount_of_lines = last_n_lines(f"../logs/{exchange_name}.log",width,amount)
|
||||
return jsonify({"line": last_lines, "amount_of_lines": total_amount_of_lines})
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return {"line": [""]*10,"amount_of_lines": 0}
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
if not "X-API-KEY" in request.headers or not request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
try:
|
||||
exchange_name = request.args.get("exchange_name")
|
||||
width = int(request.args.get("width")) # type: ignore
|
||||
amount = int(request.args.get("amount")) # type: ignore
|
||||
last_lines,total_amount_of_lines = last_n_lines(f"../logs/{exchange_name}.log",width,amount)
|
||||
return jsonify({"line": last_lines, "amount_of_lines": total_amount_of_lines})
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return {"line": [""]*10,"amount_of_lines": 0}
|
||||
|
||||
|
||||
@stats_api.route("/combined_totals")
|
||||
def combined_totals():
|
||||
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
daily_totals = daily_and_monthly_totals()
|
||||
return jsonify({"combined": daily_totals})
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
if not "X-API-KEY" in request.headers or not request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
daily_totals = daily_and_monthly_totals()
|
||||
return jsonify({"combined": daily_totals})
|
||||
|
||||
|
||||
@stats_api.route("/daily_totals")
|
||||
def get_daily_totals():
|
||||
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
daily_totals = query_daily_totals()
|
||||
return jsonify(daily_totals)
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
if not "X-API-KEY" in request.headers or not request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
daily_totals = query_daily_totals()
|
||||
return jsonify(daily_totals)
|
||||
|
||||
|
||||
@stats_api.route("/daily_totals_by_pair")
|
||||
|
|
@ -487,24 +479,24 @@ def get_daily_totals_by_pair():
|
|||
Parameters: 'base' -> string
|
||||
'quote' -> string
|
||||
'''
|
||||
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
try:
|
||||
base = request.args.get("base")
|
||||
quote = request.args.get("quote")
|
||||
daily_totals = query_daily_totals(f"{base}{quote}")
|
||||
return jsonify(daily_totals)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({'Error': 'Halp'})
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
if not "X-API-KEY" in request.headers or not request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
try:
|
||||
base = request.args.get("base")
|
||||
quote = request.args.get("quote")
|
||||
daily_totals = query_daily_totals(f"{base}{quote}")
|
||||
return jsonify(daily_totals)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({'Error': 'Halp'})
|
||||
|
||||
|
||||
@stats_api.route("/monthly_totals")
|
||||
def get_monthly_totals():
|
||||
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
monthly_totals = query_monthly_totals()
|
||||
return jsonify(monthly_totals)
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
if not "X-API-KEY" in request.headers or not request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
monthly_totals = query_monthly_totals()
|
||||
return jsonify(monthly_totals)
|
||||
|
||||
|
||||
@stats_api.route("/monthly_totals_by_pair")
|
||||
|
|
@ -514,55 +506,47 @@ def get_monthly_totals_by_pair():
|
|||
Parameters: 'base' -> string
|
||||
'quote' -> string
|
||||
'''
|
||||
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
try:
|
||||
base = request.args.get("base")
|
||||
quote = request.args.get("quote")
|
||||
monthly_totals = query_monthly_totals(f"{base}{quote}")
|
||||
return jsonify(monthly_totals)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({'Error': 'Halp'})
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
if not "X-API-KEY" in request.headers or not request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
try:
|
||||
base = request.args.get("base")
|
||||
quote = request.args.get("quote")
|
||||
monthly_totals = query_monthly_totals(f"{base}{quote}")
|
||||
return jsonify(monthly_totals)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({'Error': 'Halp'})
|
||||
|
||||
|
||||
@stats_api.route("/get_averages")
|
||||
def get_averages():
|
||||
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
try:
|
||||
daily_totals = query_daily_totals()
|
||||
|
||||
val_30 = 0
|
||||
val_7 = 0
|
||||
#acc_30 = []
|
||||
#acc_7 = []
|
||||
#for x in sorted(daily_totals):
|
||||
# acc_30.append(daily_totals[x])
|
||||
# acc_7.append(daily_totals[x])
|
||||
|
||||
recent_days = sorted(daily_totals.keys(), reverse=True)[:30]
|
||||
acc_30 = [daily_totals[date] for date in recent_days[:30]]
|
||||
acc_7 = [daily_totals[date] for date in recent_days[:7]]
|
||||
|
||||
length_30 = min(30,len(acc_30)) #Last 30 days
|
||||
length_7 = min(7,len(acc_7)) #Last 7 days
|
||||
for _ in range(length_30):
|
||||
val_30 += acc_30.pop()
|
||||
for _ in range(length_7):
|
||||
val_7 += acc_7.pop()
|
||||
return jsonify({"30_day": val_30/length_30, "7_day": val_7/length_7})
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({'Error': 'Halp'})
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
if not "X-API-KEY" in request.headers or not request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
try:
|
||||
daily_totals = query_daily_totals()
|
||||
val_30 = 0
|
||||
val_7 = 0
|
||||
recent_days = sorted(daily_totals.keys(), reverse=True)[:30]
|
||||
acc_30 = [daily_totals[date] for date in recent_days[:30]]
|
||||
acc_7 = [daily_totals[date] for date in recent_days[:7]]
|
||||
length_30 = min(30,len(acc_30)) #Last 30 days
|
||||
length_7 = min(7,len(acc_7)) #Last 7 days
|
||||
for _ in range(length_30):
|
||||
val_30 += acc_30.pop()
|
||||
for _ in range(length_7):
|
||||
val_7 += acc_7.pop()
|
||||
return jsonify({"30_day": val_30/length_30, "7_day": val_7/length_7})
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({'Error': 'Halp'})
|
||||
|
||||
|
||||
@stats_api.route("/total_profit")
|
||||
def total_profit():
|
||||
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
total = query_total_profit()
|
||||
return jsonify({"Total profit": total})
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
if not "X-API-KEY" in request.headers or not request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
total = query_total_profit()
|
||||
return jsonify({"Total profit": total})
|
||||
|
||||
|
||||
@stats_api.route("/total_profit_by_pair")
|
||||
|
|
@ -572,22 +556,20 @@ def total_profit_by_pair():
|
|||
Parameters: 'base' -> string
|
||||
'quote' -> string
|
||||
'''
|
||||
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
try:
|
||||
base = request.args.get("base")
|
||||
quote = request.args.get("quote")
|
||||
total = query_total_profit(f"{base}{quote}")
|
||||
return jsonify({"Total profit": total})
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({'Error': 'Halp'})
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
if not "X-API-KEY" in request.headers or not request.headers.get("X-API-KEY") in get_valid_keys():
|
||||
return jsonify({'Error': 'API key invalid'}), 401
|
||||
try:
|
||||
base = request.args.get("base")
|
||||
quote = request.args.get("quote")
|
||||
total = query_total_profit(f"{base}{quote}")
|
||||
return jsonify({"Total profit": total})
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({'Error': 'Halp'})
|
||||
|
||||
|
||||
|
||||
if __name__=="__main__":
|
||||
# Load valid keys from database
|
||||
#valid_keys = load_keys_from_db("api_credentials.db")
|
||||
|
||||
#Waitress
|
||||
logger = logging.getLogger('waitress')
|
||||
|
|
|
|||
Loading…
Reference in New Issue