-
Notifications
You must be signed in to change notification settings - Fork 7
/
annotations.go
215 lines (175 loc) · 5.62 KB
/
annotations.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package appoptics
import (
"fmt"
"net/http"
"strconv"
"time"
)
// AnnotationStream is a group of AnnotationEvents with a common name, representing a timeseries of occurrences of similar events
type AnnotationStream struct {
Name string `json:"name"`
DisplayName string `json:"display_name,omitempty"`
Events []map[string][]AnnotationEvent `json:"events,omitempty"` // keys are Source names
}
// AnnotationEvent is the main data structure for the Annotations API
type AnnotationEvent struct {
ID int `json:"id"`
Title string `json:"title"`
Source string `json:"source,omitempty"`
Description string `json:"description,omitempty"`
Links []AnnotationLink `json:"links,omitempty"`
StartTime int64 `json:"start_time,omitempty"`
EndTime int64 `json:"end_time,omitempty"`
}
// AnnotationLink represents the Link metadata for on the AnnotationEvent
type AnnotationLink struct {
Rel string `json:"rel"`
Href string `json:"href"`
Label string `json:"label,omitempty"`
}
type RetrieveAnnotationsRequest struct {
Name string
StartTime time.Time
EndTime time.Time
Sources []string
}
type ListAnnotationsResponse struct {
AnnotationStreams []*AnnotationStream `json:"annotations"`
Query QueryInfo `json:"query"`
}
// AnnotationsCommunicator provides an interface to the Annotations API from AppOptics
type AnnotationsCommunicator interface {
List(*string) (*ListAnnotationsResponse, error)
Retrieve(*RetrieveAnnotationsRequest) (*AnnotationStream, error)
RetrieveEvent(string, int) (*AnnotationEvent, error)
Create(*AnnotationEvent, string) (*AnnotationEvent, error)
UpdateStream(string, string) error
UpdateEvent(string, int, *AnnotationLink) (*AnnotationLink, error)
Delete(string) error
}
type AnnotationsService struct {
client *Client
}
func NewAnnotationsService(c *Client) *AnnotationsService {
return &AnnotationsService{c}
}
// List retrieves paginated AnnotationEvents for all streams with name LIKE argument string
func (as *AnnotationsService) List(streamNameSearch *string) (*ListAnnotationsResponse, error) {
var (
path string
annotations *ListAnnotationsResponse
)
if streamNameSearch != nil {
path = fmt.Sprintf("annotations?name=%s", *streamNameSearch)
} else {
path = "annotations"
}
req, err := as.client.NewRequest("GET", path, nil)
if err != nil {
return nil, err
}
_, err = as.client.Do(req, &annotations)
if err != nil {
return nil, err
}
return annotations, nil
}
// Retrieve fetches all AnnotationEvents matching the provided sources
func (as *AnnotationsService) Retrieve(retReq *RetrieveAnnotationsRequest) (*AnnotationStream, error) {
stream := &AnnotationStream{}
path := fmt.Sprintf("annotations/%s", retReq.Name)
req, err := as.client.NewRequest("GET", path, nil)
if err != nil {
return nil, err
}
req.URL.RawQuery = retReq.queryString(req)
_, err = as.client.Do(req, stream)
if err != nil {
return nil, err
}
return stream, nil
}
func (retReq *RetrieveAnnotationsRequest) queryString(req *http.Request) string {
q := req.URL.Query()
if !retReq.StartTime.IsZero() {
q.Add("start_time", strconv.FormatInt(retReq.StartTime.Unix(), 10))
}
if !retReq.EndTime.IsZero() {
q.Add("end_time", strconv.FormatInt(retReq.EndTime.Unix(), 10))
}
if len(retReq.Sources) > 0 {
for _, source := range retReq.Sources {
q.Add("sources[]", source)
}
}
return q.Encode()
}
// RetrieveEvent returns a single event identified by an integer ID from a given stream
func (as *AnnotationsService) RetrieveEvent(streamName string, id int) (*AnnotationEvent, error) {
event := &AnnotationEvent{}
path := fmt.Sprintf("annotations/%s/%d", streamName, id)
req, err := as.client.NewRequest("GET", path, nil)
if err != nil {
return nil, err
}
_, err = as.client.Do(req, event)
if err != nil {
return nil, err
}
return event, nil
}
// Create makes an AnnotationEvent on the stream with the given name
func (as *AnnotationsService) Create(event *AnnotationEvent, streamName string) (*AnnotationEvent, error) {
path := fmt.Sprintf("annotations/%s", streamName)
req, err := as.client.NewRequest("POST", path, event)
if err != nil {
return nil, err
}
createdEvent := &AnnotationEvent{}
_, err = as.client.Do(req, createdEvent)
if err != nil {
return nil, err
}
return createdEvent, nil
}
// UpdateStream updates the display name of the stream
func (as *AnnotationsService) UpdateStream(streamName, displayName string) error {
path := fmt.Sprintf("annotations/%s", streamName)
jsonTemplate := `{"display_name": %s}`
req, err := as.client.NewRequest("POST", path, fmt.Sprintf(jsonTemplate, displayName))
if err != nil {
return err
}
_, err = as.client.Do(req, nil)
if err != nil {
return err
}
return nil
}
// UpdateEvent adds a link to an annotation Event
func (as *AnnotationsService) UpdateEvent(streamName string, id int, link *AnnotationLink) (*AnnotationLink, error) {
newLink := &AnnotationLink{}
path := fmt.Sprintf("annotations/%s/%d/links", streamName, id)
req, err := as.client.NewRequest("POST", path, link)
if err != nil {
return nil, err
}
_, err = as.client.Do(req, newLink)
if err != nil {
return nil, err
}
return newLink, nil
}
// Delete deletes the annotation stream matching the provided name
func (as *AnnotationsService) Delete(streamName string) error {
path := fmt.Sprintf("annotations/%s", streamName)
req, err := as.client.NewRequest("DELETE", path, nil)
if err != nil {
return err
}
_, err = as.client.Do(req, nil)
if err != nil {
return err
}
return nil
}