Commit e4416afb authored by Andreas Muttscheller's avatar Andreas Muttscheller

Add more documentation

parent 5ff49c97
......@@ -63,24 +63,45 @@ class SNSChangeSlackReporter {
def parseDelayInformation(newDelayInformation: SNSNewDelayInformation): Unit = {
Some(newDelayInformation.tti)
// eva must be correct
.filter(_.eva == newDelayInformation.eva)
// The train must depart in the future
.filter(!_.isHistory)
// Show only delays with more than 2 minutes
.filter(_.delayInMinutes > 2)
// Cross timetable information with all users
.cross(slackUserCache.get(Unit))
// Filter only users that haven't seen this delay or if the difference between the
// last delay shown is more than 2 minutes
.filter { case (tti, sr) =>
!sr.seenTimetableInformation.contains(tti.id) ||
// Only send messages when time difference is greater than 2 minutes
Math.abs(tti.delayInMinutes - sr.seenTimetableInformation(tti.id).delayInMinutes) > 2
}
// Flatten the list and add the delay registration information to it
.flatMap { case (tti, sr) =>
sr.delayRegistration.map(d => (tti, sr, d))
}
// Compare the train name of the delay with the given user input
// Regex works as well as prefixing the train name, e.g. RB.* or RB68 or RB 68
.filter { case (tti, _, d) => d.eva == tti.eva &&
(tti.train.matches(d.trainRegex) || tti.train.startsWith(d.trainRegex) || tti.train.replace(" ", "").startsWith(d.trainRegex)) }
// If the user wants only trains going in a specific direction, filter the results
.filter { case (tti, _, d) => d.direction.isEmpty || tti.pathTo.contains(d.direction.get) }
// Check if the planned departure time is in the given time range
.filter { case (tti, _, d) => d.from.isEmpty || d.until.isEmpty ||
(d.from.isDefined && d.until.isDefined &&
d.from.get.isBefore(LocalTime.from(tti.plannedTime)) && d.until.get.isAfter(LocalTime.from(tti.plannedTime))) }
// All checks have passed, send the delay information to the user
.foreach { case (tti, sr, _) =>
slackApiClient.postMessage(
sr.channel,
......
......@@ -10,18 +10,26 @@ import de.codecentric.amuttsch.bahndelayinfo.aws
import de.codecentric.amuttsch.bahndelayinfo.aws.sns.SNSNewDelayInformation
import de.codecentric.amuttsch.bahndelayinfo.fetcher.DBChangedTimetableFetcher
import de.codecentric.amuttsch.bahndelayinfo.models.{PlannedTimetableElement, TimetableInformation}
import de.codecentric.amuttsch.bahndelayinfo.utils.JsonSerializers
import org.json4s._
import org.json4s.native.JsonMethods._
import org.json4s.native.Serialization.write
import scala.util.{Failure, Success, Try}
object ScheduledChangesFetchService extends App {
new ScheduledChangesFetchService().handleRequest(null, null)
}
class ScheduledChangesFetchService {
def handleRequest(event: ScheduledEvent, context: Context): Unit = {
implicit val logger: Logger = Logger(this.getClass)
implicit val jsonFormats: DefaultFormats.type = DefaultFormats
implicit val jsonFormats: Formats = DefaultFormats +
JsonSerializers.NoEmptyStringSerializer +
JsonSerializers.LocalTimeSerializer +
JsonSerializers.LocalDateTimeSerializer
val snsClient = AmazonSNSClientBuilder.defaultClient()
val ddbClient = AmazonDynamoDBClientBuilder.standard.build
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment