Skip to content

Commit

Permalink
Added ua_parser_context to derived_contexts field (see #62, see #787)
Browse files Browse the repository at this point in the history
  • Loading branch information
fblundun committed Mar 2, 2015
1 parent 53f4ea9 commit 015a972
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import scala.collection.mutable.ListBuffer
import scalaz._
import Scalaz._

// json4s
import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.jackson.JsonMethods._

// SnowPlow Utils
import util.Tap._

Expand Down Expand Up @@ -283,16 +288,16 @@ object EnrichmentManager {
}
}

// Create the ua_parser_context
val uaParser = {
registry.getUaParserEnrichment match {
case Some(uap) => {
Option(event.useragent) match {
case Some(ua) =>
var uaJsonPlaceholder = uap.extractUserAgent(ua)
case None => unitSuccess // No fields updated
case Some(ua) => uap.extractUserAgent(ua).map(_.some)
case None => None.success // No fields updated
}
}
case None => unitSuccess
case None => None.success
}
}

Expand Down Expand Up @@ -373,6 +378,18 @@ object EnrichmentManager {
case None => unitSuccessNel // No fields updated
})

// Assemble array of derived contexts
val derived_contexts = List(uaParser) collect {
case Success(Some(context)) => context
}

if (derived_contexts.size > 0) {
event.derived_contexts = compact(render(
("schema" -> "iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-1") ~
("data" -> JArray(derived_contexts))
))
}

// Some quick and dirty truncation to ensure the load into Redshift doesn't error. Yech this is pretty dirty
// TODO: move this into the db-specific ETL phase (when written) & _programmatically_ apply to all strings, not just these 6
event.useragent = CU.truncate(event.useragent, 1000)
Expand Down

0 comments on commit 015a972

Please sign in to comment.