]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - store.py
Store last cryptoportfolio repartition to redis
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / store.py
index cd0bf7babe2e7240588f4605571365f98096305b..81828f0ae3df4a1c60ea5a287a7b4fe531cacf90 100644 (file)
--- a/store.py
+++ b/store.py
@@ -7,6 +7,7 @@ import datetime
 import inspect
 from json import JSONDecodeError
 from simplejson.errors import JSONDecodeError as SimpleJSONDecodeError
+import dbs
 
 __all__ = ["Portfolio", "BalanceStore", "ReportStore", "TradeStore"]
 
@@ -530,11 +531,24 @@ class Portfolio:
         try:
             cls.data.set(r.json(parse_int=D, parse_float=D))
             cls.parse_cryptoportfolio()
+            cls.store_cryptoportfolio()
         except (JSONDecodeError, SimpleJSONDecodeError):
             cls.data.set(None)
             cls.last_date.set(None)
             cls.liquidities.set({})
 
+    @classmethod
+    def store_cryptoportfolio(cls):
+        if dbs.redis_connected():
+            hash_ = {}
+            for liquidity, repartitions in cls.liquidities.items():
+                hash_[liquidity] = repartitions[cls.last_date.get()]
+            dump = json.dumps(hash_)
+            key = "/cryptoportfolio/repartition/latest"
+            dbs.redis.set(key, dump)
+            key = "/cryptoportfolio/repartition/date"
+            dbs.redis.set(key, cls.last_date.date().isoformat())
+
     @classmethod
     def parse_cryptoportfolio(cls):
         def filter_weights(weight_hash):