-
Notifications
You must be signed in to change notification settings - Fork 1
/
event.go
116 lines (96 loc) · 2.46 KB
/
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
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
package engine
import (
"encoding/json"
"errors"
"fmt"
"github.com/gorilla/mux"
"github.com/khyurri/speedlog/engine/mongo"
"github.com/khyurri/speedlog/utils"
"net/http"
"time"
)
const (
timeLayout = "2006-01-02T15:04:05"
)
// badRequest returns true if StatusErr is set
func badRequest(err error, r *Resp) bool {
if err != nil {
utils.Ok(err)
r.Status = StatusErr
return true
}
return false
}
func (env *Env) createEventHttp() http.HandlerFunc {
type request struct {
MetricName string `json:"metricName"`
DurationMs float64 `json:"durationMs"`
Project string `json:"project"`
}
mapRequestToStruct := func(r *http.Request, target *request) (err error) {
dec := json.NewDecoder(r.Body)
err = dec.Decode(target)
if err != nil {
utils.Ok(fmt.Errorf(err.Error()+". Body: %+v", r.Body))
return
}
utils.Debug(fmt.Sprintf("metricName: %s, durationMs: %f\n",
target.MetricName, target.DurationMs))
if len(target.MetricName) == 0 {
return errors.New("empty metricName")
}
return
}
return func(w http.ResponseWriter, r *http.Request) {
response := &Resp{}
defer response.Render(w)
req := &request{}
err := mapRequestToStruct(r, req)
if err != nil {
utils.Ok(err)
response.Status = StatusIntErr
return
}
err = env.DBEngine.SaveEvent(req.MetricName, req.Project, req.DurationMs)
if err != nil {
utils.Ok(err)
response.Status = StatusIntErr
return
}
utils.Debug(fmt.Sprintf("requested params: %s", r.Body))
saved := struct {
Saved bool `json:"saved"`
}{true}
response.Status = StatusOk
response.JsonBody, err = json.Marshal(saved)
utils.Ok(err)
}
}
func (env *Env) getEventsHttp() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
response := &Resp{}
defer response.Render(w)
// see request validation in env.ExportEventRoutes
params := mux.Vars(r)
metricTimeFrom, err := time.Parse(timeLayout, params["metricTimeFrom"])
if badRequest(err, response) {
return
}
metricTimeTo, err := time.Parse(timeLayout, params["metricTimeTo"])
if badRequest(err, response) {
return
}
utils.Debug(fmt.Sprintf("%s -> %s", metricTimeFrom, metricTimeTo))
events, err := env.DBEngine.FilterEvents(
metricTimeFrom,
metricTimeTo,
params["metricName"],
params["project"])
grouped, err := mongo.GroupBy(params["groupBy"], events)
if len(grouped) == 0 {
response.Status = StatusOk
return
}
response.JsonBody, err = json.Marshal(grouped)
}
}