-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
161 lines (131 loc) · 2.53 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package ga
import (
"io"
"net/url"
"sort"
"time"
)
// Event represents a single Google Analytics event.
// It is unaware of the exact GA api but knows how to format data for GA.
type Event map[string]string
// Set inserts a key value pair into Event.
func (e Event) Set(key, value string) {
e[key] = value
}
// Get retrieves the value for key or an empty string.
func (e Event) Get(key string) string {
if v, ok := e[key]; ok {
return v
}
return ""
}
// Del removes a key value pair from Event.
func (e Event) Del(key string) {
delete(e, key)
}
func (e Event) sortedKeyValues() pairs {
l := make([]pair, 0, len(e))
for k, v := range e {
if k != "" && v != "" {
l = append(l, pair{key: k, value: v})
}
}
sort.Sort(pairs(l))
return l
}
// WriteTo formats the key value pairs in Event and writes them to w.
func (e Event) WriteTo(w io.Writer) (int64, error) {
ws, ok := w.(writeStringer)
if !ok {
ws = stringWriter{w}
}
var (
err error
n int
sumN int64
)
s := e.sortedKeyValues()
for _, p := range s {
var ps string
if sumN == 0 {
ps = p.key + "=" + url.QueryEscape(p.value)
} else {
ps = "&" + p.key + "=" + url.QueryEscape(p.value)
}
n, err = ws.WriteString(ps)
sumN += int64(n)
if err != nil {
return sumN, err
}
}
return sumN, err
}
type event struct {
reportedAt time.Time
e Event
}
type events []event
func (l events) cleanEvents() []Event {
x := make([]Event, len(l), len(l))
for _, e := range l {
x = append(x, e.e)
}
return x
}
// WriteTo formats a batch of Events and writes them to w.
func (l events) WriteTo(w io.Writer) (int64, error) {
ws, ok := w.(writeStringer)
if !ok {
ws = stringWriter{w}
}
var (
err error
sumN int64
firstWritten bool
)
for _, e := range l {
if len(e.e) == 0 {
continue
}
if firstWritten {
var n int
n, err = ws.WriteString("\n")
sumN += int64(n)
if err != nil {
return sumN, err
}
}
var n int64
n, err = e.e.WriteTo(ws)
sumN += n
if err != nil {
return sumN, err
}
firstWritten = true
}
return sumN, err
}
type writeStringer interface {
io.Writer
WriteString(string) (int, error)
}
type stringWriter struct {
io.Writer
}
func (w stringWriter) WriteString(s string) (n int, err error) {
return w.Writer.Write([]byte(s))
}
type pair struct {
key string
value string
}
type pairs []pair
func (p pairs) Len() int {
return len(p)
}
func (p pairs) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p pairs) Less(i, j int) bool {
return p[i].key < p[j].key
}