-
Notifications
You must be signed in to change notification settings - Fork 14
/
Event.scala
272 lines (253 loc) · 13.1 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
/*
* Copyright (c) 2016-2019 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
// circe
import io.circe.{Encoder, Json, JsonObject, Decoder}
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.{Parser, DecodeResult}
import com.snowplowanalytics.snowplow.analytics.scalasdk.SnowplowEvent.{Contexts, UnstructEvent}
import com.snowplowanalytics.snowplow.analytics.scalasdk.SnowplowEvent._
/**
* Case class representing a canonical Snowplow event.
*
* @see https://github.com/snowplow/snowplow/wiki/canonical-event-model
*/
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]) {
/**
* 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] = toJsonMap - "contexts" - "unstruct_event" - "derived_contexts"
/**
* Returns the event as a list of key/Circe JSON value pairs.
* Unlike `toJsonMap` and `atomic`, these keys use the ordering of the canonical event model
*/
def ordered: List[(String, Option[Json])] =
Event.parser.knownKeys.map(key => (key.name, toJsonMap.get(key.name)))
/**
* 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
}
/**
* Returns the event as a map of keys to Circe JSON values
*/
private def toJsonMap: Map[String, Json] = this.asJsonObject.toMap
}
object Event {
/**
* Automatically derived Circe encoder
*/
implicit val jsonEncoder: Encoder.AsObject[Event] = deriveEncoder[Event]
implicit def eventDecoder: Decoder[Event] = deriveDecoder[Event]
/**
* Derived TSV parser for the Event class
*/
private val parser: Parser[Event] = Parser.deriveFor[Event].get
/**
* 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] =
parser.parse(line)
/**
* 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)
}