-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathevents_output.go
194 lines (180 loc) · 5.88 KB
/
events_output.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package ldclient
// The types in this file are for analytics event data structures that we send to
// LaunchDarkly.
// Serializable form of a feature request event. This differs from the event that was
// passed in to us in that it usually has a user key instead of a user object.
type featureRequestEventOutput struct {
Kind string `json:"kind"`
CreationDate uint64 `json:"creationDate"`
Key string `json:"key"`
UserKey *string `json:"userKey,omitempty"`
User *serializableUser `json:"user,omitempty"`
Variation *int `json:"variation,omitempty"`
Value interface{} `json:"value"`
Default interface{} `json:"default"`
Version *int `json:"version,omitempty"`
PrereqOf *string `json:"prereqOf,omitempty"`
Reason EvaluationReason `json:"reason,omitempty"`
}
// Serializable form of an identify event.
type identifyEventOutput struct {
Kind string `json:"kind"`
CreationDate uint64 `json:"creationDate"`
Key *string `json:"key"`
User *serializableUser `json:"user"`
}
// Serializable form of a custom event. It has a user key instead of a user object.
type customEventOutput struct {
Kind string `json:"kind"`
CreationDate uint64 `json:"creationDate"`
Key string `json:"key"`
UserKey *string `json:"userKey,omitempty"`
User *serializableUser `json:"user,omitempty"`
Data interface{} `json:"data,omitempty"`
MetricValue *float64 `json:"metricValue,omitempty"`
}
// Serializable form of an index event. This is not generated by an explicit client call,
// but is created automatically whenever we see a user we haven't seen before in a feature
// request event or custom event.
type indexEventOutput struct {
Kind string `json:"kind"`
CreationDate uint64 `json:"creationDate"`
User *serializableUser `json:"user"`
}
// Serializable form of a summary event, containing data generated by EventSummarizer.
type summaryEventOutput struct {
Kind string `json:"kind"`
StartDate uint64 `json:"startDate"`
EndDate uint64 `json:"endDate"`
Features map[string]flagSummaryData `json:"features"`
}
type flagSummaryData struct {
Default interface{} `json:"default"`
Counters []flagCounterData `json:"counters"`
}
type flagCounterData struct {
Value interface{} `json:"value"`
Variation *int `json:"variation,omitempty"`
Version *int `json:"version,omitempty"`
Count int `json:"count"`
Unknown *bool `json:"unknown,omitempty"`
}
// Event types
const (
FeatureRequestEventKind = "feature"
FeatureDebugEventKind = "debug"
CustomEventKind = "custom"
IdentifyEventKind = "identify"
IndexEventKind = "index"
SummaryEventKind = "summary"
)
type eventOutputFormatter struct {
userFilter userFilter
inlineUsers bool
config Config
}
func (ef eventOutputFormatter) makeOutputEvents(events []Event, summary eventSummary) []interface{} {
out := make([]interface{}, 0, len(events)+1) // leave room for summary, if any
for _, e := range events {
oe := ef.makeOutputEvent(e)
if oe != nil {
out = append(out, oe)
}
}
if len(summary.counters) > 0 {
out = append(out, ef.makeSummaryEvent(summary))
}
return out
}
func (ef eventOutputFormatter) makeOutputEvent(evt interface{}) interface{} {
switch evt := evt.(type) {
case FeatureRequestEvent:
fe := featureRequestEventOutput{
CreationDate: evt.BaseEvent.CreationDate,
Key: evt.Key,
Variation: evt.Variation,
Value: evt.Value,
Default: evt.Default,
Version: evt.Version,
PrereqOf: evt.PrereqOf,
Reason: evt.Reason.Reason,
}
if ef.inlineUsers || evt.Debug {
fe.User = ef.userFilter.scrubUser(evt.User)
} else {
fe.UserKey = evt.User.Key
}
if evt.Debug {
fe.Kind = FeatureDebugEventKind
} else {
fe.Kind = FeatureRequestEventKind
}
return fe
case CustomEvent:
ce := customEventOutput{
Kind: CustomEventKind,
CreationDate: evt.BaseEvent.CreationDate,
Key: evt.Key,
Data: evt.Data,
MetricValue: evt.MetricValue,
}
if ef.inlineUsers {
ce.User = ef.userFilter.scrubUser(evt.User)
} else {
ce.UserKey = evt.User.Key
}
return ce
case IdentifyEvent:
return identifyEventOutput{
Kind: IdentifyEventKind,
CreationDate: evt.BaseEvent.CreationDate,
Key: evt.User.Key,
User: ef.userFilter.scrubUser(evt.User),
}
case IndexEvent:
return indexEventOutput{
Kind: IndexEventKind,
CreationDate: evt.BaseEvent.CreationDate,
User: ef.userFilter.scrubUser(evt.User),
}
default:
return nil
}
}
// Transforms the summary data into the format used for event sending.
func (ef eventOutputFormatter) makeSummaryEvent(snapshot eventSummary) summaryEventOutput {
features := make(map[string]flagSummaryData, len(snapshot.counters))
for key, value := range snapshot.counters {
var flagData flagSummaryData
var known bool
if flagData, known = features[key.key]; !known {
flagData = flagSummaryData{
Default: value.flagDefault,
Counters: make([]flagCounterData, 0, 2),
}
}
data := flagCounterData{
Value: value.flagValue,
Count: value.count,
}
if key.variation != nilVariation {
v := key.variation
data.Variation = &v
}
if key.version == 0 {
unknown := true
data.Unknown = &unknown
} else {
version := key.version
data.Version = &version
}
flagData.Counters = append(flagData.Counters, data)
features[key.key] = flagData
}
return summaryEventOutput{
Kind: SummaryEventKind,
StartDate: snapshot.startDate,
EndDate: snapshot.endDate,
Features: features,
}
}