209 lines
5.8 KiB
Go
209 lines
5.8 KiB
Go
|
package timeSeriesDB
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
client "github.com/influxdata/influxdb1-client"
|
||
|
"gullion/currency_convert_ssheets/exchangeUtility"
|
||
|
"gullion/currency_convert_ssheets/models"
|
||
|
"gullion/currency_convert_ssheets/utility"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
const numberTransaction = "Number_transaction"
|
||
|
const VolumeTransaction = "Volume_transaction"
|
||
|
|
||
|
func getTimeSeriesWriteablePoints(exchangeRecords []models.ExchangeRecord, measurement string) []client.Point {
|
||
|
|
||
|
pts := make([]client.Point, len(exchangeRecords))
|
||
|
|
||
|
for i, exchangeRecord := range exchangeRecords {
|
||
|
pts[i] = client.Point{
|
||
|
Measurement: measurement,
|
||
|
Tags: map[string]string{
|
||
|
"Conversion": exchangeRecord.Tag,
|
||
|
},
|
||
|
Fields: map[string]interface{}{
|
||
|
"value": exchangeRecord.ToCurrency,
|
||
|
},
|
||
|
Time: exchangeRecord.Date.UTC(),
|
||
|
Precision: "n",
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return pts
|
||
|
}
|
||
|
|
||
|
func getExchangeRatesModel(response *client.Response) []models.ExchangeRecord {
|
||
|
var exchangeRecords []models.ExchangeRecord
|
||
|
if response.Results[0].Series != nil {
|
||
|
for _, record := range response.Results[0].Series[0].Values {
|
||
|
exchangeRecord := models.ExchangeRecord{}
|
||
|
for j, item := range record {
|
||
|
itemString := fmt.Sprintf("%v", item)
|
||
|
if j == 0 {
|
||
|
t, err := time.Parse(time.RFC3339Nano, itemString)
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
}
|
||
|
exchangeRecord.Date = t
|
||
|
} else if j == 1 {
|
||
|
exchangeRecord.Tag = itemString
|
||
|
} else if j == 2 {
|
||
|
exchangeRecord.ToCurrency = utility.StringToFloat(itemString)
|
||
|
}
|
||
|
}
|
||
|
exchangeRecords = append(exchangeRecords, exchangeRecord)
|
||
|
}
|
||
|
}
|
||
|
return exchangeRecords
|
||
|
|
||
|
}
|
||
|
|
||
|
func ConvertDailyToMonthlyExchange(exchangeRecords []models.ExchangeRecord, dailyToMonthlyAggregationMethod string) map[string]models.ExchangeRecord {
|
||
|
|
||
|
monthlyExchangeRatesHashMap := make(map[string]models.ExchangeRecord)
|
||
|
|
||
|
resolveDaysToMonthRecursion(exchangeRecords, monthlyExchangeRatesHashMap, dailyToMonthlyAggregationMethod)
|
||
|
|
||
|
return monthlyExchangeRatesHashMap
|
||
|
}
|
||
|
|
||
|
func resolveDaysToMonthRecursion(exchangeRecords []models.ExchangeRecord,
|
||
|
monthlyExchangeRatesHashMap map[string]models.ExchangeRecord, dailyToMonthlyAggregationMethod string) {
|
||
|
var tempMonth time.Month
|
||
|
var tempMonthList []models.ExchangeRecord
|
||
|
nextMonthIndex := 0
|
||
|
for i, record := range exchangeRecords {
|
||
|
|
||
|
if utility.DateToMonth(record.Date) == tempMonth {
|
||
|
tempMonthList = append(tempMonthList, record)
|
||
|
} else if tempMonth == 0 {
|
||
|
tempMonth = utility.DateToMonth(record.Date)
|
||
|
tempMonthList = append(tempMonthList, record)
|
||
|
} else {
|
||
|
nextMonthIndex = i
|
||
|
break
|
||
|
}
|
||
|
|
||
|
}
|
||
|
if nextMonthIndex == 0 {
|
||
|
nextMonthIndex = len(exchangeRecords)
|
||
|
}
|
||
|
|
||
|
exchangeRecords = append(exchangeRecords[:0], exchangeRecords[nextMonthIndex:]...)
|
||
|
|
||
|
//fmt.Println(exchangeRecords)
|
||
|
aggregator := exchangeUtility.AggregateDailyToMonthly{}
|
||
|
|
||
|
for _, records := range tempMonthList {
|
||
|
aggregator.AggregatedNumbers = append(aggregator.AggregatedNumbers, records.ToCurrency)
|
||
|
}
|
||
|
if len(tempMonthList) > 0 {
|
||
|
|
||
|
avg := aggregator.CalcValue(dailyToMonthlyAggregationMethod)
|
||
|
newRecord := tempMonthList[0]
|
||
|
newRecord.ToCurrency = utility.SetFloatPrecision(avg, 9)
|
||
|
key := strconv.Itoa(utility.DateToYear(newRecord.Date)) + " " + strconv.Itoa(int(utility.DateToMonth(newRecord.Date)))
|
||
|
monthlyExchangeRatesHashMap[key] = newRecord
|
||
|
}
|
||
|
|
||
|
if len(exchangeRecords) > 0 {
|
||
|
resolveDaysToMonthRecursion(exchangeRecords, monthlyExchangeRatesHashMap, dailyToMonthlyAggregationMethod)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func getTimeSeriesWriteablePointsBKM(bkmRecords []models.BKMRecord, measurement string) []client.Point {
|
||
|
|
||
|
pts := make([]client.Point, len(bkmRecords)*2)
|
||
|
|
||
|
for i, bkmRecord := range bkmRecords {
|
||
|
newIndex := i * 2
|
||
|
pts[newIndex] = client.Point{
|
||
|
Measurement: measurement,
|
||
|
Tags: map[string]string{
|
||
|
"Conversion": numberTransaction,
|
||
|
},
|
||
|
Fields: map[string]interface{}{
|
||
|
"purchase": bkmRecord.NumberOfTransactions.Purchase,
|
||
|
"cash": bkmRecord.NumberOfTransactions.Cash,
|
||
|
"total": bkmRecord.NumberOfTransactions.Total,
|
||
|
},
|
||
|
Time: bkmRecord.Date.UTC(),
|
||
|
Precision: "n",
|
||
|
}
|
||
|
|
||
|
pts[newIndex+1] = client.Point{
|
||
|
Measurement: measurement,
|
||
|
Tags: map[string]string{
|
||
|
"Conversion": VolumeTransaction,
|
||
|
},
|
||
|
Fields: map[string]interface{}{
|
||
|
"purchase": bkmRecord.VolumeOfTransactions.Purchase,
|
||
|
"cash": bkmRecord.VolumeOfTransactions.Cash,
|
||
|
"total": bkmRecord.VolumeOfTransactions.Total,
|
||
|
},
|
||
|
Time: bkmRecord.Date.UTC(),
|
||
|
Precision: "n",
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return pts
|
||
|
}
|
||
|
|
||
|
func getBKMModelList(response *client.Response) []models.BKMRecord {
|
||
|
var bkmRecords []models.BKMRecord
|
||
|
if response.Results[0].Series != nil {
|
||
|
for i := 0; i < len(response.Results[0].Series[0].Values)/2; i++ {
|
||
|
newIndex := i * 2
|
||
|
bkmRecord := models.BKMRecord{}
|
||
|
//READ first record
|
||
|
record := response.Results[0].Series[0].Values[newIndex]
|
||
|
bkmRecord = getBKMRecordPopulated(bkmRecord, record)
|
||
|
//READ Second record
|
||
|
record = response.Results[0].Series[0].Values[newIndex+1]
|
||
|
bkmRecord = getBKMRecordPopulated(bkmRecord, record)
|
||
|
|
||
|
bkmRecords = append(bkmRecords, bkmRecord)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return bkmRecords
|
||
|
|
||
|
}
|
||
|
|
||
|
func getBKMRecordPopulated(bkmRecord models.BKMRecord, record []interface{}) models.BKMRecord {
|
||
|
isNumberTransaction := false
|
||
|
tmpTransaction := models.Transaction{}
|
||
|
for j, item := range record {
|
||
|
|
||
|
itemString := fmt.Sprintf("%v", item)
|
||
|
if j == 0 {
|
||
|
t, err := time.Parse(time.RFC3339Nano, itemString)
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
}
|
||
|
bkmRecord.Date = t
|
||
|
} else if j == 1 {
|
||
|
if numberTransaction == itemString {
|
||
|
isNumberTransaction = true
|
||
|
}
|
||
|
|
||
|
} else if j == 2 {
|
||
|
tmpTransaction.Cash = utility.StringToFloat(itemString)
|
||
|
} else if j == 3 {
|
||
|
tmpTransaction.Purchase = utility.StringToFloat(itemString)
|
||
|
} else if j == 4 {
|
||
|
tmpTransaction.Total = utility.StringToFloat(itemString)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if isNumberTransaction {
|
||
|
bkmRecord.NumberOfTransactions = tmpTransaction
|
||
|
} else {
|
||
|
bkmRecord.VolumeOfTransactions = tmpTransaction
|
||
|
}
|
||
|
|
||
|
return bkmRecord
|
||
|
}
|