]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - store.py
Add latest date to redis
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / store.py
index 467dd4b40cfd9ef05a9a0974c88e40f1560d8c1b..072d3a2f02ccbc7dd17c1e92b51a786f57d7c6a5 100644 (file)
--- a/store.py
+++ b/store.py
@@ -17,6 +17,7 @@ class ReportStore:
 
         self.print_logs = []
         self.logs = []
+        self.redis_status = []
 
         self.no_http_dup = no_http_dup
         self.last_http = None
@@ -46,6 +47,10 @@ class ReportStore:
         self.logs.append(hash_)
         return hash_
 
+    def add_redis_status(self, hash_):
+        self.redis_status.append(hash_)
+        return hash_
+
     @staticmethod
     def default_json_serial(obj):
         if isinstance(obj, (datetime.datetime, datetime.date)):
@@ -63,6 +68,13 @@ class ReportStore:
                     json.dumps(log, default=self.default_json_serial, indent="  ")
                     )
 
+    def to_json_redis(self):
+        for log in (x.copy() for x in self.redis_status):
+            yield (
+                    log.pop("type"),
+                    json.dumps(log, default=self.default_json_serial)
+                    )
+
     def set_verbose(self, verbose_print):
         self.verbose_print = verbose_print
 
@@ -91,11 +103,14 @@ class ReportStore:
         for currency, balance in self.market.balances.all.items():
             self.print_log("\t{}".format(balance))
 
-        self.add_log({
-            "type": "balance",
-            "tag": tag,
-            "balances": self.market.balances.as_json()
-            })
+        log = {
+                "type": "balance",
+                "tag": tag,
+                "balances": self.market.balances.as_json()
+                }
+
+        self.add_log(log.copy())
+        self.add_redis_status(log)
 
     def log_tickers(self, amounts, other_currency,
             compute_value, type):
@@ -107,15 +122,18 @@ class ReportStore:
         for currency, amount in amounts.items():
             values[currency] = amount.as_json()["value"]
             rates[currency] = amount.rate
-        self.add_log({
-            "type": "tickers",
-            "compute_value": compute_value,
-            "balance_type": type,
-            "currency": other_currency,
-            "balances": values,
-            "rates": rates,
-            "total": sum(amounts.values()).as_json()["value"]
-            })
+        log = {
+                "type": "tickers",
+                "compute_value": compute_value,
+                "balance_type": type,
+                "currency": other_currency,
+                "balances": values,
+                "rates": rates,
+                "total": sum(amounts.values()).as_json()["value"]
+                }
+
+        self.add_log(log.copy())
+        self.add_redis_status(log)
 
     def log_dispatch(self, amount, amounts, liquidity, repartition):
         self.add_log({
@@ -211,6 +229,7 @@ class ReportStore:
                 "body": body,
                 "headers": headers,
                 "status": response.status_code,
+                "duration": response.elapsed.total_seconds(),
                 "response": None,
                 "response_same_as": self.last_http["date"]
                 })
@@ -222,6 +241,7 @@ class ReportStore:
                 "body": body,
                 "headers": headers,
                 "status": response.status_code,
+                "duration": response.elapsed.total_seconds(),
                 "response": response.text,
                 "response_same_as": None,
                 })
@@ -409,6 +429,7 @@ class Portfolio:
     last_date = LockedVar(None)
     report = LockedVar(ReportStore(None, no_http_dup=True))
     worker = None
+    worker_tag = ""
     worker_started = False
     worker_notify = None
     callback = None
@@ -426,6 +447,7 @@ class Portfolio:
         cls.liquidities.start_lock()
         cls.report.start_lock()
 
+        cls.worker_tag = "[Worker] "
         cls.worker_started = True
         cls.worker.start()
 
@@ -445,7 +467,7 @@ class Portfolio:
             cls.worker_notify.wait()
             if cls.worker_started:
                 cls.worker_notify.clear()
-                cls.report.print_log("Fetching cryptoportfolio")
+                cls.report.print_log("[Worker] Fetching cryptoportfolio")
                 cls.get_cryptoportfolio(refetch=True)
                 cls.callback.set()
                 time.sleep(poll)
@@ -488,7 +510,7 @@ class Portfolio:
             cls.report.log_http_request(r.request.method,
                     r.request.url, r.request.body, r.request.headers, r)
         except Exception as e:
-            cls.report.log_error("get_cryptoportfolio", exception=e)
+            cls.report.log_error("{}get_cryptoportfolio".format(cls.worker_tag), exception=e)
             return
         try:
             cls.data.set(r.json(parse_int=D, parse_float=D))