Skip to content

Commit

Permalink
scala tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Nov 8, 2024
1 parent 4a617cd commit 623f4cd
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions modules/insight/src/main/InsightIndexer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ final private class InsightIndexer(
)

def all(user: User): Funit =
workQueue {
storage.fetchLast(user.id).flatMap {
_.fold(fromScratch(user)) { e =>
computeFrom(user, e.date.plusSeconds(1))
}
}
}
workQueue:
storage
.fetchLast(user.id)
.flatMap:
_.fold(fromScratch(user)): e =>
computeFrom(user, e.date.plusSeconds(1))

def update(game: Game, userId: UserId, previous: InsightEntry): Funit =
povToEntry(game, userId, previous.provisional).flatMap {
Expand All @@ -39,11 +38,8 @@ final private class InsightIndexer(
}

private def fromScratch(user: User): Funit =
fetchFirstGame(user).flatMap {
_.so { g =>
computeFrom(user, g.createdAt)
}
}
fetchFirstGame(user).flatMapz: g =>
computeFrom(user, g.createdAt)

private def gameQuery(user: User) =
Query.user(user.id) ++
Expand All @@ -58,25 +54,23 @@ final private class InsightIndexer(
private def fetchFirstGame(user: User): Fu[Option[Game]] =
if user.count.rated == 0 then fuccess(none)
else
{
(user.count.rated >= maxGames).so(
(user.count.rated >= maxGames)
.so:
gameRepo.coll
.find(gameQuery(user))
.sort(Query.sortCreated)
.skip(maxGames - 1)
.one[Game](ReadPref.sec)
)
}.orElse(
gameRepo.coll
.find(gameQuery(user))
.sort(Query.sortChronological)
.one[Game](ReadPref.sec)
)
.orElse:
gameRepo.coll
.find(gameQuery(user))
.sort(Query.sortChronological)
.one[Game](ReadPref.sec)

private def computeFrom(user: User, from: Instant): Funit =
storage
.nbByPerf(user.id)
.flatMap { nbs =>
.flatMap: nbs =>
var nbByPerf = nbs
def toEntry(game: Game): Fu[Option[InsightEntry]] =
val nb = nbByPerf.getOrElse(game.perfKey, 0) + 1
Expand All @@ -94,4 +88,4 @@ final private class InsightIndexer(
.grouped(100.atMost(maxGames))
.map(storage.bulkInsert)
.runWith(Sink.ignore)
} void
.void

0 comments on commit 623f4cd

Please sign in to comment.