32 lines
861 B
Go
32 lines
861 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
|
)
|
|
|
|
type InfluxDB struct {
|
|
client influxdb2.Client
|
|
}
|
|
|
|
func ConnectInfluxDB(remote string, token string) (InfluxDB, error) {
|
|
ret := InfluxDB{}
|
|
ret.client = influxdb2.NewClient(remote, token)
|
|
_, err := ret.client.Ping(context.Background())
|
|
return ret, err
|
|
}
|
|
|
|
// Ping the InfluxDB server
|
|
func (influx *InfluxDB) Ping() (bool, error) {
|
|
return influx.client.Ping(context.Background())
|
|
}
|
|
|
|
// Write arbitrary measurement into the database
|
|
func (influx *InfluxDB) Write(org string, bucket string, measurement string, tags map[string]string, fields map[string]interface{}) error {
|
|
writeAPI := influx.client.WriteAPIBlocking(org, bucket)
|
|
point := influxdb2.NewPoint(measurement, tags, fields, time.Now())
|
|
return writeAPI.WritePoint(context.Background(), point)
|
|
}
|