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 =>
|
2015-11-28 03:48:27 -07:00
|
|
|
env.api indexAll user inject Ok
|
2015-11-23 23:42:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-14 08:34:07 -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,
|
2017-02-14 08:34:07 -07:00
|
|
|
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
|
2017-02-14 08:34:07 -07:00
|
|
|
case Empty => Ok(html.insight.empty(user)).fuccess
|
2015-11-28 09:04:10 -07:00
|
|
|
case s => for {
|
2015-11-28 21:50:54 -07:00
|
|
|
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,
|
2015-11-28 21:50:54 -07:00
|
|
|
cache = cache,
|
2015-11-28 09:04:10 -07:00
|
|
|
prefId = prefId,
|
2015-11-28 21:50:54 -07:00
|
|
|
ui = env.jsonView.ui(cache.ecos),
|
2015-11-28 09:04:10 -07:00
|
|
|
question = env.jsonView.question(metric, dimension, filters),
|
2017-02-14 08:34:07 -07:00
|
|
|
stale = s == Stale
|
|
|
|
))
|
2015-11-25 22:20:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-09 09:01:35 -06: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(
|
2016-02-28 18:54:23 -07:00
|
|
|
err => BadRequest(jsonError(err.toString)).fuccess,
|
2015-11-26 09:32:47 -07:00
|
|
|
qJson => qJson.question.fold(BadRequest.fuccess) { q =>
|
2015-11-26 08:17:35 -07:00
|
|
|
env.api.ask(q, user) map
|
2017-01-25 04:33:04 -07:00
|
|
|
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 {
|
2015-12-08 10:20:15 -07:00
|
|
|
_.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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|