lila/modules/insight/src/main/InsightApi.scala

88 lines
2.5 KiB
Scala
Raw Normal View History

2015-11-26 21:05:59 -07:00
package lila.insight
2015-11-24 23:49:13 -07:00
import lila.game.{ Game, GameRepo, Pov }
2015-11-25 03:27:01 -07:00
import lila.user.User
2015-11-26 21:05:59 -07:00
final class InsightApi(
2015-11-26 12:12:34 -07:00
storage: Storage,
pipeline: AggregationPipeline,
2019-12-03 12:15:15 -07:00
userCacheApi: UserCacheApi,
gameRepo: GameRepo,
indexer: Indexer
)(implicit ec: scala.concurrent.ExecutionContext) {
2015-11-25 03:27:01 -07:00
2015-11-26 21:05:59 -07:00
import InsightApi._
2015-11-25 03:27:01 -07:00
2020-05-05 22:11:15 -06:00
def userCache(user: User): Fu[UserCache] =
userCacheApi find user.id flatMap {
case Some(c) => fuccess(c)
case None =>
for {
count <- storage count user.id
ecos <- storage ecos user.id
2020-05-27 21:08:37 -06:00
c = UserCache.make(user.id, count, ecos)
2020-05-05 22:11:15 -06:00
_ <- userCacheApi save c
} yield c
}
2015-11-28 07:23:47 -07:00
2015-11-26 12:12:34 -07:00
def ask[X](question: Question[X], user: User): Fu[Answer[X]] =
2019-12-13 07:30:20 -07:00
pipeline
.aggregate(question, user)
.flatMap { aggDocs =>
val clusters = AggregationClusters(question, aggDocs)
val gameIds = scala.util.Random.shuffle(clusters.flatMap(_.gameIds)) take 4
gameRepo.userPovsByGameIds(gameIds, user) map { povs =>
Answer(question, clusters, povs)
}
2015-11-29 05:59:11 -07:00
}
2019-12-13 07:30:20 -07:00
.monSuccess(_.insight.request)
2015-11-26 12:12:34 -07:00
def userStatus(user: User): Fu[UserStatus] =
2019-12-03 12:15:15 -07:00
gameRepo lastFinishedRatedNotFromPosition user flatMap {
2015-11-26 12:12:34 -07:00
case None => fuccess(UserStatus.NoGame)
2019-12-13 07:30:20 -07:00
case Some(game) =>
storage fetchLast user.id map {
case None => UserStatus.Empty
case Some(entry) if entry.date isBefore game.createdAt => UserStatus.Stale
case _ => UserStatus.Fresh
}
}
2020-05-28 14:38:23 -06:00
def ensureLatest(userId: User.ID): Funit =
2020-05-27 21:08:37 -06:00
userCacheApi version userId flatMap {
2020-05-28 14:38:23 -06:00
case Some(v) if v == latestVersion => funit
case _ => storage.removeAll(userId) >> indexAll(userId)
2020-05-27 21:08:37 -06:00
}
def indexAll(userId: User.ID) =
2019-12-13 07:30:20 -07:00
indexer
2020-05-27 21:08:37 -06:00
.all(userId)
2019-12-10 14:01:18 -07:00
.monSuccess(_.insight.index) >>
2020-05-27 21:08:37 -06:00
userCacheApi.remove(userId)
2019-12-13 07:30:20 -07:00
def updateGame(g: Game) =
Pov(g)
.map { pov =>
pov.player.userId ?? { userId =>
storage find Entry.povToId(pov) flatMap {
_ ?? { old =>
indexer.update(g, userId, old)
}
}
}
}
2019-12-13 07:30:20 -07:00
.sequenceFu
.void
2015-11-24 23:49:13 -07:00
}
2015-11-26 12:12:34 -07:00
2015-11-26 21:05:59 -07:00
object InsightApi {
2015-11-26 12:12:34 -07:00
sealed trait UserStatus
object UserStatus {
case object NoGame extends UserStatus
2019-12-13 07:30:20 -07:00
case object Empty extends UserStatus
case object Stale extends UserStatus
case object Fresh extends UserStatus
2015-11-26 12:12:34 -07:00
}
}