From b4e0ba0b0aa84550d0b06338b59557c3050798c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Fri, 23 Mar 2018 01:11:34 +0100 Subject: Store reports to database Fixes https://git.immae.eu/mantisbt/view.php?id=57 --- market.py | 49 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) (limited to 'market.py') diff --git a/market.py b/market.py index fc5832c..78ced1a 100644 --- a/market.py +++ b/market.py @@ -1,6 +1,7 @@ from ccxt import ExchangeError, NotSupported import ccxt_wrapper as ccxt import time +import psycopg2 from store import * from cachetools.func import ttl_cache from datetime import datetime @@ -13,7 +14,9 @@ class Market: trades = None balances = None - def __init__(self, ccxt_instance, args, user_id=None, report_path=None): + def __init__(self, ccxt_instance, args, + user_id=None, market_id=None, + report_path=None, pg_config=None): self.args = args self.debug = args.debug self.ccxt = ccxt_instance @@ -24,10 +27,13 @@ class Market: self.processor = Processor(self) self.user_id = user_id + self.market_id = market_id self.report_path = report_path + self.pg_config = pg_config @classmethod - def from_config(cls, config, args, user_id=None, report_path=None): + def from_config(cls, config, args, + user_id=None, market_id=None, report_path=None, pg_config=None): config["apiKey"] = config.pop("key", None) ccxt_instance = ccxt.poloniexE(config) @@ -44,20 +50,45 @@ class Market: ccxt_instance.session.request = request_wrap.__get__(ccxt_instance.session, ccxt_instance.session.__class__) - return cls(ccxt_instance, args, user_id=user_id, report_path=report_path) + return cls(ccxt_instance, args, + user_id=user_id, market_id=market_id, + pg_config=pg_config, report_path=report_path) def store_report(self): self.report.merge(Portfolio.report) + date = datetime.now() + if self.report_path is not None: + self.store_file_report(date) + if self.pg_config is not None: + self.store_database_report(date) + + def store_file_report(self, date): try: - if self.report_path is not None: - report_file = "{}/{}_{}".format(self.report_path, datetime.now().isoformat(), self.user_id) - with open(report_file + ".json", "w") as f: - f.write(self.report.to_json()) - with open(report_file + ".log", "w") as f: - f.write("\n".join(map(lambda x: x[1], self.report.print_logs))) + report_file = "{}/{}_{}".format(self.report_path, date.isoformat(), self.user_id) + with open(report_file + ".json", "w") as f: + f.write(self.report.to_json()) + with open(report_file + ".log", "w") as f: + f.write("\n".join(map(lambda x: x[1], self.report.print_logs))) except Exception as e: print("impossible to store report file: {}; {}".format(e.__class__.__name__, e)) + def store_database_report(self, date): + try: + report_query = 'INSERT INTO reports("date", "market_config_id", "debug") VALUES (%s, %s, %s) RETURNING id;' + line_query = 'INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);' + connection = psycopg2.connect(**self.pg_config) + cursor = connection.cursor() + cursor.execute(report_query, (date, self.market_id, self.debug)) + report_id = cursor.fetchone()[0] + for date, type_, payload in self.report.to_json_array(): + cursor.execute(line_query, (date, report_id, type_, payload)) + + connection.commit() + cursor.close() + connection.close() + except Exception as e: + print("impossible to store report to database: {}; {}".format(e.__class__.__name__, e)) + def process(self, actions, before=False, after=False): try: if len(actions or []) == 0: -- cgit v1.2.3