-
-
Notifications
You must be signed in to change notification settings - Fork 663
/
postgres.py
223 lines (181 loc) · 9.17 KB
/
postgres.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
'''
Copyright (C) 2017-2024 Bryant Moscon - bmoscon@gmail.com
Please see the LICENSE file for the terms and conditions
associated with this software.
'''
from collections import defaultdict
from datetime import datetime as dt
from typing import Tuple
import asyncpg
from yapic import json
from cryptofeed.backends.backend import BackendBookCallback, BackendCallback, BackendQueue
from cryptofeed.defines import CANDLES, FUNDING, OPEN_INTEREST, TICKER, TRADES, LIQUIDATIONS, INDEX
class PostgresCallback(BackendQueue):
def __init__(self, host='127.0.0.1', user=None, pw=None, db=None, port=None, table=None, custom_columns: dict = None, none_to=None, numeric_type=float, **kwargs):
"""
host: str
Database host address
user: str
The name of the database role used for authentication.
db: str
The name of the database to connect to.
pw: str
Password to be used for authentication, if the server requires one.
table: str
Table name to insert into. Defaults to default_table that should be specified in child class
custom_columns: dict
A dictionary which maps Cryptofeed's data type fields to Postgres's table column names, e.g. {'symbol': 'instrument', 'price': 'price', 'amount': 'size'}
Can be a subset of Cryptofeed's available fields (see the cdefs listed under each data type in types.pyx). Can be listed any order.
Note: to store BOOK data in a JSONB column, include a 'data' field, e.g. {'symbol': 'symbol', 'data': 'json_data'}
"""
self.conn = None
self.table = table if table else self.default_table
self.custom_columns = custom_columns
self.numeric_type = numeric_type
self.none_to = none_to
self.user = user
self.db = db
self.pw = pw
self.host = host
self.port = port
# Parse INSERT statement with user-specified column names
# Performed at init to avoid repeated list joins
self.insert_statement = f"INSERT INTO {self.table} ({','.join([v for v in self.custom_columns.values()])}) VALUES " if custom_columns else None
self.running = True
async def _connect(self):
if self.conn is None:
self.conn = await asyncpg.connect(user=self.user, password=self.pw, database=self.db, host=self.host, port=self.port)
def format(self, data: Tuple):
feed = data[0]
symbol = data[1]
timestamp = data[2]
receipt_timestamp = data[3]
data = data[4]
return f"(DEFAULT,'{timestamp}','{receipt_timestamp}','{feed}','{symbol}','{json.dumps(data)}')"
def _custom_format(self, data: Tuple):
d = {
**data[4],
**{
'exchange': data[0],
'symbol': data[1],
'timestamp': data[2],
'receipt': data[3],
}
}
# Cross-ref data dict with user column names from custom_columns dict, inserting NULL if requested data point not present
sequence_gen = (d[field] if d[field] else 'NULL' for field in self.custom_columns.keys())
# Iterate through the generator and surround everything except floats and NULL in single quotes
sql_string = ','.join(str(s) if isinstance(s, float) or s == 'NULL' else "'" + str(s) + "'" for s in sequence_gen)
return f"({sql_string})"
async def writer(self):
while self.running:
async with self.read_queue() as updates:
if len(updates) > 0:
batch = []
for data in updates:
ts = dt.utcfromtimestamp(data['timestamp']) if data['timestamp'] else None
rts = dt.utcfromtimestamp(data['receipt_timestamp'])
batch.append((data['exchange'], data['symbol'], ts, rts, data))
await self.write_batch(batch)
async def write_batch(self, updates: list):
await self._connect()
args_str = ','.join([self.format(u) for u in updates])
async with self.conn.transaction():
try:
if self.custom_columns:
await self.conn.execute(self.insert_statement + args_str)
else:
await self.conn.execute(f"INSERT INTO {self.table} VALUES {args_str}")
except asyncpg.UniqueViolationError:
# when restarting a subscription, some exchanges will re-publish a few messages
pass
class TradePostgres(PostgresCallback, BackendCallback):
default_table = TRADES
def format(self, data: Tuple):
if self.custom_columns:
return self._custom_format(data)
else:
exchange, symbol, timestamp, receipt, data = data
id = f"'{data['id']}'" if data['id'] else 'NULL'
otype = f"'{data['type']}'" if data['type'] else 'NULL'
return f"(DEFAULT,'{timestamp}','{receipt}','{exchange}','{symbol}','{data['side']}',{data['amount']},{data['price']},{id},{otype})"
class FundingPostgres(PostgresCallback, BackendCallback):
default_table = FUNDING
def format(self, data: Tuple):
if self.custom_columns:
if data[4]['next_funding_time']:
data[4]['next_funding_time'] = dt.utcfromtimestamp(data[4]['next_funding_time'])
return self._custom_format(data)
else:
exchange, symbol, timestamp, receipt, data = data
ts = dt.utcfromtimestamp(data['next_funding_time']) if data['next_funding_time'] else 'NULL'
return f"(DEFAULT,'{timestamp}','{receipt}','{exchange}','{symbol}',{data['mark_price'] if data['mark_price'] else 'NULL'},{data['rate']},'{ts}',{data['predicted_rate']})"
class TickerPostgres(PostgresCallback, BackendCallback):
default_table = TICKER
def format(self, data: Tuple):
if self.custom_columns:
return self._custom_format(data)
else:
exchange, symbol, timestamp, receipt, data = data
return f"(DEFAULT,'{timestamp}','{receipt}','{exchange}','{symbol}',{data['bid']},{data['ask']})"
class OpenInterestPostgres(PostgresCallback, BackendCallback):
default_table = OPEN_INTEREST
def format(self, data: Tuple):
if self.custom_columns:
return self._custom_format(data)
else:
exchange, symbol, timestamp, receipt, data = data
return f"(DEFAULT,'{timestamp}','{receipt}','{exchange}','{symbol}',{data['open_interest']})"
class IndexPostgres(PostgresCallback, BackendCallback):
default_table = INDEX
def format(self, data: Tuple):
if self.custom_columns:
return self._custom_format(data)
else:
exchange, symbol, timestamp, receipt, data = data
return f"(DEFAULT,'{timestamp}','{receipt}','{exchange}','{symbol}',{data['price']})"
class LiquidationsPostgres(PostgresCallback, BackendCallback):
default_table = LIQUIDATIONS
def format(self, data: Tuple):
if self.custom_columns:
return self._custom_format(data)
else:
exchange, symbol, timestamp, receipt, data = data
return f"(DEFAULT,'{timestamp}','{receipt}','{exchange}','{symbol}','{data['side']}',{data['quantity']},{data['price']},'{data['id']}','{data['status']}')"
class BookPostgres(PostgresCallback, BackendBookCallback):
default_table = 'book'
def __init__(self, *args, snapshots_only=False, snapshot_interval=1000, **kwargs):
self.snapshots_only = snapshots_only
self.snapshot_interval = snapshot_interval
self.snapshot_count = defaultdict(int)
super().__init__(*args, **kwargs)
def format(self, data: Tuple):
if self.custom_columns:
if 'book' in data[4]:
data[4]['data'] = json.dumps({'snapshot': data[4]['book']})
else:
data[4]['data'] = json.dumps({'delta': data[4]['delta']})
return self._custom_format(data)
else:
feed = data[0]
symbol = data[1]
timestamp = data[2]
receipt_timestamp = data[3]
data = data[4]
if 'book' in data:
data = {'snapshot': data['book']}
else:
data = {'delta': data['delta']}
return f"(DEFAULT,'{timestamp}','{receipt_timestamp}','{feed}','{symbol}','{json.dumps(data)}')"
class CandlesPostgres(PostgresCallback, BackendCallback):
default_table = CANDLES
def format(self, data: Tuple):
if self.custom_columns:
data[4]['start'] = dt.utcfromtimestamp(data[4]['start'])
data[4]['stop'] = dt.utcfromtimestamp(data[4]['stop'])
return self._custom_format(data)
else:
exchange, symbol, timestamp, receipt, data = data
open_ts = dt.utcfromtimestamp(data['start'])
close_ts = dt.utcfromtimestamp(data['stop'])
return f"(DEFAULT,'{timestamp}','{receipt}','{exchange}','{symbol}','{open_ts}','{close_ts}','{data['interval']}',{data['trades'] if data['trades'] is not None else 'NULL'},{data['open']},{data['close']},{data['high']},{data['low']},{data['volume']},{data['closed'] if data['closed'] else 'NULL'})"