Нет описания

AkkaStreamFileProcessingImpl.scala 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package fr.natan.akkastreamfileprocessingapi.service
  2. import akka.stream.scaladsl.{Sink, Source}
  3. import com.typesafe.scalalogging.slf4j.Logger
  4. import fr.natan.akkastreamfileprocessingapi.businessexceptions.PersonNotFoundException
  5. import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.{
  6. MAXI_ROWS,
  7. nameBasics,
  8. titleBasics,
  9. titleEpisode,
  10. titlePrincipalsBasics
  11. }
  12. import fr.natan.akkastreamfileprocessingapi.models.ModelsAndJsonMap.{Episode, Person, TvSerie}
  13. import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{
  14. actorSystem,
  15. buildAndValidateSource,
  16. buildEpisodeFlow,
  17. buildMaxEpisodesSink,
  18. buildMaxPersonsSink,
  19. buildMaxTvSeriesSink,
  20. buildPersonFlow,
  21. buildTvSerieFlow,
  22. filterPersonByIdFlow,
  23. filterPersonByNameFlow,
  24. filterTvSerieByIdFlow,
  25. filterTvSerieByPrimaryTitleFlow
  26. }
  27. import fr.natan.akkastreamfileprocessingapi.service.UtilitiesClass.{
  28. getListOfPersonsForTvSerie,
  29. getListOfPersonsIDByTvSerieID,
  30. getTvSerieIDFuture,
  31. getTvSerieIdByPrimaryTitle
  32. }
  33. import org.slf4j.LoggerFactory
  34. import org.springframework.stereotype.Component
  35. import scala.concurrent.ExecutionContext.Implicits.global
  36. import scala.concurrent.Future
  37. import scala.language.postfixOps
  38. import scala.util.{Failure, Success}
  39. //noinspection SpellCheckingInspection
  40. @Component
  41. class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
  42. implicit val logger: Logger = Logger(LoggerFactory.getLogger(this.getClass))
  43. override def getPersonByIdFuture(personID: String): Future[Option[Person]] = {
  44. val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = nameBasics)
  45. val start: Long = System.currentTimeMillis()
  46. val personFuture: Future[Option[Person]] = source
  47. .via(flow = filterPersonByIdFlow(personID = personID))
  48. .runWith(Sink.headOption[Person])
  49. personFuture.onComplete({
  50. case Failure(exception) => throw new PersonNotFoundException()
  51. case Success(value: Option[Person]) => logger.info(s"${value.get}")
  52. })
  53. personFuture
  54. }
  55. override def getPersonByNameFuture(primaryName: String): Future[Option[Person]] = {
  56. val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = nameBasics)
  57. val start: Long = System.currentTimeMillis()
  58. val personFuture: Future[Option[Person]] = source
  59. .via(flow = filterPersonByNameFlow(primaryName = primaryName))
  60. .runWith(Sink.headOption[Person])
  61. personFuture.onComplete({
  62. case Failure(exception) => throw new PersonNotFoundException()
  63. case Success(value: Option[Person]) => logger.info(s"${value.get}")
  64. })
  65. personFuture
  66. }
  67. override def getTvSerieByIdFuture(tvSerieID: String): Future[Option[TvSerie]] = {
  68. val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = titleBasics)
  69. val start: Long = System.currentTimeMillis()
  70. val tvSerieFuture: Future[Option[TvSerie]] = source
  71. .via(flow = filterTvSerieByIdFlow(tvSerieID = tvSerieID))
  72. .runWith(Sink.headOption[TvSerie])
  73. tvSerieFuture.onComplete({
  74. case Failure(exception) => logger.info(s"$exception")
  75. case Success(value: Option[TvSerie]) => logger.info(s"${value.get}")
  76. })
  77. tvSerieFuture
  78. }
  79. override def getTvSeriesByPrimaryTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[TvSerie]] = {
  80. val start: Long = System.currentTimeMillis()
  81. val tvSeriesSource: Source[Map[String, String], _] = buildAndValidateSource(inputFile = titleBasics)
  82. val tvSriesFuture: Future[IndexedSeq[TvSerie]] = tvSeriesSource
  83. .via(flow = filterTvSerieByPrimaryTitleFlow(tvSeriePrimaryTitle = tvSeriePrimaryTitle))
  84. .runWith(Sink.collection)
  85. tvSriesFuture.onComplete({
  86. case Failure(exception) => logger.info(s"$exception")
  87. case Success(value: IndexedSeq[TvSerie]) =>
  88. value.foreach((tvSrie: TvSerie) => logger.info(s"$tvSrie"))
  89. logger.info(s"SUCCESS, elapsed time:${(System.currentTimeMillis() - start) / 1000} sec")
  90. })
  91. tvSriesFuture
  92. }
  93. override def getPersonsForTvSerieByTvSerieTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[Person]] = {
  94. //futures chaining
  95. logger.info("STEP 1/3 ...")
  96. val start1: Long = System.currentTimeMillis()
  97. val tvSerieIDFuture: Future[Option[String]] = getTvSerieIdByPrimaryTitle(
  98. primaryTitle = tvSeriePrimaryTitle,
  99. inputFile = titleBasics
  100. )
  101. val finalFuture: Future[IndexedSeq[Person]] =
  102. tvSerieIDFuture.andThen({
  103. case Success(value: Option[String]) =>
  104. logger.info(s"TvSerie ID: ${value.get}")
  105. logger.info(s"STEP 1/3 END, elapsed time:${(System.currentTimeMillis() - start1) / 1000} sec")
  106. })
  107. .flatMap({
  108. _ =>
  109. logger.info("STEP 2/3 ...")
  110. val start2: Long = System.currentTimeMillis()
  111. val listPersonIDsFuture: Future[IndexedSeq[Option[String]]] = getListOfPersonsIDByTvSerieID(
  112. tvSerieIdFuture = tvSerieIDFuture, inputFile = titlePrincipalsBasics
  113. )
  114. listPersonIDsFuture.andThen({
  115. case Success(value) =>
  116. value.foreach((personID: Option[String]) => logger.info(s"Person ID:${personID.get}"))
  117. logger.info(s"STEP 2/3 END, elapsed time:${(System.currentTimeMillis() - start2) / 1000} sec")
  118. })
  119. .flatMap({
  120. _ =>
  121. logger.info("STEP 3/3 ...")
  122. val start3: Long = System.currentTimeMillis()
  123. val personsTeamFuture: Future[IndexedSeq[Person]] = getListOfPersonsForTvSerie(
  124. listPersonsIDsFuture = listPersonIDsFuture, inputFile = nameBasics)
  125. personsTeamFuture.andThen({
  126. case Success(value: IndexedSeq[Person]) =>
  127. value.foreach((person: Person) => logger.info(s"${person.toString}"))
  128. logger.info(s"STEP 3/3 END, elapsed time:${(System.currentTimeMillis() - start3) / 1000} sec")
  129. })
  130. })
  131. })
  132. finalFuture
  133. }
  134. override def getPersonsForTvSerieByTvSerieIDFuture(tvSerieId: String): Future[IndexedSeq[Person]] = {
  135. val start1: Long = System.currentTimeMillis()
  136. logger.info("STEP 1/3 ...")
  137. val tvSerieIdFuture: Future[Option[String]] = getTvSerieIDFuture(
  138. tvSerieID = tvSerieId, logger = logger, inputFile = titleBasics)
  139. tvSerieIdFuture.andThen({
  140. case Failure(exception) => logger.error(s"${exception.printStackTrace()}")
  141. case Success(value: Option[String]) =>
  142. logger.info(s"TvSerie ID: ${value.get}")
  143. logger.info(s"STEP 1/3 END, elapsed time:${(System.currentTimeMillis() - start1) / 1000} sec")
  144. })
  145. .flatMap({
  146. _ =>
  147. logger.info("STEP 2/3 ...")
  148. val start2: Long = System.currentTimeMillis()
  149. val listOfPersonsIDsFuture: Future[IndexedSeq[Option[String]]] = getListOfPersonsIDByTvSerieID(
  150. tvSerieIdFuture = tvSerieIdFuture, inputFile = titlePrincipalsBasics)
  151. listOfPersonsIDsFuture.andThen({
  152. case Failure(exception) => logger.error(s"${exception.printStackTrace()}")
  153. case Success(value: IndexedSeq[Option[String]]) =>
  154. value.foreach((personID: Option[String]) => logger.info(s"Person ID:${personID.get}"))
  155. logger.info(s"STEP 2/3 END, elapsed time:${(System.currentTimeMillis() - start2) / 1000} sec")
  156. })
  157. .flatMap({
  158. _ =>
  159. logger.info("STEP 3/3 ...")
  160. val start3: Long = System.currentTimeMillis()
  161. val personsTeamFuture: Future[IndexedSeq[Person]] = getListOfPersonsForTvSerie(
  162. listPersonsIDsFuture = listOfPersonsIDsFuture, inputFile = nameBasics)
  163. personsTeamFuture.andThen({
  164. case Failure(exception) => logger.error(s"$exception")
  165. case Success(value: IndexedSeq[Person]) =>
  166. value.foreach((person: Person) => logger.info(s"${person.toString}"))
  167. logger.info(s"STEP 3/3 END, elapsed time:${(System.currentTimeMillis() - start3) / 1000} sec")
  168. })
  169. })
  170. })
  171. }
  172. override def getMaxPersonsFuture: Future[IndexedSeq[Person]] = {
  173. val personSource: Source[Map[String, String], _] = buildAndValidateSource(inputFile = nameBasics)
  174. //graph
  175. val startTime: Long = System.currentTimeMillis()
  176. val result: Future[IndexedSeq[Person]] = personSource
  177. .via(flow = buildPersonFlow())
  178. .take(n = MAXI_ROWS)
  179. .runWith(sink = buildMaxPersonsSink())
  180. result
  181. }
  182. override def getMaxTvSeriesFuture: Future[IndexedSeq[TvSerie]] = {
  183. val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = titleBasics)
  184. val startingTime: Long = System.currentTimeMillis()
  185. //graph sink->flow->sink
  186. val results: Future[IndexedSeq[TvSerie]] = source
  187. .via(flow = buildTvSerieFlow())
  188. .take(n = MAXI_ROWS)
  189. .runWith(sink = buildMaxTvSeriesSink())
  190. results
  191. }
  192. override def getMaxEpisodes: Future[IndexedSeq[Episode]] = {
  193. val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = titleEpisode)
  194. val listEpisodeFuture: Future[IndexedSeq[Episode]] = source
  195. .via(flow = buildEpisodeFlow())
  196. .take(n = MAXI_ROWS)
  197. .runWith(sink = buildMaxEpisodesSink())
  198. listEpisodeFuture
  199. }
  200. }

Powered by TurnKey Linux.