-
Notifications
You must be signed in to change notification settings - Fork 0
/
importer.go
284 lines (243 loc) · 7.48 KB
/
importer.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package appmetrica
import (
"errors"
"io"
"strconv"
"strings"
)
const requiredCols = `application_id,event_name,event_timestamp`
var allowedHeader map[string]struct{}
func init() {
var header = []string{
"app_package_name", "app_version_name", "connection_type",
"device_ipv6", "device_locale", "device_manufacturer", "device_model",
"device_type", "event_json", "google_aid", "ios_ifa", "ios_ifv", "mcc",
"mnc", "operator_name", "os_name", "os_version", "session_type",
"windows_aid",
}
allowedHeader = make(map[string]struct{}, len(header))
for _, label := range header {
allowedHeader[label] = struct{}{}
}
}
// EventIdentifierType encodes identifier which could be used in event import.
type EventIdentifierType int
const (
DeviceID EventIdentifierType = iota
ProfileID
)
// EventImporterState codes inner state of reader in event importer.
type EventImporterState int
const (
Initial EventImporterState = iota
HeaderWriting
EventWriting
EndOfEvents
)
// EventImporter
type EventImporter struct {
err error
state EventImporterState
kind EventIdentifierType
offset int
buffer []byte // line buffer
header []string
events []*ImportEvent
}
// NewEventImporter creates new instance of EventImporter. Developer has to
// specify type of user identifer which is either DeviceID or ProfileID and
// optional list of attribution columns. The list of mandatory columns should
// not to be specified since EventImporter adds them automatically. Mandatory
// columns are ApplicationID, either ProfileID or DeviceId, EventName, and
// EventTimestamp.
func NewEventImporter(kind EventIdentifierType, header ...string) *EventImporter {
var importer = new(EventImporter)
importer.state = Initial
importer.buffer = make([]byte, 0, 4096) // 4kb
importer.SetEventIdentifierType(kind)
importer.SetHeader(header...)
return importer
}
func (e *EventImporter) Import(events ...*ImportEvent) {
if e.state != EndOfEvents {
e.events = append(e.events, events...)
} else {
var msg = "failed to add event(s) to completed importer"
e.err = errors.New(prefix + msg)
}
}
func (e *EventImporter) ImportOne(event *ImportEvent) {
e.Import(event)
}
func (e *EventImporter) ImportMany(events []*ImportEvent) {
e.Import(events...)
}
func (e *EventImporter) Reset() {
e.err = nil
e.state = Initial
e.offset = 0
e.buffer = e.buffer[:0]
e.header = e.header[:0]
e.events = e.events[:0]
}
func (e *EventImporter) SetEventIdentifierType(kind EventIdentifierType) {
if e.state == Initial {
e.kind = kind
} else {
var msg = "failed to set identifier type during reading"
e.err = errors.New(prefix + msg)
}
}
func (e *EventImporter) SetHeader(header ...string) {
if e.state != Initial {
var msg = "failed to set identifier type during reading"
e.err = errors.New(prefix + msg)
return
}
e.header = make([]string, 0, len(header))
for _, col := range header {
if _, ok := allowedHeader[col]; ok {
e.header = append(e.header, col)
}
}
}
func (e *EventImporter) Read(buffer []byte) (int, error) {
// Short circut for EOF state.
if e.state == EndOfEvents {
return 0, io.EOF
}
// If there is nothing to flush or no events to read then EOF.
if len(e.buffer) == 0 && len(e.events) == 0 {
e.state = EndOfEvents
return 0, io.EOF
}
switch e.state {
case Initial:
e.state = HeaderWriting
fallthrough
case HeaderWriting:
return e.readHeader(buffer)
case EventWriting:
return e.readEvents(buffer)
default:
panic(prefix + "unexpected execution branch")
}
}
func (e *EventImporter) encodeEvent(event *ImportEvent) {
// Encode identifier device identifier.
if e.kind == DeviceID {
e.buffer = strconv.AppendUint(e.buffer, event.DeviceID, 10)
} else {
e.buffer = append(e.buffer, event.ProfileID...)
}
e.encodeRequiredColumns(event)
e.encodeOptionalColumns(event)
e.buffer = append(e.buffer, "\n"...)
}
func (e *EventImporter) encodeOptionalColumns(event *ImportEvent) {
for _, label := range e.header {
// Append column delimiter.
e.buffer = append(e.buffer, ","...)
// Append column value.
switch label {
case "app_package_name":
e.buffer = append(e.buffer, event.AppPackageName...)
case "app_version_name":
e.buffer = append(e.buffer, event.AppVersionName...)
case "connection_type":
e.buffer = append(e.buffer, event.ConnectionType...)
case "device_ipv6":
e.buffer = append(e.buffer, event.DeviceIPv6...)
case "device_locale":
e.buffer = append(e.buffer, event.DeviceLocale...)
case "device_manufacturer":
e.buffer = append(e.buffer, event.DeviceManufacturer...)
case "device_model":
e.buffer = append(e.buffer, event.DeviceModel...)
case "device_type":
e.buffer = append(e.buffer, event.DeviceType...)
case "event_json":
println(prefix + "field `event_json` in event is not supported")
case "google_aid":
e.buffer = append(e.buffer, event.GoogleAID...)
case "ios_ifa":
e.buffer = append(e.buffer, event.IFA...)
case "ios_ifv":
e.buffer = append(e.buffer, event.IFV...)
case "mcc":
e.buffer = strconv.AppendInt(e.buffer, int64(event.MCC), 10)
case "mnc":
e.buffer = strconv.AppendInt(e.buffer, int64(event.MNC), 10)
case "operator_name":
e.buffer = append(e.buffer, event.OperatorName...)
case "os_name":
e.buffer = append(e.buffer, event.OSName...)
case "os_version":
e.buffer = append(e.buffer, event.OSVersion...)
case "session_type":
e.buffer = append(e.buffer, event.SessionType...)
case "windows_aid":
e.buffer = append(e.buffer, event.WindowsAID...)
default:
panic(prefix + "unexpected execution branch")
}
}
}
func (e *EventImporter) encodeRequiredColumns(event *ImportEvent) {
e.buffer = append(e.buffer, ","...)
e.buffer = strconv.AppendInt(e.buffer, int64(event.ApplicationID), 10)
e.buffer = append(e.buffer, ","...)
e.buffer = append(e.buffer, event.EventName...)
e.buffer = append(e.buffer, ","...)
e.buffer = strconv.AppendInt(e.buffer, event.EventTimestamp, 10)
}
func (e *EventImporter) readHeader(buffer []byte) (int, error) {
// Initialize buffer with header line.
if len(e.buffer) == 0 {
if e.kind == DeviceID {
e.buffer = append(e.buffer, `appmetrica_device_id,`...)
} else {
e.buffer = append(e.buffer, `profile_id,`...)
}
e.buffer = append(e.buffer, requiredCols...)
if len(e.header) > 0 {
e.buffer = append(e.buffer, ","...)
e.buffer = append(e.buffer, strings.Join(e.header, ",")...)
}
e.buffer = append(e.buffer, "\n"...)
}
// Write rest of header line to output buffer.
var read = copy(buffer, e.buffer[e.offset:])
e.offset += read
// If we wrote all header line then change state.
if e.offset == len(e.buffer) {
e.offset = 0
e.buffer = e.buffer[:e.offset]
e.state = EventWriting
}
// There are no any errors but we force similarity to Reader interface.
return read, nil
}
func (e *EventImporter) readEvents(buffer []byte) (int, error) {
// Initialize buffer with import event line.
if len(e.buffer) == 0 {
var last = len(e.events) - 1 // Index of last event.
var event = e.events[last] // Get last element.
e.events = e.events[:last] // Remove last element.
e.encodeEvent(event)
}
// Write rest of header line to output buffer.
var read = copy(buffer, e.buffer[e.offset:])
e.offset += read
// If we wrote all header line then change state.
if e.offset == len(e.buffer) {
e.offset = 0
e.buffer = e.buffer[:e.offset]
}
// If there is no events by now then transit to termination state.
if len(e.events) == 0 {
e.state = EndOfEvents
}
// There are no any errors but we force similarity to Reader interface.
return read, nil
}