cache user elo chart
This commit is contained in:
parent
89414db6e9
commit
acde7095b3
|
@ -32,6 +32,7 @@ final class Env(
|
|||
relationApi = Env.relation.api,
|
||||
gameCached = Env.game.cached,
|
||||
postApi = Env.forum.postApi,
|
||||
getEloChart = Env.user.eloChart,
|
||||
getRank = Env.user.ranking.get) _
|
||||
|
||||
if (config getBoolean "ai.stress") {
|
||||
|
|
|
@ -38,6 +38,7 @@ object UserInfo {
|
|||
relationApi: RelationApi,
|
||||
gameCached: lila.game.Cached,
|
||||
postApi: PostApi,
|
||||
getEloChart: User => Fu[Option[EloChart]],
|
||||
getRank: String ⇒ Fu[Option[Int]])(user: User, ctx: Context): Fu[UserInfo] =
|
||||
(getRank(user.id) flatMap {
|
||||
_ ?? { rank ⇒ countUsers() map { nb ⇒ (rank -> nb).some } }
|
||||
|
@ -49,7 +50,7 @@ object UserInfo {
|
|||
gameCached.confrontation(me, user) map (_.some filterNot (_.empty))
|
||||
}) zip
|
||||
(bookmarkApi countByUser user) zip
|
||||
EloChart(user) zip
|
||||
getEloChart(user) zip
|
||||
relationApi.nbFollowing(user.id) zip
|
||||
relationApi.nbFollowers(user.id) zip
|
||||
((ctx.me ?? Granter(_.UserSpy)) ?? {
|
||||
|
|
|
@ -8,17 +8,21 @@ import lila.db.api.$count
|
|||
import lila.memo.AsyncCache
|
||||
import tube.userTube
|
||||
|
||||
final class Cached(ttl: Duration) {
|
||||
final class Cached(nbTtl: Duration, eloChartTtl: Duration) {
|
||||
|
||||
val username = AsyncCache(UserRepo.usernameById, maxCapacity = 50000)
|
||||
|
||||
def usernameOrAnonymous(id: String): Fu[String] =
|
||||
def usernameOrAnonymous(id: String): Fu[String] =
|
||||
username(id) map (_ | User.anonymous)
|
||||
|
||||
def usernameOrAnonymous(id: Option[String]): Fu[String] =
|
||||
def usernameOrAnonymous(id: Option[String]): Fu[String] =
|
||||
id.fold(fuccess(User.anonymous))(usernameOrAnonymous)
|
||||
|
||||
val count = AsyncCache((o: JsObject) ⇒ $count(o), timeToLive = ttl)
|
||||
val count = AsyncCache((o: JsObject) ⇒ $count(o), timeToLive = nbTtl)
|
||||
|
||||
def countEnabled: Fu[Int] = count(UserRepo.enabledSelect)
|
||||
|
||||
val eloChart = AsyncCache(EloChart.apply,
|
||||
maxCapacity = 5000,
|
||||
timeToLive = eloChartTtl)
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import org.joda.time.DateTime
|
|||
import org.joda.time.format.{ DateTimeFormat, DateTimeFormatter }
|
||||
import play.api.libs.json.Json
|
||||
|
||||
final class EloChart(rawElos: List[(Int, Int, Option[Int])]) {
|
||||
final class EloChart private (rawElos: List[(Int, Int, Option[Int])]) {
|
||||
|
||||
private val points = 100
|
||||
private val eloMedian = 30
|
||||
|
@ -55,14 +55,14 @@ final class EloChart(rawElos: List[(Int, Int, Option[Int])]) {
|
|||
|
||||
object EloChart {
|
||||
|
||||
val columns = Json.arr(
|
||||
private val columns = Json.arr(
|
||||
Json.arr("string", "Game"),
|
||||
Json.arr("number", "Elo"),
|
||||
Json.arr("number", "Opponent Elo"),
|
||||
Json.arr("number", "Average")
|
||||
)
|
||||
|
||||
def apply(user: User): Fu[Option[EloChart]] =
|
||||
private[user] def apply(user: User): Fu[Option[EloChart]] =
|
||||
HistoryRepo userElos user.id map { elos ⇒
|
||||
(elos.size > 1) option {
|
||||
new EloChart((user.createdAt.getSeconds.toInt, User.STARTING_ELO, None) :: elos.toList)
|
||||
|
|
|
@ -16,6 +16,7 @@ final class Env(
|
|||
val PaginatorMaxPerPage = config getInt "paginator.max_per_page"
|
||||
val EloUpdaterFloor = config getInt "elo_updater.floor"
|
||||
val CachedNbTtl = config duration "cached.nb.ttl"
|
||||
val CachedEloChartTtl = config duration "cached.elo_chart.ttl"
|
||||
val OnlineTtl = config duration "online.ttl"
|
||||
val RankingTtl = config duration "ranking.ttl"
|
||||
val CollectionUser = config getString "collection.user"
|
||||
|
@ -37,6 +38,8 @@ final class Env(
|
|||
|
||||
lazy val ranking = new Ranking(ttl = RankingTtl)
|
||||
|
||||
def eloChart = cached.eloChart.apply _
|
||||
|
||||
val forms = DataForm
|
||||
|
||||
def usernameOption(id: String): Fu[Option[String]] = cached username id
|
||||
|
@ -68,7 +71,9 @@ final class Env(
|
|||
}
|
||||
}
|
||||
|
||||
private lazy val cached = new Cached(ttl = CachedNbTtl)
|
||||
private lazy val cached = new Cached(
|
||||
nbTtl = CachedNbTtl,
|
||||
eloChartTtl = CachedEloChartTtl)
|
||||
}
|
||||
|
||||
object Env {
|
||||
|
|
Loading…
Reference in a new issue