<commit_before>package com.blinkbox.books.marvin.magrathea.api

import akka.actor.ActorRefFactory
import com.blinkbox.books.logging.DiagnosticExecutionContext
import com.blinkbox.books.marvin.magrathea.message.DocumentDao
import com.blinkbox.books.marvin.magrathea.{SchemaConfig, ServiceConfig}
import com.blinkbox.books.spray.v1.Error
import com.blinkbox.books.spray.{Directives => CommonDirectives, _}
import org.slf4j.LoggerFactory
import spray.http.HttpHeaders.RawHeader
import spray.http.StatusCodes._
import spray.routing._
import spray.util.LoggingContext

import scala.util.control.NonFatal

trait RestRoutes extends HttpService {
  def getLatestBookById: Route
  def reIndexBook: Route
  def getLatestContributorById: Route
  def reIndexContributor: Route
  def search: Route
  def reIndexLatestSearch: Route
  def reIndexHistorySearch: Route
}

class RestApi(config: ServiceConfig, schemas: SchemaConfig, documentDao: DocumentDao, indexService: IndexService)
  (implicit val actorRefFactory: ActorRefFactory) extends RestRoutes with CommonDirectives with v2.JsonSupport {

  implicit val ec = DiagnosticExecutionContext(actorRefFactory.dispatcher)
  implicit val timeout = config.api.timeout
  implicit val log = LoggerFactory.getLogger(classOf[RestApi])

  override val getLatestBookById = get {
    path("books" / Segment) { id =>
      onSuccess(documentDao.getLatestDocumentById(id, Option(schemas.book))) {
        case Some(doc) => uncacheable(doc)
        case _ => uncacheable(NotFound, Error("not_found", "The requested book was not found."))
      }
    }
  }

  override val reIndexBook = put {
    path("books" / Segment / "reindex") { id =>
      onSuccess(indexService.reIndexLatestDocument(id, schemas.book)) { found =>
        if (found) uncacheable(OK, None)
        else uncacheable(NotFound, Error("not_found", "The requested book was not found."))
      }
    }
  }

  override val getLatestContributorById = get {
    path("contributors" / Segment) { id =>
      onSuccess(documentDao.getLatestDocumentById(id, Option(schemas.contributor))) {
        case Some(doc) => uncacheable(doc)
        case _ => uncacheable(NotFound, Error("not_found", "The requested contributor was not found."))
      }
    }
  }

  override val reIndexContributor = put {
    path("contributors" / Segment / "reindex") { id =>
      onSuccess(indexService.reIndexLatestDocument(id, schemas.contributor)) { found =>
        if (found) uncacheable(OK, None)
        else uncacheable(NotFound, Error("not_found", "The requested contributor was not found."))
      }
    }
  }

  override val search = get {
    path("search") {
      parameter('q) { q =>
        paged(defaultCount = 50) { paged =>
          onSuccess(indexService.searchInLatest(q)(paged))(uncacheable(_))
        }
      }
    }
  }

  override val reIndexLatestSearch = put {
    path("search/reindex/latest") {
      uncacheable(Accepted, None)
    }
  }

  override val reIndexHistorySearch = put {
    path("search/reindex/history") {
      uncacheable(Accepted, None)
    }
  }

  val routes = rootPath(config.api.localUrl.path) {
    monitor() {
      respondWithHeader(RawHeader("Vary", "Accept, Accept-Encoding")) {
        handleExceptions(exceptionHandler) {
          getLatestBookById ~ getLatestContributorById ~ search ~<commit_msg>Calling reIndexing from the API<commit_after>package com.blinkbox.books.marvin.magrathea.api

import akka.actor.ActorRefFactory
import com.blinkbox.books.logging.DiagnosticExecutionContext
import com.blinkbox.books.marvin.magrathea.message.DocumentDao
import com.blinkbox.books.marvin.magrathea.{SchemaConfig, ServiceConfig}
import com.blinkbox.books.spray.v1.Error
import com.blinkbox.books.spray.{Directives => CommonDirectives, _}
import org.slf4j.LoggerFactory
import spray.http.HttpHeaders.RawHeader
import spray.http.StatusCodes._
import spray.routing._
import spray.util.LoggingContext

import scala.util.control.NonFatal

trait RestRoutes extends HttpService {
  def getLatestBookById: Route
  def reIndexBook: Route
  def getLatestContributorById: Route
  def reIndexContributor: Route
  def search: Route
  def reIndexLatestSearch: Route
  def reIndexHistorySearch: Route
}

class RestApi(config: ServiceConfig, schemas: SchemaConfig, documentDao: DocumentDao, indexService: IndexService)
  (implicit val actorRefFactory: ActorRefFactory) extends RestRoutes with CommonDirectives with v2.JsonSupport {

  implicit val ec = DiagnosticExecutionContext(actorRefFactory.dispatcher)
  implicit val timeout = config.api.timeout
  implicit val log = LoggerFactory.getLogger(classOf[RestApi])

  override val getLatestBookById = get {
    path("books" / Segment) { id =>
      onSuccess(documentDao.getLatestDocumentById(id, Option(schemas.book))) {
        case Some(doc) => uncacheable(doc)
        case _ => uncacheable(NotFound, Error("not_found", "The requested book was not found."))
      }
    }
  }

  override val reIndexBook = put {
    path("books" / Segment / "reindex") { id =>
      onSuccess(indexService.reIndexLatestDocument(id, schemas.book)) { found =>
        if (found) uncacheable(OK, None)
        else uncacheable(NotFound, Error("not_found", "The requested book was not found."))
      }
    }
  }

  override val getLatestContributorById = get {
    path("contributors" / Segment) { id =>
      onSuccess(documentDao.getLatestDocumentById(id, Option(schemas.contributor))) {
        case Some(doc) => uncacheable(doc)
        case _ => uncacheable(NotFound, Error("not_found", "The requested contributor was not found."))
      }
    }
  }

  override val reIndexContributor = put {
    path("contributors" / Segment / "reindex") { id =>
      onSuccess(indexService.reIndexLatestDocument(id, schemas.contributor)) { found =>
        if (found) uncacheable(OK, None)
        else uncacheable(NotFound, Error("not_found", "The requested contributor was not found."))
      }
    }
  }

  override val search = get {
    path("search") {
      parameter('q) { q =>
        paged(defaultCount = 50) { paged =>
          onSuccess(indexService.searchInLatest(q)(paged))(uncacheable(_))
        }
      }
    }
  }

  override val reIndexLatestSearch = put {
    path("search/reindex/latest") {
      indexService.reIndexLatest()
      uncacheable(Accepted, None)
    }
  }

  override val reIndexHistorySearch = put {
    path("search/reindex/history") {
      indexService.reIndexHistory()
      uncacheable(Accepted, None)
    }
  }

  val routes = rootPath(config.api.localUrl.path) {
    monitor() {
      respondWithHeader(RawHeader("Vary", "Accept, Accept-Encoding")) {
        handleExceptions(exceptionHandler) {
          getLatestBookById ~ getLatestContributorById ~ search ~
{'fraction_non_alphanumeric': 0.10515372048121194, 'fraction_numerical': 0.0014852220406950838, 'mean_word_length': 3.8620938628158843, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '1464236', 'n_tokens_mistral': 2069, 'n_tokens_neox': 2040, 'n_words': 426}