-
Notifications
You must be signed in to change notification settings - Fork 116
/
examples_test.go
152 lines (127 loc) · 4.33 KB
/
examples_test.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2020-2021 InfluxData, Inc. All rights reserved.
// Use of this source code is governed by MIT
// license that can be found in the LICENSE file.
package influxdb2_test
import (
"context"
"fmt"
"github.com/influxdata/influxdb-client-go/v2"
"github.com/influxdata/influxdb-client-go/v2/domain"
)
func ExampleClient_newClient() {
// Create a new client using an InfluxDB server base URL and an authentication token
client := influxdb2.NewClient("http://localhost:8086", "my-token")
// Always close client at the end
defer client.Close()
}
func ExampleClient_newClientWithOptions() {
// Create a new client using an InfluxDB server base URL and an authentication token
// Create client and set batch size to 20
client := influxdb2.NewClientWithOptions("http://localhost:8086", "my-token",
influxdb2.DefaultOptions().SetBatchSize(20))
// Always close client at the end
defer client.Close()
}
func ExampleClient_customServerAPICall() {
// This example shows how to perform custom server API invocation for any endpoint
// Here we will create a DBRP mapping which allows using buckets in legacy write and query (InfluxQL) endpoints
// Create client. You need an admin token for creating DBRP mapping
client := influxdb2.NewClient("http://localhost:8086", "my-token")
// Always close client at the end
defer client.Close()
// Get generated client for server API calls
apiClient := client.APIClient()
ctx := context.Background()
// Get a bucket we would like to query using InfluxQL
b, err := client.BucketsAPI().FindBucketByName(ctx, "my-bucket")
if err != nil {
panic(err)
}
// Get an organization that will own the mapping
o, err := client.OrganizationsAPI().FindOrganizationByName(ctx, "my-org")
if err != nil {
panic(err)
}
yes := true
// Fill required fields of the DBRP struct
dbrp := domain.DBRPCreate{
BucketID: *b.Id,
Database: "my-bucket",
Default: &yes,
OrgID: o.Id,
RetentionPolicy: "autogen",
}
params := &domain.PostDBRPAllParams{
Body: domain.PostDBRPJSONRequestBody(dbrp),
}
// Call server API
newDbrp, err := apiClient.PostDBRP(ctx, params)
if err != nil {
panic(err)
}
// Check generated response errors
fmt.Printf("Created DBRP: %#v\n", newDbrp)
}
func ExampleClient_checkAPICall() {
// This example shows how to perform custom server API invocation for checks API
// Create client. You need an admin token for creating DBRP mapping
client := influxdb2.NewClient("http://localhost:8086", "my-token")
// Always close client at the end
defer client.Close()
ctx := context.Background()
// Create a new threshold check
greater := domain.GreaterThreshold{}
greater.Value = 10.0
lc := domain.CheckStatusLevelCRIT
greater.Level = &lc
greater.AllValues = &[]bool{true}[0]
lesser := domain.LesserThreshold{}
lesser.Value = 1.0
lo := domain.CheckStatusLevelOK
lesser.Level = &lo
rang := domain.RangeThreshold{}
rang.Min = 3.0
rang.Max = 8.0
lw := domain.CheckStatusLevelWARN
rang.Level = &lw
thresholds := []domain.Threshold{&greater, &lesser, &rang}
// Get organization where check will be created
org, err := client.OrganizationsAPI().FindOrganizationByName(ctx, "my-org")
if err != nil {
panic(err)
}
// Prepare necessary parameters
msg := "Check: ${ r._check_name } is: ${ r._level }"
flux := `from(bucket: "foo") |> range(start: -1d, stop: now()) |> aggregateWindow(every: 1m, fn: mean) |> filter(fn: (r) => r._field == "usage_user") |> yield()`
every := "1h"
offset := "0s"
c := domain.ThresholdCheck{
CheckBaseExtend: domain.CheckBaseExtend{
CheckBase: domain.CheckBase{
Name: "My threshold check",
OrgID: *org.Id,
Query: domain.DashboardQuery{Text: &flux},
Status: domain.TaskStatusTypeActive,
},
Every: &every,
Offset: &offset,
StatusMessageTemplate: &msg,
},
Thresholds: &thresholds,
}
params := domain.CreateCheckAllParams{
Body: &c,
}
// Call checks API using internal API client
check, err := client.APIClient().CreateCheck(context.Background(), ¶ms)
if err != nil {
panic(err)
}
// Optionally verify type
if check.Type() != string(domain.ThresholdCheckTypeThreshold) {
panic("Check type is not threshold")
}
// Cast check to threshold check
thresholdCheck := check.(*domain.ThresholdCheck)
fmt.Printf("Created threshold check with id %s\n", *thresholdCheck.Id)
}