lila/app/controllers/Insight.scala

70 lines
2 KiB
Scala
Raw Normal View History

2015-11-23 23:42:08 -07:00
package controllers
import lila.api.Context
import lila.app._
2015-11-28 01:12:11 -07:00
import lila.insight.{ Metric, Dimension }
2015-11-26 08:17:35 -07:00
import play.api.mvc._
2015-11-23 23:42:08 -07:00
import views._
2015-11-26 21:05:59 -07:00
object Insight extends LilaController {
2015-11-23 23:42:08 -07:00
2015-11-26 21:05:59 -07:00
private def env = Env.insight
2015-11-23 23:42:08 -07:00
def refresh(username: String) = Open { implicit ctx =>
Accessible(username) { user =>
env.api indexAll user inject Ok
2015-11-23 23:42:08 -07:00
}
}
def index(username: String) = path(
username,
2015-11-28 01:12:11 -07:00
metric = Metric.MeanCpl.key,
2015-11-29 10:58:35 -07:00
dimension = Dimension.Perf.key,
filters = ""
)
2015-11-28 01:12:11 -07:00
def path(username: String, metric: String, dimension: String, filters: String) = Open { implicit ctx =>
2015-11-25 22:20:25 -07:00
Accessible(username) { user =>
2015-11-26 21:05:59 -07:00
import lila.insight.InsightApi.UserStatus._
2015-11-28 07:23:47 -07:00
env.api userStatus user flatMap {
case NoGame => Ok(html.insight.noGame(user)).fuccess
case Empty => Ok(html.insight.empty(user)).fuccess
2015-11-28 09:04:10 -07:00
case s => for {
cache <- env.api userCache user
2015-11-28 09:04:10 -07:00
prefId <- env.share getPrefId user
} yield Ok(html.insight.index(
u = user,
cache = cache,
2015-11-28 09:04:10 -07:00
prefId = prefId,
ui = env.jsonView.ui(cache.ecos),
2015-11-28 09:04:10 -07:00
question = env.jsonView.question(metric, dimension, filters),
stale = s == Stale
))
2015-11-25 22:20:25 -07:00
}
}
}
def json(username: String) = OpenBody(BodyParsers.parse.json) { implicit ctx =>
2015-11-26 21:05:59 -07:00
import lila.insight.JsonQuestion, JsonQuestion._
2015-11-25 23:24:06 -07:00
Accessible(username) { user =>
2015-11-26 08:17:35 -07:00
ctx.body.body.validate[JsonQuestion].fold(
err => BadRequest(jsonError(err.toString)).fuccess,
qJson => qJson.question.fold(BadRequest.fuccess) { q =>
2015-11-26 08:17:35 -07:00
env.api.ask(q, user) map
lila.insight.Chart.fromAnswer(Env.user.lightUserSync) map
2015-11-26 08:17:35 -07:00
env.jsonView.chart.apply map { Ok(_) }
2015-11-25 23:24:06 -07:00
}
2015-11-26 08:17:35 -07:00
)
2015-11-25 23:24:06 -07:00
}
}
2015-11-23 23:42:08 -07:00
private def Accessible(username: String)(f: lila.user.User => Fu[Result])(implicit ctx: Context) =
lila.user.UserRepo named username flatMap {
_.fold(notFound) { u =>
env.share.grant(u, ctx.me) flatMap {
_.fold(f(u), fuccess(Forbidden(html.insight.forbidden(u))))
}
2015-11-23 23:42:08 -07:00
}
}
}