placidenduwayo hace 1 año
padre
commit
0707094eab

+ 12
- 9
src/main/java/fr/natan/akkastreamfileprocessingapi/controller/TvSeriesController.java Ver fichero

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

+ 2
- 1
src/main/java/fr/natan/akkastreamfileprocessingapi/futurecompleteness/CompletableFutureBuilder.java Ver fichero

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

+ 29
- 16
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessingImpl.scala Ver fichero

@@ -4,9 +4,7 @@ import akka.Done
4 4
 import akka.actor.ActorSystem
5 5
 import akka.stream.scaladsl.{Sink, Source}
6 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 8
 import fr.natan.akkastreamfileprocessingapi.models.Models.{Person, TvSerie}
11 9
 import fr.natan.akkastreamfileprocessingapi.models.ModelsBuilder.buildPersonModel
12 10
 import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{
@@ -38,13 +36,17 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
38 36
 
39 37
   override def getPersonByIdFuture(personID: String): Future[Person] = {
40 38
     val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
39
+
40
+    val start: Long = System.currentTimeMillis()
41 41
     val personFuture: Future[Person] = source
42 42
       .via(flow = filterPersonByIdFlow(personID = personID))
43 43
       .runWith(Sink.head)
44 44
 
45 45
     personFuture.andThen({
46 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 52
     personFuture
@@ -52,6 +54,8 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
52 54
 
53 55
   override def getPersonByNameFuture(primaryName: String): Future[Person] = {
54 56
     val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
57
+
58
+    val start: Long = System.currentTimeMillis()
55 59
     val personFuture: Future[Person] = source
56 60
       .via(flow = filterPersonByNameFlow(primaryName = primaryName))
57 61
       .runWith(Sink.head)
@@ -59,7 +63,7 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
59 63
     personFuture.onComplete({
60 64
       case Failure(exception) => logger.error(s"$exception")
61 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 69
     personFuture
@@ -67,20 +71,24 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
67 71
 
68 72
   override def getTvSerieByIdFuture(tvSerieID: String): Future[TvSerie] = {
69 73
     val source: Source[Map[String, String], _] = buildSource(inputFile = titleBasics)
74
+
75
+    val start: Long = System.currentTimeMillis()
70 76
     val tvSerieFuture: Future[TvSerie] = source
71 77
       .via(flow = filterTvSerieByIdFlow(tvSerieID = tvSerieID))
72 78
       .runWith(Sink.head)
73 79
 
74 80
     tvSerieFuture.onComplete({
75 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 86
     tvSerieFuture
79 87
   }
80 88
   override def getTvSeriesByPrimaryTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[TvSerie]] = {
81 89
 
90
+    val start: Long = System.currentTimeMillis()
82 91
     val tvSeriesSource: Source[Map[String, String], _] = buildAndValidateSource(inputFile = titleBasics)
83
-
84 92
     val tvSriesFuture: Future[IndexedSeq[TvSerie]] = tvSeriesSource
85 93
       .via(flow = filterTvSerieByPrimaryTitleFlow(tvSeriePrimaryTitle = tvSeriePrimaryTitle))
86 94
       .runWith(Sink.collection)
@@ -89,7 +97,7 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
89 97
       case Failure(exception) => logger.info(s"$exception")
90 98
       case Success(value: IndexedSeq[TvSerie]) =>
91 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 103
     tvSriesFuture
@@ -98,7 +106,6 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
98 106
 
99 107
   private def getTvSerieIdByPrimaryTitle(primaryTitle: String): Future[Option[String]] = {
100 108
     val source: Source[Map[String, String], _] = buildSource(inputFile = titleBasics)
101
-
102 109
     val tvSerieIdFuture: Future[Option[String]] = source
103 110
       .filter((rowMap: Map[String, String]) => {
104 111
         rowMap.getOrElse(key = "primaryTitle", default = "") == primaryTitle
@@ -142,6 +149,7 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
142 149
 
143 150
     //futures chaining
144 151
     logger.info("STEP 1/3 START")
152
+    val start1: Long = System.currentTimeMillis()
145 153
     val tvSerieIDFuture: Future[Option[String]] = getTvSerieIdByPrimaryTitle(primaryTitle = tvSeriePrimaryTitle)
146 154
 
147 155
     val finalFuture: Future[IndexedSeq[Person]] =
@@ -149,27 +157,29 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
149 157
         case Failure(exception) => logger.error(s"$exception")
150 158
         case Success(value: Option[String]) =>
151 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 162
         .flatMap({
155 163
           _ =>
156 164
             logger.info("STEP 2/3 START")
165
+            val  start2: Long = System.currentTimeMillis()
157 166
             val listPersonIDsFuture: Future[IndexedSeq[Option[String]]] = getListOfPersonsIDByTvSerieID(tvSerieIdFuture = tvSerieIDFuture)
158 167
             listPersonIDsFuture.andThen({
159 168
               case Failure(exception) => logger.error(s"$exception")
160 169
               case Success(value) =>
161 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 173
               .flatMap({
165 174
                 _ =>
166 175
                   logger.info("STEP 3/3 START")
176
+                  val start3: Long = System.currentTimeMillis()
167 177
                   val personsTeamFuture: Future[IndexedSeq[Person]] = getListOfPersonsForTvSerie(listPersonsIDsFuture = listPersonIDsFuture)
168 178
                   personsTeamFuture.andThen({
169 179
                     case Failure(exception) => logger.error(s"$exception")
170 180
                     case Success(value: IndexedSeq[Person]) =>
171 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,32 +200,35 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
190 200
 
191 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 204
     logger.info("STEP 1/3")
205
+    val tvSerieIdFuture: Future[Option[String]] = getTvSerieIDFuture(tvSerieId = tvSerieId)
195 206
     tvSerieIdFuture.andThen({
196 207
       case Failure(exception) => logger.error(s"${exception.printStackTrace()}")
197 208
       case Success(value:Option[String]) =>
198 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 213
       .flatMap({
203 214
         _ => logger.info("STEP 2/3")
215
+          val start2: Long = System.currentTimeMillis()
204 216
           val  listOfPersonsIDsFuture : Future[IndexedSeq[Option[String]]] = getListOfPersonsIDByTvSerieID(tvSerieIdFuture = tvSerieIdFuture)
205 217
           listOfPersonsIDsFuture.andThen({
206 218
             case Failure(exception) => logger.error(s"${exception.printStackTrace()}")
207 219
             case Success(value) =>
208 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 223
             .flatMap({
212 224
               _ => logger.info("STEP 3/3")
225
+                val start3: Long = System.currentTimeMillis()
213 226
                 val personsTeamFuture: Future[IndexedSeq[Person]] = getListOfPersonsForTvSerie(listPersonsIDsFuture = listOfPersonsIDsFuture)
214 227
                 personsTeamFuture.andThen({
215 228
                   case Failure(exception) => logger.error(s"$exception")
216 229
                   case Success(value: IndexedSeq[Person]) =>
217 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.