-
Notifications
You must be signed in to change notification settings - Fork 128
/
filters.go
317 lines (282 loc) · 9.46 KB
/
filters.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
package models
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"time"
"github.com/diadata-org/diadata/pkg/dia"
"github.com/go-redis/redis"
clientInfluxdb "github.com/influxdata/influxdb1-client/v2"
)
// SetFilter stores a filter point
func (datastore *DB) SetFilter(filter string, asset dia.Asset, exchange string, value float64, t time.Time) error {
// return datastore.SaveFilterInflux(filter, asset, exchange, value, t)
err := datastore.SaveFilterInflux(filter, asset, exchange, value, t)
if err != nil {
return err
}
err = datastore.setZSETValue(getKeyFilterZSET(getKey(filter, asset, exchange)), value, t.Unix(), BiggestWindow)
if err != nil {
return err
}
return err
}
func (datastore *DB) GetFilterPointsAsset(filter string, exchange string, address string, blockchain string, starttime time.Time, endtime time.Time) (*Points, error) {
exchangeQuery := "AND exchange='" + exchange + "' "
q := fmt.Sprintf("SELECT time,address,blockchain,exchange,filter,symbol,value FROM %s"+
" WHERE filter='%s' %s AND address='%s' and blockchain='%s' AND time>%d and time<=%d ORDER BY DESC",
influxDbFiltersTable, filter, exchangeQuery, address, blockchain, starttime.UnixNano(), endtime.UnixNano())
res, err := queryInfluxDB(datastore.influxClient, q)
if err != nil {
log.Errorln("GetFilterPoints", err)
}
log.Info("GetFilterPoints query: ", q, " returned ", len(res))
return &Points{
DataPoints: res,
}, err
}
// GetFilterPoints returns filter points from either a specific exchange or all exchanges.
// symbol is mapped to the underlying asset with biggest market cap.
func (datastore *DB) GetFilterPoints(filter string, exchange string, symbol string, scale string, starttime time.Time, endtime time.Time) (*Points, error) {
relDB, err := NewRelDataStore()
if err != nil {
log.Errorln("NewDataStore:", err)
}
// First get asset with @symbol with largest volume.
// topAsset, err := db.GetTopAssetByVolume(symbol, relDB)
sortedAssets, err := relDB.GetTopAssetByVolume(symbol)
if err != nil {
log.Error(err)
return &Points{}, err
}
if len(sortedAssets) == 0 {
return nil, errors.New("no traded assets found")
}
topAsset := sortedAssets[0]
exchangeQuery := "and exchange='" + exchange + "' "
table := ""
// 5m 30m 1h 4h 1d 1w
if scale != "" {
if filter == "VOL120" {
table = "a_year.filters_sum_"
} else {
table = "a_year.filters_mean_"
}
table = table + scale
} else {
table = influxDbFiltersTable
}
q := fmt.Sprintf("SELECT time,exchange,filter,symbol,value FROM %s"+
" WHERE filter='%s' %sand address='%s' and blockchain='%s' and time>%d and time<%d ORDER BY DESC",
table, filter, exchangeQuery, topAsset.Address, topAsset.Blockchain, starttime.UnixNano(), endtime.UnixNano())
res, err := queryInfluxDB(datastore.influxClient, q)
if err != nil {
log.Errorln("GetFilterPoints", err)
}
log.Info("GetFilterPoints query: ", q, " returned ", len(res))
return &Points{
DataPoints: res,
}, err
}
func (datastore *DB) GetFilter(filter string, topAsset dia.Asset, scale string, starttime time.Time, endtime time.Time) ([]dia.FilterPoint, error) {
var allFilters []dia.FilterPoint
table := ""
// 5m 30m 1h 4h 1d 1w
if scale != "" {
if filter == "VOL120" {
table = "a_year.filters_sum_"
} else {
table = "a_year.filters_mean_"
}
table = table + scale
} else {
table = influxDbFiltersTable
}
q := fmt.Sprintf("SELECT last(*) FROM %s"+
" WHERE filter='%s' and address='%s' and blockchain='%s' and time>%d and time<%d and allExchanges=true group by time(1d) fill(previous) ORDER BY DESC",
table, filter, topAsset.Address, topAsset.Blockchain, starttime.UnixNano(), endtime.UnixNano())
res, err := queryInfluxDB(datastore.influxClient, q)
if err != nil {
log.Errorln("GetFilterPoints", err)
}
log.Info("GetFilterPoints query: ", q, " returned ", len(res))
if len(res) > 0 && len(res[0].Series) > 0 {
for i := 0; i < len(res[0].Series[0].Values); i++ {
var filterpoint dia.FilterPoint
filterpoint.Time, err = time.Parse(time.RFC3339, res[0].Series[0].Values[i][0].(string))
if err != nil {
return allFilters, err
}
// if res[0].Series[0].Values[i][1] != nil {
// filterpoint.Exchange = res[0].Series[0].Values[i][1].(string)
// }
// if res[0].Series[0].Values[i][2] != nil {
// filterpoint.Name = res[0].Series[0].Values[i][2].(string)
// }
// if res[0].Series[0].Values[i][3] != nil {
// filterpoint.Asset.Symbol = res[0].Series[0].Values[i][3].(string)
// }
if res[0].Series[0].Values[i][2] != nil {
filterpoint.Value, err = res[0].Series[0].Values[i][2].(json.Number).Float64()
} else {
log.Errorln("res[0].Series[0].Values[i][2]", res[0].Series[0].Values[i][2])
}
if err != nil {
return allFilters, err
}
allFilters = append(allFilters, filterpoint)
}
} else {
return allFilters, errors.New("no filter points in time range")
}
return allFilters, err
}
// GetFilterAllExchanges returns a slice of quotations for each exchange the asset given by
// @address and @blockchain has a filter value in the given time-range.
// It returns the most recent filter value in the given time-range for each exchange resp.
func (datastore *DB) GetFilterAllExchanges(
filter string,
address string,
blockchain string,
starttime time.Time,
endtime time.Time,
) (assetQuotations []AssetQuotation, err error) {
q := fmt.Sprintf(`
SELECT time,address,blockchain,symbol,value
FROM %s
WHERE filter='%s'
AND allExchanges=false
AND address='%s'
AND blockchain='%s'
AND time>%d
AND time<=%d
GROUP BY "exchange"
ORDER BY DESC
LIMIT 1`,
influxDbFiltersTable, filter, address, blockchain, starttime.UnixNano(), endtime.UnixNano())
res, err := queryInfluxDB(datastore.influxClient, q)
if err != nil {
log.Errorln("GetFilterPoints", err)
return
}
if len(res) > 0 && len(res[0].Series) > 0 {
for _, row := range res[0].Series {
var aq AssetQuotation
aq.Source = row.Tags["exchange"]
aq.Price, err = row.Values[0][4].(json.Number).Float64()
if err != nil {
log.Warn("parse price: ", err)
}
aq.Asset = dia.Asset{Address: address, Blockchain: blockchain, Symbol: row.Values[0][3].(string)}
aq.Time, err = time.Parse(time.RFC3339, row.Values[0][0].(string))
if err != nil {
log.Warn("parse time: ", err)
}
assetQuotations = append(assetQuotations, aq)
}
}
return
}
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"
}
}
// 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) getZSETLastValue(key string) (float64, int64, error) {
value := 0.0
var unixTime int64
vals, err := datastore.redisClient.ZRange(key, -1, -1).Result()
log.Debug(key, "on getZSETLastValue:", vals)
if err == nil {
if len(vals) == 1 {
_, err = fmt.Sscanf(vals[0], "%f %d", &value, &unixTime)
if err != nil {
log.Error(err)
}
log.Debugf("returned value: %v", value)
} else {
err = errors.New("getZSETLastValue no value found")
log.Errorln("Error: on getZSETLastValue", err, key)
}
}
return value, unixTime, err
}