Browse Source

first commit

placidenduwayo 1 year ago
parent
commit
0707094eab

+ 12
- 9
src/main/java/fr/natan/akkastreamfileprocessingapi/controller/TvSeriesController.java View File

45
             Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
45
             Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
46
         }
46
         }
47
         Models.Person person = completableFuture.get();
47
         Models.Person person = completableFuture.get();
48
-
49
-        return Json.toJson(person, person.personFormat()).toString();
48
+        JsValue personJs = Json.toJson(person, person.personFormat());
49
+        return Json.prettyPrint(personJs);
50
     }
50
     }
51
 
51
 
52
     @RequestMapping(value = "/persons/name/{primaryName}", method = RequestMethod.GET)
52
     @RequestMapping(value = "/persons/name/{primaryName}", method = RequestMethod.GET)
61
             Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
61
             Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
62
         }
62
         }
63
         Models.Person person = personCompletableFuture.get();
63
         Models.Person person = personCompletableFuture.get();
64
-        return Json.toJson(person, person.personFormat()).toString();
64
+        JsValue personJs = Json.toJson(person, person.personFormat());
65
+        return Json.prettyPrint(personJs);
65
     }
66
     }
66
 
67
 
67
     @RequestMapping(value = "/tvseries/id/{tvSerieId}", method = RequestMethod.GET)
68
     @RequestMapping(value = "/tvseries/id/{tvSerieId}", method = RequestMethod.GET)
75
             Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
76
             Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
76
         }
77
         }
77
         Models.TvSerie tvSerie = tvSerieCompletableFuture.get();
78
         Models.TvSerie tvSerie = tvSerieCompletableFuture.get();
78
-        return Json.toJson(tvSerie, tvSerie.tvSerieFormat()).toString();
79
+        JsValue tvSerieJs = Json.toJson(tvSerie, tvSerie.tvSerieFormat());
80
+        return Json.prettyPrint(tvSerieJs);
79
     }
81
     }
80
 
82
 
81
     @RequestMapping(value = "/tvseries/title/{tvseriePrimaryTitle}", method = RequestMethod.GET)
83
     @RequestMapping(value = "/tvseries/title/{tvseriePrimaryTitle}", method = RequestMethod.GET)
93
         List<String> tvSeries = new ArrayList<>();
95
         List<String> tvSeries = new ArrayList<>();
94
         tvSerieList.foreach(tvSerie -> {
96
         tvSerieList.foreach(tvSerie -> {
95
             JsValue tvSerieJs = Json.toJson(tvSerie, tvSerie.tvSerieFormat());
97
             JsValue tvSerieJs = Json.toJson(tvSerie, tvSerie.tvSerieFormat());
96
-            tvSeries.add(tvSerieJs.toString());
98
+            tvSeries.add(Json.prettyPrint(tvSerieJs));
97
             return null;
99
             return null;
98
         });
100
         });
99
 
101
 
112
         }
114
         }
113
 
115
 
114
         IndexedSeq<Models.Person> personIndexedSeq = completableFuture.get();
116
         IndexedSeq<Models.Person> personIndexedSeq = completableFuture.get();
115
-        List<String> persons = new ArrayList<>();
117
+        List<String> personList = new ArrayList<>();
116
         personIndexedSeq.foreach(person -> {
118
         personIndexedSeq.foreach(person -> {
117
             JsValue personJs = Json.toJson(person, person.personFormat());
119
             JsValue personJs = Json.toJson(person, person.personFormat());
118
-            persons.add(personJs.toString());
120
+            personList.add(Json.prettyPrint(personJs));
119
             return null;
121
             return null;
120
         });
122
         });
121
 
123
 
122
-        return persons;
124
+        return personList;
123
     }
125
     }
124
 
126
 
125
     @RequestMapping(value = "/persons/tvseries/id/{tvSerieID}", method = RequestMethod.GET)
127
     @RequestMapping(value = "/persons/tvseries/id/{tvSerieID}", method = RequestMethod.GET)
136
         IndexedSeq<Models.Person> personIndexedSeq = seqCompletableFuture.get();
138
         IndexedSeq<Models.Person> personIndexedSeq = seqCompletableFuture.get();
137
         List<String> personsList = new ArrayList<>();
139
         List<String> personsList = new ArrayList<>();
