72 lines
2.6 KiB
Go
72 lines
2.6 KiB
Go
|
package timeSeriesDB
|
||
|
|
||
|
import (
|
||
|
"gullion/currency_convert_ssheets/models"
|
||
|
utils "gullion/currency_convert_ssheets/utility"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
const layoutDbTime = "2006-01-02T15:04:05Z07:00"
|
||
|
|
||
|
func WriteExchangeRecords(exchangeRecords models.ExchangeRecords) {
|
||
|
pts := getTimeSeriesWriteablePoints(exchangeRecords.ExchangeRecords, "forex_data")
|
||
|
doClientWrite(pts)
|
||
|
}
|
||
|
|
||
|
func ReadExchangeRecords(tagSet string, startDate time.Time, endDate time.Time) []models.ExchangeRecord {
|
||
|
response := doClientQuery("select * from forex_data WHERE \"Conversion\" = '" + tagSet + "' " +
|
||
|
"AND time >= '" + startDate.Format(layoutDbTime) + "' AND time <= '" + endDate.Format(layoutDbTime) + "'")
|
||
|
return getExchangeRatesModel(response)
|
||
|
}
|
||
|
|
||
|
func ReadExchangeRecordsMonthly(tagSet string, dailyToMonthlyAggregationMethod string) map[string]models.ExchangeRecord {
|
||
|
response := doClientQuery("select * from forex_data WHERE \"Conversion\" = '" + tagSet + "' AND time >= '2011-01-01T00:00:00Z' AND time <= '2020-12-31T00:54:00Z'")
|
||
|
return ConvertDailyToMonthlyExchange(getExchangeRatesModel(response), dailyToMonthlyAggregationMethod)
|
||
|
}
|
||
|
|
||
|
func GetLatestExchangeRecordByTimeStamp(tagSet string) models.ExchangeRecord {
|
||
|
response := doClientQuery("select * from forex_data WHERE \"Conversion\" = '" + tagSet + "' " +
|
||
|
" ORDER BY time DESC limit 1")
|
||
|
if len(getExchangeRatesModel(response)) > 0 {
|
||
|
return getExchangeRatesModel(response)[0]
|
||
|
} else {
|
||
|
return models.ExchangeRecord{}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func GetLatestBKMRecordByTimeStamp(measurement string) models.BKMRecord {
|
||
|
response := doClientQuery("select * from " + measurement + " ORDER BY time DESC limit 2")
|
||
|
bkmRecordsList := getBKMModelList(response)
|
||
|
|
||
|
if len(bkmRecordsList) > 0 {
|
||
|
return bkmRecordsList[0]
|
||
|
} else {
|
||
|
return models.BKMRecord{}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func ReadBKMRecords(measurement string) map[int][]models.BKMRecord {
|
||
|
response := doClientQuery("select * from " + measurement)
|
||
|
bkmYearlyHashMap := make(map[int][]models.BKMRecord)
|
||
|
bkmRecordsList := getBKMModelList(response)
|
||
|
|
||
|
for _, record := range bkmRecordsList {
|
||
|
if _, ok := bkmYearlyHashMap[utils.DateToYear(record.Date)]; ok {
|
||
|
tempList := bkmYearlyHashMap[utils.DateToYear(record.Date)]
|
||
|
tempList = append(tempList, record)
|
||
|
bkmYearlyHashMap[utils.DateToYear(record.Date)] = tempList
|
||
|
} else {
|
||
|
var tempList []models.BKMRecord
|
||
|
tempList = append(tempList, record)
|
||
|
bkmYearlyHashMap[utils.DateToYear(record.Date)] = tempList
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return bkmYearlyHashMap
|
||
|
}
|
||
|
|
||
|
func WriteBkmRecordsRecords(bkmRecords models.BKMRecords, measurement string) {
|
||
|
pts := getTimeSeriesWriteablePointsBKM(bkmRecords.BKMRecords, measurement)
|
||
|
doClientWrite(pts)
|
||
|
}
|