@@ -6,15 +6,19 @@ import java.util.concurrent.TimeUnit
66import com .amazonaws .regions .Regions
77import com .amazonaws .services .dynamodbv2 .{AmazonDynamoDBAsync , AmazonDynamoDBAsyncClientBuilder }
88import com .gu .contentapi .client .GuardianContentClient
9- import com .gu .mobile .notifications .football .lib .{ArticleSearcher , DynamoDistinctCheck , EventConsumer , EventFilter , FootballData , NotificationHttpProvider , NotificationSender , NotificationsApiClient , PaFootballClient , SyntheticMatchEventGenerator }
10- import com .gu .mobile .notifications .football .notificationbuilders .MatchStatusNotificationBuilder
9+ import com .gu .mobile .notifications .football .lib .{ArticleSearcher , DynamoDistinctCheck , EventConsumer , LiveActivityEventConsumer , EventFilter , FootballData , LiveActivityPusher , NotificationHttpProvider , NotificationSender , NotificationsApiClient , PaFootballClient , SyntheticMatchEventGenerator }
10+ import com .gu .mobile .notifications .football .notificationbuilders .{ MatchStatusLiveActivityPayloadBuilder , MatchStatusNotificationBuilder }
1111import play .api .libs .json .Json
1212
1313import scala .concurrent .ExecutionContext .Implicits .global
1414import scala .concurrent .duration .Duration
1515import scala .concurrent .{Await , TimeoutException }
1616import scala .io .Source
1717import scala .util .Failure
18+ import com .gu .mobile .notifications .client .models .NotificationPayload
19+ import com .gu .mobile .notifications .client .models .liveActitivites .LiveActivityPayload
20+ import com .gu .mobile .notifications .football .models .MatchDataWithArticle
21+ import scala .concurrent .Future
1822
1923object Lambda extends Logging {
2024
@@ -48,19 +52,18 @@ object Lambda extends Logging {
4852
4953 val apiClient = new NotificationsApiClient (configuration)
5054
51- lazy val notificationSender = new NotificationSender (apiClient)
52-
53- lazy val matchStatusNotificationBuilder = new MatchStatusNotificationBuilder (configuration.mapiHost)
54-
55- lazy val eventConsumer = new EventConsumer (matchStatusNotificationBuilder)
55+ lazy val footballData = new FootballData (paFootballClient, syntheticMatchEventGenerator)
5656
57- lazy val distinctCheck = new DynamoDistinctCheck (dynamoDBClient, tableName )
57+ lazy val articleSearcher = new ArticleSearcher (capiClient )
5858
59- lazy val eventFilter = new EventFilter (distinctCheck )
59+ lazy val notificationHandler = new NotificationHandler (configuration, apiClient, dynamoDBClient, tableName )
6060
61- lazy val footballData = new FootballData (paFootballClient, syntheticMatchEventGenerator )
61+ lazy val liveActivityHandler = new LiveActivityHandler (configuration, dynamoDBClient, tableName )
6262
63- lazy val articleSearcher = new ArticleSearcher (capiClient)
63+ // live activities //
64+ lazy val liveActivityPusher = new LiveActivityPusher ()
65+ lazy val matchStatusLiveActivityPayloadBuilder = new MatchStatusLiveActivityPayloadBuilder (configuration.mapiHost)
66+ lazy val liveActivityEventConsumer = new LiveActivityEventConsumer (matchStatusLiveActivityPayloadBuilder)
6467
6568 def getZonedDateTime (): ZonedDateTime = {
6669 val zonedDateTime = if (configuration.stage == " CODE" ) {
@@ -87,14 +90,16 @@ object Lambda extends Logging {
8790
8891 logContainer()
8992
90- val processing = footballData.pollFootballData(getZonedDateTime())
91- .flatMap(articleSearcher.tryToMatchWithCapiArticle)
92- .map(_.flatMap(eventConsumer.eventsToNotifications))
93- .flatMap(eventFilter.filterNotifications)
94- .flatMap(notificationSender.sendNotifications)
93+ val articlesMatches = for {
94+ footballDataResult <- footballData.pollFootballData(getZonedDateTime())
95+ articleMatches <- articleSearcher.tryToMatchWithCapiArticle(footballDataResult)
96+ } yield articleMatches
97+
98+ val notificationsProcessing = articlesMatches.flatMap(notificationHandler.process)
99+ val liveActivitiesProcessing = articlesMatches.flatMap(liveActivityHandler.process)
95100
96101 // we're in a lambda so we do need to block the main thread until processing is finished
97- val result = Await .ready(processing , Duration (40 , TimeUnit .SECONDS ))
102+ val result = Await .ready(Future .sequence( List (notificationsProcessing, liveActivitiesProcessing)) , Duration (40 , TimeUnit .SECONDS ))
98103
99104 result.value match {
100105 case Some (Failure (e : TimeoutException )) => logger.error(" Task timed out" , e)
@@ -116,3 +121,50 @@ object Lambda extends Logging {
116121 }
117122
118123}
124+
125+ class NotificationHandler (configuration : Configuration , apiClient : NotificationsApiClient , dynamoDBClient : AmazonDynamoDBAsync , tableName : String ) extends Logging {
126+
127+ lazy val notificationSender = new NotificationSender (apiClient)
128+
129+ lazy val matchStatusNotificationBuilder = new MatchStatusNotificationBuilder (configuration.mapiHost)
130+
131+ lazy val eventConsumer = new EventConsumer (matchStatusNotificationBuilder)
132+
133+ lazy val distinctCheck = new DynamoDistinctCheck [NotificationPayload ](dynamoDBClient, tableName)
134+
135+ lazy val eventFilter = new EventFilter [NotificationPayload ](distinctCheck)
136+
137+ def process (rawEvents : List [MatchDataWithArticle ]): Future [Unit ] = {
138+ val notifications = rawEvents.flatMap(eventConsumer.eventsToNotifications)
139+ for {
140+ filteredNotifications <- eventFilter.filterNotifications(notifications)
141+ result <- notificationSender.sendNotifications(filteredNotifications)
142+ } yield result
143+ }
144+ }
145+
146+ class LiveActivityHandler (configuration : Configuration , dynamoDBClient : AmazonDynamoDBAsync , tableName : String ) extends Logging {
147+
148+ lazy val liveActivityPusher = new LiveActivityPusher ()
149+
150+ lazy val matchStatusLiveActivityPayloadBuilder = new MatchStatusLiveActivityPayloadBuilder (configuration.mapiHost)
151+
152+ lazy val liveActivityEventConsumer = new LiveActivityEventConsumer (matchStatusLiveActivityPayloadBuilder)
153+
154+ lazy val distinctCheck = new DynamoDistinctCheck [LiveActivityPayload ](dynamoDBClient, tableName)
155+
156+ lazy val eventFilter = new EventFilter [LiveActivityPayload ](distinctCheck)
157+
158+ def process (rawEvents : List [MatchDataWithArticle ]): Future [Unit ] = {
159+ val liveActivities = rawEvents.flatMap(liveActivityEventConsumer.eventsToLiveActivityPayload)
160+
161+ for {
162+ // filteredLiveActivities <- eventFilter.filterNotifications(liveActivities)
163+ result <- if (configuration.stage != " PROD" ) {
164+ liveActivityPusher.pushEvents(liveActivities)
165+ } else {
166+ Future .successful(())
167+ }
168+ } yield result
169+ }
170+ }
0 commit comments