forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncLog.scala
46 lines (34 loc) · 1.05 KB
/
SyncLog.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package lila.relay
import org.joda.time.DateTime
case class SyncLog(events: Vector[SyncLog.Event]) extends AnyVal {
def isOk = events.lastOption.exists(_.isOk)
def alwaysFails = events.sizeIs == SyncLog.historySize && events.forall(_.isKo)
def justTimedOut = events.lastOption.exists(_.isTimeout)
def updatedAt = events.lastOption.map(_.at)
def add(event: SyncLog.Event) =
copy(
events = events.take(SyncLog.historySize - 1) :+ event
)
}
object SyncLog {
val historySize = 5
val empty = SyncLog(Vector.empty)
case class Event(
moves: Int,
error: Option[String],
at: DateTime
) {
def isOk = error.isEmpty
def isKo = error.nonEmpty
def isTimeout = error has SyncResult.Timeout.getMessage
}
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
)
}