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