dedup and filter patrons - closes #2108

pull/1268/merge
Thibault Duplessis 2016-07-20 00:50:37 +02:00
parent 3a231c7617
commit 90164b6d1c
3 changed files with 17 additions and 4 deletions

View File

@ -33,7 +33,7 @@ object Plan extends LilaController {
}
private def renderIndex(email: Option[String], patron: Option[lila.plan.Patron])(implicit ctx: Context): Fu[Result] =
Env.plan.api.recentChargeUserIds(40) zip
Env.plan.api.recentChargeUserIds(50) zip
Env.plan.api.topPatronUserIds(120) map {
case (recentIds, bestIds) =>
Ok(html.plan.index(

View File

@ -203,7 +203,9 @@ final class PlanApi(
}
private val recentChargeUserIdsCache = AsyncCache[Int, List[User.ID]](
f = nb => chargeColl.primitive[User.ID]($empty, sort = $doc("date" -> -1), nb = nb, "userId"),
f = nb => chargeColl.primitive[User.ID](
$empty, sort = $doc("date" -> -1), nb = nb, "userId"
) flatMap filterUserIds,
timeToLive = 1 hour)
def recentChargeUserIds(nb: Int): Fu[List[User.ID]] = recentChargeUserIdsCache(nb)
@ -218,11 +220,19 @@ final class PlanApi(
Sort(Descending("total")),
Limit(nb))).map {
_.documents.flatMap { _.getAs[User.ID]("_id") }
},
} flatMap filterUserIds,
timeToLive = 1 hour)
def topPatronUserIds(nb: Int): Fu[List[User.ID]] = topPatronUserIdsCache(nb)
private def filterUserIds(ids: List[User.ID]): Fu[List[User.ID]] = {
val dedup = ids.distinct
UserRepo.filterByEnabled(dedup) map { enableds =>
val set = enableds.toSet
dedup filter set.contains
}
}
private def addCharge(charge: Charge): Funit =
chargeColl.insert(charge) >>
recentChargeUserIdsCache.clear >>

View File

@ -343,7 +343,10 @@ object UserRepo {
).map(~_)
def filterByEngine(userIds: List[String]): Fu[List[String]] =
coll.primitive[String]($inIds(userIds) ++ engineSelect(true), F.username)
coll.primitive[String]($inIds(userIds) ++ engineSelect(true), F.id)
def filterByEnabled(userIds: List[String]): Fu[List[String]] =
coll.primitive[String]($inIds(userIds) ++ enabledSelect, F.id)
def countEngines(userIds: List[String]): Fu[Int] =
coll.countSel($inIds(userIds) ++ engineSelect(true))