-
Notifications
You must be signed in to change notification settings - Fork 36
/
EnrichmentManager.scala
894 lines (840 loc) · 38.2 KB
/
EnrichmentManager.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
/*
* Copyright (c) 2012-present Snowplow Analytics Ltd.
* All rights reserved.
*
* This software is made available by Snowplow Analytics, Ltd.,
* under the terms of the Snowplow Limited Use License Agreement, Version 1.0
* located at https://docs.snowplow.io/limited-use-license-1.0
* BY INSTALLING, DOWNLOADING, ACCESSING, USING OR DISTRIBUTING ANY PORTION
* OF THE SOFTWARE, YOU AGREE TO THE TERMS OF SUCH LICENSE AGREEMENT.
*/
package com.snowplowanalytics.snowplow.enrich.common.enrichments
import java.nio.charset.Charset
import java.net.URI
import java.time.Instant
import org.joda.time.DateTime
import io.circe.Json
import cats.{Applicative, Functor, Monad}
import cats.data.{EitherT, Ior, IorT, NonEmptyList, OptionT, StateT}
import cats.implicits._
import cats.effect.kernel.{Clock, Sync}
import com.snowplowanalytics.refererparser._
import com.snowplowanalytics.iglu.client.IgluCirceClient
import com.snowplowanalytics.iglu.client.resolver.registries.RegistryLookup
import com.snowplowanalytics.iglu.client.validator.ValidatorReport
import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaVer, SelfDescribingData}
import com.snowplowanalytics.iglu.core.circe.implicits._
import com.snowplowanalytics.snowplow.badrows._
import com.snowplowanalytics.snowplow.badrows.{Failure => BadRowFailure}
import com.snowplowanalytics.snowplow.enrich.common.{EtlPipeline, QueryStringParameters, RawEventParameters}
import com.snowplowanalytics.snowplow.enrich.common.adapters.RawEvent
import com.snowplowanalytics.snowplow.enrich.common.enrichments.{EventEnrichments => EE}
import com.snowplowanalytics.snowplow.enrich.common.enrichments.{MiscEnrichments => ME}
import com.snowplowanalytics.snowplow.enrich.common.enrichments.registry.{CrossNavigationEnrichment => CNE, _}
import com.snowplowanalytics.snowplow.enrich.common.enrichments.registry.apirequest.ApiRequestEnrichment
import com.snowplowanalytics.snowplow.enrich.common.enrichments.registry.pii.PiiPseudonymizerEnrichment
import com.snowplowanalytics.snowplow.enrich.common.enrichments.registry.sqlquery.SqlQueryEnrichment
import com.snowplowanalytics.snowplow.enrich.common.enrichments.web.{PageEnrichments => WPE}
import com.snowplowanalytics.snowplow.enrich.common.outputs.EnrichedEvent
import com.snowplowanalytics.snowplow.enrich.common.utils.{IgluUtils, ConversionUtils => CU}
object EnrichmentManager {
/**
* Run the enrichment workflow
* @param registry Contain configuration for all enrichments to apply
* @param client Iglu Client, for schema lookups and validation
* @param processor Meta information about processing asset, for bad rows
* @param etlTstamp ETL timestamp
* @param raw Canonical input event to enrich
* @param featureFlags The feature flags available in the current version of Enrich
* @param invalidCount Function to increment the count of invalid events
* @return Right(EnrichedEvent) if everything went well.
* Left(BadRow) if something went wrong and incomplete events are not enabled.
* Both(BadRow, EnrichedEvent) if something went wrong but incomplete events are enabled.
*/
def enrichEvent[F[_]: Sync](
registry: EnrichmentRegistry[F],
client: IgluCirceClient[F],
processor: Processor,
etlTstamp: DateTime,
raw: RawEvent,
featureFlags: EtlPipeline.FeatureFlags,
invalidCount: F[Unit],
registryLookup: RegistryLookup[F],
atomicFields: AtomicFields,
emitIncomplete: Boolean
): IorT[F, BadRow, EnrichedEvent] = {
def enrich(enriched: EnrichedEvent): IorT[F, NonEmptyList[NonEmptyList[Failure]], List[SelfDescribingData[Json]]] =
for {
extractResult <- mapAndValidateInput(
raw,
enriched,
etlTstamp,
processor,
client,
registryLookup
)
.leftMap(NonEmptyList.one)
.possiblyExitingEarly(emitIncomplete)
// Next 2 lines remove the invalid contexts and the invalid unstructured event from the event.
// This should be done after the bad row was created and only if emitIncomplete is enabled.
_ = {
enriched.contexts = ME.formatContexts(extractResult.contexts).orNull
enriched.unstruct_event = ME.formatUnstructEvent(extractResult.unstructEvent).orNull
}
enrichmentsContexts <- runEnrichments(
registry,
raw,
enriched,
extractResult.contexts,
extractResult.unstructEvent,
featureFlags.legacyEnrichmentOrder
)
.leftMap(NonEmptyList.one)
.possiblyExitingEarly(emitIncomplete)
validContexts <- validateEnriched(
enriched,
enrichmentsContexts,
client,
registryLookup,
featureFlags.acceptInvalid,
invalidCount,
atomicFields,
emitIncomplete
)
.leftMap(NonEmptyList.one)
.possiblyExitingEarly(emitIncomplete)
derivedContexts = validContexts ::: extractResult.validationInfoContexts
} yield derivedContexts
// derived contexts are set lastly because we want to include failure entities
// to derived contexts as well and we can get failure entities only in the end
// of the enrichment process
IorT(
for {
enrichedEvent <- Sync[F].delay(new EnrichedEvent)
enrichmentResult <- enrich(enrichedEvent).value
now = Instant.now()
_ = setDerivedContexts(enrichedEvent, enrichmentResult, now, processor)
result = enrichmentResult
.leftMap { fe =>
createBadRow(
fe,
EnrichedEvent.toPartiallyEnrichedEvent(enrichedEvent),
RawEvent.toRawEvent(raw),
now,
processor
)
}
.map(_ => enrichedEvent)
} yield result
)
}
private def createBadRow(
fe: NonEmptyList[NonEmptyList[Failure]],
pe: Payload.PartiallyEnrichedEvent,
re: Payload.RawEvent,
timestamp: Instant,
processor: Processor
): BadRow = {
val firstList = fe.head
firstList.head match {
case h: Failure.SchemaViolation =>
val sv = firstList.tail.collect { case f: Failure.SchemaViolation => f }
BadRow.SchemaViolations(
processor,
BadRowFailure.SchemaViolations(timestamp, NonEmptyList(h, sv).map(_.schemaViolation)),
Payload.EnrichmentPayload(pe, re)
)
case h: Failure.EnrichmentFailure =>
val ef = firstList.tail.collect { case f: Failure.EnrichmentFailure => f }
BadRow.EnrichmentFailures(
processor,
BadRowFailure.EnrichmentFailures(timestamp, NonEmptyList(h, ef).map(_.enrichmentFailure)),
Payload.EnrichmentPayload(pe, re)
)
}
}
def setDerivedContexts(
enriched: EnrichedEvent,
enrichmentResult: Ior[NonEmptyList[NonEmptyList[Failure]], List[SelfDescribingData[Json]]],
timestamp: Instant,
processor: Processor
): Unit = {
val derivedContexts = enrichmentResult.leftMap { ll =>
ll.flatten.toList
.map(_.toSDJ(timestamp, processor))
}.merge
ME.formatContexts(derivedContexts).foreach(c => enriched.derived_contexts = c)
}
private def mapAndValidateInput[F[_]: Sync](
raw: RawEvent,
enrichedEvent: EnrichedEvent,
etlTstamp: DateTime,
processor: Processor,
client: IgluCirceClient[F],
registryLookup: RegistryLookup[F]
): IorT[F, NonEmptyList[Failure], IgluUtils.EventExtractResult] =
for {
_ <- setupEnrichedEvent[F](raw, enrichedEvent, etlTstamp, processor)
.leftMap(NonEmptyList.one)
extract <- IgluUtils
.extractAndValidateInputJsons(enrichedEvent, client, registryLookup)
.leftMap { l: NonEmptyList[Failure] => l }
} yield extract
/**
* Run all the enrichments
* @param enriched /!\ MUTABLE enriched event, mutated IN-PLACE /!\
* @return All the contexts produced by the enrichments are in the Right.
* All the errors are aggregated in the bad row in the Left.
*/
private def runEnrichments[F[_]: Monad](
registry: EnrichmentRegistry[F],
raw: RawEvent,
enriched: EnrichedEvent,
inputContexts: List[SelfDescribingData[Json]],
unstructEvent: Option[SelfDescribingData[Json]],
legacyOrder: Boolean
): IorT[F, NonEmptyList[Failure], List[SelfDescribingData[Json]]] =
IorT {
accState(registry, raw, inputContexts, unstructEvent, legacyOrder)
.runS(Accumulation(enriched, Nil, Nil))
.map {
case Accumulation(_, failures, contexts) =>
failures.toNel match {
case Some(nel) =>
Ior.both(
nel.map(Failure.EnrichmentFailure),
contexts
)
case None =>
Ior.right(contexts)
}
}
}
private def validateEnriched[F[_]: Clock: Monad](
enriched: EnrichedEvent,
enrichmentsContexts: List[SelfDescribingData[Json]],
client: IgluCirceClient[F],
registryLookup: RegistryLookup[F],
acceptInvalid: Boolean,
invalidCount: F[Unit],
atomicFields: AtomicFields,
emitIncomplete: Boolean
): IorT[F, NonEmptyList[Failure], List[SelfDescribingData[Json]]] =
for {
validContexts <- IgluUtils.validateEnrichmentsContexts[F](client, enrichmentsContexts, registryLookup)
_ <- AtomicFieldsLengthValidator
.validate[F](enriched, acceptInvalid, invalidCount, atomicFields, emitIncomplete)
.leftMap { v: Failure => NonEmptyList.one(v) }
} yield validContexts
private[enrichments] case class Accumulation(
event: EnrichedEvent,
errors: List[FailureDetails.EnrichmentFailure],
contexts: List[SelfDescribingData[Json]]
)
private[enrichments] type EStateT[F[_], A] = StateT[F, Accumulation, A]
private object EStateT {
def apply[F[_]: Applicative, A](f: Accumulation => F[(Accumulation, A)]): EStateT[F, A] =
StateT(f)
def fromEither[F[_]: Applicative](
f: (EnrichedEvent,
List[SelfDescribingData[Json]]) => Either[NonEmptyList[FailureDetails.EnrichmentFailure], List[SelfDescribingData[Json]]]
): EStateT[F, Unit] =
fromEitherF[F] { case (x, y) => f(x, y).pure[F] }
def fromEitherF[F[_]: Applicative](
f: (EnrichedEvent,
List[SelfDescribingData[Json]]) => F[Either[NonEmptyList[FailureDetails.EnrichmentFailure], List[SelfDescribingData[Json]]]]
): EStateT[F, Unit] =
EStateT {
case Accumulation(event, errors, contexts) =>
f(event, contexts).map {
case Right(contexts2) => (Accumulation(event, errors, contexts2 ::: contexts), ())
case Left(moreErrors) => (Accumulation(event, moreErrors.toList ::: errors, contexts), ())
}
}
def fromEitherOpt[F[_]: Applicative, A](
f: EnrichedEvent => Either[NonEmptyList[FailureDetails.EnrichmentFailure], Option[A]]
): EStateT[F, Option[A]] =
EStateT {
case acc @ Accumulation(event, errors, contexts) =>
f(event) match {
case Right(opt) => (acc, opt).pure[F]
case Left(moreErrors) => (Accumulation(event, moreErrors.toList ::: errors, contexts), Option.empty[A]).pure[F]
}
}
}
private def accState[F[_]: Monad](
registry: EnrichmentRegistry[F],
raw: RawEvent,
inputContexts: List[SelfDescribingData[Json]],
unstructEvent: Option[SelfDescribingData[Json]],
legacyOrder: Boolean
): EStateT[F, Unit] = {
val getCookieContexts = headerContexts[F, CookieExtractorEnrichment](
raw.context.headers,
registry.cookieExtractor,
(e, hs) => e.extract(hs)
)
val getHttpHeaderContexts = headerContexts[F, HttpHeaderExtractorEnrichment](
raw.context.headers,
registry.httpHeaderExtractor,
(e, hs) => e.extract(hs)
)
val sqlContexts = getSqlQueryContexts[F](inputContexts, unstructEvent, registry.sqlQuery)
val apiContexts = getApiRequestContexts[F](inputContexts, unstructEvent, registry.apiRequest)
if (legacyOrder)
for {
// format: off
_ <- getCollectorVersionSet[F] // The load fails if the collector version is not set
pageUri <- getPageUri[F](raw.context.refererUri) // Potentially update the page_url and set the page URL components
_ <- getDerivedTstamp[F] // Calculate the derived timestamp
_ <- getIabContext[F](registry.iab) // Fetch IAB enrichment context (before anonymizing the IP address)
_ <- getUaUtils[F](registry.userAgentUtils) // Parse the useragent using user-agent-utils
_ <- getUaParser[F](registry.uaParser) // Create the ua_parser_context
_ <- getRefererUri[F](registry.refererParser) // Potentially set the referrer details and URL components
qsMap <- extractQueryString[F](pageUri, raw.source.encoding) // Parse the page URI's querystring
_ <- setCampaign[F](qsMap, registry.campaignAttribution) // Marketing attribution
_ <- getCrossDomain[F](qsMap, registry.crossNavigation) // Cross-domain tracking
_ <- setEventFingerprint[F](raw.parameters, registry.eventFingerprint) // This enrichment cannot fail
_ <- getCookieContexts // Execute cookie extractor enrichment
_ <- getHttpHeaderContexts // Execute header extractor enrichment
_ <- getYauaaContext[F](registry.yauaa, raw.context.headers) // Runs YAUAA enrichment (gets info thanks to user agent)
_ <- extractSchemaFields[F](unstructEvent) // Extract the event vendor/name/format/version
_ <- registry.javascriptScript.traverse(getJsScript[F](_)) // Execute the JavaScript scripting enrichment
_ <- getCurrency[F](raw.context.timestamp, registry.currencyConversion) // Finalize the currency conversion
_ <- getWeatherContext[F](registry.weather) // Fetch weather context
_ <- geoLocation[F](registry.ipLookups) // Execute IP lookup enrichment
_ <- sqlContexts // Derive some contexts with custom SQL Query enrichment
_ <- apiContexts // Derive some contexts with custom API Request enrichment
_ <- anonIp[F](registry.anonIp) // Anonymize the IP
_ <- piiTransform[F](registry.piiPseudonymizer) // Run PII pseudonymization
// format: on
} yield ()
else
for {
// format: off
_ <- getCollectorVersionSet[F] // The load fails if the collector version is not set
pageUri <- getPageUri[F](raw.context.refererUri) // Potentially update the page_url and set the page URL components
_ <- getDerivedTstamp[F] // Calculate the derived timestamp
_ <- getIabContext[F](registry.iab) // Fetch IAB enrichment context (before anonymizing the IP address)
_ <- getUaUtils[F](registry.userAgentUtils) // Parse the useragent using user-agent-utils
_ <- getUaParser[F](registry.uaParser) // Create the ua_parser_context
_ <- getCurrency[F](raw.context.timestamp, registry.currencyConversion) // Finalize the currency conversion
_ <- getRefererUri[F](registry.refererParser) // Potentially set the referrer details and URL components
qsMap <- extractQueryString[F](pageUri, raw.source.encoding) // Parse the page URI's querystring
_ <- setCampaign[F](qsMap, registry.campaignAttribution) // Marketing attribution
_ <- getCrossDomain[F](qsMap, registry.crossNavigation) // Cross-domain tracking
_ <- setEventFingerprint[F](raw.parameters, registry.eventFingerprint) // This enrichment cannot fail
_ <- getCookieContexts // Execute cookie extractor enrichment
_ <- getHttpHeaderContexts // Execute header extractor enrichment
_ <- getWeatherContext[F](registry.weather) // Fetch weather context
_ <- getYauaaContext[F](registry.yauaa, raw.context.headers) // Runs YAUAA enrichment (gets info thanks to user agent)
_ <- extractSchemaFields[F](unstructEvent) // Extract the event vendor/name/format/version
_ <- geoLocation[F](registry.ipLookups) // Execute IP lookup enrichment
_ <- registry.javascriptScript.traverse(getJsScript[F](_)) // Execute the JavaScript scripting enrichment
_ <- sqlContexts // Derive some contexts with custom SQL Query enrichment
_ <- apiContexts // Derive some contexts with custom API Request enrichment
_ <- anonIp[F](registry.anonIp) // Anonymize the IP
_ <- piiTransform[F](registry.piiPseudonymizer) // Run PII pseudonymization
// format: on
} yield ()
}
/** Initialize the mutable [[EnrichedEvent]]. */
private def setupEnrichedEvent[F[_]: Sync](
raw: RawEvent,
e: EnrichedEvent,
etlTstamp: DateTime,
processor: Processor
): IorT[F, Failure.SchemaViolation, Unit] =
IorT {
Sync[F].delay {
e.event_id = EE.generateEventId() // May be updated later if we have an `eid` parameter
e.v_collector = raw.source.name // May be updated later if we have a `cv` parameter
e.v_etl = ME.etlVersion(processor)
e.etl_tstamp = EE.toTimestamp(etlTstamp)
e.network_userid = raw.context.userId.map(_.toString).orNull // May be updated later by 'nuid'
e.user_ipaddress = ME
.extractIp("user_ipaddress", raw.context.ipAddress.orNull)
.toOption
.orNull // May be updated later by 'ip'
// May be updated later if we have a `ua` parameter
setUseragent(e, raw.context.useragent)
// Validate that the collectorTstamp exists and is Redshift-compatible
val collectorTstamp = setCollectorTstamp(e, raw.context.timestamp).toValidatedNel
// Map/validate/transform input fields to enriched event fields
val transformed = Transform.transform(raw, e)
(collectorTstamp |+| transformed).void.toIor
.leftMap(AtomicFields.errorsToSchemaViolation)
.putRight(())
}
}
def setCollectorTstamp(event: EnrichedEvent, timestamp: Option[DateTime]): Either[ValidatorReport, Unit] =
EE.formatCollectorTstamp(timestamp).map { t =>
event.collector_tstamp = t
().asRight
}
def setUseragent(
event: EnrichedEvent,
useragent: Option[String]
): Unit =
useragent match {
case Some(ua) =>
val s = ua.replaceAll("(\\r|\\n)", "").replaceAll("\\t", " ")
event.useragent = s
case None => () // No fields updated
}
// The load fails if the collector version is not set
def getCollectorVersionSet[F[_]: Applicative]: EStateT[F, Unit] =
EStateT.fromEither {
case (event, _) =>
event.v_collector match {
case "" | null =>
NonEmptyList
.one(
FailureDetails
.EnrichmentFailure(
None,
FailureDetails.EnrichmentFailureMessage
.InputData("v_collector", None, "should be set")
)
)
.asLeft
case _ => Nil.asRight
}
}
// If our IpToGeo enrichment is enabled, get the geo-location from the IP address
// enrichment doesn't fail to maintain the previous approach where failures were suppressed
// c.f. https://github.com/snowplow/snowplow/issues/351
def geoLocation[F[_]: Monad](ipLookups: Option[IpLookupsEnrichment[F]]): EStateT[F, Unit] =
EStateT.fromEitherF {
case (event, _) =>
val ipLookup = for {
enrichment <- OptionT.fromOption[F](ipLookups)
ip <- OptionT.fromOption[F](Option(event.user_ipaddress))
result <- OptionT.liftF(enrichment.extractIpInformation(ip))
} yield result
ipLookup.value.map {
case Some(lookup) =>
lookup.ipLocation.flatMap(_.toOption).foreach { loc =>
event.geo_country = loc.countryCode
event.geo_region = loc.region.orNull
event.geo_city = loc.city.orNull
event.geo_zipcode = loc.postalCode.orNull
event.geo_latitude = loc.latitude
event.geo_longitude = loc.longitude
event.geo_region_name = loc.regionName.orNull
event.geo_timezone = loc.timezone.orNull
}
lookup.isp.flatMap(_.toOption).foreach { i =>
event.ip_isp = i
}
lookup.organization.flatMap(_.toOption).foreach { org =>
event.ip_organization = org
}
lookup.domain.flatMap(_.toOption).foreach { d =>
event.ip_domain = d
}
lookup.connectionType.flatMap(_.toOption).foreach { ct =>
event.ip_netspeed = ct
}
Nil.asRight
case None =>
Nil.asRight
}
}
// Potentially update the page_url and set the page URL components
def getPageUri[F[_]: Applicative](refererUri: Option[String]): EStateT[F, Option[URI]] =
EStateT.fromEitherOpt { event =>
val pageUri = WPE.extractPageUri(refererUri, Option(event.page_url))
for {
uri <- pageUri
u <- uri
} {
// Update the page_url
event.page_url = u.toString
// Set the URL components
val components = CU.explodeUri(u)
event.page_urlscheme = components.scheme
event.page_urlhost = components.host
event.page_urlport = components.port
event.page_urlpath = components.path.orNull
event.page_urlquery = components.query.orNull
event.page_urlfragment = components.fragment.orNull
}
pageUri.leftMap(NonEmptyList.one(_))
}
// Calculate the derived timestamp
def getDerivedTstamp[F[_]: Applicative]: EStateT[F, Unit] =
EStateT.fromEither {
case (event, _) =>
EE.getDerivedTimestamp(
Option(event.dvce_sent_tstamp),
Option(event.dvce_created_tstamp),
Option(event.collector_tstamp),
Option(event.true_tstamp)
).map { dt =>
dt.foreach(event.derived_tstamp = _)
Nil
}.leftMap(NonEmptyList.one(_))
}
// Fetch IAB enrichment context (before anonymizing the IP address).
// IAB enrichment is called only if the IP is v4 (and after removing the port if any)
// and if the user agent is defined.
def getIabContext[F[_]: Applicative](iabEnrichment: Option[IabEnrichment]): EStateT[F, Unit] =
EStateT.fromEither {
case (event, _) =>
val result = for {
iab <- iabEnrichment
useragent <- Option(event.useragent).filter(_.trim.nonEmpty)
ipString <- Option(event.user_ipaddress)
ip <- CU.extractInetAddress(ipString)
tstamp <- Option(event.derived_tstamp).map(EventEnrichments.fromTimestamp)
} yield iab.getIabContext(useragent, ip, tstamp)
result.sequence.bimap(NonEmptyList.one(_), _.toList)
}
def anonIp[F[_]: Applicative](anonIp: Option[AnonIpEnrichment]): EStateT[F, Unit] =
EStateT.fromEither {
case (event, _) =>
anonIp match {
case Some(anon) =>
Option(event.user_ipaddress) match {
case Some(ip) =>
Option(anon.anonymizeIp(ip)).foreach(event.user_ipaddress = _)
Nil.asRight
case None =>
Nil.asRight
}
case None =>
Nil.asRight
}
}
def getUaUtils[F[_]: Applicative](userAgentUtils: Option[UserAgentUtilsEnrichment]): EStateT[F, Unit] =
EStateT.fromEither {
case (event, _) =>
userAgentUtils match {
case Some(uap) =>
Option(event.useragent) match {
case Some(ua) =>
val ca = uap.extractClientAttributes(ua)
ca.map { c =>
event.br_name = c.browserName
event.br_family = c.browserFamily
c.browserVersion.foreach(bv => event.br_version = bv)
event.br_type = c.browserType
event.br_renderengine = c.browserRenderEngine
event.os_name = c.osName
event.os_family = c.osFamily
event.os_manufacturer = c.osManufacturer
event.dvce_type = c.deviceType
event.dvce_ismobile = CU.booleanToJByte(c.deviceIsMobile)
Nil
}.leftMap(NonEmptyList.one(_))
case None => Nil.asRight // No fields updated
}
case None => Nil.asRight
}
}
def getUaParser[F[_]: Monad](uaParser: Option[UaParserEnrichment[F]]): EStateT[F, Unit] =
EStateT.fromEitherF {
case (event, _) =>
uaParser match {
case Some(uap) =>
Option(event.useragent) match {
case Some(ua) =>
uap.extractUserAgent(ua).map {
case Left(failure) => Left(NonEmptyList.one(failure))
case Right(context) => Right(List(context))
}
case None => Monad[F].pure(Nil.asRight) // No fields updated
}
case None => Monad[F].pure(Nil.asRight)
}
}
def getCurrency[F[_]: Monad](
timestamp: Option[DateTime],
currencyConversion: Option[CurrencyConversionEnrichment[F]]
): EStateT[F, Unit] =
EStateT.fromEitherF {
case (event, _) =>
currencyConversion match {
case Some(currency) =>
event.base_currency = currency.baseCurrency.getCode
// Note that jBigDecimalToDouble is applied to either-valid-or-null event POJO
// properties, so we don't expect any of these four vals to be a Failure
val enrichmentInfo = FailureDetails.EnrichmentInformation(
SchemaKey("com.snowplowanalytics.snowplow", "currency_conversion_config", "jsonschema", SchemaVer.Full(1, 0, 0)),
"currency_conversion"
)
val trTax = CU.jBigDecimalToDouble("tr_tx", event.tr_tax, enrichmentInfo).toValidatedNel
val tiPrice = CU.jBigDecimalToDouble("ti_pr", event.ti_price, enrichmentInfo).toValidatedNel
val trTotal = CU.jBigDecimalToDouble("tr_tt", event.tr_total, enrichmentInfo).toValidatedNel
val trShipping = CU.jBigDecimalToDouble("tr_sh", event.tr_shipping, enrichmentInfo).toValidatedNel
EitherT(
(trTotal, trTax, trShipping, tiPrice)
.mapN {
currency.convertCurrencies(
Option(event.tr_currency),
_,
_,
_,
Option(event.ti_currency),
_,
timestamp
)
}
.toEither
.sequence
.map(_.flatMap(_.toEither))
).map {
case (trTotalBase, trTaxBase, trShippingBase, tiPriceBase) =>
trTotalBase.foreach(v => event.tr_total_base = v)
trTaxBase.foreach(v => event.tr_tax_base = v)
trShippingBase.foreach(v => event.tr_shipping_base = v)
tiPriceBase.foreach(v => event.ti_price_base = v)
List.empty[SelfDescribingData[Json]]
}.value
case None => Monad[F].pure(Nil.asRight)
}
}
def getRefererUri[F[_]: Applicative](
refererParser: Option[RefererParserEnrichment]
): EStateT[F, Unit] =
EStateT.fromEither {
case (event, _) =>
CU.stringToUri(event.page_referrer)
.map { uri =>
for {
u <- uri
} {
// Set the URL components
val components = CU.explodeUri(u)
event.refr_urlscheme = components.scheme
event.refr_urlhost = components.host
event.refr_urlport = components.port
event.refr_urlpath = components.path.orNull
event.refr_urlquery = components.query.orNull
event.refr_urlfragment = components.fragment.orNull
// Set the referrer details
refererParser match {
case Some(rp) =>
for (refr <- rp.extractRefererDetails(u, event.page_urlhost))
refr match {
case UnknownReferer(medium) => event.refr_medium = CU.makeTsvSafe(medium.value)
case SearchReferer(medium, source, term) =>
event.refr_medium = CU.makeTsvSafe(medium.value)
event.refr_source = CU.makeTsvSafe(source)
event.refr_term = CU.makeTsvSafe(term.orNull)
case InternalReferer(medium) => event.refr_medium = CU.makeTsvSafe(medium.value)
case SocialReferer(medium, source) =>
event.refr_medium = CU.makeTsvSafe(medium.value)
event.refr_source = CU.makeTsvSafe(source)
case EmailReferer(medium, source) =>
event.refr_medium = CU.makeTsvSafe(medium.value)
event.refr_source = CU.makeTsvSafe(source)
case PaidReferer(medium, source) =>
event.refr_medium = CU.makeTsvSafe(medium.value)
event.refr_source = CU.makeTsvSafe(source)
}
case None => ()
}
}
Nil
}
.leftMap(f =>
NonEmptyList.one(
FailureDetails.EnrichmentFailure(
None,
FailureDetails.EnrichmentFailureMessage.Simple(f)
)
)
)
}
// Parse the page URI's querystring
def extractQueryString[F[_]: Applicative](
pageUri: Option[URI],
encoding: String
): EStateT[F, Option[QueryStringParameters]] =
EStateT.fromEitherOpt { _ =>
pageUri match {
case Some(u) =>
CU.extractQuerystring(u, Charset.forName(encoding)).bimap(NonEmptyList.one(_), _.some)
case None => None.asRight
}
}
def setCampaign[F[_]: Applicative](
pageQsMap: Option[QueryStringParameters],
campaignAttribution: Option[CampaignAttributionEnrichment]
): EStateT[F, Unit] =
EStateT.fromEither {
case (event, _) =>
pageQsMap match {
case Some(qsList) =>
campaignAttribution match {
case Some(ce) =>
val cmp = ce.extractMarketingFields(qsList)
event.mkt_medium = CU.makeTsvSafe(cmp.medium.orNull)
event.mkt_source = CU.makeTsvSafe(cmp.source.orNull)
event.mkt_term = CU.makeTsvSafe(cmp.term.orNull)
event.mkt_content = CU.makeTsvSafe(cmp.content.orNull)
event.mkt_campaign = CU.makeTsvSafe(cmp.campaign.orNull)
event.mkt_clickid = CU.makeTsvSafe(cmp.clickId.orNull)
event.mkt_network = CU.makeTsvSafe(cmp.network.orNull)
Nil.asRight
case None => Nil.asRight
}
case None => Nil.asRight
}
}
def getCrossDomain[F[_]: Applicative](
pageQsMap: Option[QueryStringParameters],
crossNavEnrichment: Option[CNE]
): EStateT[F, Unit] =
EStateT.fromEither {
case (event, _) =>
pageQsMap match {
case Some(qsMap) =>
CNE
.parseCrossDomain(qsMap)
.bimap(
err =>
crossNavEnrichment match {
case Some(cn) => NonEmptyList.one(cn.addEnrichmentInfo(err))
case None => NonEmptyList.one(err)
},
crossNavMap => {
crossNavMap.duid.foreach(event.refr_domain_userid = _)
crossNavMap.tstamp.foreach(event.refr_dvce_tstamp = _)
crossNavEnrichment match {
case Some(_) => crossNavMap.getCrossNavigationContext
case None => Nil
}
}
)
case None => Nil.asRight
}
}
def setEventFingerprint[F[_]: Applicative](
parameters: RawEventParameters,
eventFingerprint: Option[EventFingerprintEnrichment]
): EStateT[F, Unit] =
EStateT.fromEither {
case (event, _) =>
eventFingerprint match {
case Some(efe) =>
event.event_fingerprint = efe.getEventFingerprint(parameters)
Nil.asRight
case _ => Nil.asRight
}
}
// Extract the event vendor/name/format/version
def extractSchemaFields[F[_]: Applicative](
unstructEvent: Option[SelfDescribingData[Json]]
): EStateT[F, Unit] =
EStateT.fromEither {
case (event, _) =>
SchemaEnrichment
.extractSchema(event, unstructEvent)
.map {
case Some(schemaKey) =>
event.event_vendor = schemaKey.vendor
event.event_name = schemaKey.name
event.event_format = schemaKey.format
event.event_version = schemaKey.version.asString
Nil
case None => Nil
}
.leftMap(NonEmptyList.one(_))
}
// Execute the JavaScript scripting enrichment
def getJsScript[F[_]: Applicative](
javascriptScript: JavascriptScriptEnrichment
): EStateT[F, Unit] =
EStateT.fromEither {
case (event, derivedContexts) =>
ME.formatContexts(derivedContexts).foreach(c => event.derived_contexts = c)
javascriptScript.process(event).leftMap(NonEmptyList.one)
}
def headerContexts[F[_]: Applicative, A](
headers: List[String],
enrichment: Option[A],
f: (A, List[String]) => List[SelfDescribingData[Json]]
): EStateT[F, Unit] =
EStateT.fromEither[F] {
case (_, _) =>
enrichment match {
case Some(e) => f(e, headers).asRight
case None => Nil.asRight
}
}
// Fetch weather context
def getWeatherContext[F[_]: Monad](weather: Option[WeatherEnrichment[F]]): EStateT[F, Unit] =
EStateT.fromEitherF {
case (event, _) =>
weather match {
case Some(we) =>
we.getWeatherContext(
Option(event.geo_latitude),
Option(event.geo_longitude),
Option(event.derived_tstamp).map(EventEnrichments.fromTimestamp)
).map(_.map(List(_)))
case None => Monad[F].pure(Nil.asRight)
}
}
def getYauaaContext[F[_]: Applicative](yauaa: Option[YauaaEnrichment], headers: List[String]): EStateT[F, Unit] =
EStateT.fromEither {
case (event, _) =>
yauaa.map(_.getYauaaContext(event.useragent, headers)).toList.asRight
}
// Derive some contexts with custom SQL Query enrichment
def getSqlQueryContexts[F[_]: Monad](
inputContexts: List[SelfDescribingData[Json]],
unstructEvent: Option[SelfDescribingData[Json]],
sqlQuery: Option[SqlQueryEnrichment[F]]
): EStateT[F, Unit] =
EStateT.fromEitherF {
case (event, derivedContexts) =>
sqlQuery match {
case Some(enrichment) =>
enrichment.lookup(event, derivedContexts, inputContexts, unstructEvent).map(_.toEither)
case None =>
List.empty[SelfDescribingData[Json]].asRight.pure[F]
}
}
// Derive some contexts with custom API Request enrichment
def getApiRequestContexts[F[_]: Monad](
inputContexts: List[SelfDescribingData[Json]],
unstructEvent: Option[SelfDescribingData[Json]],
apiRequest: Option[ApiRequestEnrichment[F]]
): EStateT[F, Unit] =
EStateT.fromEitherF {
case (event, derivedContexts) =>
apiRequest match {
case Some(enrichment) =>
enrichment.lookup(event, derivedContexts, inputContexts, unstructEvent).map(_.toEither)
case None =>
List.empty[SelfDescribingData[Json]].asRight.pure[F]
}
}
def piiTransform[F[_]: Applicative](piiPseudonymizer: Option[PiiPseudonymizerEnrichment]): EStateT[F, Unit] =
EStateT.fromEither {
case (event, _) =>
piiPseudonymizer match {
case Some(pseudonymizer) =>
pseudonymizer.transformer(event).foreach(p => event.pii = p.asString)
Nil.asRight
case None =>
Nil.asRight
}
}
private implicit class IorTOps[F[_], A, B](val iorT: IorT[F, A, B]) extends AnyVal {
/** If the incomplete events feature is disabled, then convert a Both to a Left, so we don't waste time with next steps */
def possiblyExitingEarly(emitIncomplete: Boolean)(implicit F: Functor[F]): IorT[F, A, B] =
if (emitIncomplete) iorT
else
IorT {
iorT.value.map {
case Ior.Both(bad, _) => Ior.Left(bad)
case other => other
}
}
}
}