Browse Source

first commit

placidenduwayo 1 year ago
parent
commit
53d4708cca

+ 10
- 5
src/main/java/fr/natan/akkastreamfileprocessingapi/controller/TvSeriesController.java View File

84
     private List<String> getTvserieByPrimaryTitle(
84
     private List<String> getTvserieByPrimaryTitle(
85
             @PathVariable(name = "tvseriePrimaryTitle") String tvseriePrimaryTitle) throws InterruptedException, ExecutionException {
85
             @PathVariable(name = "tvseriePrimaryTitle") String tvseriePrimaryTitle) throws InterruptedException, ExecutionException {
86
 
86
 
87
-        Future<IndexedSeq<Models.TvSerie>> listFuture = akkaStreamFilesProcessing.getTvSeriesByPrimaryTitleFuture(tvseriePrimaryTitle);
87
+        Future<IndexedSeq<Models.TvSerie>> listFuture = akkaStreamFilesProcessing
88
+                .getTvSeriesByPrimaryTitleFuture(tvseriePrimaryTitle);
88
         CompletableFuture<IndexedSeq<Models.TvSerie>> completableFuture =
89
         CompletableFuture<IndexedSeq<Models.TvSerie>> completableFuture =
89
                 CompletableFutureBuilder.buildcompletableFuture2(listFuture);
90
                 CompletableFutureBuilder.buildcompletableFuture2(listFuture);
90
         while (!completableFuture.isDone()) {
91
         while (!completableFuture.isDone()) {
106
     private List<String> getPersonsForTvSerieByTvSerieTitle(@PathVariable(name = "tvSerieTitle") String tvSerieTitle)
107
     private List<String> getPersonsForTvSerieByTvSerieTitle(@PathVariable(name = "tvSerieTitle") String tvSerieTitle)
107
             throws InterruptedException, ExecutionException {
108
             throws InterruptedException, ExecutionException {
108
 
109
 
109
-        Future<IndexedSeq<Models.Person>> futurePersonSeq = akkaStreamFilesProcessing.getPersonsForTvSerieByTvSerieTitleFuture(tvSerieTitle);
110
-        CompletableFuture<IndexedSeq<Models.Person>> completableFuture = CompletableFutureBuilder.buildcompletableFuture2(futurePersonSeq);
110
+        Future<IndexedSeq<Models.Person>> futurePersonSeq = akkaStreamFilesProcessing
111
+                .getPersonsForTvSerieByTvSerieTitleFuture(tvSerieTitle);
112
+        CompletableFuture<IndexedSeq<Models.Person>> completableFuture = CompletableFutureBuilder
113
+                .buildcompletableFuture2(futurePersonSeq);
111
         while (!completableFuture.isDone()){
114
         while (!completableFuture.isDone()){
112
             logger.info("IS PROCESSING...");
115
             logger.info("IS PROCESSING...");
113
             Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
116
             Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
128
     private List<String> getPersonsForTvSerieByTvSerieID(@PathVariable(name = "tvSerieID") String tvSerieID)
131
     private List<String> getPersonsForTvSerieByTvSerieID(@PathVariable(name = "tvSerieID") String tvSerieID)
129
             throws InterruptedException, ExecutionException {
132
             throws InterruptedException, ExecutionException {
130
 
133
 
131
-        Future<IndexedSeq<Models.Person>> futureIndexedSeqPersons = akkaStreamFilesProcessing.getPersonsForTvSerieByTvSerieIdFuture(tvSerieID);
132
-        CompletableFuture<IndexedSeq<Models.Person>> seqCompletableFuture = CompletableFutureBuilder.buildcompletableFuture2(futureIndexedSeqPersons);
134
+        Future<IndexedSeq<Models.Person>> futureIndexedSeqPersons = akkaStreamFilesProcessing
135
+                .getPersonsForTvSerieByTvSerieIDFuture(tvSerieID);
136
+        CompletableFuture<IndexedSeq<Models.Person>> seqCompletableFuture = CompletableFutureBuilder
137
+                .buildcompletableFuture2(futureIndexedSeqPersons);
133
 
138
 
134
         while (!seqCompletableFuture.isDone()){
139
         while (!seqCompletableFuture.isDone()){
135
             logger.info("IS PROCESSING");
140
             logger.info("IS PROCESSING");

+ 1
- 1
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamComponents.scala View File

17
 //noinspection SpellCheckingInspection
17
 //noinspection SpellCheckingInspection
18
 object AkkaStreamComponents {
18
 object AkkaStreamComponents {
19
 
19
 
20
-  implicit val actor: ActorSystem = ActorSystem("AkkaStreamActor")
20
+  implicit val actorSystem: ActorSystem = ActorSystem("AkkaStreamActor")
21
 
21
 
22
   //flows building
22
   //flows building
23
   def buildPersonFlow(): Flow[Map[String, String], Person, NotUsed] = {
23
   def buildPersonFlow(): Flow[Map[String, String], Person, NotUsed] = {

+ 1
- 1
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessingFuture.scala View File

14
   def getTvSeriesByPrimaryTitleFuture(tvSerieTitle: String):Future[IndexedSeq[TvSerie]]
14
   def getTvSeriesByPrimaryTitleFuture(tvSerieTitle: String):Future[IndexedSeq[TvSerie]]
15
 
15
 
16
   def getPersonsForTvSerieByTvSerieTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[Person]]
16
   def getPersonsForTvSerieByTvSerieTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[Person]]
17
-  def getPersonsForTvSerieByTvSerieIdFuture(tvSerieId: String): Future[IndexedSeq[Person]]
17
+  def getPersonsForTvSerieByTvSerieIDFuture(tvSerieId: String): Future[IndexedSeq[Person]]
18
   def getAllTvSeriesFuture(): Future[Done]
18
   def getAllTvSeriesFuture(): Future[Done]
19
   def getAllPersonsFuture(): Future[Done]
19
   def getAllPersonsFuture(): Future[Done]
20
 }
20
 }

+ 18
- 63
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessingImpl.scala View File

1
 package fr.natan.akkastreamfileprocessingapi.service
1
 package fr.natan.akkastreamfileprocessingapi.service
2
 
2
 
3
 import akka.Done
3
 import akka.Done
4
-import akka.actor.ActorSystem
5
 import akka.stream.scaladsl.{Sink, Source}
4
 import akka.stream.scaladsl.{Sink, Source}
6
 import com.typesafe.scalalogging.slf4j.Logger
5
 import com.typesafe.scalalogging.slf4j.Logger
7
-import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.{nameBasics, titleBasics, titlePrincipalsBasics}
6
+import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.{nameBasics, titleBasics}
8
 import fr.natan.akkastreamfileprocessingapi.models.Models.{Person, TvSerie}
7
 import fr.natan.akkastreamfileprocessingapi.models.Models.{Person, TvSerie}
9
-import fr.natan.akkastreamfileprocessingapi.models.ModelsBuilder.buildPersonModel
10
 import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{
8
 import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{
9
+  actorSystem,
11
   buildAllPersonsSink,
10
   buildAllPersonsSink,
12
   buildAllTvSeriesSink,
11
   buildAllTvSeriesSink,
13
   buildAndValidateSource,
12
   buildAndValidateSource,
19
   filterTvSerieByIdFlow,
18
   filterTvSerieByIdFlow,
20
   filterTvSerieByPrimaryTitleFlow
19
   filterTvSerieByPrimaryTitleFlow
21
 }
20
 }
21
+import fr.natan.akkastreamfileprocessingapi.service.UtilitiesClass.{
22
+  getListOfPersonsForTvSerie,
23
+  getListOfPersonsIDByTvSerieID,
24
+  getTvSerieIDFuture,
25
+  getTvSerieIdByPrimaryTitle
26
+}
22
 import org.slf4j.LoggerFactory
27
 import org.slf4j.LoggerFactory
23
 import org.springframework.stereotype.Component
28
 import org.springframework.stereotype.Component
24
 
29
 
31
 @Component
36
 @Component
32
 class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
37
 class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
33
 
38
 
34
-  implicit val actorSystem: ActorSystem = ActorSystem("AkkaStreamActor")
35
   implicit val logger: Logger = Logger(LoggerFactory.getLogger(this.getClass))
39
   implicit val logger: Logger = Logger(LoggerFactory.getLogger(this.getClass))
36
 
40
 
37
   override def getPersonByIdFuture(personID: String): Future[Person] = {
41
   override def getPersonByIdFuture(personID: String): Future[Person] = {
103
     tvSriesFuture
107
     tvSriesFuture
104
   }
108
   }
105
 
109
 
106
-
107
-  private def getTvSerieIdByPrimaryTitle(primaryTitle: String): Future[Option[String]] = {
108
-    val source: Source[Map[String, String], _] = buildSource(inputFile = titleBasics)
109
-    val tvSerieIdFuture: Future[Option[String]] = source
110
-      .filter((rowMap: Map[String, String]) => {
111
-        rowMap.getOrElse(key = "primaryTitle", default = "") == primaryTitle
112
-      })
113
-      .map((rowMap: Map[String, String]) => rowMap.get("tconst"))
114
-      .runWith(Sink.head)
115
-
116
-    tvSerieIdFuture
117
-  }
118
-
119
-  private def getListOfPersonsIDByTvSerieID(tvSerieIdFuture: Future[Option[String]]): Future[IndexedSeq[Option[String]]] = {
120
-    val source: Source[Map[String, String], _] = buildSource(inputFile = titlePrincipalsBasics)
121
-    val personsIDsFuture: Future[IndexedSeq[Option[String]]] = source
122
-      .filter((rowMaps: Map[String, String]) => {
123
-        rowMaps.getOrElse(key = "tconst", default = "") == tvSerieIdFuture.value.get.get.get
124
-      })
125
-      .map((rowMap: Map[String, String]) => {
126
-        rowMap.get(key = "nconst")
127
-      })
128
-      .runWith(Sink.collection)
129
-
130
-    personsIDsFuture
131
-  }
132
-
133
-  private def getListOfPersonsForTvSerie(listPersonsIDsFuture: Future[IndexedSeq[Option[String]]]): Future[IndexedSeq[Person]] = {
134
-
135
-    val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
136
-    val personsFuture: Future[IndexedSeq[Person]] = source
137
-      .filter((rowMaps: Map[String, String]) => {
138
-        listPersonsIDsFuture.value.get.get.contains(rowMaps.get(key = "nconst"))
139
-      })
140
-      .map((rowMap: Map[String, String]) => {
141
-        buildPersonModel(rowMap)
142
-      })
143
-      .runWith(Sink.collection)
144
-
145
-    personsFuture
146
-  }
147
-
148
   override def getPersonsForTvSerieByTvSerieTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[Person]] = {
110
   override def getPersonsForTvSerieByTvSerieTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[Person]] = {
149
 
111
 
150
     //futures chaining
112
     //futures chaining
163
           _ =>
125
           _ =>
164
             logger.info("STEP 2/3 START")
126
             logger.info("STEP 2/3 START")
165
             val  start2: Long = System.currentTimeMillis()
127
             val  start2: Long = System.currentTimeMillis()
166
-            val listPersonIDsFuture: Future[IndexedSeq[Option[String]]] = getListOfPersonsIDByTvSerieID(tvSerieIdFuture = tvSerieIDFuture)
128
+            val listPersonIDsFuture: Future[IndexedSeq[Option[String]]] = getListOfPersonsIDByTvSerieID(
129
+              tvSerieIdFuture = tvSerieIDFuture)
167
             listPersonIDsFuture.andThen({
130
             listPersonIDsFuture.andThen({
168
               case Failure(exception) => logger.error(s"$exception")
131
               case Failure(exception) => logger.error(s"$exception")
169
               case Success(value) =>
132
               case Success(value) =>
174
                 _ =>
137
                 _ =>
175
                   logger.info("STEP 3/3 START")
138
                   logger.info("STEP 3/3 START")
176
                   val start3: Long = System.currentTimeMillis()
139
                   val start3: Long = System.currentTimeMillis()
177
-                  val personsTeamFuture: Future[IndexedSeq[Person]] = getListOfPersonsForTvSerie(listPersonsIDsFuture = listPersonIDsFuture)
140
+                  val personsTeamFuture: Future[IndexedSeq[Person]] = getListOfPersonsForTvSerie(
141
+                    listPersonsIDsFuture = listPersonIDsFuture)
178
                   personsTeamFuture.andThen({
142
                   personsTeamFuture.andThen({
179
                     case Failure(exception) => logger.error(s"$exception")
143
                     case Failure(exception) => logger.error(s"$exception")
180
                     case Success(value: IndexedSeq[Person]) =>
144
                     case Success(value: IndexedSeq[Person]) =>
187
     finalFuture
151
     finalFuture
188
   }
152
   }
189
 
153
 
190
-  private def getTvSerieIDFuture(tvSerieId: String): Future[Option[String]] ={
191
-    val source: Source[Map[String, String], _] = buildSource(inputFile = titlePrincipalsBasics)
192
-
193
-    val tvSerieIdFuture : Future[Option[String]]= source.
194
-      filter((rowMap: Map[String, String]) => rowMap.getOrElse(key = "tconst", default = "") == tvSerieId)
195
-      .map((rowMap: Map[String, String])=>rowMap.get(key = "tconst"))
196
-      .runWith(Sink.head)
197
-
198
-    tvSerieIdFuture
199
-  }
200
-
201
-  override def getPersonsForTvSerieByTvSerieIdFuture(tvSerieId: String): Future[IndexedSeq[Person]] = {
154
+  override def getPersonsForTvSerieByTvSerieIDFuture(tvSerieId: String): Future[IndexedSeq[Person]] = {
202
 
155
 
203
     val start1: Long = System.currentTimeMillis()
156
     val start1: Long = System.currentTimeMillis()
204
     logger.info("STEP 1/3")
157
     logger.info("STEP 1/3")
213
       .flatMap({
166
       .flatMap({
214
         _ => logger.info("STEP 2/3")
167
         _ => logger.info("STEP 2/3")
215
           val start2: Long = System.currentTimeMillis()
168
           val start2: Long = System.currentTimeMillis()
216
-          val  listOfPersonsIDsFuture : Future[IndexedSeq[Option[String]]] = getListOfPersonsIDByTvSerieID(tvSerieIdFuture = tvSerieIdFuture)
169
+          val  listOfPersonsIDsFuture : Future[IndexedSeq[Option[String]]] = getListOfPersonsIDByTvSerieID(
170
+            tvSerieIdFuture = tvSerieIdFuture)
217
           listOfPersonsIDsFuture.andThen({
171
           listOfPersonsIDsFuture.andThen({
218
             case Failure(exception) => logger.error(s"${exception.printStackTrace()}")
172
             case Failure(exception) => logger.error(s"${exception.printStackTrace()}")
219
-            case Success(value) =>
173
+            case Success(value: IndexedSeq[Option[String]]) =>
220
               value.foreach((personID: Option[String]) => logger.info(s"Person ID:$personID"))
174
               value.foreach((personID: Option[String]) => logger.info(s"Person ID:$personID"))
221
               logger.info(s"STEP 2/3 END, elapsed time:${(System.currentTimeMillis() - start2) / 1000} sec")
175
               logger.info(s"STEP 2/3 END, elapsed time:${(System.currentTimeMillis() - start2) / 1000} sec")
222
           })
176
           })
223
             .flatMap({
177
             .flatMap({
224
               _ => logger.info("STEP 3/3")
178
               _ => logger.info("STEP 3/3")
225
                 val start3: Long = System.currentTimeMillis()
179
                 val start3: Long = System.currentTimeMillis()
226
-                val personsTeamFuture: Future[IndexedSeq[Person]] = getListOfPersonsForTvSerie(listPersonsIDsFuture = listOfPersonsIDsFuture)
180
+                val personsTeamFuture: Future[IndexedSeq[Person]] = getListOfPersonsForTvSerie(
181
+                  listPersonsIDsFuture = listOfPersonsIDsFuture)
227
                 personsTeamFuture.andThen({
182
                 personsTeamFuture.andThen({
228
                   case Failure(exception) => logger.error(s"$exception")
183
                   case Failure(exception) => logger.error(s"$exception")
229
                   case Success(value: IndexedSeq[Person]) =>
184
                   case Success(value: IndexedSeq[Person]) =>

+ 65
- 0
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/UtilitiesClass.scala View File

1
+package fr.natan.akkastreamfileprocessingapi.service
2
+
3
+import akka.stream.scaladsl.{Sink, Source}
4
+import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.{nameBasics, titleBasics, titlePrincipalsBasics}
5
+import fr.natan.akkastreamfileprocessingapi.models.Models.Person
6
+import fr.natan.akkastreamfileprocessingapi.models.ModelsBuilder.buildPersonModel
7
+import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{actorSystem, buildSource}
8
+
9
+import scala.concurrent.Future
10
+
11
+object UtilitiesClass {
12
+
13
+  def getTvSerieIdByPrimaryTitle(primaryTitle: String): Future[Option[String]] = {
14
+    val source: Source[Map[String, String], _] = buildSource(inputFile = titleBasics)
15
+    val tvSerieIdFuture: Future[Option[String]] = source
16
+      .filter((rowMap: Map[String, String]) => {
17
+        rowMap.getOrElse(key = "primaryTitle", default = "") == primaryTitle
18
+      })
19
+      .map((rowMap: Map[String, String]) => rowMap.get("tconst"))
20
+      .runWith(Sink.head)
21
+
22
+    tvSerieIdFuture
23
+  }
24
+
25
+  def getListOfPersonsIDByTvSerieID(tvSerieIdFuture: Future[Option[String]]): Future[IndexedSeq[Option[String]]] = {
26
+    val source: Source[Map[String, String], _] = buildSource(inputFile = titlePrincipalsBasics)
27
+    val personsIDsFuture: Future[IndexedSeq[Option[String]]] = source
28
+      .filter((rowMaps: Map[String, String]) => {
29
+        rowMaps.getOrElse(key = "tconst", default = "") == tvSerieIdFuture.value.get.get.get
30
+      })
31
+      .map((rowMap: Map[String, String]) => {
32
+        rowMap.get(key = "nconst")
33
+      })
34
+      .runWith(Sink.collection)
35
+
36
+    personsIDsFuture
37
+  }
38
+
39
+  def getListOfPersonsForTvSerie(listPersonsIDsFuture: Future[IndexedSeq[Option[String]]]): Future[IndexedSeq[Person]] = {
40
+
41
+    val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
42
+    val personsFuture: Future[IndexedSeq[Person]] = source
43
+      .filter((rowMaps: Map[String, String]) => {
44
+        listPersonsIDsFuture.value.get.get.contains(rowMaps.get(key = "nconst"))
45
+      })
46
+      .map((rowMap: Map[String, String]) => {
47
+        buildPersonModel(rowMap)
48
+      })
49
+      .runWith(Sink.collection)
50
+
51
+    personsFuture
52
+  }
53
+
54
+  def getTvSerieIDFuture(tvSerieId: String): Future[Option[String]] = {
55
+    val source: Source[Map[String, String], _] = buildSource(inputFile = titlePrincipalsBasics)
56
+
57
+    val tvSerieIdFuture: Future[Option[String]] = source.
58
+      filter((rowMap: Map[String, String]) => rowMap.getOrElse(key = "tconst", default = "") == tvSerieId)
59
+      .map((rowMap: Map[String, String]) => rowMap.get(key = "tconst"))
60
+      .runWith(Sink.head)
61
+
62
+    tvSerieIdFuture
63
+  }
64
+
65
+}

Powered by TurnKey Linux.