Bladeren bron

first commit

placidenduwayo 2 jaren geleden
bovenliggende
commit
90aa9a6711

+ 5
- 30
src/main/java/fr/natan/akkastreamfileprocessingapi/controller/TvSeriesController.java Bestand weergeven

@@ -13,13 +13,11 @@ import org.springframework.web.bind.annotation.RequestMethod;
13 13
 import org.springframework.web.bind.annotation.RestController;
14 14
 import play.api.libs.json.JsValue;
15 15
 import play.api.libs.json.Json;
16
-import scala.Option;
17 16
 import scala.collection.IndexedSeq;
18 17
 import scala.concurrent.Future;
19 18
 
20 19
 import java.util.ArrayList;
21 20
 import java.util.List;
22
-import java.util.Random;
23 21
 import java.util.concurrent.CompletableFuture;
24 22
 import java.util.concurrent.ExecutionException;
25 23
 
@@ -30,7 +28,6 @@ public class TvSeriesController {
30 28
 
31 29
     private final AkkaStreamFileProcessingFuture akkaStreamFilesProcessing;
32 30
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
33
-    private static final int BORN_INF = 100, BORN_SUP = 2000;
34 31
 
35 32
     public TvSeriesController(AkkaStreamFileProcessingFuture akkaStreamFilesProcessing) {
36 33
         this.akkaStreamFilesProcessing = akkaStreamFilesProcessing;
@@ -38,13 +35,9 @@ public class TvSeriesController {
38 35
 
39 36
     @RequestMapping(value = "/persons/id/{personID}", method = RequestMethod.GET)
40 37
     private String getPersonByID(@PathVariable(name = "personID") String personID) throws ExecutionException, InterruptedException {
41
-        Future<Option<ModelsAndJsonMap.Person>> futurePerson = akkaStreamFilesProcessing.getPersonByIdFuture(personID);
38
+        Future<ModelsAndJsonMap.Person> futurePerson = akkaStreamFilesProcessing.getPersonByIdFuture(personID);
42 39
         CompletableFuture<ModelsAndJsonMap.Person> completableFuture = CompletableFutureBuilder
43 40
                 .buildcompletableFuture1(futurePerson);
44
-        while (!completableFuture.isDone()) {
45
-            logger.info("IS PROCESSING...");
46
-            Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
47
-        }
48 41
         ModelsAndJsonMap.Person person = completableFuture.get();
49 42
 
50 43
         JsValue personJs = Json.toJson(person, person.personJsonFormatter());
@@ -55,13 +48,9 @@ public class TvSeriesController {
55 48
     private String getPersonByName(@PathVariable(name = "primaryName") String primaryName)
56 49
             throws ExecutionException, InterruptedException {
57 50
 
58
-        Future<Option<ModelsAndJsonMap.Person>> personFuture = akkaStreamFilesProcessing.getPersonByNameFuture(primaryName);
51
+        Future<ModelsAndJsonMap.Person> personFuture = akkaStreamFilesProcessing.getPersonByNameFuture(primaryName);
59 52
         CompletableFuture<ModelsAndJsonMap.Person> personCompletableFuture = CompletableFutureBuilder
60 53
                 .buildcompletableFuture1(personFuture);
61
-        while (!personCompletableFuture.isDone()) {
62
-            logger.info("IS PROCESSING...");
63
-            Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
64
-        }
65 54
         ModelsAndJsonMap.Person person = personCompletableFuture.get();
66 55
         JsValue personJs = Json.toJson(person, person.personJsonFormatter());
67 56
         return Json.prettyPrint(personJs);
@@ -70,13 +59,10 @@ public class TvSeriesController {
70 59
     @RequestMapping(value = "/tvseries/id/{tvSerieId}", method = RequestMethod.GET)
71 60
     private String getTvSerieByID(@PathVariable(name = "tvSerieId") String tvSerieID)
72 61
             throws ExecutionException, InterruptedException {
73
-        Future<Option<ModelsAndJsonMap.TvSerie>> tvSerieFuture = akkaStreamFilesProcessing.getTvSerieByIdFuture(tvSerieID);
62
+        Future<ModelsAndJsonMap.TvSerie> tvSerieFuture = akkaStreamFilesProcessing.getTvSerieByIdFuture(tvSerieID);
74 63
         CompletableFuture<ModelsAndJsonMap.TvSerie> tvSerieCompletableFuture = CompletableFutureBuilder
75 64
                 .buildcompletableFuture1(tvSerieFuture);
76
-        while (!tvSerieCompletableFuture.isDone()) {
77
-            logger.info("IS PROCESSING...");
78
-            Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
79
-        }
65
+
80 66
         ModelsAndJsonMap.TvSerie tvSerie = tvSerieCompletableFuture.get();
81 67
         JsValue tvSerieJs = Json.toJson(tvSerie, tvSerie.tvSerieJsonFormatter());
82 68
         return Json.prettyPrint(tvSerieJs);
@@ -90,10 +76,7 @@ public class TvSeriesController {
90 76
                 .getTvSeriesByPrimaryTitleFuture(tvseriePrimaryTitle);
91 77
         CompletableFuture<IndexedSeq<ModelsAndJsonMap.TvSerie>> completableFuture =
92 78
                 CompletableFutureBuilder.buildcompletableFuture2(listFuture);
93
-        while (!completableFuture.isDone()) {
94
-            logger.info("IS PROCESSING...");
95
-            Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
96
-        }
79
+
97 80
         IndexedSeq<ModelsAndJsonMap.TvSerie> tvSerieList = completableFuture.get();
98 81
         List<String> tvSeries = new ArrayList<>();
99 82
         tvSerieList.foreach(tvSerie -> {
@@ -113,10 +96,6 @@ public class TvSeriesController {
113 96
                 .getPersonsForTvSerieByTvSerieTitleFuture(tvSerieTitle);
114 97
         CompletableFuture<IndexedSeq<ModelsAndJsonMap.Person>> completableFuture = CompletableFutureBuilder
115 98
                 .buildcompletableFuture2(futurePersonSeq);
116
-        while (!completableFuture.isDone()){
117
-            logger.info("IS PROCESSING...");
118
-            Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
119
-        }
120 99
 
121 100
         IndexedSeq<ModelsAndJsonMap.Person> personIndexedSeq = completableFuture.get();
122 101
         List<String> personList = new ArrayList<>();
@@ -138,10 +117,6 @@ public class TvSeriesController {
138 117
         CompletableFuture<IndexedSeq<ModelsAndJsonMap.Person>> seqCompletableFuture = CompletableFutureBuilder
139 118
                 .buildcompletableFuture2(futureIndexedSeqPersons);
140 119
 
141
-        while (!seqCompletableFuture.isDone()){
142
-            logger.info("IS PROCESSING...");
143
-            Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
144
-        }
145 120
         IndexedSeq<ModelsAndJsonMap.Person> personIndexedSeq = seqCompletableFuture.get();
146 121
         List<String> personsList = new ArrayList<>();
147 122
         personIndexedSeq.foreach(person ->{

+ 4
- 3
src/main/java/fr/natan/akkastreamfileprocessingapi/exceptionshandler/BusinessExceptionHandler.java Bestand weergeven

@@ -1,7 +1,7 @@
1 1
 package fr.natan.akkastreamfileprocessingapi.exceptionshandler;
2 2
 
3 3
 import fr.natan.akkastreamfileprocessingapi.businessexceptions.FileNotFoundException;
4
-import fr.natan.akkastreamfileprocessingapi.businessexceptions.MovieNotFoundException;
4
+import fr.natan.akkastreamfileprocessingapi.businessexceptions.TvSerieNotFoundException;
5 5
 import org.springframework.http.HttpStatus;
6 6
 import org.springframework.http.ResponseEntity;
7 7
 import org.springframework.web.bind.annotation.ControllerAdvice;
@@ -15,8 +15,9 @@ public class BusinessExceptionHandler {
15 15
         return new ResponseEntity<>("The File Is Not Found Exception", HttpStatus.NOT_FOUND);
16 16
     }
17 17
 
18
-    @ExceptionHandler(value = MovieNotFoundException.class)
19
-    private ResponseEntity<String> handleMovieNotFoundException(){
18
+    @ExceptionHandler(value = TvSerieNotFoundException.class)
19
+    private ResponseEntity<String> handleTvSerieNotFoundException(){
20 20
         return new ResponseEntity<>("Movie Title Not Found Exception", HttpStatus.NOT_FOUND);
21 21
     }
22
+
22 23
 }

+ 17
- 7
src/main/java/fr/natan/akkastreamfileprocessingapi/futurecompleteness/CompletableFutureBuilder.java Bestand weergeven

@@ -1,25 +1,31 @@
1 1
 package fr.natan.akkastreamfileprocessingapi.futurecompleteness;
2 2
 
3
-import scala.Option;
4 3
 import scala.collection.IndexedSeq;
5 4
 import scala.concurrent.Future;
6 5
 
7 6
 import java.util.concurrent.CompletableFuture;
8 7
 import java.util.concurrent.Executors;
9 8
 import java.util.concurrent.TimeUnit;
9
+import java.util.logging.Logger;
10 10
 
11 11
 
12 12
 public class CompletableFutureBuilder<T> {
13 13
 
14
-    public static<T> CompletableFuture<T> buildcompletableFuture1(Future<Option<T>> futureT) {
14
+   private static final Logger logger = Logger.getLogger("log");
15
+    public static<T> CompletableFuture<T> buildcompletableFuture1(Future<T> futureT) {
15 16
         CompletableFuture<T> completableFuture = new CompletableFuture<>();
17
+
18
+
16 19
         Executors.newSingleThreadExecutor().submit(() -> {
17 20
             while(!futureT.isCompleted()){
18
-                TimeUnit.MILLISECONDS.sleep(300);
21
+                try {
22
+                    TimeUnit.MILLISECONDS.sleep(300);
23
+                } catch (InterruptedException e) {
24
+                    throw new RuntimeException(e);
25
+                }
19 26
             }
20
-            T task = futureT.value().get().get().get();
27
+            T task = futureT.value().get().get();
21 28
             completableFuture.complete(task);
22
-            return null;
23 29
         });
24 30
 
25 31
         return completableFuture;
@@ -30,12 +36,16 @@ public class CompletableFutureBuilder<T> {
30 36
         CompletableFuture<IndexedSeq<T>> completableFuture = new CompletableFuture<>();
31 37
         Executors.newSingleThreadExecutor().submit(() -> {
32 38
             while (!futureListT.isCompleted()) {
33
-                TimeUnit.MILLISECONDS.sleep(300);
39
+                try {
40
+                    TimeUnit.MILLISECONDS.sleep(300);
41
+                } catch (InterruptedException e) {
42
+                    throw new RuntimeException(e);
43
+                }
34 44
             }
35 45
 
36 46
             IndexedSeq<T> task = futureListT.value().get().get();
37 47
             completableFuture.complete(task);
38
-            return null;
48
+
39 49
         });
40 50
 
41 51
         return completableFuture;

src/main/scala/fr/natan/akkastreamfileprocessingapi/businessexceptions/MovieNotFoundException.scala → src/main/scala/fr/natan/akkastreamfileprocessingapi/businessexceptions/PersonNotFoundException.scala Bestand weergeven

@@ -1,3 +1,3 @@
1 1
 package fr.natan.akkastreamfileprocessingapi.businessexceptions
2 2
 
3
-class MovieNotFoundException extends Exception{}
3
+class PersonNotFoundException extends Exception{}

+ 3
- 0
src/main/scala/fr/natan/akkastreamfileprocessingapi/businessexceptions/TvSerieNotFoundException.scala Bestand weergeven

@@ -0,0 +1,3 @@
1
+package fr.natan.akkastreamfileprocessingapi.businessexceptions
2
+
3
+class TvSerieNotFoundException extends Exception{}

+ 2
- 2
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamComponents.scala Bestand weergeven

@@ -7,8 +7,8 @@ import akka.{Done, NotUsed}
7 7
 import com.typesafe.scalalogging.slf4j.Logger
8 8
 import fr.natan.akkastreamfileprocessingapi.businessexceptions.FileNotFoundException
9 9
 import fr.natan.akkastreamfileprocessingapi.models.ModelsAndJsonMap.{Person, TvSerie}
10
-import fr.natan.akkastreamfileprocessingapi.valitator.Validators.fileExists
11 10
 import fr.natan.akkastreamfileprocessingapi.models.ModelsBuilder.{buildPersonModel, buildTvSerieModel}
11
+import fr.natan.akkastreamfileprocessingapi.valitator.Validators.fileExists
12 12
 
13 13
 import java.io.File
14 14
 import java.nio.file.Paths
@@ -91,7 +91,7 @@ object AkkaStreamComponents {
91 91
 
92 92
   //source building
93 93
 
94
-  def buildSource(inputFile: File): Source[Map[String, String], NotUsed] = {
94
+  private def buildSource(inputFile: File): Source[Map[String, String], NotUsed] = {
95 95
 
96 96
     var datasource: Source[Map[String, String], NotUsed] = null
97 97
     if (!fileExists(inputFile.getPath)) {

+ 3
- 4
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessingFuture.scala Bestand weergeven

@@ -5,12 +5,11 @@ import fr.natan.akkastreamfileprocessingapi.models.ModelsAndJsonMap.{Person, TvS
5 5
 
6 6
 import scala.concurrent.Future
7 7
 
8
-//noinspection SpellCheckingInspection,AccessorLikeMethodIsUnit
9 8
 trait AkkaStreamFileProcessingFuture {
10
-  def getPersonByIdFuture(personID: String): Future[Option[Person]]
11
-  def getPersonByNameFuture(primaryName: String):  Future[Option[Person]]
9
+  def getPersonByIdFuture(personID: String): Future[Person]
10
+  def getPersonByNameFuture(primaryName: String):  Future[Person]
12 11
 
13
-  def getTvSerieByIdFuture(tvSerieID: String): Future[Option[TvSerie]]
12
+  def getTvSerieByIdFuture(tvSerieID: String): Future[TvSerie]
14 13
   def getTvSeriesByPrimaryTitleFuture(tvSerieTitle: String):Future[IndexedSeq[TvSerie]]
15 14
 
16 15
   def getPersonsForTvSerieByTvSerieTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[Person]]

+ 37
- 40
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessingImpl.scala Bestand weergeven

@@ -3,13 +3,13 @@ package fr.natan.akkastreamfileprocessingapi.service
3 3
 import akka.Done
4 4
 import akka.stream.scaladsl.{Sink, Source}
5 5
 import com.typesafe.scalalogging.slf4j.Logger
6
-import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.{nameBasics, titleBasics}
6
+import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.{nameBasics, titleBasics, titlePrincipalsBasics}
7 7
 import fr.natan.akkastreamfileprocessingapi.models.ModelsAndJsonMap.{Person, TvSerie}
8 8
 import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{
9
-  actorSystem, buildAllPersonsSink, buildAllTvSeriesSink, buildAndValidateSource, buildPersonFlow, buildSource,
10
-  buildTvSerieFlow, filterPersonByIdFlow, filterPersonByNameFlow, filterTvSerieByIdFlow, filterTvSerieByPrimaryTitleFlow}
11
-import fr.natan.akkastreamfileprocessingapi.service.UtilitiesClass.{
12
-  getListOfPersonsForTvSerie, getListOfPersonsIDByTvSerieID, getTvSerieIDFuture, getTvSerieIdByPrimaryTitle}
9
+  actorSystem, buildAllPersonsSink, buildAllTvSeriesSink, buildAndValidateSource, buildPersonFlow, buildTvSerieFlow,
10
+  filterPersonByIdFlow, filterPersonByNameFlow, filterTvSerieByIdFlow, filterTvSerieByPrimaryTitleFlow}
11
+import fr.natan.akkastreamfileprocessingapi.service.UtilitiesClass.{getListOfPersonsForTvSerie, getListOfPersonsIDByTvSerieID,
12
+  getTvSerieIDFuture, getTvSerieIdByPrimaryTitle}
13 13
 import org.slf4j.LoggerFactory
14 14
 import org.springframework.stereotype.Component
15 15
 
@@ -24,46 +24,43 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
24 24
 
25 25
   implicit val logger: Logger = Logger(LoggerFactory.getLogger(this.getClass))
26 26
 
27
-  override def getPersonByIdFuture(personID: String): Future[Option[Person]] = {
28
-    val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
27
+  override def getPersonByIdFuture(personID: String): Future[Person] = {
28
+    val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = nameBasics)
29 29
 
30 30
     val start: Long = System.currentTimeMillis()
31
-    val personFuture: Future[Option[Person]] = source
31
+    val personFuture: Future[Person] = source
32 32
       .via(flow = filterPersonByIdFlow(personID = personID))
33
-      .runWith(Sink.headOption[Person])
33
+      .runWith(Sink.head[Person])
34 34
 
35 35
     personFuture.andThen({
36
-      case Failure(exception) => logger.info(s"$exception")
37
-      case Success(value: Option[Person]) =>
38
-        logger.info(s"$value")
39
-        logger.info(s"SUCCESS, elapsed time:${(System.currentTimeMillis() - start) / 1000} sec")
36
+      case Failure(exception) => logger.error(s"!${exception.printStackTrace()}")
40 37
     })
41 38
 
42 39
     personFuture
43 40
   }
44 41
 
45
-  override def getPersonByNameFuture(primaryName: String): Future[Option[Person]] = {
46
-    val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
42
+  override def getPersonByNameFuture(primaryName: String): Future[Person] = {
43
+    val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = nameBasics)
47 44
 
48 45
     val start: Long = System.currentTimeMillis()
49
-    val personFuture: Future[Option[Person]] = source
46
+    val personFuture: Future[Person] = source
50 47
       .via(flow = filterPersonByNameFlow(primaryName = primaryName))
51
-      .runWith(Sink.headOption[Person])
48
+      .runWith(Sink.head[Person])
52 49
 
53 50
     personFuture
54 51
   }
55 52
 
56
-  override def getTvSerieByIdFuture(tvSerieID: String): Future[Option[TvSerie]] = {
57
-    val source: Source[Map[String, String], _] = buildSource(inputFile = titleBasics)
53
+  override def getTvSerieByIdFuture(tvSerieID: String): Future[TvSerie] = {
54
+    val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = titleBasics)
58 55
 
59 56
     val start: Long = System.currentTimeMillis()
60
-    val tvSerieFuture: Future[Option[TvSerie]] = source
57
+    val tvSerieFuture: Future[TvSerie] = source
61 58
       .via(flow = filterTvSerieByIdFlow(tvSerieID = tvSerieID))
62
-      .runWith(Sink.headOption[TvSerie])
59
+      .runWith(Sink.head[TvSerie])
63 60
 
64 61
     tvSerieFuture.onComplete({
65 62
       case Failure(exception) => logger.info(s"$exception")
66
-      case Success(value: Option[TvSerie]) =>
63
+      case Success(value: TvSerie) =>
67 64
         logger.info(s"$value")
68 65
         logger.info(s"SUCCESS, elapsed time:${(System.currentTimeMillis() - start) / 1000} sec")
69 66
     })
@@ -91,13 +88,15 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
91 88
   override def getPersonsForTvSerieByTvSerieTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[Person]] = {
92 89
 
93 90
     //futures chaining
94
-    logger.info("STEP 1/3 START")
91
+    logger.info("STEP 1/3 ...")
95 92
     val start1: Long = System.currentTimeMillis()
96
-    val tvSerieIDFuture: Future[Option[String]] = getTvSerieIdByPrimaryTitle(primaryTitle = tvSeriePrimaryTitle)
93
+    val tvSerieIDFuture: Future[Option[String]] = getTvSerieIdByPrimaryTitle(
94
+      primaryTitle = tvSeriePrimaryTitle,
95
+      inputFile = titleBasics
96
+    )
97 97
 
98 98
     val finalFuture: Future[IndexedSeq[Person]] =
99 99
       tvSerieIDFuture.andThen({
100
-        case Failure(exception) => logger.error(s"$exception")
101 100
         case Success(value: Option[String]) =>
102 101
           logger.info(s"TvSerie ID: $value")
103 102
           logger.info(s"STEP 1/3 END, elapsed time:${(System.currentTimeMillis() - start1) / 1000} sec")
@@ -105,24 +104,23 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
105 104
       })
106 105
         .flatMap({
107 106
           _ =>
108
-            logger.info("STEP 2/3 START")
107
+            logger.info("STEP 2/3 ...")
109 108
             val start2: Long = System.currentTimeMillis()
110 109
             val listPersonIDsFuture: Future[IndexedSeq[Option[String]]] = getListOfPersonsIDByTvSerieID(
111
-              tvSerieIdFuture = tvSerieIDFuture)
110
+              tvSerieIdFuture = tvSerieIDFuture, inputFile = titlePrincipalsBasics
111
+            )
112 112
             listPersonIDsFuture.andThen({
113
-              case Failure(exception) => logger.error(s"$exception")
114 113
               case Success(value) =>
115 114
                 value.foreach((personID: Option[String]) => logger.info(s"Person ID:$personID"))
116 115
                 logger.info(s"STEP 2/3 END, elapsed time:${(System.currentTimeMillis() - start2) / 1000} sec")
117 116
             })
118 117
               .flatMap({
119 118
                 _ =>
120
-                  logger.info("STEP 3/3 START")
119
+                  logger.info("STEP 3/3 ...")
121 120
                   val start3: Long = System.currentTimeMillis()
122 121
                   val personsTeamFuture: Future[IndexedSeq[Person]] = getListOfPersonsForTvSerie(
123
-                    listPersonsIDsFuture = listPersonIDsFuture)
122
+                    listPersonsIDsFuture = listPersonIDsFuture, inputFile = nameBasics)
124 123
                   personsTeamFuture.andThen({
125
-                    case Failure(exception) => logger.error(s"$exception")
126 124
                     case Success(value: IndexedSeq[Person]) =>
127 125
                       value.foreach((person: Person) => logger.info(s"${person.toString}"))
128 126
                       logger.info(s"STEP 3/3 END, elapsed time:${(System.currentTimeMillis() - start3) / 1000} sec")
@@ -136,8 +134,9 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
136 134
   override def getPersonsForTvSerieByTvSerieIDFuture(tvSerieId: String): Future[IndexedSeq[Person]] = {
137 135
 
138 136
     val start1: Long = System.currentTimeMillis()
139
-    logger.info("STEP 1/3")
140
-    val tvSerieIdFuture: Future[Option[String]] = getTvSerieIDFuture(tvSerieId = tvSerieId)
137
+    logger.info("STEP 1/3 ...")
138
+    val tvSerieIdFuture: Future[Option[String]] = getTvSerieIDFuture(
139
+      tvSerieID = tvSerieId, logger = logger, inputFile = titleBasics)
141 140
     tvSerieIdFuture.andThen({
142 141
       case Failure(exception) => logger.error(s"${exception.printStackTrace()}")
143 142
       case Success(value: Option[String]) =>
@@ -147,10 +146,10 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
147 146
     })
148 147
       .flatMap({
149 148
         _ =>
150
-          logger.info("STEP 2/3")
149
+          logger.info("STEP 2/3 ...")
151 150
           val start2: Long = System.currentTimeMillis()
152 151
           val listOfPersonsIDsFuture: Future[IndexedSeq[Option[String]]] = getListOfPersonsIDByTvSerieID(
153
-            tvSerieIdFuture = tvSerieIdFuture)
152
+            tvSerieIdFuture = tvSerieIdFuture, inputFile = titlePrincipalsBasics)
154 153
           listOfPersonsIDsFuture.andThen({
155 154
             case Failure(exception) => logger.error(s"${exception.printStackTrace()}")
156 155
             case Success(value: IndexedSeq[Option[String]]) =>
@@ -159,10 +158,10 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
159 158
           })
160 159
             .flatMap({
161 160
               _ =>
162
-                logger.info("STEP 3/3")
161
+                logger.info("STEP 3/3 ...")
163 162
                 val start3: Long = System.currentTimeMillis()
164 163
                 val personsTeamFuture: Future[IndexedSeq[Person]] = getListOfPersonsForTvSerie(
165
-                  listPersonsIDsFuture = listOfPersonsIDsFuture)
164
+                  listPersonsIDsFuture = listOfPersonsIDsFuture, inputFile = nameBasics)
166 165
                 personsTeamFuture.andThen({
167 166
                   case Failure(exception) => logger.error(s"$exception")
168 167
                   case Success(value: IndexedSeq[Person]) =>
@@ -175,7 +174,7 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
175 174
   }
176 175
 
177 176
   override def getAllPersonsFuture: Future[Done] = {
178
-    val personSource: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
177
+    val personSource: Source[Map[String, String], _] = buildAndValidateSource(inputFile = nameBasics)
179 178
     //graph
180 179
     val startTime: Long = System.currentTimeMillis()
181 180
     val result: Future[Done] = personSource
@@ -189,7 +188,6 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
189 188
         val time: Long = (System.currentTimeMillis() - startTime) / 100
190 189
         logger.info(s"elapsed time: $time")
191 190
     }
192
-
193 191
     result
194 192
   }
195 193
 
@@ -211,7 +209,6 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
211 209
 
212 210
     results
213 211
   }
214
-
215 212
 }
216 213
 
217 214
 

+ 33
- 14
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/UtilitiesClass.scala Bestand weergeven

@@ -1,17 +1,22 @@
1 1
 package fr.natan.akkastreamfileprocessingapi.service
2 2
 
3
-import akka.stream.scaladsl.{Sink, Source}
4
-import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.{nameBasics, titleBasics, titlePrincipalsBasics}
3
+import akka.NotUsed
4
+import akka.stream.scaladsl.{Flow, Sink, Source}
5
+import com.typesafe.scalalogging.slf4j.Logger
6
+import fr.natan.akkastreamfileprocessingapi.businessexceptions.TvSerieNotFoundException
5 7
 import fr.natan.akkastreamfileprocessingapi.models.ModelsAndJsonMap.Person
6 8
 import fr.natan.akkastreamfileprocessingapi.models.ModelsBuilder.buildPersonModel
7
-import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{actorSystem, buildSource}
9
+import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{actorSystem, buildAndValidateSource}
8 10
 
11
+import java.io.File
12
+import scala.concurrent.ExecutionContext.Implicits.global
9 13
 import scala.concurrent.Future
14
+import scala.util.{Failure, Success}
10 15
 
11 16
 object UtilitiesClass {
12 17
 
13
-  def getTvSerieIdByPrimaryTitle(primaryTitle: String): Future[Option[String]] = {
14
-    val source: Source[Map[String, String], _] = buildSource(inputFile = titleBasics)
18
+  def getTvSerieIdByPrimaryTitle(primaryTitle: String, inputFile: File): Future[Option[String]] = {
19
+    val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = inputFile)
15 20
     val tvSerieIdFuture: Future[Option[String]] = source
16 21
       .filter((rowMap: Map[String, String]) => {
17 22
         rowMap.getOrElse(key = "primaryTitle", default = "") == primaryTitle
@@ -22,8 +27,8 @@ object UtilitiesClass {
22 27
     tvSerieIdFuture
23 28
   }
24 29
 
25
-  def getListOfPersonsIDByTvSerieID(tvSerieIdFuture: Future[Option[String]]): Future[IndexedSeq[Option[String]]] = {
26
-    val source: Source[Map[String, String], _] = buildSource(inputFile = titlePrincipalsBasics)
30
+  def getListOfPersonsIDByTvSerieID(tvSerieIdFuture: Future[Option[String]], inputFile: File): Future[IndexedSeq[Option[String]]] = {
31
+    val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = inputFile)
27 32
     val personsIDsFuture: Future[IndexedSeq[Option[String]]] = source
28 33
       .filter((rowMaps: Map[String, String]) => {
29 34
         rowMaps.getOrElse(key = "tconst", default = "") == tvSerieIdFuture.value.get.get.get
@@ -36,9 +41,9 @@ object UtilitiesClass {
36 41
     personsIDsFuture
37 42
   }
38 43
 
39
-  def getListOfPersonsForTvSerie(listPersonsIDsFuture: Future[IndexedSeq[Option[String]]]): Future[IndexedSeq[Person]] = {
44
+  def getListOfPersonsForTvSerie(listPersonsIDsFuture: Future[IndexedSeq[Option[String]]], inputFile: File): Future[IndexedSeq[Person]] = {
40 45
 
41
-    val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
46
+    val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = inputFile)
42 47
     val personsFuture: Future[IndexedSeq[Person]] = source
43 48
       .filter((rowMaps: Map[String, String]) => {
44 49
         listPersonsIDsFuture.value.get.get.contains(rowMaps.get(key = "nconst"))
@@ -51,14 +56,28 @@ object UtilitiesClass {
51 56
     personsFuture
52 57
   }
53 58
 
54
-  def getTvSerieIDFuture(tvSerieId: String): Future[Option[String]] = {
55
-    val source: Source[Map[String, String], _] = buildSource(inputFile = titlePrincipalsBasics)
59
+  private def getTvSerieIdFlow(tvSerieID: String): Flow[Map[String, String], Option[String], NotUsed] = {
60
+    val tvSerieIDFlow = Flow[Map[String, String]]
61
+      .filter(rowMap => rowMap.getOrElse(key = "tconst", default = "") == tvSerieID)
62
+      .map(row => row.get("tconst"))
56 63
 
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"))
64
+    tvSerieIDFlow
65
+  }
66
+
67
+  def getTvSerieIDFuture(tvSerieID: String, logger: Logger, inputFile: File): Future[Option[String]] = {
68
+    val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = inputFile)
69
+
70
+    val tvSerieIdFuture: Future[Option[String]] = source
71
+      .filter(rowMap => rowMap.getOrElse(key = "tconst", default = "") == tvSerieID)
72
+      .map(row => row.get("tconst"))
60 73
       .runWith(Sink.head)
61 74
 
75
+    tvSerieIdFuture.onComplete({
76
+      case Failure(noSuchElementException: NoSuchElementException) => throw new TvSerieNotFoundException()
77
+      case Failure(exception: Exception) => throw new RuntimeException()
78
+      case Success(value) => logger.info(s"$value")
79
+    })
80
+
62 81
     tvSerieIdFuture
63 82
   }
64 83
 

Powered by TurnKey Linux.