forked from cncf/devstats.archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idb_conn.go
88 lines (79 loc) · 2.22 KB
/
idb_conn.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package gha2db
import (
"fmt"
"time"
client "github.com/influxdata/influxdb/client/v2"
)
// IDBConn Connects to InfluxDB database
func IDBConn(ctx *Ctx) client.Client {
con, err := client.NewHTTPClient(client.HTTPConfig{
Addr: fmt.Sprintf("%s:%s", ctx.IDBHost, ctx.IDBPort),
Username: ctx.IDBUser,
Password: ctx.IDBPass,
})
FatalOnError(err)
return con
}
// IDBBatchPoints returns batch points for given connection and database from context
func IDBBatchPoints(ctx *Ctx, con *client.Client) client.BatchPoints {
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: ctx.IDBDB,
Precision: "h", // Was "s" - but GHA resolution is hours
})
FatalOnError(err)
return bp
}
// IDBBatchPointsWithDB returns batch points for given connection and database from context
func IDBBatchPointsWithDB(ctx *Ctx, con *client.Client, db string) client.BatchPoints {
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: db,
Precision: "h", // Was "s" - but GHA resolution is hours
})
FatalOnError(err)
return bp
}
// IDBNewPointWithErr - return InfluxDB Point, on error exit
func IDBNewPointWithErr(name string, tags map[string]string, fields map[string]interface{}, dt time.Time) *client.Point {
pt, err := client.NewPoint(name, tags, fields, dt)
FatalOnError(err)
return pt
}
// QueryIDB - do InfluxDB query
func QueryIDB(con client.Client, ctx *Ctx, query string) []client.Result {
if ctx.QOut {
Printf("%s\n", query)
}
q := client.Query{
Command: query,
Database: ctx.IDBDB,
}
response, err := con.Query(q)
FatalOnError(err)
FatalOnError(response.Error())
return response.Results
}
// QueryIDBWithDB - do InfluxDB query
func QueryIDBWithDB(con client.Client, ctx *Ctx, query, db string) []client.Result {
if ctx.QOut {
Printf("%s\n", query)
}
q := client.Query{
Command: query,
Database: db,
}
response, err := con.Query(q)
FatalOnError(err)
FatalOnError(response.Error())
return response.Results
}
// SafeQueryIDB - do InfluxDB query, on error return error data
func SafeQueryIDB(con client.Client, ctx *Ctx, query string) (*client.Response, error) {
if ctx.QOut {
Printf("%s\n", query)
}
q := client.Query{
Command: query,
Database: ctx.IDBDB,
}
return con.Query(q)
}