<commit_before>package controllers

import lila.api.Context
import lila.app._
import lila.insight.{ Metric, Dimension }
import play.api.libs.json.Json
import play.api.mvc._
import views._

object Insight extends LilaController {

  private def env = Env.insight

  def refresh(username: String) = Open { implicit ctx =>
    Accessible(username) { user =>
      env.api indexAll user inject Ok
    }
  }

  def index(username: String) = path(username,
    metric = Metric.MeanCpl.key,
    dimension = Dimension.PieceRole.key,
    filters = "")

  def path(username: String, metric: String, dimension: String, filters: String) = Open { implicit ctx =>
    Accessible(username) { user =>
      import lila.insight.InsightApi.UserStatus._
      env.api userStatus user flatMap {
        case NoGame => Ok(html.insight.noGame(user)).fuccess
        case Empty  => Ok(html.insight.empty(user)).fuccess
        case s => for {
          cache <- env.api userCache user
          prefId <- env.share getPrefId user
        } yield Ok(html.insight.index(
          u = user,
          cache = cache,
          prefId = prefId,
          ui = env.jsonView.ui(cache.ecos),
          question = env.jsonView.question(metric, dimension, filters),
          stale = s == Stale))
      }
    }
  }

  def json(username: String) = OpenBody(BodyParsers.parse.json) { implicit ctx =>
    import lila.insight.JsonQuestion, JsonQuestion._
    Accessible(username) { user =>
      ctx.body.body.validate[JsonQuestion].fold(
        err => BadRequest(Json.obj("error" -> err.toString)).fuccess,
        qJson => qJson.question.fold(BadRequest.fuccess) { q =>
          env.api.ask(q, user) map
            lila.insight.Chart.fromAnswer map
            env.jsonView.chart.apply map { Ok(_) }
        }
      )
    }
  }

  private def Accessible(username: String)(f: lila.user.User => Fu[Result])(implicit ctx: Context) =
    lila.user.UserRepo named username flatMap {
      case None => notFound
      case Some(u) => env.share.grant(u, ctx.me) flatMap {
        case true                          => f(u)
        // case false if isGranted(_.UserSpy) => f(u)
        case false                         => fuccess(Forbidden(html.insight.forbidden(u)))
      }
    }

  private def AccessibleJson(username: String)(f: lila.user.User => Fu[Result])(implicit ctx: Context) =
    lila.user.UserRepo named username flatMap {
      case None => notFoundJson(s"No such user: $username")
      case Some(u) => env.share.grant(u, ctx.me) flatMap {<commit_msg>remove debug code<commit_after>package controllers

import lila.api.Context
import lila.app._
import lila.insight.{ Metric, Dimension }
import play.api.libs.json.Json
import play.api.mvc._
import views._

object Insight extends LilaController {

  private def env = Env.insight

  def refresh(username: String) = Open { implicit ctx =>
    Accessible(username) { user =>
      env.api indexAll user inject Ok
    }
  }

  def index(username: String) = path(username,
    metric = Metric.MeanCpl.key,
    dimension = Dimension.PieceRole.key,
    filters = "")

  def path(username: String, metric: String, dimension: String, filters: String) = Open { implicit ctx =>
    Accessible(username) { user =>
      import lila.insight.InsightApi.UserStatus._
      env.api userStatus user flatMap {
        case NoGame => Ok(html.insight.noGame(user)).fuccess
        case Empty  => Ok(html.insight.empty(user)).fuccess
        case s => for {
          cache <- env.api userCache user
          prefId <- env.share getPrefId user
        } yield Ok(html.insight.index(
          u = user,
          cache = cache,
          prefId = prefId,
          ui = env.jsonView.ui(cache.ecos),
          question = env.jsonView.question(metric, dimension, filters),
          stale = s == Stale))
      }
    }
  }

  def json(username: String) = OpenBody(BodyParsers.parse.json) { implicit ctx =>
    import lila.insight.JsonQuestion, JsonQuestion._
    Accessible(username) { user =>
      ctx.body.body.validate[JsonQuestion].fold(
        err => BadRequest(Json.obj("error" -> err.toString)).fuccess,
        qJson => qJson.question.fold(BadRequest.fuccess) { q =>
          env.api.ask(q, user) map
            lila.insight.Chart.fromAnswer map
            env.jsonView.chart.apply map { Ok(_) }
        }
      )
    }
  }

  private def Accessible(username: String)(f: lila.user.User => Fu[Result])(implicit ctx: Context) =
    lila.user.UserRepo named username flatMap {
      case None => notFound
      case Some(u) => env.share.grant(u, ctx.me) flatMap {
        case true                          => f(u)
        case false if isGranted(_.UserSpy) => f(u)
        case false                         => fuccess(Forbidden(html.insight.forbidden(u)))
      }
    }

  private def AccessibleJson(username: String)(f: lila.user.User => Fu[Result])(implicit ctx: Context) =
    lila.user.UserRepo named username flatMap {
      case None => notFoundJson(s"No such user: $username")
      case Some(u) => env.share.grant(u, ctx.me) flatMap {
{'fraction_non_alphanumeric': 0.12057718916781973, 'fraction_numerical': 0.0, 'mean_word_length': 2.4800550206327374, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5975366', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1555, 'n_words': 362}