-
Notifications
You must be signed in to change notification settings - Fork 129
/
fiatQuotation.go
74 lines (59 loc) · 1.69 KB
/
fiatQuotation.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
package models
import (
"fmt"
clientInfluxdb "github.com/influxdata/influxdb1-client/v2"
)
func (datastore *DB) SetBatchFiatPriceInflux(fiatQuotations []*FiatQuotation) error {
err := checkInfluxIsAvailable(datastore)
if err != nil {
return err
}
addMultiplePointsToBatch(datastore, fiatQuotations)
err = datastore.WriteBatchInflux()
if err != nil {
log.Printf("Error on WriteBatchInflux: %v\n", err)
}
return err
}
func checkInfluxIsAvailable(db *DB) error {
if db.influxClient == nil {
return fmt.Errorf("SetInfluxFiatPrice error: no influx database available")
}
return nil
}
func addMultiplePointsToBatch(db *DB, fiatQuotations []*FiatQuotation) {
for _, fq := range fiatQuotations {
tags := map[string]string{
"quote_currency": fq.QuoteCurrency,
"base_currency": fq.BaseCurrency,
"source": fq.Source,
}
fields := map[string]interface{}{
"price": fq.Price,
}
pt, err := clientInfluxdb.NewPoint(influxDbFiatQuotationsTable, tags, fields, fq.Time)
if err != nil {
log.Printf("Error: %v on NewPoint %v\n", err, fq.BaseCurrency)
}
db.addPoint(pt)
}
}
func (datastore *DB) SetSingleFiatPriceRedis(fiatQuotation *FiatQuotation) error {
err := checkRedisIsAvailable(datastore)
if err != nil {
return err
}
key := getKeyQuotation(fiatQuotation.QuoteCurrency)
log.Info("setting ", key, fiatQuotation)
err = datastore.redisClient.Set(key, fiatQuotation, TimeOutRedis).Err()
if err != nil {
log.Printf("Error: %v on SetQuotation %v\n", err, fiatQuotation.QuoteCurrency)
}
return err
}
func checkRedisIsAvailable(db *DB) error {
if db.redisClient == nil {
return fmt.Errorf("SetRedisFiatPrice error: no redis database available")
}
return nil
}