<commit_before>package controllers

// external
import akka.actor.ActorRef
import akka.pattern.ask
import akka.util.Timeout
import javax.inject.{Inject, Named}

import jp.t2v.lab.play2.auth.OptionalAuthElement
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.libs.json.{JsError, Json}
import play.api.mvc.{Action, Controller}
import services.actors.Exchange.{BookData, BookmakerNames}
import services.actors.Matrix.Gleam

import scala.concurrent.duration._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.language.postfixOps

// Internal
import models.sports._
import services.DBService


class Arbiter @Inject() (val database: DBService,
                         val messagesApi: MessagesApi,
                         @Named("exchange") exchange: ActorRef,
                         @Named("matrix") matrix: ActorRef,
                         implicit val webJarAssets: WebJarAssets)
  extends Controller with AuthConfigTrait with OptionalAuthElement with I18nSupport  {

  implicit val timeout = Timeout(5 seconds)

  /**
    * Post new sportsbook events here.
    */
  def addEvents() = Action.async( parse.json ) { implicit request =>
    request.body.validate[SportsBookData].fold (
      errors => {
        Future.successful(BadRequest(Json.obj("status" ->"KO", "message" -> JsError.toJson(errors))))
      },
      newBook => {
        exchange ! newBook
        Future.successful(Ok(Json.obj("status" ->"OK", "message" -> ("Received") )))
      }
    )
  }

  /**
    * Should display a view of betting lines with odds for a specific sport.
    */
  def matrices(filter: Option[String]) = AsyncStack { implicit request =>
    (matrix ? Gleam(20)).mapTo[List[(String, OddsMatrixAB)]].map { list =>
      val mlMatrices = list.filter(_._1.endsWith("ML"))
      Ok(views.html.arbiter.grid(loggedIn, mlMatrices))
    }
  }

  def bookmaker(bookname: String) = AsyncStack { implicit request =>
    (exchange ? BookData(bookname)).mapTo[List[SportsData]].map { list =>
      if (list.nonEmpty) {
        Ok(views.html.arbiter.bookmaker(loggedIn, list))
      } else {
        NotFound(bookname)
      }
    }
  }

  def bookmakerNames() = AsyncStack { implicit request =>
    (exchange ? BookmakerNames).mapTo[List[String]].map { names =>
      Ok(names.toString)
    }
  }
}
<commit_msg>Need to normalize sports names because they different across bookmakers.<commit_after>package controllers

// external
import akka.actor.ActorRef
import akka.pattern.ask
import akka.util.Timeout
import javax.inject.{Inject, Named}

import jp.t2v.lab.play2.auth.OptionalAuthElement
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.libs.json.{JsError, Json}
import play.api.mvc.{Action, Controller}
import services.actors.Exchange.{BookData, BookmakerNames}
import services.actors.Matrix.Gleam

import scala.concurrent.duration._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.language.postfixOps

// Internal
import models.sports._
import services.DBService


class Arbiter @Inject() (val database: DBService,
                         val messagesApi: MessagesApi,
                         @Named("exchange") exchange: ActorRef,
                         @Named("matrix") matrix: ActorRef,
                         implicit val webJarAssets: WebJarAssets)
  extends Controller with AuthConfigTrait with OptionalAuthElement with I18nSupport  {

  implicit val timeout = Timeout(5 seconds)

  /**
    * Post new sportsbook events here.
    */
  def addEvents() = Action.async( parse.json ) { implicit request =>
    request.body.validate[SportsBookData].fold (
      errors => {
        Future.successful(BadRequest(Json.obj("status" ->"KO", "message" -> JsError.toJson(errors))))
      },
      newBook => {
        // we shall normalize the sport names from the books because these sport names
        // may be labeled differently on various bookmakers sites
        val sportname = newBook.sport
        val normalized = sportname match {
          case "American FootballUSANFL" => "NFL Football"
          case _ => sportname
        }

        exchange ! newBook.copy(sport = normalized)
        Future.successful(Ok(Json.obj("status" ->"OK", "message" -> ("Received") )))
      }
    )
  }

  /**
    * Should display a view of betting lines with odds for a specific sport.
    */
  def matrices(filter: Option[String]) = AsyncStack { implicit request =>
    (matrix ? Gleam(20)).mapTo[List[(String, OddsMatrixAB)]].map { list =>
      val mlMatrices = list.filter(_._1.endsWith("ML"))
      Ok(views.html.arbiter.grid(loggedIn, mlMatrices))
    }
  }

  def bookmaker(bookname: String) = AsyncStack { implicit request =>
    (exchange ? BookData(bookname)).mapTo[List[SportsData]].map { list =>
      if (list.nonEmpty) {
        Ok(views.html.arbiter.bookmaker(loggedIn, list))
      } else {
        NotFound(bookname)
      }
    }
  }

  def bookmakerNames() = AsyncStack { implicit request =>
    (exchange ? BookmakerNames).mapTo[List[String]].map { names =>
      Ok(names.toString)
    }
  }
}

{'fraction_non_alphanumeric': 0.11721183800623053, 'fraction_numerical': 0.004672897196261682, 'mean_word_length': 3.0385220125786163, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '4201096', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1512, 'n_words': 343}