@@ -20,9 +20,12 @@ final private class RelayCrowd(roomCrowd: RoomCrowd, mongo: Mongo)(using ex: Exe
20
20
21
21
import reactivemongo .api .bson .*
22
22
23
+ /* selects the last non-finished round of each active broadcast.
24
+ * or the last round that was finished less than 2 hours ago. */
23
25
def ongoingIds : Future [Set [RoomId ]] = for
24
26
tourColl <- mongo.relayTourColl
25
27
roundColl <- mongo.relayRoundColl
28
+ now = nowInstant
26
29
result <- tourColl
27
30
.aggregateWith[BSONDocument ](): framework =>
28
31
import framework .*
@@ -38,22 +41,27 @@ final private class RelayCrowd(roomCrowd: RoomCrowd, mongo: Mongo)(using ex: Exe
38
41
" pipeline" -> List (
39
42
BSONDocument (
40
43
" $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 )))
55
62
)
56
63
)
64
+ )
57
65
)
58
66
)
59
67
),
@@ -73,9 +81,11 @@ final private class RelayCrowd(roomCrowd: RoomCrowd, mongo: Mongo)(using ex: Exe
73
81
74
82
// couldn't make update.many work
75
83
def setMembers (all : Map [RoomId , Int ]): Future [Unit ] = mongo.relayRoundColl.flatMap: coll =>
84
+ val crowdAt = BSONDocument (" crowdAt" -> nowInstant)
76
85
all.toSeq.traverse_ { (id, crowd) =>
86
+ val set = BSONDocument (" crowd" -> crowd) ++ crowdAt
77
87
coll.update.one(
78
88
q = BSONDocument (" _id" -> id),
79
- u = BSONDocument (" $set" -> BSONDocument ( " crowd " -> crowd) )
89
+ u = BSONDocument (" $set" -> set )
80
90
)
81
91
}
0 commit comments