Нет описания

AkkaStreamFileProcessingImpl.scala 9.1KB

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

Powered by TurnKey Linux.