<commit_before>package org.thp.cortex.controllers

import javax.inject.{ Inject, Named, Singleton }

import akka.actor.{ ActorRef, ActorSystem }
import akka.pattern.ask
import akka.stream.Materializer
import akka.stream.scaladsl.Sink
import akka.util.Timeout
import org.elastic4play.controllers.{ Authenticated, Fields, FieldsBodyParser, Renderer }
import org.elastic4play.models.JsonFormat.baseModelEntityWrites
import org.elastic4play.services.JsonFormat.queryReads
import org.elastic4play.services.{ QueryDSL, QueryDef }
import org.elastic4play.utils.RichFuture
import org.thp.cortex.models.{ Job, JobStatus, Roles }
import org.thp.cortex.services.AuditActor.{ JobEnded, Register }
import org.thp.cortex.services.{ JobSrv, UserSrv }
import play.api.http.Status
import play.api.libs.json.{ JsObject, JsString, JsValue, Json }
import play.api.mvc.{ AbstractController, Action, AnyContent, ControllerComponents }

import scala.concurrent.duration.{ Duration, FiniteDuration }
import scala.concurrent.{ ExecutionContext, Future }

@Singleton
class JobCtrl @Inject() (
    jobSrv: JobSrv,
    userSrv: UserSrv,
    @Named("audit") auditActor: ActorRef,
    fieldsBodyParser: FieldsBodyParser,
    authenticated: Authenticated,
    renderer: Renderer,
    components: ControllerComponents,
    implicit val ec: ExecutionContext,
    implicit val mat: Materializer,
    implicit val actorSystem: ActorSystem) extends AbstractController(components) with Status {

  def list(dataTypeFilter: Option[String], dataFilter: Option[String], analyzerFilter: Option[String], range: Option[String]): Action[AnyContent] = authenticated(Roles.read).async { implicit request ⇒
    val (jobs, jobTotal) = jobSrv.listForUser(request.userId, dataTypeFilter, dataFilter, analyzerFilter, range)
    renderer.toOutput(OK, jobs, jobTotal)
  }

  def find: Action[Fields] = authenticated(Roles.read).async(fieldsBodyParser) { implicit request ⇒
    import QueryDSL._
    val deleteFilter = "status" ~!= "Deleted"
    val query = request.body.getValue("query").fold[QueryDef](deleteFilter)(q ⇒ and(q.as[QueryDef], deleteFilter))
    val range = request.body.getString("range")
    val sort = request.body.getStrings("sort").getOrElse(Nil)
    val (users, total) = jobSrv.findForUser(request.userId, query, range, sort)
    renderer.toOutput(OK, users, total)

  }

  def get(jobId: String): Action[AnyContent] = authenticated(Roles.read).async { implicit request ⇒
    jobSrv.getForUser(request.userId, jobId).map { job ⇒
      renderer.toOutput(OK, job)
    }
  }

  def delete(jobId: String): Action[AnyContent] = authenticated(Roles.orgAdmin).async { implicit request ⇒
    jobSrv.getForUser(request.userId, jobId)
      .flatMap(job ⇒ jobSrv.delete(job))
      .map(_ ⇒ NoContent)
  }

  def create(analyzerId: String): Action[Fields] = authenticated(Roles.analyze).async(fieldsBodyParser) { implicit request ⇒
    jobSrv.create(analyzerId, request.body)
      .map { job ⇒
        renderer.toOutput(OK, job)
      }
  }

  private def getJobWithReport(userId: String, jobId: String): Future[JsValue] = {
    jobSrv.getForUser(userId, jobId).flatMap(getJobWithReport(userId, _))
  }

  private def getJobWithReport(userId: String, job: Job): Future[JsValue] = {
    (job.status() match {
      case JobStatus.Success ⇒<commit_msg>Analyze role can remove job<commit_after>package org.thp.cortex.controllers

import javax.inject.{ Inject, Named, Singleton }

import akka.actor.{ ActorRef, ActorSystem }
import akka.pattern.ask
import akka.stream.Materializer
import akka.stream.scaladsl.Sink
import akka.util.Timeout
import org.elastic4play.controllers.{ Authenticated, Fields, FieldsBodyParser, Renderer }
import org.elastic4play.models.JsonFormat.baseModelEntityWrites
import org.elastic4play.services.JsonFormat.queryReads
import org.elastic4play.services.{ QueryDSL, QueryDef }
import org.elastic4play.utils.RichFuture
import org.thp.cortex.models.{ Job, JobStatus, Roles }
import org.thp.cortex.services.AuditActor.{ JobEnded, Register }
import org.thp.cortex.services.{ JobSrv, UserSrv }
import play.api.http.Status
import play.api.libs.json.{ JsObject, JsString, JsValue, Json }
import play.api.mvc.{ AbstractController, Action, AnyContent, ControllerComponents }

import scala.concurrent.duration.{ Duration, FiniteDuration }
import scala.concurrent.{ ExecutionContext, Future }

@Singleton
class JobCtrl @Inject() (
    jobSrv: JobSrv,
    userSrv: UserSrv,
    @Named("audit") auditActor: ActorRef,
    fieldsBodyParser: FieldsBodyParser,
    authenticated: Authenticated,
    renderer: Renderer,
    components: ControllerComponents,
    implicit val ec: ExecutionContext,
    implicit val mat: Materializer,
    implicit val actorSystem: ActorSystem) extends AbstractController(components) with Status {

  def list(dataTypeFilter: Option[String], dataFilter: Option[String], analyzerFilter: Option[String], range: Option[String]): Action[AnyContent] = authenticated(Roles.read).async { implicit request ⇒
    val (jobs, jobTotal) = jobSrv.listForUser(request.userId, dataTypeFilter, dataFilter, analyzerFilter, range)
    renderer.toOutput(OK, jobs, jobTotal)
  }

  def find: Action[Fields] = authenticated(Roles.read).async(fieldsBodyParser) { implicit request ⇒
    import QueryDSL._
    val deleteFilter = "status" ~!= "Deleted"
    val query = request.body.getValue("query").fold[QueryDef](deleteFilter)(q ⇒ and(q.as[QueryDef], deleteFilter))
    val range = request.body.getString("range")
    val sort = request.body.getStrings("sort").getOrElse(Nil)
    val (users, total) = jobSrv.findForUser(request.userId, query, range, sort)
    renderer.toOutput(OK, users, total)

  }

  def get(jobId: String): Action[AnyContent] = authenticated(Roles.read).async { implicit request ⇒
    jobSrv.getForUser(request.userId, jobId).map { job ⇒
      renderer.toOutput(OK, job)
    }
  }

  def delete(jobId: String): Action[AnyContent] = authenticated(Roles.analyze, Roles.orgAdmin).async { implicit request ⇒
    jobSrv.getForUser(request.userId, jobId)
      .flatMap(job ⇒ jobSrv.delete(job))
      .map(_ ⇒ NoContent)
  }

  def create(analyzerId: String): Action[Fields] = authenticated(Roles.analyze).async(fieldsBodyParser) { implicit request ⇒
    jobSrv.create(analyzerId, request.body)
      .map { job ⇒
        renderer.toOutput(OK, job)
      }
  }

  private def getJobWithReport(userId: String, jobId: String): Future[JsValue] = {
    jobSrv.getForUser(userId, jobId).flatMap(getJobWithReport(userId, _))
  }

  private def getJobWithReport(userId: String, job: Job): Future[JsValue] = {
    (job.status() match {
      case JobStatus.Success ⇒
{'fraction_non_alphanumeric': 0.11557562076749435, 'fraction_numerical': 0.0015048908954100827, 'mean_word_length': 4.876215738284704, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1551128', 'n_tokens_mistral': 2162, 'n_tokens_neox': 2137, 'n_words': 466}