-
Notifications
You must be signed in to change notification settings - Fork 0
/
influxdb.go
48 lines (43 loc) · 954 Bytes
/
influxdb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package util
import (
"time"
client "github.com/influxdata/influxdb/client/v2"
)
//Influxdb 历史数据库连接
var Influxdb client.Client
//InitInflux 连接influxdb
func InitInflux() error {
var err error
for i := 0; i < 3; i++ {
Influxdb, err = client.NewHTTPClient(client.HTTPConfig{
Addr: cfg.Influx.Host,
Username: cfg.Influx.User,
Password: cfg.Influx.Password,
})
if err != nil {
Logger.Error("influx连接失败" + err.Error())
time.Sleep(3 * time.Second)
} else {
break
}
}
return err
}
//QueryDB 查询influxdb数据
func QueryDB(cmd string) (res []client.Result, err error) {
database := cfg.Influx.Database
q := client.Query{
Command: cmd,
// Database: "funcInfluxdb",
Database: database,
}
if response, err := Influxdb.Query(q); err == nil {
if response.Error() != nil {
return res, response.Error()
}
res = response.Results
} else {
return res, err
}
return res, nil
}