-
Notifications
You must be signed in to change notification settings - Fork 129
/
db.go
1073 lines (969 loc) · 37.3 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package models
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"time"
"github.com/diadata-org/diadata/pkg/dia/helpers/db"
"github.com/diadata-org/diadata/pkg/dia"
"github.com/go-redis/redis"
clientInfluxdb "github.com/influxdata/influxdb1-client/v2"
)
type Datastore interface {
SetInfluxClient(url string)
// Deprecating
SetPriceUSD(symbol string, price float64) error
SetPriceEUR(symbol string, price float64) error
GetPriceUSD(symbol string) (float64, error)
GetQuotation(symbol string) (*Quotation, error)
SetQuotation(quotation *Quotation) error
SetQuotationEUR(quotation *Quotation) error
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)
SetDiaTotalSupply(totalSupply float64) error
GetDiaTotalSupply() (float64, error)
SetDiaCirculatingSupply(circulatingSupply float64) error
GetDiaCirculatingSupply() (float64, error)
GetSymbols(exchange string) ([]string, error)
// Deprecating: GetExchangesForSymbol(symbol string) ([]string, error)
// Deprecating: GetSymbolExchangeDetails(symbol string, exchange string) (*SymbolExchangeDetails, 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
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
GetLastTrades(asset dia.Asset, exchange string, maxTrades int, fullAsset bool) ([]dia.Trade, error)
GetAllTrades(t time.Time, maxTrades int) ([]dia.Trade, error)
GetTradesByExchanges(asset dia.Asset, baseAssets []dia.Asset, exchange []string, startTime, endTime time.Time) ([]dia.Trade, error)
GetTradesByExchangesFull(asset dia.Asset, baseAssets []dia.Asset, exchanges []string, returnBasetoken bool, startTime, endTime time.Time) ([]dia.Trade, error)
GetTradesByExchangesBatched(asset dia.Asset, baseAssets []dia.Asset, exchanges []string, startTimes, endTimes []time.Time) ([]dia.Trade, error)
GetTradesByExchangesBatchedFull(asset dia.Asset, baseAssets []dia.Asset, exchanges []string, returnBasetoken bool, startTimes, endTimes []time.Time) ([]dia.Trade, error)
GetActiveExchangesAndPairs(address string, blockchain string, starttime time.Time, endtime time.Time) (map[string][]string, 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)
DeleteInfluxMeasurement(dbName string, tableName string, timeInit time.Time, timeFinal time.Time) error
Flush() error
ExecuteRedisPipe() error
FlushRedisPipe() error
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)
SetOptionMeta(optionMeta *dia.OptionMeta) error
GetOptionMeta(baseCurrency string) ([]dia.OptionMeta, error)
SaveCVIInflux(float64, time.Time) error
GetCVIInflux(time.Time, time.Time, string) ([]dia.CviDataPoint, 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(
exchange string,
pair string,
starttime time.Time,
endtime time.Time,
grouping string,
quotetoken dia.Asset,
basetoken dia.Asset,
) ([]int, error)
// New Asset pricing methods: 23/02/2021
SetAssetPriceUSD(asset dia.Asset, price float64, timestamp time.Time) error
GetAssetPriceUSD(asset dia.Asset, timestamp time.Time) (float64, error)
GetAssetPriceUSDLatest(asset dia.Asset) (price float64, err error)
SetAssetQuotation(quotation *AssetQuotation) error
GetAssetQuotation(asset dia.Asset, timestamp time.Time) (*AssetQuotation, error)
GetAssetQuotations(asset dia.Asset, starttime time.Time, endtime time.Time) ([]AssetQuotation, error)
GetAssetQuotationLatest(asset dia.Asset) (*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)
// DEX Pool methods
SavePoolInflux(p dia.Pool) error
GetPoolInflux(poolAddress string, starttime time.Time, endtime time.Time) ([]dia.Pool, error)
// 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)
// Itin methods
SetItinData(token dia.ItinToken) error
GetItinBySymbol(symbol string) (dia.ItinToken, error)
// Defi rates
SetDefiProtocol(dia.DefiProtocol) error
GetDefiProtocol(string) (dia.DefiProtocol, error)
GetDefiProtocols() ([]dia.DefiProtocol, error)
GetDefiRateInflux(time.Time, time.Time, string, string) ([]dia.DefiRate, error)
SetDefiRateInflux(rate *dia.DefiRate) error
GetDefiStateInflux(time.Time, time.Time, string) ([]dia.DefiProtocolState, error)
SetDefiStateInflux(state *dia.DefiProtocolState) 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) (symbols []SymbolShort, err error)
SetVWAPFirefly(foreignName string, value float64, timestamp time.Time) error
GetVWAPFirefly(foreignName string, starttime time.Time, endtime time.Time) ([]float64, []time.Time, error)
// Gold token methods
GetPaxgQuotationOunces() (*Quotation, error)
GetPaxgQuotationGrams() (*Quotation, 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)
// Github methods
SetCommit(commit GithubCommit) error
GetCommitByDate(user, repository string, date time.Time) (GithubCommit, error)
GetCommitByHash(user, repository, hash string) (GithubCommit, error)
GetLatestCommit(user, repository string) (GithubCommit, 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)
}
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
}
const (
influxDbName = "dia"
influxDbTradesTable = "trades"
influxDbFiltersTable = "filters"
influxDbFiatQuotationsTable = "fiat"
influxDbOptionsTable = "options"
influxDbCVITable = "cvi"
influxDbETHCVITable = "cviETH"
influxDbSupplyTable = "supplies"
influxDbDefiRateTable = "defiRate"
influxDbDefiStateTable = "defiState"
influxDbDEXPoolTable = "DEXPools"
influxDbGithubCommitTable = "githubcommits"
influxDbStockQuotationsTable = "stockquotations"
influxDBAssetQuotationsTable = "assetQuotations"
influxDbBenchmarkedIndexTableName = "benchmarkedIndexValues"
influxDbVwapFireflyTable = "vwapFirefly"
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
var influxBatchPoints clientInfluxdb.BatchPoints
var redisClient *redis.Client
var 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 getKey(filter string, asset dia.Asset, exchange string) string {
key := filter + "_" + asset.Blockchain + "_" + asset.Address
if exchange != "" {
key = key + "_" + exchange
}
return key
}
func getKeyFilterZSET(key string) string {
return "dia_" + key + "_ZSET"
}
func getKeyFilterSymbolAndExchangeZSET(filter string, asset dia.Asset, exchange string) string {
if exchange == "" {
return "dia_" + filter + "_" + asset.Blockchain + "_" + asset.Address + "_ZSET"
} else {
return "dia_" + filter + "_" + asset.Blockchain + "_" + asset.Address + "_ZSET"
}
}
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)
}
}
}
// SaveTradeInflux stores a trade in influx. Flushed when more than maxPoints in batch.
// Wrapper around SaveTradeInfluxToTable.
func (datastore *DB) SaveTradeInflux(t *dia.Trade) error {
return datastore.SaveTradeInfluxToTable(t, influxDbTradesTable)
}
// SaveTradeInfluxToTable stores a trade in influx into @table.
// Flushed when more than maxPoints in batch.
func (datastore *DB) SaveTradeInfluxToTable(t *dia.Trade, table string) error {
// Create a point and add to batch
tags := map[string]string{
"symbol": t.Symbol,
"pair": t.Pair,
"exchange": t.Source,
"verified": strconv.FormatBool(t.VerifiedPair),
"quotetokenaddress": t.QuoteToken.Address,
"basetokenaddress": t.BaseToken.Address,
"quotetokenblockchain": t.QuoteToken.Blockchain,
"basetokenblockchain": t.BaseToken.Blockchain,
}
fields := map[string]interface{}{
"price": t.Price,
"volume": t.Volume,
"estimatedUSDPrice": t.EstimatedUSDPrice,
"foreignTradeID": t.ForeignTradeID,
}
pt, err := clientInfluxdb.NewPoint(table, tags, fields, t.Time)
if err != nil {
log.Errorln("NewTradeInflux:", err)
} else {
datastore.addPoint(pt)
}
return err
}
// GetTradeInflux returns the latest trade of @asset on @exchange before @timestamp in the time-range [endtime-window, endtime].
func (datastore *DB) GetTradeInflux(asset dia.Asset, exchange string, endtime time.Time, window time.Duration) (*dia.Trade, error) {
starttime := endtime.Add(-window)
retval := dia.Trade{}
var q string
if exchange != "" {
queryString := "SELECT estimatedUSDPrice,\"exchange\",foreignTradeID,\"pair\",price,\"symbol\",volume FROM %s WHERE quotetokenaddress='%s' AND quotetokenblockchain='%s' AND exchange='%s' AND time >= %d AND time < %d ORDER BY DESC LIMIT 1"
q = fmt.Sprintf(queryString, influxDbTradesTable, asset.Address, asset.Blockchain, exchange, starttime.UnixNano(), endtime.UnixNano())
} else {
queryString := "SELECT estimatedUSDPrice,\"exchange\",foreignTradeID,\"pair\",price,\"symbol\",volume FROM %s WHERE quotetokenaddress='%s' AND quotetokenblockchain='%s' AND time >= %d AND time < %d ORDER BY DESC LIMIT 1"
q = fmt.Sprintf(queryString, influxDbTradesTable, asset.Address, asset.Blockchain, starttime.UnixNano(), endtime.UnixNano())
}
/// TODO
res, err := queryInfluxDB(datastore.influxClient, q)
if err != nil {
return &retval, err
}
if len(res) > 0 && len(res[0].Series) > 0 {
for i := 0; i < len(res[0].Series[0].Values); i++ {
retval.Time, err = time.Parse(time.RFC3339, res[0].Series[0].Values[i][0].(string))
if err != nil {
return &retval, err
}
retval.EstimatedUSDPrice, err = res[0].Series[0].Values[i][1].(json.Number).Float64()
if err != nil {
return &retval, err
}
retval.Source = res[0].Series[0].Values[i][2].(string)
retval.ForeignTradeID = res[0].Series[0].Values[i][3].(string)
retval.Pair = res[0].Series[0].Values[i][4].(string)
retval.Price, err = res[0].Series[0].Values[i][5].(json.Number).Float64()
if err != nil {
return &retval, err
}
retval.Symbol = res[0].Series[0].Values[i][6].(string)
retval.Volume, err = res[0].Series[0].Values[i][7].(json.Number).Float64()
if err != nil {
return &retval, err
}
}
} else {
return &retval, errors.New("parsing trade from database")
}
return &retval, nil
}
// GetOldTradesFromInflux returns all recorded trades from @table done on @exchange between @timeInit and @timeFinal
// where the time interval is closed on the left and open on the right side.
// If @exchange is empty, trades across all exchanges are returned.
// If @verified is true, address and blockchain are also parsed for both assets.
func (datastore *DB) GetOldTradesFromInflux(table string, exchange string, verified bool, timeInit, timeFinal time.Time) ([]dia.Trade, error) {
allTrades := []dia.Trade{}
var queryString, query, addQueryString string
if verified {
addQueryString = ",\"quotetokenaddress\",\"basetokenaddress\",\"quotetokenblockchain\",\"basetokenblockchain\",\"verified\""
}
if exchange == "" {
queryString = "SELECT estimatedUSDPrice,\"exchange\",foreignTradeID,\"pair\",price,\"symbol\",volume" +
addQueryString +
" FROM %s WHERE time>=%d and time<%d order by asc"
query = fmt.Sprintf(queryString, table, timeInit.UnixNano(), timeFinal.UnixNano())
} else {
queryString = "SELECT estimatedUSDPrice,\"exchange\",foreignTradeID,\"pair\",price,\"symbol\",volume" +
addQueryString +
" FROM %s WHERE exchange='%s' and time>=%d and time<%d order by asc"
query = fmt.Sprintf(queryString, table, exchange, timeInit.UnixNano(), timeFinal.UnixNano())
}
res, err := queryInfluxDB(datastore.influxClient, query)
if err != nil {
log.Error("influx query: ", err)
return allTrades, err
}
log.Info("query: ", query)
if len(res) > 0 && len(res[0].Series) > 0 {
for i := 0; i < len(res[0].Series[0].Values); i++ {
var trade dia.Trade
trade.Time, err = time.Parse(time.RFC3339, res[0].Series[0].Values[i][0].(string))
if err != nil {
return allTrades, err
}
trade.EstimatedUSDPrice, err = res[0].Series[0].Values[i][1].(json.Number).Float64()
if err != nil {
return allTrades, err
}
if res[0].Series[0].Values[i][2] != nil {
trade.Source = res[0].Series[0].Values[i][2].(string)
}
if res[0].Series[0].Values[i][3] != nil {
trade.ForeignTradeID = res[0].Series[0].Values[i][3].(string)
}
if res[0].Series[0].Values[i][4] != nil {
trade.Pair = res[0].Series[0].Values[i][4].(string)
}
trade.Price, err = res[0].Series[0].Values[i][5].(json.Number).Float64()
if err != nil {
return allTrades, err
}
if res[0].Series[0].Values[i][6] == nil {
continue
}
if res[0].Series[0].Values[i][6] != nil {
trade.Symbol = res[0].Series[0].Values[i][6].(string)
}
trade.Volume, err = res[0].Series[0].Values[i][7].(json.Number).Float64()
if err != nil {
return allTrades, err
}
if verified {
if res[0].Series[0].Values[i][8] != nil {
trade.QuoteToken.Address = res[0].Series[0].Values[i][8].(string)
}
if res[0].Series[0].Values[i][9] != nil {
trade.BaseToken.Address = res[0].Series[0].Values[i][9].(string)
}
if res[0].Series[0].Values[i][10] != nil {
trade.QuoteToken.Blockchain = res[0].Series[0].Values[i][10].(string)
}
if res[0].Series[0].Values[i][11] != nil {
trade.BaseToken.Blockchain = res[0].Series[0].Values[i][11].(string)
}
verifiedPair, ok := res[0].Series[0].Values[i][12].(string)
if ok {
trade.VerifiedPair, err = strconv.ParseBool(verifiedPair)
if err != nil {
log.Error("parse verified pair boolean: ", err)
}
}
}
allTrades = append(allTrades, trade)
}
} else {
return allTrades, errors.New("no trades in time range")
}
return allTrades, nil
}
// 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
}
// DeleteInfluxMeasurement deletes data from influx database @dbName and measurement @tableName in the given time range.
func (datastore *DB) DeleteInfluxMeasurement(dbName string, tableName string, timeInit time.Time, timeFinal time.Time) (err error) {
queryString := "DELETE FROM %s WHERE time>%d AND time<=%d"
query := fmt.Sprintf(queryString, tableName, timeInit.UnixNano(), timeFinal.UnixNano())
_, err = queryInfluxDBName(datastore.influxClient, dbName, query)
return
}
func (datastore *DB) GetFirstTradeDate(table string) (time.Time, error) {
var query string
queryString := "SELECT \"exchange\",price FROM %s where time<now() order by asc limit 1"
query = fmt.Sprintf(queryString, table)
res, err := queryInfluxDB(datastore.influxClient, query)
if err != nil {
return time.Time{}, err
}
if len(res) > 0 && len(res[0].Series) > 0 {
return time.Parse(time.RFC3339, res[0].Series[0].Values[0][0].(string))
}
return time.Time{}, errors.New("no trade found")
}
func (datastore *DB) SaveCVIInflux(cviValue float64, observationTime time.Time) error {
fields := map[string]interface{}{
"value": cviValue,
}
pt, err := clientInfluxdb.NewPoint(influxDbCVITable, nil, fields, observationTime)
if err != nil {
log.Errorln("NewOptionInflux:", err)
} else {
datastore.addPoint(pt)
}
err = datastore.WriteBatchInflux()
if err != nil {
log.Errorln("SaveOptionOrderbookDatumInflux", err)
}
return err
}
func (datastore *DB) SaveETHCVIInflux(cviValue float64, observationTime time.Time) error {
fields := map[string]interface{}{
"value": cviValue,
}
pt, err := clientInfluxdb.NewPoint(influxDbETHCVITable, nil, fields, observationTime)
if err != nil {
log.Errorln("NewOptionInflux:", err)
} else {
datastore.addPoint(pt)
}
err = datastore.WriteBatchInflux()
if err != nil {
log.Errorln("SaveOptionOrderbookDatumInflux", err)
}
return err
}
func (datastore *DB) GetCVIInflux(starttime time.Time, endtime time.Time, symbol string) ([]dia.CviDataPoint, error) {
retval := []dia.CviDataPoint{}
var q string
if symbol == "ETH" {
q = fmt.Sprintf("SELECT * FROM %s WHERE time > %d and time < %d", influxDbETHCVITable, starttime.UnixNano(), endtime.UnixNano())
} else {
q = fmt.Sprintf("SELECT * FROM %s WHERE time > %d and time < %d", influxDbCVITable, starttime.UnixNano(), endtime.UnixNano())
}
res, err := queryInfluxDB(datastore.influxClient, q)
if err != nil {
return retval, err
}
if len(res) > 0 && len(res[0].Series) > 0 {
for i := 0; i < len(res[0].Series[0].Values); i++ {
currentPoint := dia.CviDataPoint{}
currentPoint.Timestamp, err = time.Parse(time.RFC3339, res[0].Series[0].Values[i][0].(string))
if err != nil {
return retval, err
}
currentPoint.Value, err = res[0].Series[0].Values[i][1].(json.Number).Float64()
if err != nil {
return retval, err
}
retval = append(retval, currentPoint)
}
} else {
return retval, errors.New("parsing CVI value from database")
}
return retval, nil
}
func (datastore *DB) SaveOptionOrderbookDatumInflux(t dia.OptionOrderbookDatum) error {
tags := map[string]string{"instrumentName": t.InstrumentName}
fields := map[string]interface{}{
"askPrice": t.AskPrice,
"bidPrice": t.BidPrice,
"askSize": t.AskSize,
"bidSize": t.BidSize,
}
pt, err := clientInfluxdb.NewPoint(influxDbOptionsTable, tags, fields, t.ObservationTime)
if err != nil {
log.Errorln("NewOptionInflux:", err)
} else {
datastore.addPoint(pt)
}
err = datastore.WriteBatchInflux()
if err != nil {
log.Errorln("SaveOptionOrderbookDatumInflux", err)
}
return err
}
func (datastore *DB) GetOptionOrderbookDataInflux(t dia.OptionMeta) (dia.OptionOrderbookDatum, error) {
retval := dia.OptionOrderbookDatum{}
q := fmt.Sprintf("SELECT LAST(askPrice), bidPrice, askSize, bidSize, observationTime FROM %s WHERE instrumentName ='%s'", influxDbOptionsTable, t.InstrumentName)
res, err := queryInfluxDB(datastore.influxClient, q)
if err != nil {
return retval, err
}
if len(res) > 0 && len(res[0].Series) > 0 {
retval.InstrumentName = t.InstrumentName
retval.ObservationTime, err = time.Parse(time.RFC3339, res[0].Series[0].Values[0][0].(string))
if err != nil {
return retval, err
}
retval.AskPrice, err = res[0].Series[0].Values[0][1].(json.Number).Float64()
if err != nil {
return retval, err
}
retval.BidPrice, err = res[0].Series[0].Values[0][2].(json.Number).Float64()
if err != nil {
return retval, err
}
retval.AskSize, err = res[0].Series[0].Values[0][3].(json.Number).Float64()
if err != nil {
return retval, err
}
retval.BidSize, err = res[0].Series[0].Values[0][4].(json.Number).Float64()
if err != nil {
return retval, err
}
return retval, nil
}
return retval, nil
}
func (datastore *DB) SetDefiRateInflux(rate *dia.DefiRate) error {
fields := map[string]interface{}{
"lendingRate": rate.LendingRate,
"borrowRate": rate.BorrowingRate,
}
tags := map[string]string{
"asset": rate.Asset,
"protocol": rate.Protocol,
}
pt, err := clientInfluxdb.NewPoint(influxDbDefiRateTable, tags, fields, rate.Timestamp)
if err != nil {
log.Errorln("SetDefiRateInflux:", err)
} else {
datastore.addPoint(pt)
}
err = datastore.WriteBatchInflux()
if err != nil {
log.Errorln("SetDefiRateInflux", err)
}
return err
}
func (datastore *DB) GetDefiRateInflux(starttime time.Time, endtime time.Time, asset string, protocol string) ([]dia.DefiRate, error) {
retval := []dia.DefiRate{}
influxQuery := "SELECT \"asset\",borrowRate,lendingRate,\"protocol\" FROM %s WHERE time > %d and time < %d and asset = '%s' and protocol = '%s'"
q := fmt.Sprintf(influxQuery, influxDbDefiRateTable, starttime.UnixNano(), endtime.UnixNano(), asset, protocol)
fmt.Println("influx query: ", q)
res, err := queryInfluxDB(datastore.influxClient, q)
fmt.Println("res, err: ", res, err)
if err != nil {
return retval, err
}
if len(res) > 0 && len(res[0].Series) > 0 {
for i := 0; i < len(res[0].Series[0].Values); i++ {
currentRate := dia.DefiRate{}
currentRate.Timestamp, err = time.Parse(time.RFC3339, res[0].Series[0].Values[i][0].(string))
if err != nil {
return retval, err
}
currentRate.Asset = res[0].Series[0].Values[i][1].(string)
if err != nil {
return retval, err
}
currentRate.BorrowingRate, err = res[0].Series[0].Values[i][2].(json.Number).Float64()
if err != nil {
return retval, err
}
currentRate.LendingRate, err = res[0].Series[0].Values[i][3].(json.Number).Float64()
if err != nil {
return retval, err
}
currentRate.Protocol = protocol
retval = append(retval, currentRate)
}
} else {
return retval, errors.New("parsing defi lending rate from database")
}
return retval, nil
}
func (datastore *DB) SetDefiStateInflux(state *dia.DefiProtocolState) error {
fields := map[string]interface{}{
"totalUSD": state.TotalUSD,
"totalETH": state.TotalETH,
}
tags := map[string]string{
"protocol": state.Protocol.Name,
}
pt, err := clientInfluxdb.NewPoint(influxDbDefiStateTable, tags, fields, state.Timestamp)
if err != nil {
log.Errorln("SetDefiStateInflux:", err)
} else {
datastore.addPoint(pt)
}
err = datastore.WriteBatchInflux()
if err != nil {
log.Errorln("SetDefiStateInflux", err)
}
return err
}
func (datastore *DB) GetDefiStateInflux(starttime time.Time, endtime time.Time, protocol string) (retval []dia.DefiProtocolState, err error) {
influxQuery := "SELECT totalETH,totalUSD FROM %s WHERE time > %d and time < %d and protocol = '%s'"
q := fmt.Sprintf(influxQuery, influxDbDefiStateTable, starttime.UnixNano(), endtime.UnixNano(), protocol)
res, err := queryInfluxDB(datastore.influxClient, q)
if err != nil {
return retval, err
}
if len(res) > 0 && len(res[0].Series) > 0 {
for i := 0; i < len(res[0].Series[0].Values); i++ {
defiState := dia.DefiProtocolState{}
defiState.Timestamp, err = time.Parse(time.RFC3339, res[0].Series[0].Values[i][0].(string))
if err != nil {
return
}
// defiState.Protocol.Name = res[0].Series[0].Values[i][1].(string)
// if err != nil {
// return
// }
defiState.TotalETH, err = res[0].Series[0].Values[i][1].(json.Number).Float64()
if err != nil {
return
}
defiState.TotalUSD, err = res[0].Series[0].Values[i][2].(json.Number).Float64()
if err != nil {
return
}
defiState.Protocol, err = datastore.GetDefiProtocol(protocol)
if err != nil {
return
}
retval = append(retval, defiState)
}
} else {
err = errors.New("parsing defi lending state from database")
return
}
return
}
func (datastore *DB) SaveSupplyInflux(supply *dia.Supply) error {
fields := map[string]interface{}{
"supply": supply.Supply,
"circulatingsupply": supply.CirculatingSupply,
"source": supply.Source,
}
tags := map[string]string{
"symbol": supply.Asset.Symbol,
"name": supply.Asset.Name,
"address": supply.Asset.Address,
"blockchain": supply.Asset.Blockchain,
}
pt, err := clientInfluxdb.NewPoint(influxDbSupplyTable, tags, fields, supply.Time)
if err != nil {
log.Errorln("NewSupplyInflux:", err)
} else {
datastore.addPoint(pt)
}
err = datastore.WriteBatchInflux()
if err != nil {
log.Errorln("SaveSupplyInflux", err)
}
return err
}
// GetSupplyInflux returns supply and circulating supply of @asset. Needs asset.Address and asset.Blockchain.
// If no time range is given it returns the latest supply.
func (datastore *DB) GetSupplyInflux(asset dia.Asset, starttime time.Time, endtime time.Time) ([]dia.Supply, error) {
retval := []dia.Supply{}
var q string
if starttime.IsZero() || endtime.IsZero() {
queryString := "SELECT supply,circulatingsupply,source,\"name\",\"symbol\" FROM %s WHERE \"address\" = '%s' AND \"blockchain\"='%s' AND time<now() ORDER BY DESC LIMIT 1"
q = fmt.Sprintf(queryString, influxDbSupplyTable, asset.Address, asset.Blockchain)
} else {
queryString := "SELECT supply,circulatingsupply,source,\"name\",\"symbol\" FROM %s WHERE time > %d AND time < %d AND \"address\" = '%s' AND \"blockchain\"='%s' ORDER BY DESC"
q = fmt.Sprintf(queryString, influxDbSupplyTable, starttime.UnixNano(), endtime.UnixNano(), asset.Address, asset.Blockchain)
}
res, err := queryInfluxDB(datastore.influxClient, q)
if err != nil {
return retval, err
}
if len(res) > 0 && len(res[0].Series) > 0 {
for i := 0; i < len(res[0].Series[0].Values); i++ {
currentSupply := dia.Supply{Asset: asset}
if res[0].Series[0].Values[i][0] != nil {
currentSupply.Time, err = time.Parse(time.RFC3339, res[0].Series[0].Values[i][0].(string))
if err != nil {
return retval, err
}
}
currentSupply.Supply, err = res[0].Series[0].Values[i][1].(json.Number).Float64()
if err != nil {
return retval, err
}
currentSupply.CirculatingSupply, err = res[0].Series[0].Values[i][2].(json.Number).Float64()
if err != nil {
return retval, err
}
if res[0].Series[0].Values[i][3] != nil {
currentSupply.Source = res[0].Series[0].Values[i][3].(string)
if err != nil {
return retval, err
}
}
if res[0].Series[0].Values[i][4] != nil {
currentSupply.Asset.Name = res[0].Series[0].Values[i][4].(string)
if err != nil {
log.Error("error getting symbol name from influx: ", err)
}
}
if res[0].Series[0].Values[i][5] != nil {
currentSupply.Asset.Symbol = res[0].Series[0].Values[i][5].(string)
if err != nil {
log.Error("error getting symbol name from influx: ", err)
}
}
retval = append(retval, currentSupply)
}
} else {
return retval, errors.New("parsing supply value from database")
}
return retval, nil
}
// SaveFilterInflux stores a filter point in influx.
func (datastore *DB) SaveFilterInflux(filter string, asset dia.Asset, exchange string, value float64, t time.Time) error {
// Create a point and add to batch
tags := map[string]string{
"filter": filter,
"symbol": asset.Symbol,
"address": asset.Address,
"blockchain": asset.Blockchain,
"exchange": exchange,
}
fields := map[string]interface{}{
"value": value,
"allExchanges": exchange == "",
}
pt, err := clientInfluxdb.NewPoint(influxDbFiltersTable, tags, fields, t)
if err != nil {
log.Errorln("new filter influx:", err)
} else {
datastore.addPoint(pt)
}
return err
}
func (datastore *DB) setZSETValue(key string, value float64, unixTime int64, maxWindow int64) error {
if datastore.redisClient == nil {
return nil
}
member := strconv.FormatFloat(value, 'f', -1, 64) + " " + strconv.FormatInt(unixTime, 10)
err := datastore.redisPipe.ZAdd(key, redis.Z{
Score: float64(unixTime),
Member: member,
}).Err()
log.Debug("SetZSETValue ", key, member, unixTime)
if err != nil {
log.Errorf("Error: %v on SetZSETValue %v\n", err, key)
}
// purging old values
err = datastore.redisPipe.ZRemRangeByScore(key, "-inf", "("+strconv.FormatInt(unixTime-maxWindow, 10)).Err()
if err != nil {
log.Errorf("Error: %v on SetZSETValue %v\n", err, key)
}
if err = datastore.redisPipe.Expire(key, TimeOutRedis).Err(); err != nil {
log.Error(err)
} //TODO put two commands together ?
return err
}
func (datastore *DB) getZSETValue(key string, atUnixTime int64) (float64, error) {
result := 0.0
max := strconv.FormatInt(atUnixTime, 10)
vals, err := datastore.redisClient.ZRangeByScoreWithScores(key, redis.ZRangeBy{
Min: "-inf",
Max: max,
}).Result()
log.Debug(key, "vals: %v on getZSETValue maxScore: %v", vals, max)
if err == nil {
if len(vals) > 0 {
_, err = fmt.Sscanf(vals[len(vals)-1].Member.(string), "%f", &result)
if err != nil {
log.Error(err)
}
log.Debugf("returned value: %v", result)
} else {
err = errors.New("getZSETValue no value found")
}
}
return result, err
}
func (datastore *DB) ExecuteRedisPipe() (err error) {
// TO DO: Handle first return value for read requests.
_, err = datastore.redisPipe.Exec()
return
}
func (datastore *DB) FlushRedisPipe() error {
return datastore.redisPipe.Discard()
}
/*
func (db *DB) getZSETSum(key string) (*float64, error) {
log.Debugf("getZSETSum: %v \n", key)
vals, err := db.redisClient.ZRange(key, 0, -1).Result()
if err != nil {
log.Errorf("Error: %v on getZSETSum %v\n", err, key)
return nil, err
} else {
result := 0.0
for _, v := range vals {
f := 0.0
fmt.Sscanf(v, "%f", &f)
result += f
}
return &result, err
}
}
*/