-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
105 lines (94 loc) · 2.88 KB
/
events.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
package box
import (
"fmt"
"net/http"
"time"
)
type EventService struct {
*Client
}
type EventDetails struct {
ServiceID string `json:"service_id"`
EkmID string `json:"ekm_id"`
VersionID string `json:"version_id"`
ServiceName string `json:"service_name"`
}
type EventSource struct {
ItemID string `json:"item_id"`
ItemType string `json:"item_type"`
Parent *ItemParent `json:"parent"`
ItemName string `json:"item_name"`
}
type Event struct {
EventType string `json:"event_type"`
EventID string `json:"event_id"`
Type string `json:"type"`
CreatedAt string `json:"created_at"`
CreatedBy *User `json:"created_by"`
Source *EventSource `json:"source"`
SessionID interface{} `json:"session_id"`
AdditionalDetails *EventDetails `json:"additional_details"`
IPAddress string `json:"ip_address"`
}
type EventsCollection struct {
ChunkSize int `json:"chunk_size"`
NextStreamPosition string `json:"next_stream_position"`
Entries []*Event `json:"entries"`
}
type EventChanMessage struct {
Event *Event
Err error
}
func (e *EventService) getSeedStreamPos(startTime string) string {
var respBoxEventsJSON EventsCollection
req, err := http.NewRequest("GET", e.BaseUrl.String()+"/events?stream_type=admin_logs&limit=1&created_after="+startTime, nil)
req.Header.Add("Authorization", "Bearer "+e.Token)
_, err = e.DoWithRetries(req, &respBoxEventsJSON, 5)
if err != nil {
fmt.Println("this unmarshal")
fmt.Println(err)
}
return respBoxEventsJSON.NextStreamPosition
}
func (e *EventService) getEvents(eventLimit string, streamPos string) (*EventsCollection, error) {
var respBoxEventsJSON EventsCollection
req, err := http.NewRequest("GET", e.BaseUrl.String()+"/events?stream_type=admin_logs&limit="+eventLimit+"&stream_position="+streamPos, nil)
req.Header.Add("Authorization", "Bearer "+e.Token)
_, err = e.DoWithRetries(req, &respBoxEventsJSON, 5)
if err != nil {
return nil, err
}
return &respBoxEventsJSON, nil
}
func (e *EventService) streamEvents(eventLimit string, streamPos string, tunnel chan *EventChanMessage) {
for {
if streamPos == "" {
close(tunnel)
return
}
eventsCollection, err := e.getEvents(eventLimit, streamPos)
if err != nil {
message := &EventChanMessage{
nil,
err,
}
tunnel <- message
continue
}
for _, event := range eventsCollection.Entries {
message := &EventChanMessage{
event,
nil,
}
tunnel <- message
}
streamPos = eventsCollection.NextStreamPosition
time.Sleep(2 * time.Second)
}
}
func (e *EventService) Channel(eventLimit string, startTime string) chan *EventChanMessage {
streamPos := e.getSeedStreamPos(startTime)
eventStream := make(chan *EventChanMessage)
go e.streamEvents(eventLimit, streamPos, eventStream)
return eventStream
}