No Description

AkkaStreamFileProcessingImpl.scala 9.0KB

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

Powered by TurnKey Linux.