-
Notifications
You must be signed in to change notification settings - Fork 6
VAR reversals 2: add football match diffing to generate a synth state change event #1857
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
Draft
vlbee
wants to merge
7
commits into
live/var1-gsi-setup
Choose a base branch
from
live/var2-addSynthEvent
base: live/var1-gsi-setup
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+514
−87
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8c7b20b
Add DynamoPayloadStateCheck - spec generated with copilot
vlbee c5b1275
Add state change synthetic event to generator
vlbee 46ac5da
Add state change event to def fromPaMatchEvent
vlbee d62ae5d
Wire up new state change check in FootballData
vlbee c243fee
Prevent EventConsumer from processing state-change events
vlbee ac9f9a9
Tidy synth stateChangeEvent
vlbee f020532
Refactor bug in isFootballMatchStateIdentical
vlbee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...all/src/main/scala/com/gu/mobile/notifications/football/lib/DynamoPayloadStateCheck.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package com.gu.mobile.notifications.football.lib | ||
|
|
||
| import com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync | ||
| import com.gu.mobile.notifications.client.models.liveActitivites.{FootballMatchContentState, LiveActivityPayload} | ||
| import com.gu.mobile.notifications.football.Logging | ||
| import org.scanamo.{ScanamoAsync, Table} | ||
| import play.api.libs.json.Json | ||
|
|
||
| import scala.concurrent.{ExecutionContext, Future} | ||
|
|
||
| // This is used to pre-diff the state in FootballData fetcher to determine if state-change synthetic event should be generated. | ||
| // It is not used to determine if a notification should be sent, that is handled by DynamoDistinctCheck. | ||
| // Both classes read the same table data. | ||
| class DynamoPayloadStateCheck(client: AmazonDynamoDBAsync, tableName: String) extends Logging { | ||
|
|
||
| def isMatchStateIdentical(id: String, state: FootballMatchContentState)(implicit ec: ExecutionContext): Future[Boolean] = { | ||
| import org.scanamo.generic.auto._ | ||
| import org.scanamo.syntax._ | ||
|
|
||
| val scanamoAsync: ScanamoAsync = ScanamoAsync(client) | ||
| val payloadTable = Table[DynamoMatchLiveActivity](tableName) | ||
| val lastPayloadIndex = payloadTable.index("lastPayload-index") // only live activities payload table has an index. | ||
|
|
||
| val gsiQuery = lastPayloadIndex.descending.limit(1).query("liveActivityID" -> id) | ||
|
|
||
| scanamoAsync.exec(gsiQuery).map { rows => | ||
| isIdenticalToLatest(rows.collect { case Right(row) => row }, state) | ||
| } recover { | ||
| case e => | ||
| logger.error(s"Failure while checking for dynamodb GSI for match state $tableName: ${e.getMessage}.") | ||
| false // todo should this be true - do we want to generate a synthetic state-change event if we can't check the latest state? | ||
| } | ||
| } | ||
|
|
||
| private[lib] def isIdenticalToLatest(rows: List[DynamoMatchLiveActivity], state: FootballMatchContentState): Boolean = | ||
| rows.headOption | ||
| .flatMap(row => footballStateFromPayload(row.payload)) | ||
| .forall(latest => ignoringArticleUrl(latest, state)) | ||
|
|
||
| // articleUrl is only added to the payload after isMatchStateIdentical is called, so we must ignore it. | ||
| private def ignoringArticleUrl(a: FootballMatchContentState, b: FootballMatchContentState): Boolean = | ||
| a.copy(articleUrl = None, currentMinute = None) == b.copy(articleUrl = None, currentMinute = None) // these are calculated outside of state generation | ||
|
|
||
| // The stored `payload` column is the JSON-serialised LiveActivityPayload; the match state we diff | ||
| // against is its `broadcastContentStateData` subfield. | ||
| private[lib] def footballStateFromPayload(payloadJson: String): Option[FootballMatchContentState] = | ||
| Json.parse(payloadJson).as[LiveActivityPayload].broadcastContentStateData.collect { | ||
| case footballState: FootballMatchContentState => footballState | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,19 @@ | ||
| package com.gu.mobile.notifications.football.lib | ||
|
|
||
| import com.gu.mobile.notifications.client.models.liveActitivites.LiveActivityPayload | ||
|
|
||
| import java.time.{LocalDate, LocalTime, ZonedDateTime} | ||
| import com.gu.mobile.notifications.football.{Configuration, Logging} | ||
| import com.gu.mobile.notifications.football.models.RawMatchData | ||
| import org.joda.time.DateTime | ||
| import pa.MatchDay | ||
| import com.gu.mobile.notifications.football.Logging | ||
| import com.gu.mobile.notifications.football.models.{FootballMatchEvent, RawMatchData} | ||
| import com.gu.mobile.notifications.football.notificationbuilders.MatchStatusLiveActivityPayloadBuilder | ||
| import pa.{MatchDay, MatchEvent} | ||
| import play.api.libs.json.{Format, Json} | ||
|
|
||
| import scala.concurrent.ExecutionContext.Implicits.global | ||
| import scala.concurrent.Future | ||
| import scala.util.control.NonFatal | ||
| import scala.util.{Failure, Success, Try} | ||
|
|
||
| case class EndedMatch(matchId: String, startTime: DateTime) | ||
|
|
||
| case class PACompetition( | ||
| id: String, | ||
| tag: String, | ||
|
|
@@ -31,6 +31,7 @@ class FootballData( | |
| paClient: PaFootballClient, | ||
| syntheticEvents: SyntheticMatchEventGenerator, | ||
| competitionsDataStore: S3DataStore[PACompetition], | ||
| payloadStateCheck: DynamoPayloadStateCheck, | ||
| stage: String, | ||
| ) extends Logging { | ||
|
|
||
|
|
@@ -169,15 +170,47 @@ class FootballData( | |
| } | ||
| } | ||
|
|
||
| private def processMatch(matchDay: MatchDay): Future[Option[RawMatchData]] = { | ||
| private def appendSyntheticEvents(matchDay: MatchDay, events: List[MatchEvent], stateChange: Boolean)(syntheticMatchEventGenerator: SyntheticMatchEventGenerator): Future[(MatchDay, List[MatchEvent])] = { | ||
| val eventsWithSyntheticEvents = syntheticMatchEventGenerator.generate(events, matchDay.id, matchDay, stateChange ) | ||
| Future.successful((matchDay, eventsWithSyntheticEvents)) | ||
| } | ||
|
|
||
| // Process individual match | ||
| private[lib] def processMatch(matchDay: MatchDay): Future[Option[RawMatchData]] = { | ||
| val matchData = for { | ||
| (matchDay, events) <- paClient.eventsForMatch(matchDay, syntheticEvents) | ||
| } yield Some(RawMatchData(matchDay, events)) | ||
| // fetch current PA events timeline for match | ||
| (_, events) <- paClient.eventsForMatch(matchDay) | ||
| // check if the current match state is identical to the last known payload state sent. | ||
| stateChange <- isFootballMatchStateIdentical(matchDay, events).map(!_) | ||
| // add synthetic events to the PA events timeline, if any are generated. | ||
| // a state change synth even will be generated for every update, but we filter out the superfluous ones in the EventConsumer. | ||
| (_, eventsWithSyntheticEvents) <- appendSyntheticEvents(matchDay, events, stateChange)(syntheticEvents) | ||
| } yield Some(RawMatchData(matchDay, eventsWithSyntheticEvents)) | ||
|
|
||
| matchData.recover { case NonFatal(exception) => | ||
| logger.error(s"Failed to process match ${matchDay.id}: ${exception.getMessage}", exception) | ||
| None | ||
| } | ||
| } | ||
|
|
||
| private[lib] def isFootballMatchStateIdentical(matchDay: MatchDay, events: List[pa.MatchEvent]): Future[Boolean] = { | ||
| val payloadBuilder = new MatchStatusLiveActivityPayloadBuilder() | ||
| val toFootballEvent = FootballMatchEvent.fromPaMatchEvent(matchDay.homeTeam, matchDay.awayTeam) _ | ||
|
|
||
| if (events.isEmpty) Future.successful(true) // no stage change before a match has started. | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. verify this behaviour withing 2-hr create channel window |
||
| else { | ||
| val footballMatchState = payloadBuilder.buildFootballContentState( | ||
| currentMinute = None, | ||
| matchInfo = matchDay, | ||
| allEvents = events.flatMap(toFootballEvent), | ||
| articleId = None, | ||
| ) | ||
| payloadStateCheck | ||
| .isMatchStateIdentical(matchDay.id, footballMatchState) | ||
| .recover { case NonFatal(exception) => | ||
| logger.error(s"Error checking for for identical football match state ${matchDay.id}: ${exception.getMessage}") | ||
| true // assume identical // todo confirm this behaviour is desired. | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this what should happen? |
||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it safer to compare a checksum?