lila/modules/relay/src/main/SyncLog.scala

50 lines
1.1 KiB
Scala
Raw Normal View History

2017-09-29 17:14:43 -06:00
package lila.relay
import org.joda.time.DateTime
case class SyncLog(events: Vector[SyncLog.Event]) extends AnyVal {
2017-09-29 17:14:43 -06:00
def isOk = events.lastOption.exists(_.isOk)
2017-10-01 14:49:10 -06:00
def alwaysFails = events.sizeIs == SyncLog.historySize && events.forall(_.isKo)
2017-10-01 14:49:10 -06:00
def justTimedOut = events.lastOption.exists(_.isTimeout)
2017-10-01 14:49:10 -06:00
def updatedAt = events.lastOption.map(_.at)
2020-05-05 22:11:15 -06:00
def add(event: SyncLog.Event) =
copy(
2021-11-28 07:36:17 -07:00
events = {
if (events.sizeIs > SyncLog.historySize) events drop 1
else events
} :+ event
2020-05-05 22:11:15 -06:00
)
2017-09-29 17:14:43 -06:00
}
object SyncLog {
2017-09-30 17:34:07 -06:00
val historySize = 5
2018-09-06 10:32:35 -06:00
val empty = SyncLog(Vector.empty)
2017-09-29 17:14:43 -06:00
case class Event(
2017-10-04 23:59:35 -06:00
moves: Int,
error: Option[String],
2017-09-29 17:14:43 -06:00
at: DateTime
) {
2019-12-13 07:30:20 -07:00
def isOk = error.isEmpty
def isKo = error.nonEmpty
def isTimeout = error has SyncResult.Timeout.getMessage
}
2017-10-01 16:07:55 -06:00
2020-05-05 22:11:15 -06:00
def event(moves: Int, e: Option[Exception]) =
Event(
moves = moves,
error = e map {
case _: java.util.concurrent.TimeoutException => "Request timeout"
case e: Exception => e.getMessage take 100
},
at = DateTime.now
)
2017-09-29 17:14:43 -06:00
}