-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
143 lines (120 loc) · 3.59 KB
/
log.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
package lrq
import (
"context"
"encoding/json"
"time"
)
type LogRequestAttribs struct {
StartTime *time.Time
EndTime *time.Time
Filter *string
Limit *uint
}
type logRequest struct {
QueryType string `json:"queryType"`
StartTime *int64 `json:"startTime,omitempty"`
EndTime *int64 `json:"endTime,omitempty"`
Log logRequestOpts `json:"log"`
}
type logRequestOpts struct {
Filter *string `json:"filter,omitempty"`
Limit *uint `json:"limit,omitempty"`
Cursor *string `json:"cursor,omitempty"`
Ascending *bool `json:"ascending,omitempty"`
}
type logResponseMatch struct {
LogResponseMatch
Cursor string `json:"cursor"`
}
type LogResponseMatch struct {
ServerInfo map[string]interface{} `json:"serverInfo"`
SessionId string `json:"sessionId"`
Severity int `json:"severity"`
ThreadId string `json:"threadId"`
Timestamp int64 `json:"timestamp"`
Values map[string]interface{} `json:"values"`
}
func (c *Client) DoLogRequest(ctx context.Context, attribs LogRequestAttribs) ([]LogResponseMatch, error) {
reqBody := logRequest{
QueryType: "LOG",
StartTime: timeToInt64(attribs.StartTime),
EndTime: timeToInt64(attribs.EndTime),
Log: logRequestOpts{
Filter: attribs.Filter,
Limit: attribs.Limit,
},
}
reqBytes, err := json.Marshal(reqBody)
if err != nil {
return nil, err
}
respBytes, err := c.doRequest(ctx, reqBytes)
if err != nil {
return nil, err
}
var respBody struct {
Matches []LogResponseMatch `json:"matches"`
}
if err := json.Unmarshal(respBytes, &respBody); err != nil {
return nil, err
}
return respBody.Matches, nil
}
func (c *Client) DoLogRequestPaginated(ctx context.Context, attribs LogRequestAttribs, cursor *string) ([]LogResponseMatch, *string, error) {
reqBody := logRequest{
QueryType: "LOG",
StartTime: timeToInt64(attribs.StartTime),
EndTime: timeToInt64(attribs.EndTime),
Log: logRequestOpts{
Filter: attribs.Filter,
Limit: attribs.Limit,
Cursor: cursor,
// This is needed for cursor matching later
Ascending: func() *bool { b := true; return &b }(),
},
}
reqBytes, err := json.Marshal(reqBody)
if err != nil {
return nil, nil, err
}
respBytes, err := c.doRequest(ctx, reqBytes)
if err != nil {
return nil, nil, err
}
var respBody struct {
Matches []logResponseMatch `json:"matches"`
}
if err := json.Unmarshal(respBytes, &respBody); err != nil {
return nil, nil, err
}
// The cursor represents an identifier associated with a specific event.
// So specifying a cursor means the matches should start with that event,
// which here is the last event of the previous paginated results.
if cursor == nil {
matches := make([]LogResponseMatch, len(respBody.Matches))
for i := range respBody.Matches {
matches[i] = respBody.Matches[i].LogResponseMatch
}
lastCursor := respBody.Matches[len(respBody.Matches)-1].Cursor
return matches, &lastCursor, nil
} else {
if len(respBody.Matches) == 0 {
return []LogResponseMatch{}, nil, nil
}
var matches []LogResponseMatch
firstCursor := respBody.Matches[0].Cursor
if firstCursor == *cursor {
matches = make([]LogResponseMatch, len(respBody.Matches)-1)
for i := range respBody.Matches[1:] {
matches[i] = respBody.Matches[i+1].LogResponseMatch
}
} else {
matches = make([]LogResponseMatch, len(respBody.Matches))
for i := range respBody.Matches {
matches[i] = respBody.Matches[i].LogResponseMatch
}
}
lastCursor := respBody.Matches[len(respBody.Matches)-1].Cursor
return matches, &lastCursor, nil
}
}