github.com/diadata-org/diadata@v1.4.593/pkg/model/fiatQuotation.go (about)

     1  package models
     2  
     3  import (
     4  	"fmt"
     5  
     6  	clientInfluxdb "github.com/influxdata/influxdb1-client/v2"
     7  )
     8  
     9  func (datastore *DB) SetBatchFiatPriceInflux(fiatQuotations []*FiatQuotation) error {
    10  	err := checkInfluxIsAvailable(datastore)
    11  	if err != nil {
    12  		return err
    13  	}
    14  
    15  	addMultiplePointsToBatch(datastore, fiatQuotations)
    16  
    17  	err = datastore.WriteBatchInflux()
    18  	if err != nil {
    19  		log.Printf("Error on WriteBatchInflux: %v\n", err)
    20  	}
    21  
    22  	return err
    23  }
    24  
    25  func checkInfluxIsAvailable(db *DB) error {
    26  	if db.influxClient == nil {
    27  		return fmt.Errorf("SetInfluxFiatPrice error: no influx database available")
    28  	}
    29  	return nil
    30  }
    31  
    32  func addMultiplePointsToBatch(db *DB, fiatQuotations []*FiatQuotation) {
    33  	for _, fq := range fiatQuotations {
    34  		tags := map[string]string{
    35  			"quote_currency": fq.QuoteCurrency,
    36  			"base_currency":  fq.BaseCurrency,
    37  			"source":         fq.Source,
    38  		}
    39  		fields := map[string]interface{}{
    40  			"price": fq.Price,
    41  		}
    42  
    43  		pt, err := clientInfluxdb.NewPoint(influxDbFiatQuotationsTable, tags, fields, fq.Time)
    44  		if err != nil {
    45  			log.Printf("Error: %v on NewPoint %v\n", err, fq.BaseCurrency)
    46  		}
    47  
    48  		db.addPoint(pt)
    49  	}
    50  }
    51  
    52  func (datastore *DB) SetSingleFiatPriceRedis(fiatQuotation *FiatQuotation) error {
    53  	err := checkRedisIsAvailable(datastore)
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	key := getKeyQuotation(fiatQuotation.QuoteCurrency)
    59  	log.Info("setting ", key, fiatQuotation)
    60  
    61  	err = datastore.redisClient.Set(key, fiatQuotation, TimeOutRedis).Err()
    62  	if err != nil {
    63  		log.Printf("Error: %v on SetQuotation %v\n", err, fiatQuotation.QuoteCurrency)
    64  	}
    65  
    66  	return err
    67  }
    68  
    69  func checkRedisIsAvailable(db *DB) error {
    70  	if db.redisClient == nil {
    71  		return fmt.Errorf("SetRedisFiatPrice error: no redis database available")
    72  	}
    73  	return nil
    74  }