Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/controllers/Puzzle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ final class Puzzle(env: Env, apiC: => Api) extends LilaController(env):
val config = lila.puzzle.PuzzleActivity.Config(
user = me,
max = getIntAs[Max]("max").map(_.atLeast(1)),
before = getTimestamp("before")
before = getTimestamp("before"),
since = getTimestamp("since")
)
apiC.GlobalConcurrencyLimitPerIpAndUserOption(me.some)(env.puzzle.activity.stream(config))(jsToNdJson)
}
Expand Down
35 changes: 20 additions & 15 deletions modules/puzzle/src/main/PuzzleActivity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,25 @@ final class PuzzleActivity(

def stream(config: Config): Source[JsObject, ?] =
val perSecond = MaxPerSecond(20)

val baseQuery = $doc(PuzzleRound.BSONFields.user -> config.user.id)
val timeQueries = List(
config.before.map(before => $doc(PuzzleRound.BSONFields.date.$lt(before))),
config.since.map(since => $doc(PuzzleRound.BSONFields.date.$gte(since)))
).flatten
val finalQuery = baseQuery.++(timeQueries*)

Source.futureSource:
colls.round.map:
_.find(
$doc(PuzzleRound.BSONFields.user -> config.user.id) ++
config.before.so: before =>
$doc(PuzzleRound.BSONFields.date.$lt(before))
)
.sort($sort.desc(PuzzleRound.BSONFields.date))
.batchSize(perSecond.value)
.cursor[PuzzleRound](ReadPref.sec)
.documentSource(config.max.fold(Int.MaxValue)(_.value))
.grouped(perSecond.value)
.throttle(1, 1 second)
.mapAsync(1)(enrich(config))
.mapConcat(identity)
colls.round
.map(_.find(finalQuery))
.map(_.sort($sort.desc(PuzzleRound.BSONFields.date)))
.map(_.batchSize(perSecond.value))
.map(_.cursor[PuzzleRound](ReadPref.sec))
.map(_.documentSource(config.max.fold(Int.MaxValue)(_.value)))
.map(_.grouped(perSecond.value))
.map(_.throttle(1, 1 second))
.map(_.mapAsync(1)(enrich(config)))
.map(_.mapConcat(identity))

private def enrich(config: Config)(rounds: Seq[PuzzleRound]): Fu[Seq[JsObject]] =
colls.puzzle:
Expand All @@ -56,5 +60,6 @@ object PuzzleActivity:
case class Config(
user: User,
max: Option[Max],
before: Option[Instant]
before: Option[Instant],
since: Option[Instant]
)