external-go/influxdb/influxdb.go
crusader d4664c368e ing
2018-05-11 19:51:52 +09:00

113 lines
2.3 KiB
Go

package influxdb
import (
"git.loafle.net/commons/logging-go"
occe "git.loafle.net/overflow/commons-go/config/external"
"github.com/influxdata/influxdb/client/v2"
)
var (
clients map[string]client.Client
batchPoints map[string]client.BatchPoints
)
func InitPackage(config *occe.InfluxDB) {
if nil == config {
return
}
if nil != config.ClientConfigs && 0 < len(config.ClientConfigs) {
clients = make(map[string]client.Client)
for n, c := range config.ClientConfigs {
switch c.ClientType {
case "http":
hc := client.HTTPConfig{
Addr: c.Address,
Username: c.Username,
Password: c.Password,
UserAgent: c.UserAgent,
Timeout: c.Timeout,
}
clnt, err := client.NewHTTPClient(hc)
if nil != err {
logging.Logger().Errorf("%v", err)
return
}
clients[n] = clnt
case "udp":
uc := client.UDPConfig{
Addr: c.Address,
PayloadSize: c.PayloadSize,
}
clnt, err := client.NewUDPClient(uc)
if nil != err {
logging.Logger().Errorf("%v", err)
return
}
clients[n] = clnt
default:
logging.Logger().Errorf("Not Supported InfluxDB Client Type[%s]", c.ClientType)
return
}
}
}
if nil != config.BatchPointsConfigs && 0 < len(config.BatchPointsConfigs) {
batchPoints = make(map[string]client.BatchPoints)
for n, c := range config.BatchPointsConfigs {
bc := client.BatchPointsConfig{
Precision: c.Precision,
Database: c.Database,
RetentionPolicy: c.RetentionPolicy,
WriteConsistency: c.WriteConsistency,
}
bp, err := client.NewBatchPoints(bc)
if nil != err {
logging.Logger().Errorf("%v", err)
return
}
batchPoints[n] = bp
}
}
}
func StartPackage(config *occe.InfluxDB) {
if nil == config {
return
}
}
func StopPackage(config *occe.InfluxDB) {
if nil == config {
return
}
}
func DestroyPackage(config *occe.InfluxDB) {
if nil == config {
return
}
for _, c := range clients {
if err := c.Close(); nil != err {
logging.Logger().Errorf("%v", err)
}
}
}
func ClientFor(name string) client.Client {
if nil == clients {
return nil
}
return clients[name]
}
func BatchPointsFor(name string) client.BatchPoints {
if nil == batchPoints {
return nil
}
return batchPoints[name]
}