-
Notifications
You must be signed in to change notification settings - Fork 19
/
candles.go
318 lines (253 loc) · 9.13 KB
/
candles.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.DATANODE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package sqlstore
import (
"context"
"encoding/hex"
"fmt"
"strings"
"time"
"code.vegaprotocol.io/vega/datanode/candlesv2"
"code.vegaprotocol.io/vega/datanode/metrics"
v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
"github.com/shopspring/decimal"
"code.vegaprotocol.io/vega/datanode/entities"
"github.com/georgysavva/scany/pgxscan"
)
const (
sourceDataTableName = "trades"
candlesViewNamePrePend = sourceDataTableName + "_candle_"
)
var candleOrdering = TableOrdering{
ColumnOrdering{Name: "period_start", Sorting: ASC},
}
type Candles struct {
*ConnectionSource
config candlesv2.CandleStoreConfig
ctx context.Context
}
func NewCandles(ctx context.Context, connectionSource *ConnectionSource, config candlesv2.CandleStoreConfig) *Candles {
return &Candles{
ConnectionSource: connectionSource,
ctx: ctx,
config: config,
}
}
// GetCandleDataForTimeSpan gets the candles for a given interval, from and to are optional.
func (cs *Candles) GetCandleDataForTimeSpan(ctx context.Context, candleID string, from *time.Time, to *time.Time,
p entities.CursorPagination) ([]entities.Candle, entities.PageInfo, error,
) {
pageInfo := entities.PageInfo{}
descriptor, err := candleDescriptorFromCandleID(candleID)
if err != nil {
return nil, pageInfo, fmt.Errorf("getting candle data for time span:%w", err)
}
exists, err := cs.CandleExists(ctx, descriptor.id)
if err != nil {
return nil, pageInfo, fmt.Errorf("getting candles for time span:%w", err)
}
if !exists {
return nil, pageInfo, fmt.Errorf("no candle exists for candle id:%s", candleID)
}
var candles []entities.Candle
query := fmt.Sprintf("SELECT period_start, open, close, high, low, volume, last_update_in_period FROM %s WHERE market_id = $1",
descriptor.view)
marketAsBytes, err := hex.DecodeString(descriptor.market)
if err != nil {
return nil, pageInfo, fmt.Errorf("invalid market:%w", err)
}
args := []interface{}{marketAsBytes}
if from != nil {
query = fmt.Sprintf("%s AND period_start >= %s", query, nextBindVar(&args, from))
}
if to != nil {
query = fmt.Sprintf("%s AND period_start < %s", query, nextBindVar(&args, to))
}
query, args, err = PaginateQuery[entities.CandleCursor](query, args, candleOrdering, p)
if err != nil {
return nil, pageInfo, err
}
defer metrics.StartSQLQuery("Candles", "GetCandleDataForTimeSpan")()
err = pgxscan.Select(ctx, cs.Connection, &candles, query, args...)
if err != nil {
return nil, pageInfo, fmt.Errorf("querying candles: %w", err)
}
var pagedCandles []entities.Candle
pagedCandles, pageInfo = entities.PageEntities[*v2.CandleEdge](candles, p)
return pagedCandles, pageInfo, nil
}
// GetCandlesForMarket returns a map of existing intervals to candle ids for the given market.
func (cs *Candles) GetCandlesForMarket(ctx context.Context, market string) (map[string]string, error) {
intervalToView, err := cs.getIntervalToView(ctx)
if err != nil {
return nil, fmt.Errorf("getting existing candles:%w", err)
}
candles := map[string]string{}
for interval := range intervalToView {
candles[interval] = candleDescriptorFromIntervalAndMarket(interval, market).id
}
return candles, nil
}
func (cs *Candles) GetCandleIDForIntervalAndMarket(ctx context.Context, interval string, market string) (bool, string, error) {
interval, err := cs.normaliseInterval(ctx, interval)
if err != nil {
return false, "", fmt.Errorf("invalid interval: %w", err)
}
viewAlreadyExists, existingInterval, err := cs.viewExistsForInterval(ctx, interval)
if err != nil {
return false, "", fmt.Errorf("checking for existing view: %w", err)
}
if viewAlreadyExists {
descriptor := candleDescriptorFromIntervalAndMarket(existingInterval, market)
return true, descriptor.id, nil
}
return false, "", nil
}
func (cs *Candles) getIntervalToView(ctx context.Context) (map[string]string, error) {
query := fmt.Sprintf("SELECT view_name FROM timescaledb_information.continuous_aggregates where view_name like '%s%%'",
candlesViewNamePrePend)
defer metrics.StartSQLQuery("Candles", "GetIntervalToView")()
rows, err := cs.Connection.Query(ctx, query)
if err != nil {
return nil, fmt.Errorf("fetching existing views for interval: %w", err)
}
var viewNames []string
for rows.Next() {
var viewName string
err := rows.Scan(&viewName)
if err != nil {
return nil, fmt.Errorf("fetching existing views for interval: %w", err)
}
viewNames = append(viewNames, viewName)
}
result := map[string]string{}
for _, viewName := range viewNames {
interval, err := getIntervalFromViewName(viewName)
if err != nil {
return nil, fmt.Errorf("fetching existing views for interval: %w", err)
}
result[interval] = viewName
}
return result, nil
}
func (cs *Candles) CandleExists(ctx context.Context, candleID string) (bool, error) {
descriptor, err := candleDescriptorFromCandleID(candleID)
if err != nil {
return false, fmt.Errorf("candle exists:%w", err)
}
exists, _, err := cs.viewExistsForInterval(ctx, descriptor.interval)
if err != nil {
return false, fmt.Errorf("candle exists:%w", err)
}
return exists, nil
}
func (cs *Candles) viewExistsForInterval(ctx context.Context, interval string) (bool, string, error) {
intervalToView, err := cs.getIntervalToView(ctx)
if err != nil {
return false, "", fmt.Errorf("checking if view exist for interval:%w", err)
}
if _, ok := intervalToView[interval]; ok {
return true, interval, nil
}
// Also check for existing Intervals that are specified differently but amount to the same thing (i.e 7 days = 1 week)
existingIntervals := map[int64]string{}
for existingInterval := range intervalToView {
seconds, err := cs.getIntervalSeconds(ctx, existingInterval)
if err != nil {
return false, "", fmt.Errorf("checking if view exists for interval:%w", err)
}
existingIntervals[seconds] = existingInterval
}
seconds, err := cs.getIntervalSeconds(ctx, interval)
if err != nil {
return false, "", fmt.Errorf("checking if view exists for interval:%w", err)
}
if existingInterval, ok := existingIntervals[seconds]; ok {
return true, existingInterval, nil
}
return false, "", nil
}
func (cs *Candles) normaliseInterval(ctx context.Context, interval string) (string, error) {
var normalizedInterval string
defer metrics.StartSQLQuery("Candles", "normaliseInterval")()
_, err := cs.Connection.Exec(ctx, "SET intervalstyle = 'postgres_verbose' ")
if err != nil {
return "", fmt.Errorf("normalising interval, failed to set interval style:%w", err)
}
query := fmt.Sprintf("select cast( INTERVAL '%s' as text)", interval)
row := cs.Connection.QueryRow(ctx, query)
err = row.Scan(&normalizedInterval)
if err != nil {
return "", fmt.Errorf("normalising interval:%s :%w", interval, err)
}
normalizedInterval = strings.ReplaceAll(normalizedInterval, "@ ", "")
return normalizedInterval, nil
}
func (cs *Candles) getIntervalSeconds(ctx context.Context, interval string) (int64, error) {
var seconds decimal.Decimal
defer metrics.StartSQLQuery("Candles", "getIntervalSeconds")()
query := fmt.Sprintf("SELECT EXTRACT(epoch FROM INTERVAL '%s')", interval)
row := cs.Connection.QueryRow(ctx, query)
err := row.Scan(&seconds)
if err != nil {
return 0, err
}
return seconds.IntPart(), nil
}
func getIntervalFromViewName(viewName string) (string, error) {
split := strings.Split(viewName, candlesViewNamePrePend)
if len(split) != 2 {
return "", fmt.Errorf("view name has unexpected format:%s", viewName)
}
return strings.ReplaceAll(split[1], "_", " "), nil
}
func getViewNameForInterval(interval string) string {
return candlesViewNamePrePend + strings.ReplaceAll(interval, " ", "_")
}
type candleDescriptor struct {
id string
view string
interval string
market string
}
func candleDescriptorFromCandleID(id string) (candleDescriptor, error) {
idx := strings.LastIndex(id, "_")
if idx == -1 {
return candleDescriptor{}, fmt.Errorf("invalid candle id:%s", id)
}
market := id[idx+1:]
view := id[:idx]
split := strings.Split(view, candlesViewNamePrePend)
if len(split) != 2 {
return candleDescriptor{}, fmt.Errorf("parsing candle id, view name has unexpected format:%s", id)
}
interval, err := getIntervalFromViewName(view)
if err != nil {
return candleDescriptor{}, fmt.Errorf("parsing candleDescriptor id, failed to get interval from view name:%w", err)
}
return candleDescriptor{
id: id,
view: view,
interval: interval,
market: market,
}, nil
}
func candleDescriptorFromIntervalAndMarket(interval string, market string) candleDescriptor {
view := getViewNameForInterval(interval)
id := view + "_" + market
return candleDescriptor{
id: id,
view: view,
interval: interval,
market: market,
}
}