-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathariel_search_scroller.go
121 lines (98 loc) · 2.77 KB
/
ariel_search_scroller.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
package qradar
import (
"context"
"fmt"
"net/http"
)
// SearchResultsWindow is a default window for scrolling results of the query.
var SearchResultsWindow = 50
// SearchResultsScroller represents a scroller for the results of the query.
type SearchResultsScroller struct {
count int
client *Client
searchID string
startIdx, currIdx int
window int
events []Event
}
// NewSearchResultsScroller initializes struct to scroll the records.
func (a *ArielService) NewSearchResultsScroller(ctx context.Context, searchID string) (*SearchResultsScroller, error) {
_, num, err := a.SearchStatus(ctx, searchID)
if err != nil {
return nil, err
}
srs := &SearchResultsScroller{
count: num,
window: SearchResultsWindow,
client: a.client,
searchID: searchID,
}
err = srs.getEvents(ctx)
if err != nil {
return nil, err
}
return srs, nil
}
// Next returns true if an event is still available to be consumed by the
// Result() method.
func (s *SearchResultsScroller) Next(ctx context.Context) bool {
lastChunkOfData := len(s.events) < s.window+1 // because qradar usually returns s.window+1 events
lastEventIsRead := s.currIdx-s.startIdx == len(s.events)
if lastChunkOfData && lastEventIsRead {
return false
}
endOfChunk := s.currIdx-s.startIdx == len(s.events)
if endOfChunk {
s.startIdx += s.window
err := s.getEvents(ctx)
if err != nil {
return false
}
}
return true
}
func (s *SearchResultsScroller) getEvents(ctx context.Context) error {
req, err := s.client.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s/results", arielSearchAPIPrefix, s.searchID), nil)
if err != nil {
return err
}
req.Header.Add("Range", fmt.Sprintf("items=%d-%d", s.startIdx, s.startIdx+s.window))
var r SearchResult
_, err = s.client.Do(ctx, req, &r)
if err != nil {
return err
}
s.events = r.Events
return nil
}
// Result returns the event iterated by the Next.
func (s *SearchResultsScroller) Result() Event {
event := s.events[s.currIdx-s.startIdx]
s.currIdx++
return event
}
// Length returns the overall events count.
func (s *SearchResultsScroller) Length() int {
return s.count
}
// ScrollByQuery events in the QRadar API.
// Recommended way to retrieve large amount of events.
func (a *ArielService) ScrollByQuery(ctx context.Context, sqlQuery string) (*SearchResultsScroller, *SearchMetadata, error) {
s, err := a.SearchByQuery(ctx, sqlQuery)
if err != nil {
return nil, nil, err
}
_, err = a.WaitForSearchID(ctx, *s.SearchID, StatusCompleted, 2)
if err != nil {
return nil, nil, err
}
meta, err := a.SearchMetadata(ctx, *s.SearchID)
if err != nil {
return nil, nil, err
}
srs, err := a.NewSearchResultsScroller(ctx, *s.SearchID)
if err != nil {
return nil, meta, err
}
return srs, meta, nil
}