-
Notifications
You must be signed in to change notification settings - Fork 129
/
db.go
381 lines (331 loc) · 16.1 KB
/
db.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package models
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/diadata-org/diadata/pkg/dia/helpers/db"
"github.com/diadata-org/diadata/pkg/dia"
clientInfluxdb "github.com/influxdata/influxdb1-client/v2"
"github.com/redis/go-redis/v9"
)
type Datastore interface {
SetInfluxClient(url string)
SetBatchFiatPriceInflux(fqs []*FiatQuotation) error
SetSingleFiatPriceRedis(fiatQuotation *FiatQuotation) error
GetLatestSupply(string, *RelDB) (*dia.Supply, error)
GetSupplyCache(asset dia.Asset) (dia.Supply, error)
GetSupply(string, time.Time, time.Time, *RelDB) ([]dia.Supply, error)
SetSupply(supply *dia.Supply) error
GetSupplyInflux(dia.Asset, time.Time, time.Time) ([]dia.Supply, error)
SaveSynthSupplyInfluxToTable(*dia.SynthAssetSupply, string) error
SaveSynthSupplyInflux(*dia.SynthAssetSupply) error
GetSynthSupplyInflux(string, string, string, int, time.Time, time.Time) ([]dia.SynthAssetSupply, error)
GetSynthAssets(string, string) ([]string, error)
SetDiaTotalSupply(totalSupply float64) error
GetDiaTotalSupply() (float64, error)
SetDiaCirculatingSupply(circulatingSupply float64) error
GetDiaCirculatingSupply() (float64, error)
GetSymbols(exchange string) ([]string, error)
GetLastTradeTimeForExchange(asset dia.Asset, exchange string) (*time.Time, error)
SetLastTradeTimeForExchange(asset dia.Asset, exchange string, t time.Time) error
GetFirstTradeDate(table string) (time.Time, error)
SaveTradeInflux(t *dia.Trade) error
SaveTradeInfluxToTable(t *dia.Trade, table string) error
SaveTradeRedis(t dia.Trade) error
SaveTradeSetElementRedis(t dia.Trade) error
GetExchangepairKeysRedis(asset dia.Asset) ([]string, error)
GetRedisKeysByFeedselection(fs dia.FeedSelection) ([]string, error)
GetTradesRedis(key string, startTime time.Time, endTime time.Time, offset int64, limit int64) ([]dia.Trade, error)
GetTradesByFeedselectionRedis(feedselection []dia.FeedSelection, startTimes []time.Time, endTimes []time.Time, limit int64, desc bool) ([]dia.Trade, error)
PurgeRedisByScore(key string, scoreMax int64) (int64, error)
PurgeTradesAndKeysRedis(timestampLatest time.Time)
GetTradeInflux(dia.Asset, string, time.Time, time.Duration) (*dia.Trade, error)
SaveFilterInflux(filter string, asset dia.Asset, exchange string, value float64, t time.Time) error
GetFilterAllExchanges(filter string, address string, blockchain string, starttime time.Time, endtime time.Time) ([]AssetQuotation, error)
GetLastTrades(asset dia.Asset, exchange string, timestamp time.Time, maxTrades int, fullAsset bool) ([]dia.Trade, error)
GetAllTrades(t time.Time, maxTrades int) ([]dia.Trade, error)
GetTradesByExchangesFull(asset dia.Asset, baseAssets []dia.Asset, exchanges []string, returnBasetoken bool, startTime, endTime time.Time, maxTrades int) ([]dia.Trade, error)
GetTradesByExchangesAndBaseAssets(asset dia.Asset, baseassets []dia.Asset, exchanges []string, startTime time.Time, endTime time.Time, maxTrades int) ([]dia.Trade, error)
GetTradesByExchangesBatchedFull(asset dia.Asset, baseAssets []dia.Asset, exchanges []string, returnBasetoken bool, startTimes, endTimes []time.Time, maxTrades int) ([]dia.Trade, error)
GetTradesByExchangesBatched(asset dia.Asset, baseAssets []dia.Asset, exchanges []string, startTimes, endTimes []time.Time, maxTrades int) ([]dia.Trade, error)
GetxcTradesByExchangesBatched(quoteassets []dia.Asset, exchanges []string, startTimes []time.Time, endTimes []time.Time) ([]dia.Trade, error)
GetTradesByExchangepairs(exchangepairMap map[string][]dia.Pair, exchangepoolMap map[string][]string, starttime time.Time, endtime time.Time) ([]dia.Trade, error)
GetTradesByFeedSelection(feedselection []dia.FeedSelection, starttimes []time.Time, endtimes []time.Time, limit int) ([]dia.Trade, error)
GetAggregatedFeedSelection(feedselection []dia.FeedSelection, starttime time.Time, endtime time.Time, tradeVolumeThreshold float64) ([]dia.FeedSelectionAggregated, error)
GetActiveExchangesAndPairs(address string, blockchain string, numTradesThreshold int64, starttime time.Time, endtime time.Time) (map[string][]dia.Pair, map[string]int64, error)
GetOldTradesFromInflux(table string, exchange string, verified bool, timeInit, timeFinal time.Time) ([]dia.Trade, error)
CopyInfluxMeasurements(dbOrigin string, dbDestination string, tableOrigin string, tableDestination string, timeInit time.Time, timeFinal time.Time) (int64, error)
Flush() error
ExecuteRedisPipe() error
FlushRedisPipe()
GetFilterPoints(filter string, exchange string, symbol string, scale string, starttime time.Time, endtime time.Time) (*Points, error)
GetFilterPointsAsset(filter string, exchange string, address string, blockchain string, starttime time.Time, endtime time.Time) (*Points, error)
SetFilter(filterName string, asset dia.Asset, exchange string, value float64, t time.Time) error
GetLastPriceBefore(asset dia.Asset, filter string, exchange string, timestamp time.Time) (Price, error)
SetAvailablePairs(exchange string, pairs []dia.ExchangePair) error
GetAvailablePairs(exchange string) ([]dia.ExchangePair, error)
SetCurrencyChange(cc *Change) error
GetCurrencyChange() (*Change, error)
// Volume methods
GetVolumeInflux(asset dia.Asset, exchange string, starttime time.Time, endtime time.Time) (*float64, error)
Get24HoursAssetVolume(asset dia.Asset) (*float64, error)
Get24HoursExchangeVolume(exchange string) (*float64, error)
GetNumTradesExchange24H(exchange string) (int64, error)
GetNumTrades(exchange string, address string, blockchain string, starttime time.Time, endtime time.Time) (int64, error)
GetNumTradesSeries(asset dia.Asset, exchange string, starttime time.Time, endtime time.Time, grouping string) ([]int64, error)
GetVolumesAllExchanges(asset dia.Asset, starttime time.Time, endtime time.Time) (exchVolumes dia.ExchangeVolumesList, err error)
GetExchangePairVolumes(asset dia.Asset, starttime time.Time, endtime time.Time, threshold float64) (map[string][]dia.PairVolume, error)
GetExchangePairVolume(ep dia.ExchangePair, pooladdress string, starttime time.Time, endtime time.Time, threshold float64) (float64, int64, error)
// New Asset pricing methods: 23/02/2021
SetAssetPriceUSD(asset dia.Asset, price float64, timestamp time.Time) error
GetAssetPriceUSD(asset dia.Asset, starttime time.Time, endtime time.Time) (float64, error)
GetAssetPriceUSDLatest(asset dia.Asset) (price float64, err error)
SetAssetQuotation(quotation *AssetQuotation) error
GetAssetQuotation(asset dia.Asset, starttime time.Time, endtime time.Time) (*AssetQuotation, error)
GetAssetQuotations(asset dia.Asset, starttime time.Time, endtime time.Time) ([]AssetQuotation, error)
GetAssetQuotationLatest(asset dia.Asset, starttime time.Time) (*AssetQuotation, error)
GetSortedAssetQuotations(assets []dia.Asset) ([]AssetQuotation, error)
AddAssetQuotationsToBatch(quotations []*AssetQuotation) error
SetAssetQuotationCache(quotation *AssetQuotation, check bool) (bool, error)
GetAssetQuotationCache(asset dia.Asset) (*AssetQuotation, error)
GetAssetPriceUSDCache(asset dia.Asset) (price float64, err error)
GetTopAssetByMcap(symbol string, relDB *RelDB) (dia.Asset, error)
GetTopAssetByVolume(symbol string, relDB *RelDB) (topAsset dia.Asset, err error)
GetAssetsWithVOLInflux(timeInit time.Time) ([]dia.Asset, error)
GetOldestQuotation(asset dia.Asset) (AssetQuotation, error)
// DEX Pool methods
SavePoolInflux(p dia.Pool) error
GetPoolInflux(poolAddress string, starttime time.Time, endtime time.Time) ([]dia.Pool, error)
GetPoolLiquiditiesUSD(p *dia.Pool, priceCache map[string]float64)
// Market Measures
GetAssetsMarketCap(asset dia.Asset) (float64, error)
// Interest rates' methods
SetInterestRate(ir *InterestRate) error
GetInterestRate(symbol, date string) (*InterestRate, error)
GetInterestRateRange(symbol, dateInit, dateFinal string) ([]*InterestRate, error)
GetRatesMeta() (RatesMeta []InterestRateMeta, err error)
GetCompoundedIndex(symbol string, date time.Time, daysPerYear int, rounding int) (*InterestRate, error)
GetCompoundedIndexRange(symbol string, dateInit, dateFinal time.Time, daysPerYear int, rounding int) ([]*InterestRate, error)
GetCompoundedAvg(symbol string, date time.Time, calDays, daysPerYear int, rounding int) (*InterestRate, error)
GetCompoundedAvgRange(symbol string, dateInit, dateFinal time.Time, calDays, daysPerYear int, rounding int) ([]*InterestRate, error)
GetCompoundedAvgDIARange(symbol string, dateInit, dateFinal time.Time, calDays, daysPerYear int, rounding int) ([]*InterestRate, error)
// Foreign quotation methods
SaveForeignQuotationInflux(fq ForeignQuotation) error
GetForeignQuotationInflux(symbol, source string, timestamp time.Time) (ForeignQuotation, error)
GetForeignPriceYesterday(symbol, source string) (float64, error)
GetForeignSymbolsInflux(source string) ([]string, error)
SetVWAPFirefly(foreignName string, value float64, timestamp time.Time) error
GetVWAPFirefly(foreignName string, starttime time.Time, endtime time.Time) ([]float64, []time.Time, error)
SaveIndexEngineTimeInflux(map[string]string, map[string]interface{}, time.Time) error
GetBenchmarkedIndexValuesInflux(string, time.Time, time.Time) (BenchmarkedIndex, error)
// Token methods
// SaveTokenDetailInflux(tk Token) error
// GetTokenDetailInflux(symbol, source string, timestamp time.Time) (Token, error)
// GetCurentTotalSupply(symbol, source string) (float64, error)
// Stock methods
SetStockQuotation(sq StockQuotation) error
GetStockQuotation(source string, symbol string, timeInit time.Time, timeFinal time.Time) ([]StockQuotation, error)
GetStockSymbols() (map[Stock]string, error)
SetOracleConfigCache(dia.OracleConfig) error
GetOracleConfigCache(string) (dia.OracleConfig, error)
}
const (
influxMaxPointsInBatch = 5000
// timeOutRedisOneBlock = 60 * 3 * time.Second
)
type DB struct {
redisClient *redis.Client
redisPipe redis.Pipeliner
influxClient clientInfluxdb.Client
influxBatchPoints clientInfluxdb.BatchPoints
influxPointsInBatch int
}
var EscapeReplacer = strings.NewReplacer("\n", `\n`)
const (
influxDbName = "dia"
influxDbTradesTable = "trades"
influxDbFiltersTable = "filters"
influxDbFiatQuotationsTable = "fiat"
influxDbSupplyTable = "supplies"
influxDbDEXPoolTable = "DEXPools"
influxDbStockQuotationsTable = "stockquotations"
influxDBAssetQuotationsTable = "assetQuotations"
influxDbBenchmarkedIndexTableName = "benchmarkedIndexValues"
influxDbVwapFireflyTable = "vwapFirefly"
influxDbSynthSupplyTable = "synthsupply"
influxDBDefaultURL = "http://influxdb:8086"
)
// queryInfluxDB convenience function to query the database.
func queryInfluxDB(clnt clientInfluxdb.Client, cmd string) (res []clientInfluxdb.Result, err error) {
res, err = queryInfluxDBName(clnt, influxDbName, cmd)
return
}
// queryInfluxDBName is a wrapper for queryInfluxDB that allows for queries on the database with name @dbName.
func queryInfluxDBName(clnt clientInfluxdb.Client, dbName string, cmd string) (res []clientInfluxdb.Result, err error) {
q := clientInfluxdb.Query{
Command: cmd,
Database: dbName,
}
if response, err := clnt.Query(q); err == nil {
if response.Error() != nil {
return res, response.Error()
}
res = response.Results
} else {
return res, err
}
return res, nil
}
func NewDataStore() (*DB, error) {
return NewDataStoreWithOptions(true, true)
}
func NewInfluxDataStore() (*DB, error) {
return NewDataStoreWithOptions(false, true)
}
func NewRedisDataStore() (*DB, error) {
return NewDataStoreWithOptions(true, false)
}
func NewDataStoreWithoutInflux() (*DB, error) {
return NewDataStoreWithOptions(true, false)
}
func NewDataStoreWithoutRedis() (*DB, error) {
return NewDataStoreWithOptions(false, true)
}
func NewDataStoreWithOptions(withRedis bool, withInflux bool) (*DB, error) {
var (
influxClient clientInfluxdb.Client
influxBatchPoints clientInfluxdb.BatchPoints
redisClient *redis.Client
redisPipe redis.Pipeliner
)
if withRedis {
redisClient = db.GetRedisClient()
redisPipe = redisClient.TxPipeline()
}
if withInflux {
var err error
influxClient = db.GetInfluxClient(influxDBDefaultURL)
influxBatchPoints = createBatchInflux()
_, err = queryInfluxDB(influxClient, fmt.Sprintf("CREATE DATABASE %s", influxDbName))
if err != nil {
log.Errorln("queryInfluxDB CREATE DATABASE", err)
}
}
return &DB{redisClient, redisPipe, influxClient, influxBatchPoints, 0}, nil
}
// SetInfluxClient resets influx's client url to @url.
func (datastore *DB) SetInfluxClient(url string) {
datastore.influxClient = db.GetInfluxClient(url)
}
func createBatchInflux() clientInfluxdb.BatchPoints {
bp, err := clientInfluxdb.NewBatchPoints(clientInfluxdb.BatchPointsConfig{
Database: influxDbName,
Precision: "ns",
})
if err != nil {
log.Errorln("NewBatchPoints", err)
}
return bp
}
func (datastore *DB) Flush() error {
var err error
if datastore.influxBatchPoints != nil {
err = datastore.WriteBatchInflux()
}
return err
}
func (datastore *DB) WriteBatchInflux() (err error) {
err = datastore.influxClient.Write(datastore.influxBatchPoints)
if err != nil {
log.Errorln("WriteBatchInflux", err)
return
}
datastore.influxPointsInBatch = 0
datastore.influxBatchPoints = createBatchInflux()
return
}
func (datastore *DB) addPoint(pt *clientInfluxdb.Point) {
datastore.influxBatchPoints.AddPoint(pt)
datastore.influxPointsInBatch++
if datastore.influxPointsInBatch >= influxMaxPointsInBatch {
err := datastore.WriteBatchInflux()
if err != nil {
log.Error("write influx batch: ", err)
}
}
}
func (datastore *DB) ExecuteRedisPipe() (err error) {
// TO DO: Handle first return value for read requests.
_, err = datastore.redisPipe.Exec(context.Background())
return
}
func (datastore *DB) FlushRedisPipe() {
datastore.redisPipe.Discard()
}
// CopyInfluxMeasurements copies entries from measurement @tableOrigin in database @dbOrigin into @tableDestination in database @dbDestination.
// It takes into account all data ranging from @timeInit until @timeFinal.
func (datastore *DB) CopyInfluxMeasurements(dbOrigin string, dbDestination string, tableOrigin string, tableDestination string, timeInit time.Time, timeFinal time.Time) (numCopiedRows int64, err error) {
queryString := "select * into %s..%s from %s..%s where time>%d and time<=%d group by *"
query := fmt.Sprintf(queryString, dbDestination, tableDestination, dbOrigin, tableOrigin, timeInit.UnixNano(), timeFinal.UnixNano())
res, err := queryInfluxDB(datastore.influxClient, query)
if err != nil {
return
}
if len(res) > 0 && len(res[0].Series) > 0 {
numCopiedRows, err = res[0].Series[0].Values[0][1].(json.Number).Int64()
if err != nil {
return
}
}
return
}
func (datastore *DB) SetVWAPFirefly(foreignName string, value float64, timestamp time.Time) error {
tags := map[string]string{
"foreignName": EscapeReplacer.Replace(foreignName),
}
fields := map[string]interface{}{
"value": value,
}
pt, err := clientInfluxdb.NewPoint(influxDbVwapFireflyTable, tags, fields, timestamp)
if err != nil {
log.Errorln("new filter influx:", err)
} else {
datastore.addPoint(pt)
}
err = datastore.WriteBatchInflux()
if err != nil {
log.Errorln("Write influx batch: ", err)
}
return err
}
func (datastore *DB) GetVWAPFirefly(foreignName string, starttime time.Time, endtime time.Time) (values []float64, timestamps []time.Time, err error) {
influxQuery := "SELECT value FROM %s WHERE time > %d AND time <= %d AND foreignName = '%s' ORDER BY DESC"
q := fmt.Sprintf(influxQuery, influxDbVwapFireflyTable, starttime.UnixNano(), endtime.UnixNano(), foreignName)
res, err := queryInfluxDB(datastore.influxClient, q)
if err != nil {
return
}
if len(res) > 0 && len(res[0].Series) > 0 {
for i := 0; i < len(res[0].Series[0].Values); i++ {
var value float64
var timestamp time.Time
timestamp, err = time.Parse(time.RFC3339, res[0].Series[0].Values[i][0].(string))
if err != nil {
return
}
value, err = res[0].Series[0].Values[i][1].(json.Number).Float64()
if err != nil {
return
}
values = append(values, value)
timestamps = append(timestamps, timestamp)
}
} else {
err = errors.New("no data available in given time range")
return
}
return
}