Commit 98ccc651 authored by Tom Käsler's avatar Tom Käsler

Merge branch 'performanceOptimization' into 'master'

change loading strategy for questions w/ answeroptions

See merge request !15
parents acb353ec 18bd3188
Pipeline #7912 passed with stages
in 5 minutes and 24 seconds
...@@ -2,41 +2,43 @@ package de.thm.arsnova.services ...@@ -2,41 +2,43 @@ package de.thm.arsnova.services
import de.thm.arsnova.models._ import de.thm.arsnova.models._
import slick.driver.MySQLDriver.api._ import slick.driver.MySQLDriver.api._
import scala.concurrent.{ExecutionContext, Future} import scala.concurrent.{Future, Await}
import scala.util.{Failure, Success} import scala.concurrent.duration._
object QuestionService extends BaseService { object QuestionService extends BaseService {
import de.thm.arsnova.Context.executor import de.thm.arsnova.Context.executor
def findQuestionsBySessionIdAndVariant(sessionId: SessionId, variant: String): Future[Seq[Question]] = { def findQuestionsBySessionIdAndVariant(sessionId: SessionId, variant: String): Future[Seq[Question]] = {
db.run(questionsTable.filter(q => q.sessionId === sessionId && q.variant === variant).result).map(qSeq => val questionSeqFuture: Future[Seq[Question]] = db.run(questionsTable.filter(q => q.sessionId === sessionId && q.variant === variant).result)
Future.traverse(qSeq) { q: Question => q.format match { questionSeqFuture.map((qSequence: Seq[Question]) =>
case "mc" => { qSequence.map((q: Question) =>
AnswerOptionService.findByQuestionId(q.id.get).map(a => q.copy(answerOptions = Some(a))) q.format match {
case "mc" => Await.result(AnswerOptionService.findByQuestionId(q.id.get).map(a => q.copy(answerOptions = Some(a))), 5.seconds)
case _ => q
} }
case _ => Future(q) )
}} )
).flatMap(identity)
} }
def findAllBySessionId(sessionId: SessionId): Future[Seq[Question]] = { def findAllBySessionId(sessionId: SessionId): Future[Seq[Question]] = {
db.run(questionsTable.filter(_.sessionId === sessionId).result).map(qSeq => val questionSeqFuture: Future[Seq[Question]] = db.run(questionsTable.filter(_.sessionId === sessionId).result)
Future.traverse(qSeq) { q: Question => q.format match { questionSeqFuture.map((qSequence: Seq[Question]) =>
case "mc" => { qSequence.map((q: Question) =>
AnswerOptionService.findByQuestionId(q.id.get).map(a => q.copy(answerOptions = Some(a))) q.format match {
case "mc" => Await.result(AnswerOptionService.findByQuestionId(q.id.get).map(a => q.copy(answerOptions = Some(a))), 5.seconds)
case _ => q
} }
case _ => Future(q) )
}} )
).flatMap(identity)
} }
def findById(questionId: QuestionId): Future[Question] = { def findById(questionId: QuestionId): Future[Question] = {
db.run(questionsTable.filter(_.id === questionId).result.head).map(q => q.format match { val questionFuture: Future[Question] = db.run(questionsTable.filter(_.id === questionId).result.head)
case "mc" => { questionFuture.map((q: Question) =>
AnswerOptionService.findByQuestionId(q.id.get).map(a => q.copy(answerOptions = Some(a))) q.format match {
} case "mc" => Await.result(AnswerOptionService.findByQuestionId(q.id.get).map(a => q.copy(answerOptions = Some(a))), 5.seconds)
case _ => Future(q) case _ => q
} }
).flatMap(identity) )
} }
def create(newQuestion: Question): Future[QuestionId] = { def create(newQuestion: Question): Future[QuestionId] = {
newQuestion.format match { newQuestion.format match {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment