Skip to content

Commit 6b6096c

Browse files
authored
Merge pull request #89 from kompotkot/go-bulk-push
Added bulk report for humbug events with chunks workflow
2 parents 69fc5a6 + 3f3eaa1 commit 6b6096c

1 file changed

Lines changed: 59 additions & 15 deletions

File tree

go/pkg/reporter.go

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,26 @@ import (
88
)
99

1010
type reportRequest struct {
11-
Title string `json:"title"`
12-
Content string `json:"content"`
13-
Tags []string `json:"tags"`
11+
Title string `json:"title"`
12+
Content string `json:"content"`
13+
Tags []string `json:"tags"`
1414
}
1515

1616
type Reporter interface {
1717
Tag(key string, modifier string)
1818
Untag(key string, modifier string)
1919
Tags() []string
2020
Publish(report Report) error
21+
PublishBulk(report Report) error
2122
}
2223

2324
type HumbugReporter struct {
24-
BaseURL string
25-
clientID string
26-
sessionID string
27-
consent Consent
28-
reporterToken string
29-
tags map[string]bool
25+
BaseURL string
26+
clientID string
27+
sessionID string
28+
consent Consent
29+
reporterToken string
30+
tags map[string]bool
3031
}
3132

3233
func (reporter *HumbugReporter) Tag(key, modifier string) {
@@ -106,21 +107,64 @@ func (reporter *HumbugReporter) Publish(report Report) {
106107
request.Header.Add("Content-Type", "application/json")
107108
request.Header.Add("Accept", "application/json")
108109
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", reporter.reporterToken))
109-
110+
110111
client.Do(request)
111112

112113
}
113114
}
115+
116+
func (reporter *HumbugReporter) PublishBulk(reports []Report) {
117+
defer func() {
118+
// TODO(zomglings): Same as for Publish
119+
recover()
120+
}()
121+
122+
userHasConsented := reporter.consent.Check()
123+
124+
if userHasConsented {
125+
bulkEntriesRoute := fmt.Sprintf("%s/humbug/reports/bulk", reporter.BaseURL)
126+
127+
var reportsRequestBody []reportRequest
128+
for _, report := range reports {
129+
tags := MergeTags(report.Tags, reporter.Tags())
130+
requestBody := reportRequest{
131+
Title: report.Title,
132+
Content: report.Content,
133+
Tags: tags,
134+
}
135+
reportsRequestBody = append(reportsRequestBody, requestBody)
136+
}
137+
138+
requestBuffer := new(bytes.Buffer)
139+
json.NewEncoder(requestBuffer).Encode(reportsRequestBody)
140+
141+
request, _ := http.NewRequest("POST", bulkEntriesRoute, requestBuffer)
142+
143+
client := &http.Client{}
144+
request.Header.Add("Content-Type", "application/json")
145+
request.Header.Add("Accept", "application/json")
146+
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", reporter.reporterToken))
147+
148+
query := request.URL.Query()
149+
query.Add("sync", "true")
150+
request.URL.RawQuery = query.Encode()
151+
152+
client.Do(request)
153+
154+
}
155+
156+
}
157+
114158
func (reporter *HumbugReporter) SetBaseURL(baseURL string) {
115-
reporter.BaseURL = baseURL
159+
reporter.BaseURL = baseURL
116160
}
117161

118162
func CreateHumbugReporter(consent Consent, clientID string, sessionID string, reporterToken string) (*HumbugReporter, error) {
119163
reporter := HumbugReporter{
120-
consent: consent,
121-
clientID: clientID,
122-
sessionID: sessionID,
123-
reporterToken: reporterToken,
164+
consent: consent,
165+
clientID: clientID,
166+
sessionID: sessionID,
167+
reporterToken: reporterToken,
124168
}
125169
reporter.Tag("session", reporter.sessionID)
126170
if reporter.BaseURL == "" {

0 commit comments

Comments
 (0)