forked from grafana-tools/sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rest-annotation.go
162 lines (141 loc) · 4.48 KB
/
rest-annotation.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
153
154
155
156
157
158
159
160
161
162
package sdk
import (
"context"
"encoding/json"
"fmt"
"net/url"
"strconv"
"time"
"github.com/pkg/errors"
)
// https://grafana.com/docs/grafana/latest/http_api/annotations/
// CreateAnnotation creates a new annotation from the annotation request
func (r *Client) CreateAnnotation(ctx context.Context, a CreateAnnotationRequest) (StatusMessage, error) {
var (
raw []byte
resp StatusMessage
err error
)
if raw, err = json.Marshal(a); err != nil {
return StatusMessage{}, errors.Wrap(err, "marshal request")
}
if raw, _, err = r.post(ctx, "api/annotations", nil, raw); err != nil {
return StatusMessage{}, errors.Wrap(err, "create annotation")
}
if err = json.Unmarshal(raw, &resp); err != nil {
return StatusMessage{}, errors.Wrap(err, "unmarshal response message")
}
return resp, nil
}
// PatchAnnotation patches the annotation with id with the request
func (r *Client) PatchAnnotation(ctx context.Context, id uint, a PatchAnnotationRequest) (StatusMessage, error) {
var (
raw []byte
resp StatusMessage
err error
)
if raw, err = json.Marshal(a); err != nil {
return StatusMessage{}, errors.Wrap(err, "marshal request")
}
if raw, _, err = r.patch(ctx, fmt.Sprintf("api/annotations/%d", id), nil, raw); err != nil {
return StatusMessage{}, errors.Wrap(err, "patch annotation")
}
if err = json.Unmarshal(raw, &resp); err != nil {
return StatusMessage{}, errors.Wrap(err, "unmarshal response message")
}
return resp, nil
}
// GetAnnotations gets annotations matching the annotation parameters
func (r *Client) GetAnnotations(ctx context.Context, params ...GetAnnotationsParams) ([]AnnotationResponse, error) {
var (
raw []byte
err error
resp []AnnotationResponse
requestParams = make(url.Values)
)
for _, p := range params {
p(requestParams)
}
if raw, _, err = r.get(ctx, "api/annotations", requestParams); err != nil {
return nil, errors.Wrap(err, "get annotations")
}
if err = json.Unmarshal(raw, &resp); err != nil {
return nil, errors.Wrap(err, "unmarshal response message")
}
return resp, nil
}
// DeleteAnnotation deletes the annotation with id
func (r *Client) DeleteAnnotation(ctx context.Context, id uint) (StatusMessage, error) {
var (
raw []byte
err error
resp StatusMessage
)
if raw, _, err = r.delete(ctx, fmt.Sprintf("api/annotations/%d", id)); err != nil {
return StatusMessage{}, errors.Wrap(err, "delete annotation")
}
if err = json.Unmarshal(raw, &resp); err != nil {
return StatusMessage{}, errors.Wrap(err, "unmarshal response message")
}
return resp, nil
}
// GetAnnotationsParams is the type for all options implementing query parameters
// https://grafana.com/docs/grafana/latest/http_api/annotations/#find-annotations
type GetAnnotationsParams func(values url.Values)
// WithTag adds the tag to the
func WithTag(tag string) GetAnnotationsParams {
return func(v url.Values) {
v.Add("tags", tag)
}
}
// WithLimit sets the max number of alerts to return
func WithLimit(limit uint) GetAnnotationsParams {
return func(v url.Values) {
v.Set("limit", strconv.FormatUint(uint64(limit), 10))
}
}
// WithAnnotationType filters the type to annotations
func WithAnnotationType() GetAnnotationsParams {
return func(v url.Values) {
v.Set("type", "annotation")
}
}
// WithAlertType filters the type to alerts
func WithAlertType() GetAnnotationsParams {
return func(v url.Values) {
v.Set("type", "alert")
}
}
// WithDashboard filters the response to the specified dashboard ID
func WithDashboard(id uint) GetAnnotationsParams {
return func(v url.Values) {
v.Set("dashboardId", strconv.FormatUint(uint64(id), 10))
}
}
// WithPanel filters the response to the specified panel ID
func WithPanel(id uint) GetAnnotationsParams {
return func(v url.Values) {
v.Set("panelId", strconv.FormatUint(uint64(id), 10))
}
}
// WithUser filters the annotations to only be made by the specified user ID
func WithUser(id uint) GetAnnotationsParams {
return func(v url.Values) {
v.Set("userId", strconv.FormatUint(uint64(id), 10))
}
}
// WithStartTime filters the annotations to after the specified time
func WithStartTime(t time.Time) GetAnnotationsParams {
return func(v url.Values) {
v.Set("from", strconv.FormatInt(toMilliseconds(t), 10))
}
}
// WithEndTime filters the annotations to before the specified time
func WithEndTime(t time.Time) GetAnnotationsParams {
return func(v url.Values) {
v.Set("to", strconv.FormatInt(toMilliseconds(t), 10))
}
}
func toMilliseconds(t time.Time) int64 {
return t.UnixNano() / 1000000
}