-
Notifications
You must be signed in to change notification settings - Fork 2
/
send_metric_event.go
45 lines (38 loc) · 1.17 KB
/
send_metric_event.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
package backend
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
type sendMetricEventRequest struct {
UserData UserData `json:"user_data"`
EventType string `json:"event_type"`
EventData map[string]interface{} `json:"event_data"`
}
type sendMetricEventResponse struct {
Success bool `json:"success"`
}
func (s *APIServer) sendMetricEvent(w http.ResponseWriter, r *http.Request) {
bodyBytes, e := ioutil.ReadAll(r.Body)
if e != nil {
s.writeErrorJson(w, fmt.Sprintf("error reading request input: %s", e))
return
}
log.Printf("sendMetricEvent requestJson: %s\n", string(bodyBytes))
var req sendMetricEventRequest
e = json.Unmarshal(bodyBytes, &req)
if e != nil {
s.writeErrorJson(w, fmt.Sprintf("error unmarshaling json: %s; bodyString = %s", e, string(bodyBytes)))
return
}
// TODO DS Properly extract and compute time for SendEvent
e = s.metricsTracker.SendEventForGuiUser(req.UserData.ID, req.EventType, req.EventData, time.Now())
if e != nil {
s.writeErrorJson(w, fmt.Sprintf("error sending gui event %s: %s", string(bodyBytes), e))
return
}
s.writeJson(w, sendMetricEventResponse{Success: true})
}