1 Star 0 Fork 0

carlmax_my/console-core-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
influxdb.go 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
carlmax_my 提交于 2024-12-02 21:32 . init project
package influxdb
import (
"context"
"crypto/tls"
"log"
"time"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/influxdata/influxdb-client-go/v2/api"
)
var client influxdb2.Client = nil
type InfluxdbOptions struct {
token string
url string
org string
bucket string
}
type InfluxDbClient struct {
InfluxdbOptions
client influxdb2.Client
}
func NewClient(opts *InfluxdbOptions) *InfluxDbClient {
// Create a new client using an InfluxDB server base URL and an authentication token
if client == nil {
token := opts.token
url := opts.url
client = influxdb2.NewClientWithOptions(url, token,
influxdb2.DefaultOptions().
SetTLSConfig(&tls.Config{
InsecureSkipVerify: true,
}))
}
return &InfluxDbClient{
InfluxdbOptions: *opts,
client: client,
}
}
func (i *InfluxDbClient) SendToInfluxdb(measurement string, ts time.Time, tags map[string]string, fields map[string]interface{}) {
org := i.org
bucket := i.bucket
writeAPI := i.client.WriteAPIBlocking(org, bucket)
point := influxdb2.NewPoint(measurement, tags, fields, ts)
if err := writeAPI.WritePoint(context.Background(), point); err != nil {
log.Println("SendToInfluxdb err:" + err.Error())
}
}
func (i *InfluxDbClient) RunQuery(query string) (*api.QueryTableResult, error) {
org := i.org
queryAPI := i.client.QueryAPI(org)
return queryAPI.Query(context.Background(), query)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/carlmax_my/console-core-go.git
git@gitee.com:carlmax_my/console-core-go.git
carlmax_my
console-core-go
console-core-go
v0.0.16

搜索帮助

0d507c66 1850385 C8b1a773 1850385