diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-05-02 01:39:35 +0200 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-05-02 01:39:35 +0200 |
commit | 30700830b6c0aaaa59c148ebd8edb6931040ed13 (patch) | |
tree | 8868074072e95d9b9119b36e3d6d66a70408775a /market.py | |
parent | 2b1ee8f4d54fa1672510141a71a5817120ac031c (diff) | |
download | Trader-30700830b6c0aaaa59c148ebd8edb6931040ed13.tar.gz Trader-30700830b6c0aaaa59c148ebd8edb6931040ed13.tar.zst Trader-30700830b6c0aaaa59c148ebd8edb6931040ed13.zip |
Refactor databases access
Diffstat (limited to 'market.py')
-rw-r--r-- | market.py | 22 |
1 files changed, 9 insertions, 13 deletions
@@ -1,8 +1,7 @@ | |||
1 | from ccxt import ExchangeError, NotSupported, RequestTimeout, InvalidNonce | 1 | 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 dbs |
5 | import redis | ||
6 | from store import * | 5 | from store import * |
7 | from cachetools.func import ttl_cache | 6 | from cachetools.func import ttl_cache |
8 | from datetime import datetime | 7 | from datetime import datetime |
@@ -27,7 +26,7 @@ class Market: | |||
27 | self.balances = BalanceStore(self) | 26 | self.balances = BalanceStore(self) |
28 | self.processor = Processor(self) | 27 | self.processor = Processor(self) |
29 | 28 | ||
30 | for key in ["user_id", "market_id", "pg_config", "redis_config"]: | 29 | for key in ["user_id", "market_id"]: |
31 | setattr(self, key, kwargs.get(key, None)) | 30 | setattr(self, key, kwargs.get(key, None)) |
32 | 31 | ||
33 | self.report.log_market(self.args) | 32 | self.report.log_market(self.args) |
@@ -45,9 +44,9 @@ class Market: | |||
45 | date = datetime.datetime.now() | 44 | date = datetime.datetime.now() |
46 | if self.args.report_path is not None: | 45 | if self.args.report_path is not None: |
47 | self.store_file_report(date) | 46 | self.store_file_report(date) |
48 | if self.pg_config is not None and self.args.report_db: | 47 | if dbs.psql_connected() and self.args.report_db: |
49 | self.store_database_report(date) | 48 | self.store_database_report(date) |
50 | if self.redis_config is not None and self.args.report_redis: | 49 | if dbs.redis_connected() and self.args.report_redis: |
51 | self.store_redis_report(date) | 50 | self.store_redis_report(date) |
52 | 51 | ||
53 | def store_file_report(self, date): | 52 | def store_file_report(self, date): |
@@ -64,29 +63,26 @@ class Market: | |||
64 | try: | 63 | try: |
65 | report_query = 'INSERT INTO reports("date", "market_config_id", "debug") VALUES (%s, %s, %s) RETURNING id;' | 64 | report_query = 'INSERT INTO reports("date", "market_config_id", "debug") VALUES (%s, %s, %s) RETURNING id;' |
66 | line_query = 'INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);' | 65 | line_query = 'INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);' |
67 | connection = psycopg2.connect(**self.pg_config) | 66 | cursor = dbs.psql.cursor() |
68 | cursor = connection.cursor() | ||
69 | cursor.execute(report_query, (date, self.market_id, self.debug)) | 67 | cursor.execute(report_query, (date, self.market_id, self.debug)) |
70 | report_id = cursor.fetchone()[0] | 68 | report_id = cursor.fetchone()[0] |
71 | for date, type_, payload in self.report.to_json_array(): | 69 | for date, type_, payload in self.report.to_json_array(): |
72 | cursor.execute(line_query, (date, report_id, type_, payload)) | 70 | cursor.execute(line_query, (date, report_id, type_, payload)) |
73 | 71 | ||
74 | connection.commit() | 72 | dbs.psql.commit() |
75 | cursor.close() | 73 | cursor.close() |
76 | connection.close() | ||
77 | except Exception as e: | 74 | except Exception as e: |
78 | print("impossible to store report to database: {}; {}".format(e.__class__.__name__, e)) | 75 | print("impossible to store report to database: {}; {}".format(e.__class__.__name__, e)) |
79 | 76 | ||
80 | def store_redis_report(self, date): | 77 | def store_redis_report(self, date): |
81 | try: | 78 | try: |
82 | conn = redis.Redis(**self.redis_config) | ||
83 | for type_, log in self.report.to_json_redis(): | 79 | for type_, log in self.report.to_json_redis(): |
84 | key = "/cryptoportfolio/{}/{}/{}".format(self.market_id, date.isoformat(), type_) | 80 | key = "/cryptoportfolio/{}/{}/{}".format(self.market_id, date.isoformat(), type_) |
85 | conn.set(key, log, ex=31*24*60*60) | 81 | dbs.redis.set(key, log, ex=31*24*60*60) |
86 | key = "/cryptoportfolio/{}/latest/{}".format(self.market_id, type_) | 82 | key = "/cryptoportfolio/{}/latest/{}".format(self.market_id, type_) |
87 | conn.set(key, log) | 83 | dbs.redis.set(key, log) |
88 | key = "/cryptoportfolio/{}/latest/date".format(self.market_id) | 84 | key = "/cryptoportfolio/{}/latest/date".format(self.market_id) |
89 | conn.set(key, date.isoformat()) | 85 | dbs.redis.set(key, date.isoformat()) |
90 | except Exception as e: | 86 | except Exception as e: |
91 | print("impossible to store report to redis: {}; {}".format(e.__class__.__name__, e)) | 87 | print("impossible to store report to redis: {}; {}".format(e.__class__.__name__, e)) |
92 | 88 | ||