You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.1 KiB

package api
import (
"github.com/influxdata/influxdb/client/v2"
"github.com/spf13/viper"
"log"
"time"
)
func InsertInflux(pName string, pE5Price float64, pE10Price float64, pDieselPrice float64) {
// Create a new HTTPClient
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: viper.GetString("Database.Url"),
Username: viper.GetString("Database.Username"),
Password: viper.GetString("Database.Password"),
})
if err != nil {
log.Fatal(err)
}
defer c.Close()
// Create a new point batch
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: viper.GetString("Database.Database"),
Precision: "s",
})
if err != nil {
log.Fatal(err)
}
// Create a point and add to batch
tags := map[string]string{"petrol_station": pName}
fields := map[string]interface{}{
"E5": pE5Price,
"E10": pE10Price,
"Diesel": pDieselPrice,
}
pt, err := client.NewPoint("price", tags, fields, time.Now())
if err != nil {
log.Fatal(err)
}
bp.AddPoint(pt)
// Write the batch
if err := c.Write(bp); err != nil {
log.Fatal(err)
}
// Close client resources
if err := c.Close(); err != nil {
log.Fatal(err)
}
}