138
         personIndexedSeq.foreach(person ->{
140
         personIndexedSeq.foreach(person ->{
139
-            personsList.add(Json.toJson(person, person.personFormat()).toString());
141
+            JsValue personJs = Json.toJson(person, person.personFormat());
142
+            personsList.add(Json.prettyPrint(personJs));
140
             return null;
143
             return null;
141
         });
144
         });
142
 
145
 

+ 2
- 1
src/main/java/fr/natan/akkastreamfileprocessingapi/futurecompleteness/CompletableFutureBuilder.java View File

4
 import scala.collection.IndexedSeq;
4
 import scala.collection.IndexedSeq;
5
 import scala.concurrent.Future;
5
 import scala.concurrent.Future;
6
 
6
 
7
+import java.util.Random;
7
 import java.util.concurrent.CompletableFuture;
8
 import java.util.concurrent.CompletableFuture;
8
 import java.util.concurrent.Executors;
9
 import java.util.concurrent.Executors;
9
 
10
 
15
         CompletableFuture<T> completableFuture = new CompletableFuture<>();
16
         CompletableFuture<T> completableFuture = new CompletableFuture<>();
16
        Executors.newSingleThreadExecutor().submit(() -> {
17
        Executors.newSingleThreadExecutor().submit(() -> {
17
            while(!futureT.isCompleted()){
18
            while(!futureT.isCompleted()){
18
-               Thread.sleep(100);
19
+               Thread.sleep(new Random().nextInt(500-100)+100);
19
            }
20
            }
20
             completableFuture.complete(futureT.value().get().get());
21
             completableFuture.complete(futureT.value().get().get());
21
             return null;
22
             return null;

+ 29
- 16
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessingImpl.scala View File

4
 import akka.actor.ActorSystem
4
 import akka.actor.ActorSystem
5
 import akka.stream.scaladsl.{Sink, Source}
5
 import akka.stream.scaladsl.{Sink, Source}
6
 import com.typesafe.scalalogging.slf4j.Logger
6
 import com.typesafe.scalalogging.slf4j.Logger
7
-import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.{
8
-  nameBasics, titleBasics, titlePrincipalsBasics
9
-}
7
+import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.{nameBasics, titleBasics, titlePrincipalsBasics}
10
 import fr.natan.akkastreamfileprocessingapi.models.Models.{Person, TvSerie}
8
 import fr.natan.akkastreamfileprocessingapi.models.Models.{Person, TvSerie}
11
 import fr.natan.akkastreamfileprocessingapi.models.ModelsBuilder.buildPersonModel
9
 import fr.natan.akkastreamfileprocessingapi.models.ModelsBuilder.buildPersonModel
12
 import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{
10
 import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{
38
 
36
 
39
   override def getPersonByIdFuture(personID: String): Future[Person] = {
37
   override def getPersonByIdFuture(personID: String): Future[Person] = {
40
     val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
38
     val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
39
+
40
+    val start: Long = System.currentTimeMillis()
41
     val personFuture: Future[Person] = source
41
     val personFuture: Future[Person] = source
42
       .via(flow = filterPersonByIdFlow(personID = personID))
42
       .via(flow = filterPersonByIdFlow(personID = personID))
43
       .runWith(Sink.head)
43
       .runWith(Sink.head)
44
 
44
 
45
     personFuture.andThen({
45
     personFuture.andThen({
46
       case Failure(exception) => logger.info(s"$exception")
46
       case Failure(exception) => logger.info(s"$exception")
47
-      case Success(value:Person) => logger.info(s"$value")
47
+      case Success(value:Person) =>
48
+        logger.info(s"$value")
49
+        logger.info(s"SUCCESS, elapsed time:${(System.currentTimeMillis() - start) / 1000} sec")
48
     })
50
     })
49
 
51
 
50
     personFuture
52
     personFuture
52
 
54
 
53
   override def getPersonByNameFuture(primaryName: String): Future[Person] = {
55
   override def getPersonByNameFuture(primaryName: String): Future[Person] = {
54
     val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
56
     val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
57
+
58
+    val start: Long = System.currentTimeMillis()
55
     val personFuture: Future[Person] = source
59
     val personFuture: Future[Person] = source
56
       .via(flow = filterPersonByNameFlow(primaryName = primaryName))
60
       .via(flow = filterPersonByNameFlow(primaryName = primaryName))
57
       .runWith(Sink.head)
61
       .runWith(Sink.head)
59
     personFuture.onComplete({
63
     personFuture.onComplete({
60
       case Failure(exception) => logger.error(s"$exception")
64
       case Failure(exception) => logger.error(s"$exception")
61
       case Success(value: Person) => logger.info(s"$value")
65
       case Success(value: Person) => logger.info(s"$value")
62
-        logger.info("SUCCESS")
66
+        logger.info(s"SUCCESS, elapsed time:${(System.currentTimeMillis() - start) / 1000} sec")
63
     })
67
     })
64
 
68
 
65
     personFuture
69
     personFuture
67
 
71
 
68
   override def getTvSerieByIdFuture(tvSerieID: String): Future[TvSerie] = {
72
   override def getTvSerieByIdFuture(tvSerieID: String): Future[TvSerie] = {
69
     val source: Source[Map[String, String], _] = buildSource(inputFile = titleBasics)
73
     val source: Source[Map[String, String], _] = buildSource(inputFile = titleBasics)
74
+
75
+    val start: Long = System.currentTimeMillis()
70
     val tvSerieFuture: Future[TvSerie] = source
76
     val tvSerieFuture: Future[TvSerie] = source
71
       .via(flow = filterTvSerieByIdFlow(tvSerieID = tvSerieID))
77
       .via(flow = filterTvSerieByIdFlow(tvSerieID = tvSerieID))
72
       .runWith(Sink.head)
78
       .runWith(Sink.head)
73
 
79
 
74
     tvSerieFuture.onComplete({
80
     tvSerieFuture.onComplete({
75
       case Failure(exception) => logger.info(s"$exception")
81
       case Failure(exception) => logger.info(s"$exception")
76
-      case Success(value: TvSerie) => logger.info(s"$value")
82
+      case Success(value: TvSerie) =>
83
+        logger.info(s"$value")
84
+        logger.info(s"SUCCESS, elapsed time:${(System.currentTimeMillis() - start) / 1000} sec")
77
     })
85
     })
78
     tvSerieFuture
86
     tvSerieFuture
79
   }
87
   }
80
   override def getTvSeriesByPrimaryTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[TvSerie]] = {
88
   override def getTvSeriesByPrimaryTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[TvSerie]] = {
81
 
89
 
90
+    val start: Long = System.currentTimeMillis()
82
     val tvSeriesSource: Source[Map[String, String], _] = buildAndValidateSource(inputFile = titleBasics)
91
     val tvSeriesSource: Source[Map[String, String], _] = buildAndValidateSource(inputFile = titleBasics)
83
-
84
     val tvSriesFuture: Future[IndexedSeq[TvSerie]] = tvSeriesSource
92
     val tvSriesFuture: Future[IndexedSeq[TvSerie]] = tvSeriesSource
85
       .via(flow = filterTvSerieByPrimaryTitleFlow(tvSeriePrimaryTitle = tvSeriePrimaryTitle))
93
       .via(flow = filterTvSerieByPrimaryTitleFlow(tvSeriePrimaryTitle = tvSeriePrimaryTitle))
86
       .runWith(Sink.collection)
94
       .runWith(Sink.collection)
89
       case Failure(exception) => logger.info(s"$exception")
97
       case Failure(exception) => logger.info(s"$exception")
90
       case Success(value: IndexedSeq[TvSerie]) =>
98
       case Success(value: IndexedSeq[TvSerie]) =>
91
         value.foreach((tvSrie: TvSerie) => logger.info(s"$tvSrie"))
99
         value.foreach((tvSrie: TvSerie) => logger.info(s"$tvSrie"))
92
-        logger.info("SUCCESS")
100
+        logger.info(s"SUCCESS, elapsed time:${(System.currentTimeMillis() - start) / 1000} sec")
93
     })
101
     })
94
 
102
 
95
     tvSriesFuture
103
     tvSriesFuture
98
 
106
 
99
   private def getTvSerieIdByPrimaryTitle(primaryTitle: String): Future[Option[String]] = {
107
   private def getTvSerieIdByPrimaryTitle(primaryTitle: String): Future[Option[String]] = {
100
     val source: Source[Map[String, String], _] = buildSource(inputFile = titleBasics)
108
     val source: Source[Map[String, String], _] = buildSource(inputFile = titleBasics)
101
-
102
     val tvSerieIdFuture: Future[Option[String]] = source
109
     val tvSerieIdFuture: Future[Option[String]] = source
103
       .filter((rowMap: Map[String, String]) => {
110
       .filter((rowMap: Map[String, String]) => {
104
         rowMap.getOrElse(key = "primaryTitle", default = "") == primaryTitle
111
         rowMap.getOrElse(key = "primaryTitle", default = "") == primaryTitle
142
 
149
 
143
     //futures chaining
150
     //futures chaining
144
     logger.info("STEP 1/3 START")
151
     logger.info("STEP 1/3 START")
152
+    val start1: Long = System.currentTimeMillis()
145
     val tvSerieIDFuture: Future[Option[String]] = getTvSerieIdByPrimaryTitle(primaryTitle = tvSeriePrimaryTitle)
153
     val tvSerieIDFuture: Future[Option[String]] = getTvSerieIdByPrimaryTitle(primaryTitle = tvSeriePrimaryTitle)
146
 
154
 
147
     val finalFuture: Future[IndexedSeq[Person]] =
155
     val finalFuture: Future[IndexedSeq[Person]] =
149
         case Failure(exception) => logger.error(s"$exception")
157
         case Failure(exception) => logger.error(s"$exception")
150
         case Success(value: Option[String]) =>
158
         case Success(value: Option[String]) =>
151
           logger.info(s"TvSerie ID: $value")
159
           logger.info(s"TvSerie ID: $value")
152
-          logger.info("STEP 1/3 END")
160
+          logger.info(s"STEP 1/3 END, elapsed time:${(System.currentTimeMillis() - start1) / 1000} sec")
153
       })
161
       })
154
         .flatMap({
162
         .flatMap({
155
           _ =>
163
           _ =>
156
             logger.info("STEP 2/3 START")
164
             logger.info("STEP 2/3 START")
165
+            val  start2: Long = System.currentTimeMillis()
157
             val listPersonIDsFuture: Future[IndexedSeq[Option[String]]] = getListOfPersonsIDByTvSerieID(tvSerieIdFuture = tvSerieIDFuture)
166
             val listPersonIDsFuture: Future[IndexedSeq[Option[String]]] = getListOfPersonsIDByTvSerieID(tvSerieIdFuture = tvSerieIDFuture)
158
             listPersonIDsFuture.andThen({
167
             listPersonIDsFuture.andThen({
159
               case Failure(exception) => logger.error(s"$exception")
168
               case Failure(exception) => logger.error(s"$exception")
160
               case Success(value) =>
169
               case Success(value) =>
161
                 value.foreach((personID: Option[String]) => logger.info(s"Person ID:$personID"))
170
                 value.foreach((personID: Option[String]) => logger.info(s"Person ID:$personID"))
162
-                logger.info("STEP 2/3 END")
171
+                logger.info(s"STEP 2/3 END, elapsed time:${(System.currentTimeMillis() - start2) / 1000} sec")
163
             })
172
             })
164
               .flatMap({
173
               .flatMap({
165
                 _ =>
174
                 _ =>
166
                   logger.info("STEP 3/3 START")
175
                   logger.info("STEP 3/3 START")
176
+                  val start3: Long = System.currentTimeMillis()
167
                   val personsTeamFuture: Future[IndexedSeq[Person]] = getListOfPersonsForTvSerie(listPersonsIDsFuture = listPersonIDsFuture)
177
                   val personsTeamFuture: Future[IndexedSeq[Person]] = getListOfPersonsForTvSerie(listPersonsIDsFuture = listPersonIDsFuture)
168
                   personsTeamFuture.andThen({
178
                   personsTeamFuture.andThen({
169
                     case Failure(exception) => logger.error(s"$exception")
179
                     case Failure(exception) => logger.error(s"$exception")
170
                     case Success(value: IndexedSeq[Person]) =>
180
                     case Success(value: IndexedSeq[Person]) =>
171
                       value.foreach((person: Person) => logger.info(s"${person.toString}"))
181
                       value.foreach((person: Person) => logger.info(s"${person.toString}"))
172
-                      logger.info("STEP 3/3 END")
182
+                      logger.info(s"STEP 3/3 END, elapsed time:${(System.currentTimeMillis() - start3) / 1000} sec")
173
                   })
183
                   })
174
               })
184
               })
175
         })
185
         })
190
 
200
 
191
   override def getPersonsForTvSerieByTvSerieIdFuture(tvSerieId: String): Future[IndexedSeq[Person]] = {
201
   override def getPersonsForTvSerieByTvSerieIdFuture(tvSerieId: String): Future[IndexedSeq[Person]] = {
192
 
202
 
193
-    val tvSerieIdFuture: Future[Option[String]] = getTvSerieIDFuture(tvSerieId = tvSerieId)
203
+    val start1: Long = System.currentTimeMillis()
194
     logger.info("STEP 1/3")
204
     logger.info("STEP 1/3")
205
+    val tvSerieIdFuture: Future[Option[String]] = getTvSerieIDFuture(tvSerieId = tvSerieId)
195
     tvSerieIdFuture.andThen({
206
     tvSerieIdFuture.andThen({
196
       case Failure(exception) => logger.error(s"${exception.printStackTrace()}")
207
       case Failure(exception) => logger.error(s"${exception.printStackTrace()}")
197
       case Success(value:Option[String]) =>
208
       case Success(value:Option[String]) =>
198
         logger.info(s"$value")
209
         logger.info(s"$value")
199
-        logger.info("STEP 1/3")
210
+        logger.info(s"STEP 1/3 END, elapsed time:${(System.currentTimeMillis() - start1) / 1000} sec")
200
 
211
 
201
     })
212
     })
202
       .flatMap({
213
       .flatMap({
203
         _ => logger.info("STEP 2/3")
214
         _ => logger.info("STEP 2/3")
215
+          val start2: Long = System.currentTimeMillis()
204
           val  listOfPersonsIDsFuture : Future[IndexedSeq[Option[String]]] = getListOfPersonsIDByTvSerieID(tvSerieIdFuture = tvSerieIdFuture)
216
           val  listOfPersonsIDsFuture : Future[IndexedSeq[Option[String]]] = getListOfPersonsIDByTvSerieID(tvSerieIdFuture = tvSerieIdFuture)
205
           listOfPersonsIDsFuture.andThen({
217
           listOfPersonsIDsFuture.andThen({
206
             case Failure(exception) => logger.error(s"${exception.printStackTrace()}")
218
             case Failure(exception) => logger.error(s"${exception.printStackTrace()}")
207
             case Success(value) =>
219
             case Success(value) =>
208
               value.foreach((personID: Option[String]) => logger.info(s"Person ID:$personID"))
220
               value.foreach((personID: Option[String]) => logger.info(s"Person ID:$personID"))
209
-              logger.info("STEP 2/3 END")
221
+              logger.info(s"STEP 2/3 END, elapsed time:${(System.currentTimeMillis() - start2) / 1000} sec")
210
           })
222
           })
211
             .flatMap({
223
             .flatMap({
212
               _ => logger.info("STEP 3/3")
224
               _ => logger.info("STEP 3/3")
225
+                val start3: Long = System.currentTimeMillis()
213
                 val personsTeamFuture: Future[IndexedSeq[Person]] = getListOfPersonsForTvSerie(listPersonsIDsFuture = listOfPersonsIDsFuture)
226
                 val personsTeamFuture: Future[IndexedSeq[Person]] = getListOfPersonsForTvSerie(listPersonsIDsFuture = listOfPersonsIDsFuture)
214
                 personsTeamFuture.andThen({
227
                 personsTeamFuture.andThen({
215
                   case Failure(exception) => logger.error(s"$exception")
228
                   case Failure(exception) => logger.error(s"$exception")
216
                   case Success(value: IndexedSeq[Person]) =>
229
                   case Success(value: IndexedSeq[Person]) =>
217
                     value.foreach((person: Person) => logger.info(s"${person.toString}"))
230
                     value.foreach((person: Person) => logger.info(s"${person.toString}"))
218
-                    logger.info("STEP 3/3 END")
231
+                    logger.info(s"STEP 3/3 END, elapsed time:${(System.currentTimeMillis() - start3) / 1000} sec")
219
                 })
232
                 })
220
             })
233
             })
221
       })
234
       })

Powered by TurnKey Linux.