aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2018-03-24 10:27:39 +0100
committerIsmaël Bouya <ismael.bouya@normalesup.org>2018-03-24 11:21:28 +0100
commitec89494c4a4272c11af5f94ba4a8d77133d4678f (patch)
treeec064dc612db4dea6e0868451998b5665750eb9b
parent3faa0d739e599daafacf948534d2e66292387d02 (diff)
downloadTrader-ec89494c4a4272c11af5f94ba4a8d77133d4678f.tar.gz
Trader-ec89494c4a4272c11af5f94ba4a8d77133d4678f.tar.zst
Trader-ec89494c4a4272c11af5f94ba4a8d77133d4678f.zip
Add task to migrate reports to database
-rw-r--r--tasks/import_reports_to_database.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/tasks/import_reports_to_database.py b/tasks/import_reports_to_database.py
new file mode 100644
index 0000000..6031cbe
--- /dev/null
+++ b/tasks/import_reports_to_database.py
@@ -0,0 +1,47 @@
1import sys
2import os
3import simplejson as json
4from datetime import datetime
5from decimal import Decimal as D
6import psycopg2
7
8sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9from main import parse_config
10
11config = sys.argv[1]
12reports = sys.argv[2:]
13
14pg_config, report_path = parse_config(config)
15
16connection = psycopg2.connect(**pg_config)
17cursor = connection.cursor()
18
19report_query = 'INSERT INTO reports("date", "market_config_id", "debug") VALUES (%s, %s, %s) RETURNING id'
20line_query = 'INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s)'
21market_config_query = "SELECT id FROM market_configs WHERE user_id = %s AND market_name = 'poloniex'"
22
23for report in reports:
24 with open(report, "rb") as f:
25 json_content = json.load(f, parse_float=D)
26 basename = os.path.basename(report)
27 date, rest = basename.split("_", 1)
28 user_id, rest = rest.split(".", 1)
29
30 date = datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%f")
31 cursor.execute(market_config_query, user_id)
32 market_id = cursor.fetchone()[0]
33 debug = any("debug" in x and x["debug"] for x in json_content)
34 print(market_id, date, debug)
35 cursor.execute(report_query, (date, market_id, debug))
36 report_id = cursor.fetchone()[0]
37
38 for line in json_content:
39 date = datetime.strptime(line["date"], "%Y-%m-%dT%H:%M:%S.%f")
40 type_ = line["type"]
41 del(line["date"])
42 del(line["type"])
43
44 cursor.execute(line_query, (date, report_id, type_, json.dumps(line, indent=" ")))
45connection.commit()
46cursor.close()
47connection.close()