Skip to content

Commit d7248d4

Browse files
committed
RelayCrowd new ongoing round selector, set relay.crowdAt
1 parent 2ccce23 commit d7248d4

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

src/main/scala/RelayCrowd.scala

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ final private class RelayCrowd(roomCrowd: RoomCrowd, mongo: Mongo)(using ex: Exe
2020

2121
import reactivemongo.api.bson.*
2222

23+
/* selects the last non-finished round of each active broadcast.
24+
* or the last round that was finished less than 2 hours ago. */
2325
def ongoingIds: Future[Set[RoomId]] = for
2426
tourColl <- mongo.relayTourColl
2527
roundColl <- mongo.relayRoundColl
28+
now = nowInstant
2629
result <- tourColl
2730
.aggregateWith[BSONDocument](): framework =>
2831
import framework.*
@@ -38,22 +41,27 @@ final private class RelayCrowd(roomCrowd: RoomCrowd, mongo: Mongo)(using ex: Exe
3841
"pipeline" -> List(
3942
BSONDocument(
4043
"$match" -> BSONDocument(
41-
"$expr" -> BSONDocument(
42-
"$and" ->
43-
BSONArray(
44-
BSONDocument(
45-
BSONDocument("$eq" -> BSONArray("$tourId", "$$tourId")),
46-
"$or" -> BSONArray(
47-
BSONDocument("$exists" -> BSONArray("$finishedAt", false)),
48-
BSONDocument(
49-
"$gt" -> BSONArray(
50-
"$finishedAt",
51-
BSONDateTime(nowMillis - 1000 * 60 * 60 * 2) // 2 hours
52-
)
53-
)
54-
)
44+
"$expr" -> BSONDocument("$eq" -> BSONArray("$tourId", "$$tourId"))
45+
)
46+
),
47+
// the following matcher finds the round to monitor
48+
BSONDocument(
49+
"$match" -> BSONDocument(
50+
"$or" -> BSONArray(
51+
// either finished less than 2 hours ago
52+
BSONDocument("finishedAt" -> BSONDocument("$gt" -> now.minusHours(2))),
53+
// or unfinished, and
54+
BSONDocument(
55+
"finishedAt" -> BSONDocument("$exists" -> false),
56+
BSONDocument(
57+
"$or" -> BSONArray(
58+
// either started less than 8 hours ago
59+
BSONDocument("startedAt" -> BSONDocument("$gt" -> now.minusHours(8))),
60+
// or will start in the next 1 hour
61+
BSONDocument("startsAt" -> BSONDocument("$lt" -> now.plusHours(1)))
5562
)
5663
)
64+
)
5765
)
5866
)
5967
),
@@ -73,9 +81,11 @@ final private class RelayCrowd(roomCrowd: RoomCrowd, mongo: Mongo)(using ex: Exe
7381

7482
// couldn't make update.many work
7583
def setMembers(all: Map[RoomId, Int]): Future[Unit] = mongo.relayRoundColl.flatMap: coll =>
84+
val crowdAt = BSONDocument("crowdAt" -> nowInstant)
7685
all.toSeq.traverse_ { (id, crowd) =>
86+
val set = BSONDocument("crowd" -> crowd) ++ crowdAt
7787
coll.update.one(
7888
q = BSONDocument("_id" -> id),
79-
u = BSONDocument("$set" -> BSONDocument("crowd" -> crowd))
89+
u = BSONDocument("$set" -> set)
8090
)
8191
}

0 commit comments

Comments
 (0)