Skip to content

Commit

Permalink
on analysis page, skip puzzle opening search if slow (cold cache)
Browse files Browse the repository at this point in the history
allows for faster first load for dev purposes
  • Loading branch information
ornicar committed Dec 25, 2024
1 parent 132db2f commit 8c1c49f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/controllers/Opening.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ final class Opening(env: Env) extends LilaController(env):
s"${routes.Opening.byKeyAndMoves(query.key, page.query.pgnUnderscored)}?r=1"
else
Ok.async:
page.query.exactOpening.so(env.puzzle.opening.getClosestTo).map { puzzle =>
page.query.exactOpening.so(env.puzzle.opening.getClosestTo(_)).map { puzzle =>
val puzzleKey = puzzle.map(_.fold(_.family.key.value, _.opening.key.value))
views.opening.ui.show(page, puzzleKey)
}
Expand Down
14 changes: 4 additions & 10 deletions modules/api/src/main/RoundApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,9 @@ final private[api] class RoundApi(
tourApi.gameView.analysis(pov.game),
pov.game.simulId.so(simulApi.find),
swissApi.gameView(pov),
ctx.me
.ifTrue(ctx.isMobileApi)
.so:
noteApi.get(pov.gameId, _)
,
ctx.me.ifTrue(ctx.isMobileApi).so(noteApi.get(pov.gameId, _)),
owner.so(forecastApi.loadForDisplay(pov)),
withFlags.puzzles.so(pov.game.opening.map(_.opening)).so(puzzleOpeningApi.getClosestTo),
withFlags.puzzles.so(pov.game.opening.map(_.opening)).so(puzzleOpeningApi.getClosestTo(_, true)),
bookmarkApi.exists(pov.game, ctx.me)
).mapN: (json, tour, simul, swiss, note, fco, puzzleOpening, bookmarked) =>
(
Expand All @@ -160,17 +156,15 @@ final private[api] class RoundApi(
owner
.so(forecastApi.loadForDisplay(pov))
.map: fco =>
withForecast(pov, owner, fco) {
withTree(pov, analysis = none, initialFen, ExportOptions(opening = true)) {
withForecast(pov, owner, fco):
withTree(pov, analysis = none, initialFen, ExportOptions(opening = true)):
jsonView.userAnalysisJson(
pov,
pref,
initialFen,
orientation,
owner = owner
)
}
}
.flatMap(externalEngineApi.withExternalEngines)

private def withTree(
Expand Down
10 changes: 7 additions & 3 deletions modules/puzzle/src/main/PuzzleOpening.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ final class PuzzleOpeningApi(
gameRepo: lila.core.game.GameRepo,
cacheApi: CacheApi,
mongoCache: MongoCache.Api
)(using Executor, akka.stream.Materializer):
)(using Executor, akka.stream.Materializer, Scheduler):
import BsonHandlers.given
import SimpleOpening.*
import PuzzleOpening.*
Expand Down Expand Up @@ -94,10 +94,11 @@ final class PuzzleOpeningApi(
}

def getClosestTo(
opening: Opening
opening: Opening,
quickOrNone: Boolean = false
): Fu[Option[Either[PuzzleOpening.FamilyWithCount, PuzzleOpening.WithCount]]] =
SimpleOpening(opening).so: lilaOp =>
collection.map: coll =>
(if quickOrNone then collectionQuickly else collection.map(some)).mapz: coll =>
coll.openingMap
.get(lilaOp.key)
.map(Right.apply)
Expand All @@ -112,6 +113,9 @@ final class PuzzleOpeningApi(
def collection: Fu[PuzzleOpeningCollection] =
collectionCache.get {}

def collectionQuickly: Fu[Option[PuzzleOpeningCollection]] =
collection.map(some).withTimeoutDefault(20.millis, none)

def count(key: Either[LilaOpeningFamily.Key, SimpleOpening.Key]): Fu[Int] =
collection.dmap: coll =>
key.fold(f => coll.familyMap.get(f).so(_.count), o => coll.openingMap.get(o).so(_.count))
Expand Down

0 comments on commit 8c1c49f

Please sign in to comment.