X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=store.py;fp=store.py;h=467dd4b40cfd9ef05a9a0974c88e40f1560d8c1b;hb=e7d7c0e5645da35adcbfec9e51deb68f012c422f;hp=67e8a8fad7f9ce3698095914351eb4602fe7564d;hpb=c682bdf4a02a45312ef1aadf8aa26136cf308414;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/store.py b/store.py index 67e8a8f..467dd4b 100644 --- a/store.py +++ b/store.py @@ -3,7 +3,7 @@ import requests import portfolio import simplejson as json from decimal import Decimal as D, ROUND_DOWN -from datetime import date, datetime, timedelta +import datetime import inspect from json import JSONDecodeError from simplejson.errors import JSONDecodeError as SimpleJSONDecodeError @@ -11,13 +11,16 @@ from simplejson.errors import JSONDecodeError as SimpleJSONDecodeError __all__ = ["Portfolio", "BalanceStore", "ReportStore", "TradeStore"] class ReportStore: - def __init__(self, market, verbose_print=True): + def __init__(self, market, verbose_print=True, no_http_dup=False): self.market = market self.verbose_print = verbose_print self.print_logs = [] self.logs = [] + self.no_http_dup = no_http_dup + self.last_http = None + def merge(self, other_report): self.logs += other_report.logs self.logs.sort(key=lambda x: x["date"]) @@ -26,19 +29,26 @@ class ReportStore: self.print_logs.sort(key=lambda x: x[0]) def print_log(self, message): - now = datetime.now() + now = datetime.datetime.now() message = "{:%Y-%m-%d %H:%M:%S}: {}".format(now, str(message)) self.print_logs.append([now, message]) if self.verbose_print: print(message) def add_log(self, hash_): - hash_["date"] = datetime.now() + hash_["date"] = datetime.datetime.now() + if self.market is not None: + hash_["user_id"] = self.market.user_id + hash_["market_id"] = self.market.market_id + else: + hash_["user_id"] = None + hash_["market_id"] = None self.logs.append(hash_) + return hash_ @staticmethod def default_json_serial(obj): - if isinstance(obj, (datetime, date)): + if isinstance(obj, (datetime.datetime, datetime.date)): return obj.isoformat() return str(obj) @@ -188,7 +198,12 @@ class ReportStore: "error": response.__class__.__name__, "error_message": str(response), }) - else: + self.last_http = None + elif self.no_http_dup and \ + self.last_http is not None and \ + self.last_http["url"] == url and \ + self.last_http["method"] == method and \ + self.last_http["response"] == response.text: self.add_log({ "type": "http_request", "method": method, @@ -196,7 +211,19 @@ class ReportStore: "body": body, "headers": headers, "status": response.status_code, - "response": response.text + "response": None, + "response_same_as": self.last_http["date"] + }) + else: + self.last_http = self.add_log({ + "type": "http_request", + "method": method, + "url": url, + "body": body, + "headers": headers, + "status": response.status_code, + "response": response.text, + "response_same_as": None, }) def log_error(self, action, message=None, exception=None): @@ -222,13 +249,11 @@ class ReportStore: "action": action, }) - def log_market(self, args, user_id, market_id): + def log_market(self, args): self.add_log({ "type": "market", "commit": "$Format:%H$", "args": vars(args), - "user_id": user_id, - "market_id": market_id, }) class BalanceStore: @@ -382,7 +407,7 @@ class Portfolio: data = LockedVar(None) liquidities = LockedVar({}) last_date = LockedVar(None) - report = LockedVar(ReportStore(None)) + report = LockedVar(ReportStore(None, no_http_dup=True)) worker = None worker_started = False worker_notify = None @@ -418,11 +443,17 @@ class Portfolio: raise RuntimeError("This method needs to be ran with the worker") while cls.worker_started: cls.worker_notify.wait() - cls.worker_notify.clear() - cls.report.print_log("Fetching cryptoportfolio") - cls.get_cryptoportfolio(refetch=True) - cls.callback.set() - time.sleep(poll) + if cls.worker_started: + cls.worker_notify.clear() + cls.report.print_log("Fetching cryptoportfolio") + cls.get_cryptoportfolio(refetch=True) + cls.callback.set() + time.sleep(poll) + + @classmethod + def stop_worker(cls): + cls.worker_started = False + cls.worker_notify.set() @classmethod def notify_and_wait(cls): @@ -433,7 +464,7 @@ class Portfolio: @classmethod def wait_for_recent(cls, delta=4, poll=30): cls.get_cryptoportfolio() - while cls.last_date.get() is None or datetime.now() - cls.last_date.get() > timedelta(delta): + while cls.last_date.get() is None or datetime.datetime.now() - cls.last_date.get() > datetime.timedelta(delta): if cls.worker is None: time.sleep(poll) cls.report.print_log("Attempt to fetch up-to-date cryptoportfolio") @@ -490,7 +521,7 @@ class Portfolio: weights_hash = portfolio_hash["weights"] weights = {} for i in range(len(weights_hash["_row"])): - date = datetime.strptime(weights_hash["_row"][i], "%Y-%m-%d") + date = datetime.datetime.strptime(weights_hash["_row"][i], "%Y-%m-%d") weights[date] = dict(filter( filter_weights, map(clean_weights(i), weights_hash.items()))) @@ -504,8 +535,7 @@ class Portfolio: "high": high_liquidity, }) cls.last_date.set(max( - max(medium_liquidity.keys(), default=datetime(1, 1, 1)), - max(high_liquidity.keys(), default=datetime(1, 1, 1)) + max(medium_liquidity.keys(), default=datetime.datetime(1, 1, 1)), + max(high_liquidity.keys(), default=datetime.datetime(1, 1, 1)) )) -