<commit_before>/*
 * Copyright 2017 HM Revenue & Customs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package services.events

import model._
import model.exchange.{ CandidateAllocationPerSession, EventAssessorAllocationsSummaryPerSkill, EventWithAllocationsSummary }
import model.persisted.eventschedules.EventType.EventType
import model.persisted.eventschedules.{ Event, Venue }
import org.joda.time.DateTime
import play.api.Logger
import repositories.events.{ EventsConfigRepository, EventsMongoRepository, EventsRepository }
import repositories.{ SchemeRepository, SchemeYamlRepository, eventsRepository }
import services.allocation.{ AssessorAllocationService, CandidateAllocationService }

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

object EventsService extends EventsService {
  val eventsRepo: EventsMongoRepository = eventsRepository
  val schemeRepo = SchemeYamlRepository
  val eventsConfigRepo = EventsConfigRepository
  val assessorAllocationService: AssessorAllocationService = AssessorAllocationService
  val candidateAllocationService: CandidateAllocationService = CandidateAllocationService
}

trait EventsService {

  def eventsRepo: EventsRepository
  def schemeRepo: SchemeRepository
  def assessorAllocationService: AssessorAllocationService

  def candidateAllocationService: CandidateAllocationService

  def eventsConfigRepo: EventsConfigRepository


  def saveAssessmentEvents(): Future[Unit] = {
    eventsRepo.count.map {
      case eventCount if eventCount >= 1 =>
        throw new Exception("Events already exist in the system, batch import not possible.")
      case _ =>
      eventsConfigRepo.events.flatMap { events =>
        Logger.debug("Events have been processed!")
        eventsRepo.save(events)
      }
    }
  }

  def save(event: Event): Future[Unit] = eventsRepo.save(event :: Nil)

  def getEvent(id: String): Future[Event] = eventsRepo.getEvent(id)

  def getEvents(eventType: EventType, venue: Venue): Future[List[Event]] = {
    eventsRepo.getEvents(Some(eventType), Some(venue))
  }

  def getEvents(ids: List[String]): Future[List[Event]] = eventsRepo.getEventsById(ids)

  def getEventsWithAllocationsSummary(venue: Venue, eventType: EventType): Future[List[EventWithAllocationsSummary]] = {
    getEvents(eventType, venue).flatMap { events =>
      val res = events.map { event =>
        assessorAllocationService.getAllocations(event.id).flatMap { allocations =>
          val allocationsGroupedBySkill = allocations.allocations.groupBy(_.allocatedAs)
          val allocationsGroupedBySkillWithSummary = allocationsGroupedBySkill.map { allocationGroupedBySkill =>
            val assessorAllocation = allocationGroupedBySkill._2
            val skill = allocationGroupedBySkill._1.name
            val allocated = assessorAllocation.length
            val confirmed = assessorAllocation.count(_.status == AllocationStatuses.CONFIRMED)<commit_msg>Add flatmap to resolve futures before return<commit_after>/*
 * Copyright 2017 HM Revenue & Customs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package services.events

import model._
import model.exchange.{ CandidateAllocationPerSession, EventAssessorAllocationsSummaryPerSkill, EventWithAllocationsSummary }
import model.persisted.eventschedules.EventType.EventType
import model.persisted.eventschedules.{ Event, Venue }
import org.joda.time.DateTime
import play.api.Logger
import repositories.events.{ EventsConfigRepository, EventsMongoRepository, EventsRepository }
import repositories.{ SchemeRepository, SchemeYamlRepository, eventsRepository }
import services.allocation.{ AssessorAllocationService, CandidateAllocationService }

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

object EventsService extends EventsService {
  val eventsRepo: EventsMongoRepository = eventsRepository
  val schemeRepo = SchemeYamlRepository
  val eventsConfigRepo = EventsConfigRepository
  val assessorAllocationService: AssessorAllocationService = AssessorAllocationService
  val candidateAllocationService: CandidateAllocationService = CandidateAllocationService
}

trait EventsService {

  def eventsRepo: EventsRepository
  def schemeRepo: SchemeRepository
  def assessorAllocationService: AssessorAllocationService

  def candidateAllocationService: CandidateAllocationService

  def eventsConfigRepo: EventsConfigRepository


  def saveAssessmentEvents(): Future[Unit] = {
    eventsRepo.count.flatMap {
      case eventCount if eventCount >= 1 =>
        throw new Exception("Events already exist in the system, batch import not possible.")
      case _ =>
      eventsConfigRepo.events.flatMap { events =>
        Logger.debug("Events have been processed!")
        eventsRepo.save(events)
      }
    }
  }

  def save(event: Event): Future[Unit] = eventsRepo.save(event :: Nil)

  def getEvent(id: String): Future[Event] = eventsRepo.getEvent(id)

  def getEvents(eventType: EventType, venue: Venue): Future[List[Event]] = {
    eventsRepo.getEvents(Some(eventType), Some(venue))
  }

  def getEvents(ids: List[String]): Future[List[Event]] = eventsRepo.getEventsById(ids)

  def getEventsWithAllocationsSummary(venue: Venue, eventType: EventType): Future[List[EventWithAllocationsSummary]] = {
    getEvents(eventType, venue).flatMap { events =>
      val res = events.map { event =>
        assessorAllocationService.getAllocations(event.id).flatMap { allocations =>
          val allocationsGroupedBySkill = allocations.allocations.groupBy(_.allocatedAs)
          val allocationsGroupedBySkillWithSummary = allocationsGroupedBySkill.map { allocationGroupedBySkill =>
            val assessorAllocation = allocationGroupedBySkill._2
            val skill = allocationGroupedBySkill._1.name
            val allocated = assessorAllocation.length
            val confirmed = assessorAllocation.count(_.status == AllocationStatuses.CONFIRMED)
{'fraction_non_alphanumeric': 0.0746161572679007, 'fraction_numerical': 0.003156837422872722, 'mean_word_length': 4.822890559732665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6068906', 'n_tokens_mistral': 1836, 'n_tokens_neox': 1813, 'n_words': 501}