Skip to content

Commit d694e00

Browse files
committed
WIP needs to be refactored. not well architected.
1 parent 06f553c commit d694e00

1 file changed

Lines changed: 35 additions & 13 deletions

File tree

football/src/main/scala/com/gu/mobile/notifications/football/lib/SyntheticMatchEventGenerator.scala

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
package com.gu.mobile.notifications.football.lib
22

3+
4+
import com.gu.mobile.notifications.football.Logging
35
import com.gu.mobile.notifications.football.models.FootballMatchEvent
46
import com.gu.mobile.notifications.football.notificationbuilders.MatchStatusLiveActivityPayloadBuilder
57

68
import java.util.UUID
79
import pa.{MatchDay, MatchEvent}
810

9-
import java.time.{Instant, ZoneId, ZonedDateTime}
11+
import java.time.ZonedDateTime
12+
import scala.concurrent.duration._
13+
import scala.concurrent.Await
14+
import scala.concurrent.ExecutionContext.Implicits.global
15+
import scala.util.control.NonFatal
1016

1117
// Synthetic events create a timeline event for a match status change, that can be processed by the EventConsumer
1218
// and transformed into a NotificationPayload and/or LiveActivityPayload for broadcasting.
1319

14-
class SyntheticMatchEventGenerator(getCurrentTime: () => ZonedDateTime) {
20+
class SyntheticMatchEventGenerator(getCurrentTime: () => ZonedDateTime, matchStateDiffer: DynamoMatchStateDiffer) extends Logging {
1521

1622
def generate(events: List[MatchEvent], id: String, matchDay: MatchDay): List[MatchEvent] = {
1723
// Live activity synthetic events are appended at the end, but when they are first generated, no other timeline events exist and duplicate events are filtered so this should not matter.
@@ -196,17 +202,33 @@ class SyntheticMatchEventGenerator(getCurrentTime: () => ZonedDateTime) {
196202
articleId = None
197203
)
198204

199-
// todo clean up
200-
println(s"footballMatchState: $footballMatchState")
201-
202-
// todo this will impact a few of the test set ups
203-
val isFootballMatchStateDifferent: Boolean = false// call to DataStore with last match state sent, TRUE if diff
204-
205-
if (isFootballMatchStateDifferent) Some(emptyMatchEvent.copy(
206-
id = Some(UUID.nameUUIDFromBytes(s"football-match/${matchDay.id}/state-change/${latestEvent.eventTime}".getBytes).toString),
207-
eventType = "state-change"
208-
))
209-
else None
205+
// todo block on the async DynamoDB check but revisit making this async in future;
206+
// this will impact a few of the test set ups
207+
val isFootballMatchStateIdentical: Boolean =
208+
try {
209+
Await.result(matchStateDiffer.isIdentical(matchDay.id, footballMatchState), 5.seconds)
210+
} catch {
211+
case NonFatal(exception) =>
212+
logger.error(s"Error checking for identical football match state: ${exception.getMessage}")
213+
true // assume identical if we can't check
214+
}
215+
216+
if (!isFootballMatchStateIdentical) {
217+
// Only emit the state-change event if we successfully persist the new state, otherwise try again next polling cycle.
218+
try {
219+
Await.result(matchStateDiffer.updateState(matchDay.id, footballMatchState), 5.seconds)
220+
221+
Some(emptyMatchEvent.copy(
222+
id = Some(UUID.nameUUIDFromBytes(s"football-match/${matchDay.id}/state-change/${latestEvent.eventTime}".getBytes).toString),
223+
eventType = "state-change"
224+
))
225+
} catch {
226+
case NonFatal(exception) =>
227+
logger.error(s"Error updating football match state, not emitting state-change event: ${exception.getMessage}")
228+
None
229+
}
230+
}
231+
else None
210232
}
211233

212234
case Nil => None

0 commit comments

Comments
 (0)