-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
303 lines (277 loc) · 6.79 KB
/
database.go
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package main
import (
"database/sql"
"fmt"
"strconv"
"strings"
"time"
"github.com/petertrotman/gdax-scraper/gdax"
"github.com/pkg/errors"
)
// PrepareDB creates the necessary tables if they are not already in the database
func prepareDB(db *sql.DB) error {
var err error
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS messages (
id BIGSERIAL PRIMARY KEY,
type VARCHAR(30),
product_id VARCHAR(10),
trade_id BIGINT,
order_id VARCHAR(60),
sequence BIGINT,
maker_order_id VARCHAR(60),
taker_order_id VARCHAR(60),
time TIMESTAMP,
remaining_size NUMERIC,
new_size NUMERIC,
old_size NUMERIC,
size NUMERIC,
price NUMERIC,
side VARCHAR(10),
reason VARCHAR(60),
order_type VARCHAR(10),
funds NUMERIC,
new_funds NUMERIC,
old_funds NUMERIC,
message VARCHAR(60)
);
`)
if err != nil {
return errors.Wrap(err, "could not create messages table")
}
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS snapshots (
id BIGSERIAL PRIMARY KEY,
product_id VARCHAR(10),
sequence BIGINT,
bid_ask VARCHAR(3),
price NUMERIC,
size NUMERIC,
order_id VARCHAR(60)
);
`)
if err != nil {
return errors.Wrap(err, "could not create snapshots table")
}
return nil
}
type statements struct {
InsertMessage *sql.Stmt
InsertSnapshot *sql.Stmt
}
func prepareStmts(db *sql.DB) (*statements, error) {
insertMessageStmt, err := db.Prepare(`
INSERT INTO messages (
type, product_id, trade_id, order_id, sequence, maker_order_id, taker_order_id, time, remaining_size, new_size, old_size, size, price, side, reason, order_type, funds, new_funds, old_funds, message
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20
);
`)
if err != nil {
return nil, errors.Wrap(err, "could not prepare insert message statement")
}
insertSnapshotStmt, err := db.Prepare(`
INSERT INTO snapshots (
product_id, sequence, bid_ask, price, size, order_id
) VALUES (
$1, $2, $3, $4, $5, $6
);
`)
if err != nil {
return nil, errors.Wrap(err, "could not prepare insert snapshot statement")
}
stmts := statements{
InsertMessage: insertMessageStmt,
InsertSnapshot: insertSnapshotStmt,
}
return &stmts, nil
}
type insertResult struct {
Result sql.Result
Error error
}
// This is required because max_connections on gcloud is 100, and we easily exceed 100 connections otherwise
func batchInsertMessages(db *sql.DB, ch chan *gdax.Message) chan insertResult {
t := time.NewTicker(1 * time.Second)
var messages []*gdax.Message
result := make(chan insertResult)
go func() {
for {
<-ch
}
}()
go func() {
for {
select {
case msg := <-ch:
messages = append(messages, msg)
case <-t.C:
var values []string
for _, m := range messages {
value := fmt.Sprintf(
"('%v','%v',%v,'%v',%v,'%v','%v','%v',%v,%v,%v,%v,%v,'%v','%v','%v',%v,%v,%v,'%v')",
m.Type,
m.ProductId,
m.TradeId,
m.OrderId,
m.Sequence,
m.MakerOrderId,
m.TakerOrderId,
m.Time.Time().Format("2006-01-02T15:04:05.0000Z"), // exchange.Time() -> time.Time()
m.RemainingSize,
m.NewSize,
m.OldSize,
m.Size,
m.Price,
m.Side,
m.Reason,
m.OrderType,
m.Funds,
m.NewFunds,
m.OldFunds,
m.Message.Message, // Because the Message is embedded?
)
values = append(values, value)
}
res, err := db.Exec(fmt.Sprintf(`
INSERT INTO messages (
type, product_id, trade_id, order_id, sequence, maker_order_id, taker_order_id, time, remaining_size, new_size, old_size, size, price, side, reason, order_type, funds, new_funds, old_funds, message
) VALUES %s;
`, strings.Join(values, ",")))
// Reset the messages buffer
messages = []*gdax.Message{}
result <- insertResult{res, err}
}
}
}()
return result
}
func insertMessage(stmts *statements, m *gdax.Message) (sql.Result, error) {
return stmts.InsertMessage.Exec(
m.Type,
m.ProductId,
m.TradeId,
m.OrderId,
m.Sequence,
m.MakerOrderId,
m.TakerOrderId,
m.Time.Time(), // exchange.Time() -> time.Time()
m.RemainingSize,
m.NewSize,
m.OldSize,
m.Size,
m.Price,
m.Side,
m.Reason,
m.OrderType,
m.Funds,
m.NewFunds,
m.OldFunds,
m.Message.Message, // Because the Message is embedded?
)
}
type parsedBidAsk struct {
Price float64
Size float64
OrderID string
}
func batchInsertSnapshot(db *sql.DB, s *gdax.Snapshot) (sql.Result, error) {
parseBidAsk := func(vals []string) (*parsedBidAsk, error) {
price, err := strconv.ParseFloat(vals[0], 64)
if err != nil {
return nil, errors.Wrap(err, "could not convert price to float")
}
size, err := strconv.ParseFloat(vals[1], 64)
if err != nil {
return nil, errors.Wrap(err, "could not convert size to float")
}
return &parsedBidAsk{price, size, vals[2]}, nil
}
var values []string
for _, bid := range s.Bids {
b, err := parseBidAsk(bid)
if err != nil {
return nil, errors.Wrap(err, "could not parse bid")
}
value := fmt.Sprintf("('%v',%v,'%v',%v,%v,'%v')",
s.ProductID,
s.Sequence,
"bid",
b.Price,
b.Size,
b.OrderID,
)
values = append(values, value)
}
for _, ask := range s.Asks {
a, err := parseBidAsk(ask)
if err != nil {
return nil, errors.Wrap(err, "could not parse ask")
}
value := fmt.Sprintf("('%v',%v,'%v',%v,%v,'%v')",
s.ProductID,
s.Sequence,
"ask",
a.Price,
a.Size,
a.OrderID,
)
values = append(values, value)
}
return db.Exec(fmt.Sprintf(`
INSERT INTO snapshots (
product_id, sequence, bid_ask, price, size, order_id
) VALUES %s;
`, strings.Join(values, ",")))
}
func insertSnapshot(stmts *statements, s *gdax.Snapshot) ([]sql.Result, error) {
parseBidAsk := func(vals []string) (*parsedBidAsk, error) {
price, err := strconv.ParseFloat(vals[0], 64)
if err != nil {
return nil, errors.Wrap(err, "could not convert price to float")
}
size, err := strconv.ParseFloat(vals[1], 64)
if err != nil {
return nil, errors.Wrap(err, "could not convert size to float")
}
return &parsedBidAsk{price, size, vals[2]}, nil
}
var results []sql.Result
for _, bid := range s.Bids {
b, err := parseBidAsk(bid)
if err != nil {
return nil, errors.Wrap(err, "could not parse bid")
}
result, err := stmts.InsertSnapshot.Exec(
s.ProductID,
s.Sequence,
"bid",
b.Price,
b.Size,
b.OrderID,
)
if err != nil {
return nil, errors.Wrap(err, "could not insert bid")
}
results = append(results, result)
}
for _, ask := range s.Asks {
a, err := parseBidAsk(ask)
if err != nil {
return nil, errors.Wrap(err, "could not parse ask")
}
result, err := stmts.InsertSnapshot.Exec(
s.ProductID,
s.Sequence,
"ask",
a.Price,
a.Size,
a.OrderID,
)
if err != nil {
return nil, errors.Wrap(err, "could not insert ask")
}
results = append(results, result)
}
return results, nil
}