Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guardian provider to the list of providers #232

Merged
merged 2 commits into from Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,5 +1,3 @@
package com.gu.notifications.events.model



case class NotificationReportEvent(id: String, eventAggregation: EventAggregation)
case class NotificationReportEvent(id: String, eventAggregation: EventAggregation)
Expand Up @@ -10,6 +10,9 @@ case class PlatformCount(


object PlatformCount {

val empty: PlatformCount = PlatformCount(0, 0, 0)

def from(platform: Platform): PlatformCount = platform match {
case Ios => PlatformCount(1, 1, 0)
case Android => PlatformCount(1, 0, 1)
Expand Down
Expand Up @@ -4,11 +4,13 @@ sealed trait Provider

case object Azure extends Provider
case object Fcm extends Provider
case object Guardian extends Provider

object Provider {
def fromString(providerString: String): Option[Provider] = providerString.toLowerCase match {
case "azure" => Some(Azure)
case "fcm" => Some(Fcm)
case "guardian" => Some(Guardian)
case _ => None
}

Expand Down
@@ -1,27 +1,49 @@
package com.gu.notifications.events.model

import play.api.libs.json.Json
import play.api.libs.json._


case class ProviderCount(
total: Int,
azure: PlatformCount,
firebase: PlatformCount
azure: PlatformCount = PlatformCount.empty,
firebase: PlatformCount = PlatformCount.empty,
guardian: PlatformCount = PlatformCount.empty
)


object ProviderCount {

def from(provider: Provider, platform: Platform): ProviderCount = (provider, platform) match {
case (Azure, Ios) => ProviderCount(1, PlatformCount(1, 1, 0), PlatformCount(0, 0, 0))
case (Azure, Android) => ProviderCount(1, PlatformCount(1, 0, 1), PlatformCount(0, 0, 0))
case (Fcm, Ios) => ProviderCount(1, PlatformCount(0, 0, 0), PlatformCount(1, 1, 0))
case (Fcm, Android) => ProviderCount(1, PlatformCount(0, 0, 0), PlatformCount(1, 0, 1))
case (Azure, Ios) => ProviderCount(total = 1, azure = PlatformCount(1, 1, 0))
case (Azure, Android) => ProviderCount(total = 1, azure = PlatformCount(1, 0, 1))
case (Fcm, Ios) => ProviderCount(total = 1, firebase = PlatformCount(1, 1, 0))
case (Fcm, Android) => ProviderCount(total = 1, firebase = PlatformCount(1, 0, 1))
case (Guardian, Ios) => ProviderCount(total = 1, guardian = PlatformCount(1, 1, 0))
case (Guardian, Android) => ProviderCount(total = 1, guardian = PlatformCount(1, 0, 1))
}

def combine(countsA: ProviderCount, countsB: ProviderCount): ProviderCount = ProviderCount(
total = countsA.total + countsB.total,
azure = PlatformCount.combine(countsA.azure, countsB.azure),
firebase = PlatformCount.combine(countsA.firebase, countsB.firebase)
firebase = PlatformCount.combine(countsA.firebase, countsB.firebase),
guardian = PlatformCount.combine(countsA.guardian, countsB.guardian)
)
implicit val jf = Json.format[ProviderCount]
implicit val providerCountJF: Format[ProviderCount] = new Format[ProviderCount] {
val defaultWrites = Json.writes[ProviderCount]
override def writes(providerCount: ProviderCount): JsValue = defaultWrites.writes(providerCount)
override def reads(json: JsValue): JsResult[ProviderCount] = json match {
case JsObject(attributes) => for {
total <- attributes("total").validate[Int]
azure <- attributes.get("azure").map(_.validate[PlatformCount]).getOrElse(JsSuccess(PlatformCount.empty))
firebase <- attributes.get("firebase").map(_.validate[PlatformCount]).getOrElse(JsSuccess(PlatformCount.empty))
guardian <- attributes.get("guardian").map(_.validate[PlatformCount]).getOrElse(JsSuccess(PlatformCount.empty))
} yield ProviderCount(
total = total,
azure = azure,
firebase = firebase,
guardian = guardian
)
case _ => JsError("Was expecting a JsObject in order to parse a ProviderCount")
}
}
}
Expand Up @@ -14,27 +14,34 @@ object DynamoConversion {
val eventMap = eventAggregationAv.getM.asScala
val providerMap = eventMap("provider").getM.asScala
EventAggregation(
platformCounts = platformFromAttributeValue(eventMap("platform")),
providerCounts = ProviderCount(providerMap("total").getN.toInt, platformFromAttributeValue(providerMap("azure")), platformFromAttributeValue(providerMap("firebase"))),
platformCounts = platformFromAttributeValue(eventMap.get("platform")),
providerCounts = ProviderCount(
total = providerMap("total").getN.toInt,
azure = platformFromAttributeValue(providerMap.get("azure")),
firebase = platformFromAttributeValue(providerMap.get("firebase")),
guardian = platformFromAttributeValue(providerMap.get("guardian"))
),
timing = eventMap("timing").getL.asScala.toList.map(av => {
val list = av.getL.asScala.toList
(list(0).getN.toInt, list(1).getN.toInt)
(list.head.getN.toInt, list(1).getN.toInt)
}).foldLeft((sentTime, List[(LocalDateTime, Int)]())) {
case ((lastTime, newList), (offset, count)) => {
case ((lastTime, newList), (offset, count)) =>
val nextTime = lastTime.plusSeconds(offset * 10)
(nextTime, (nextTime, count) :: newList)
}
}._2.toMap

)
}

private def platformFromAttributeValue(platformAv: AttributeValue): PlatformCount = {
val platformMap = platformAv.getM.asScala
PlatformCount(
total = platformMap("total").getN.toInt,
ios = platformMap("ios").getN.toInt,
android = platformMap("android").getN.toInt)
private def platformFromAttributeValue(platformAv: Option[AttributeValue]): PlatformCount = platformAv match {
case None => PlatformCount.empty
case Some(platformCount) =>
val platformMap = platformCount.getM.asScala
PlatformCount(
total = platformMap("total").getN.toInt,
ios = platformMap("ios").getN.toInt,
android = platformMap("android").getN.toInt
)
}

def toAttributeValue(eventAggregation: EventAggregation, sent: LocalDateTime): AttributeValue = {
Expand All @@ -49,7 +56,8 @@ object DynamoConversion {
"provider" -> new AttributeValue().withM(Map(
"total" -> new AttributeValue().withN(provider.total.toString),
"azure" -> toAttributeValue(provider.azure),
"firebase" -> toAttributeValue(provider.firebase)
"firebase" -> toAttributeValue(provider.firebase),
"guardian" -> toAttributeValue(provider.guardian)
).asJava),
"timing" -> new AttributeValue().withL(
eventAggregation.timing.toList
Expand Down