diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-04-20 20:20:02 +0200 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-04-21 00:42:16 +0200 |
commit | 1593c7a9f58ffaea8933f30f683f67c2b155f6b2 (patch) | |
tree | 1191b8438f1f7d52aa7ef736ec2def0595e311d0 /market.py | |
parent | ceb7fc4c9e76857fefbe1dfe3f4dd3830d065a6f (diff) | |
download | Trader-1593c7a9f58ffaea8933f30f683f67c2b155f6b2.tar.gz Trader-1593c7a9f58ffaea8933f30f683f67c2b155f6b2.tar.zst Trader-1593c7a9f58ffaea8933f30f683f67c2b155f6b2.zip |
Store some information to redis
Diffstat (limited to 'market.py')
-rw-r--r-- | market.py | 16 |
1 files changed, 15 insertions, 1 deletions
@@ -2,6 +2,7 @@ from ccxt import ExchangeError, NotSupported, RequestTimeout, InvalidNonce | |||
2 | import ccxt_wrapper as ccxt | 2 | import ccxt_wrapper as ccxt |
3 | import time | 3 | import time |
4 | import psycopg2 | 4 | import psycopg2 |
5 | import redis | ||
5 | from store import * | 6 | from store import * |
6 | from cachetools.func import ttl_cache | 7 | from cachetools.func import ttl_cache |
7 | from datetime import datetime | 8 | from datetime import datetime |
@@ -26,7 +27,7 @@ class Market: | |||
26 | self.balances = BalanceStore(self) | 27 | self.balances = BalanceStore(self) |
27 | self.processor = Processor(self) | 28 | self.processor = Processor(self) |
28 | 29 | ||
29 | for key in ["user_id", "market_id", "pg_config"]: | 30 | for key in ["user_id", "market_id", "pg_config", "redis_config"]: |
30 | setattr(self, key, kwargs.get(key, None)) | 31 | setattr(self, key, kwargs.get(key, None)) |
31 | 32 | ||
32 | self.report.log_market(self.args) | 33 | self.report.log_market(self.args) |
@@ -46,6 +47,8 @@ class Market: | |||
46 | self.store_file_report(date) | 47 | self.store_file_report(date) |
47 | if self.pg_config is not None and self.args.report_db: | 48 | if self.pg_config is not None and self.args.report_db: |
48 | self.store_database_report(date) | 49 | self.store_database_report(date) |
50 | if self.redis_config is not None and self.args.report_redis: | ||
51 | self.store_redis_report(date) | ||
49 | 52 | ||
50 | def store_file_report(self, date): | 53 | def store_file_report(self, date): |
51 | try: | 54 | try: |
@@ -74,6 +77,17 @@ class Market: | |||
74 | except Exception as e: | 77 | except Exception as e: |
75 | print("impossible to store report to database: {}; {}".format(e.__class__.__name__, e)) | 78 | print("impossible to store report to database: {}; {}".format(e.__class__.__name__, e)) |
76 | 79 | ||
80 | def store_redis_report(self, date): | ||
81 | try: | ||
82 | conn = redis.Redis(**self.redis_config) | ||
83 | for type_, log in self.report.to_json_redis(): | ||
84 | key = "/cryptoportfolio/{}/{}/{}".format(self.market_id, date.isoformat(), type_) | ||
85 | conn.set(key, log, ex=31*24*60*60) | ||
86 | key = "/cryptoportfolio/{}/latest/{}".format(self.market_id, type_) | ||
87 | conn.set(key, log) | ||
88 | except Exception as e: | ||
89 | print("impossible to store report to redis: {}; {}".format(e.__class__.__name__, e)) | ||
90 | |||
77 | def process(self, actions, before=False, after=False): | 91 | def process(self, actions, before=False, after=False): |
78 | try: | 92 | try: |
79 | for action in actions: | 93 | for action in actions: |