-
Notifications
You must be signed in to change notification settings - Fork 12
/
Event.scala
433 lines (408 loc) · 13.2 KB
/
Event.scala
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* Copyright (c) 2016-2020 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.snowplowanalytics.snowplow.analytics.scalasdk
// java
import java.time.Instant
import java.util.UUID
import java.time.format.DateTimeFormatter
import java.nio.ByteBuffer
// circe
import io.circe.{Decoder, Encoder, Json, JsonObject}
import io.circe.Json.JString
import io.circe.generic.semiauto._
import io.circe.syntax._
// iglu
import com.snowplowanalytics.iglu.core.SelfDescribingData
import com.snowplowanalytics.iglu.core.circe.implicits._
// This library
import com.snowplowanalytics.snowplow.analytics.scalasdk.decode.{DecodeResult, Key, Parser, TSVParser}
import com.snowplowanalytics.snowplow.analytics.scalasdk.SnowplowEvent.{Contexts, UnstructEvent}
import com.snowplowanalytics.snowplow.analytics.scalasdk.SnowplowEvent._
import com.snowplowanalytics.snowplow.analytics.scalasdk.validate.FIELD_SIZES
import com.snowplowanalytics.snowplow.analytics.scalasdk.encode.TsvEncoder
/**
* Case class representing a canonical Snowplow event.
*
* @see https://docs.snowplowanalytics.com/docs/understanding-your-pipeline/canonical-event/
*/
// format: off
case class Event(
app_id: Option[String],
platform: Option[String],
etl_tstamp: Option[Instant],
collector_tstamp: Instant,
dvce_created_tstamp: Option[Instant],
event: Option[String],
event_id: UUID,
txn_id: Option[Int],
name_tracker: Option[String],
v_tracker: Option[String],
v_collector: String,
v_etl: String,
user_id: Option[String],
user_ipaddress: Option[String],
user_fingerprint: Option[String],
domain_userid: Option[String],
domain_sessionidx: Option[Int],
network_userid: Option[String],
geo_country: Option[String],
geo_region: Option[String],
geo_city: Option[String],
geo_zipcode: Option[String],
geo_latitude: Option[Double],
geo_longitude: Option[Double],
geo_region_name: Option[String],
ip_isp: Option[String],
ip_organization: Option[String],
ip_domain: Option[String],
ip_netspeed: Option[String],
page_url: Option[String],
page_title: Option[String],
page_referrer: Option[String],
page_urlscheme: Option[String],
page_urlhost: Option[String],
page_urlport: Option[Int],
page_urlpath: Option[String],
page_urlquery: Option[String],
page_urlfragment: Option[String],
refr_urlscheme: Option[String],
refr_urlhost: Option[String],
refr_urlport: Option[Int],
refr_urlpath: Option[String],
refr_urlquery: Option[String],
refr_urlfragment: Option[String],
refr_medium: Option[String],
refr_source: Option[String],
refr_term: Option[String],
mkt_medium: Option[String],
mkt_source: Option[String],
mkt_term: Option[String],
mkt_content: Option[String],
mkt_campaign: Option[String],
contexts: Contexts,
se_category: Option[String],
se_action: Option[String],
se_label: Option[String],
se_property: Option[String],
se_value: Option[Double],
unstruct_event: UnstructEvent,
tr_orderid: Option[String],
tr_affiliation: Option[String],
tr_total: Option[Double],
tr_tax: Option[Double],
tr_shipping: Option[Double],
tr_city: Option[String],
tr_state: Option[String],
tr_country: Option[String],
ti_orderid: Option[String],
ti_sku: Option[String],
ti_name: Option[String],
ti_category: Option[String],
ti_price: Option[Double],
ti_quantity: Option[Int],
pp_xoffset_min: Option[Int],
pp_xoffset_max: Option[Int],
pp_yoffset_min: Option[Int],
pp_yoffset_max: Option[Int],
useragent: Option[String],
br_name: Option[String],
br_family: Option[String],
br_version: Option[String],
br_type: Option[String],
br_renderengine: Option[String],
br_lang: Option[String],
br_features_pdf: Option[Boolean],
br_features_flash: Option[Boolean],
br_features_java: Option[Boolean],
br_features_director: Option[Boolean],
br_features_quicktime: Option[Boolean],
br_features_realplayer: Option[Boolean],
br_features_windowsmedia: Option[Boolean],
br_features_gears: Option[Boolean],
br_features_silverlight: Option[Boolean],
br_cookies: Option[Boolean],
br_colordepth: Option[String],
br_viewwidth: Option[Int],
br_viewheight: Option[Int],
os_name: Option[String],
os_family: Option[String],
os_manufacturer: Option[String],
os_timezone: Option[String],
dvce_type: Option[String],
dvce_ismobile: Option[Boolean],
dvce_screenwidth: Option[Int],
dvce_screenheight: Option[Int],
doc_charset: Option[String],
doc_width: Option[Int],
doc_height: Option[Int],
tr_currency: Option[String],
tr_total_base: Option[Double],
tr_tax_base: Option[Double],
tr_shipping_base: Option[Double],
ti_currency: Option[String],
ti_price_base: Option[Double],
base_currency: Option[String],
geo_timezone: Option[String],
mkt_clickid: Option[String],
mkt_network: Option[String],
etl_tags: Option[String],
dvce_sent_tstamp: Option[Instant],
refr_domain_userid: Option[String],
refr_dvce_tstamp: Option[Instant],
derived_contexts: Contexts,
domain_sessionid: Option[String],
derived_tstamp: Option[Instant],
event_vendor: Option[String],
event_name: Option[String],
event_format: Option[String],
event_version: Option[String],
event_fingerprint: Option[String],
true_tstamp: Option[Instant]
) {
// format: on
/**
* Extracts metadata from the event containing information about the types and Iglu URIs of its shred properties
*/
def inventory: Set[Data.ShreddedType] = {
val unstructEvent = unstruct_event.data.toSet
.map((ue: SelfDescribingData[Json]) => Data.ShreddedType(Data.UnstructEvent, ue.schema))
val derivedContexts = derived_contexts.data.toSet
.map((ctx: SelfDescribingData[Json]) => Data.ShreddedType(Data.Contexts(Data.DerivedContexts), ctx.schema))
val customContexts = contexts.data.toSet
.map((ctx: SelfDescribingData[Json]) => Data.ShreddedType(Data.Contexts(Data.CustomContexts), ctx.schema))
customContexts ++ derivedContexts ++ unstructEvent
}
/**
* Returns the event as a map of keys to Circe JSON values, while dropping inventory fields
*/
def atomic: Map[String, Json] = jsonMap - "contexts" - "unstruct_event" - "derived_contexts"
/**
* Returns the event as a list of key/Circe JSON value pairs.
* Unlike `jsonMap` and `atomic`, these keys use the ordering of the canonical event model
*/
def ordered: List[(String, Option[Json])] =
Event.fieldNames.map(key => (key, jsonMap.get(key)))
/**
* Returns a compound JSON field containing information about an event's latitude and longitude,
* or None if one of these fields doesn't exist
*/
def geoLocation: Option[(String, Json)] =
for {
lat <- geo_latitude
lon <- geo_longitude
} yield "geo_location" -> s"$lat,$lon".asJson
/**
* Transforms the event to a validated JSON whose keys are the field names corresponding to the
* EnrichedEvent POJO of the Scala Common Enrich project. If the lossy argument is true, any
* self-describing events in the fields (unstruct_event, contexts and derived_contexts) are returned
* in a "shredded" format (e.g. "unstruct_event_com_acme_1_myField": "value"), otherwise a standard
* self-describing format is used.
*
* @param lossy Whether unstruct_event, contexts and derived_contexts should be flattened
*/
def toJson(lossy: Boolean): Json =
if (lossy)
JsonObject
.fromMap(
atomic ++ contexts.toShreddedJson.toMap ++ derived_contexts.toShreddedJson.toMap ++ unstruct_event.toShreddedJson.toMap ++ geoLocation
)
.asJson
else
this.asJson
/** Create the TSV representation of this event. */
def toTsv: String = TsvEncoder.encode(this)
/**
* This event as a map of keys to Circe JSON values
*/
private lazy val jsonMap: Map[String, Json] = this.asJsonObject.toMap
}
object Event {
@deprecated("Event.unsafe functionality is merged into Event.parse method", "3.1.0")
object unsafe {
implicit def unsafeEventDecoder: Decoder[Event] = Event.eventDecoder
}
/**
* Automatically derived Circe encoder
*/
implicit val jsonEncoder: Encoder.AsObject[Event] = deriveEncoder[Event]
implicit def eventDecoder: Decoder[Event] = deriveDecoder[Event]
/**
* Derive a TSV parser for the Event class
*
* @param truncateAtomicFields A map from field names, e.g. "app_id", to maximum string lengths.
* If supplied, the event fields are truncated to not exceed these maximum values.
*
* @note Enrich already performs atomic field length validation since version 3.0.0. Only supply
* a non-empty map if atomic field lengths are important to you, and either you are parsing
* events generated by an older version of enrich, or if you run enrich with the
* `featureFlags.acceptInvalid` config option on.
*/
def parser(truncateAtomicFields: Map[String, Int] = Map.empty): TSVParser[Event] =
Parser.deriveFor[Event].get(truncateAtomicFields)
private lazy val stdParser: TSVParser[Event] = parser()
/**
* Converts a string with an enriched event TSV to an Event instance,
* or a ValidatedNel containing information about errors
*
* @param line Enriched event TSV line
*/
def parse(line: String): DecodeResult[Event] =
stdParser.parse(line)
def parseBytes(bytes: ByteBuffer): DecodeResult[Event] =
stdParser.parseBytes(bytes)
private lazy val fieldNames: List[String] =
Parser.deriveFor[Event].knownKeys
/**
* Creates an event with only required fields.
* All optional fields are set to [[None]].
*/
def minimal(
id: UUID,
collectorTstamp: Instant,
vCollector: String,
vEtl: String
): Event =
Event(
None,
None,
None,
collectorTstamp,
None,
None,
id,
None,
None,
None,
vCollector,
vEtl,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
Contexts(Nil),
None,
None,
None,
None,
None,
UnstructEvent(None),
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
Contexts(Nil),
None,
None,
None,
None,
None,
None,
None,
None
)
}