Browse Source

first commit

placidenduwayo 1 year ago
parent
commit
b3d2255d85

+ 3
- 6
src/main/java/fr/natan/akkastreamfileprocessingapi/controller/MovieController.java View File

8
 import org.springframework.web.bind.annotation.RequestMethod;
8
 import org.springframework.web.bind.annotation.RequestMethod;
9
 import org.springframework.web.bind.annotation.RestController;
9
 import org.springframework.web.bind.annotation.RestController;
10
 
10
 
11
-import java.util.Collection;
12
-import java.util.List;
13
-
14
 @RestController
11
 @RestController
15
 public class MovieController {
12
 public class MovieController {
16
 
13
 
51
         return new ResponseEntity<>("is running", HttpStatus.OK);
48
         return new ResponseEntity<>("is running", HttpStatus.OK);
52
     }
49
     }
53
 
50
 
54
-    @RequestMapping(value = "/movies/id/{tvSerieID}", method = RequestMethod.GET)
55
-    private ResponseEntity<String> getListOfPersonsIDByTvSerieID(@PathVariable(name = "tvSerieID") String tvSerieID){
56
-       akkaStreamFilesProcessing.getListOfPersonsIDByTvSerieID(tvSerieID);
51
+    @RequestMapping(value = "/movies/title/{tvSerieTitle}", method = RequestMethod.GET)
52
+    private ResponseEntity<String> getListOfPersonsIDByTvSerieID(@PathVariable(name = "tvSerieTitle") String tvSerieTitle){
53
+        akkaStreamFilesProcessing.generalize(tvSerieTitle);
57
        return new ResponseEntity<>("is running", HttpStatus.OK);
54
        return new ResponseEntity<>("is running", HttpStatus.OK);
58
     }
55
     }
59
 }
56
 }

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

88
 
88
 
89
   //source building
89
   //source building
90
 
90
 
91
-  def buildSource(inputFile: File): Source[Map[String, String], _] = {
91
+  def buildSource(inputFile: File): Source[Map[String, String], NotUsed] = {
92
 
92
 
93
     var datasource: Source[Map[String, String], NotUsed] = null
93
     var datasource: Source[Map[String, String], NotUsed] = null
94
     if (!fileExists(inputFile.getPath)) {
94
     if (!fileExists(inputFile.getPath)) {

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

12
 
12
 
13
   def getByTvSeriePrimaryTitle(tvSerieTitle: String): Unit
13
   def getByTvSeriePrimaryTitle(tvSerieTitle: String): Unit
14
 
14
 
15
-  def getListOfPersonsIDByTvSerieID(tconst: String): List[Option[String]]
15
+  def generalize(tvSeriePrimaryTitle: String): Unit
16
+
17
+
16
 }
18
 }

+ 50
- 9
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.actor.ActorSystem
3
 import akka.actor.ActorSystem
4
-import akka.stream.ActorAttributes.supervisionStrategy
5
-import akka.stream.Supervision.resumingDecider
6
 import akka.stream.scaladsl.{Flow, Sink, Source}
4
 import akka.stream.scaladsl.{Flow, Sink, Source}
7
 import akka.{Done, NotUsed}
5
 import akka.{Done, NotUsed}
8
 import com.typesafe.scalalogging.slf4j.Logger
6
 import com.typesafe.scalalogging.slf4j.Logger
9
 import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.{nameBasics, titleBasics, titlePrincipalsBasics}
7
 import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.{nameBasics, titleBasics, titlePrincipalsBasics}
10
 import fr.natan.akkastreamfileprocessingapi.models.Models.{Person, TvSeries}
8
 import fr.natan.akkastreamfileprocessingapi.models.Models.{Person, TvSeries}
11
-import fr.natan.akkastreamfileprocessingapi.models.ModelsBuilder.buildTvSerieModel
12
-import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{buildAllPersonsSink, buildAndValidateSource, buildPersonFlow, buildPersonSink, buildSource, buildTvSerieFlow, buildTvSeriesSink, filterByPersonIdFlow, filterByPersonNameFlow, filterByTvSeriePrimaryTitleFlow}
9
+import fr.natan.akkastreamfileprocessingapi.models.ModelsBuilder.{buildPersonModel, buildTvSerieModel}
10
+import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{
11
+  buildAllPersonsSink,
12
+  buildAndValidateSource,
13
+  buildPersonFlow,
14
+  buildPersonSink,
15
+  buildSource,
16
+  buildTvSerieFlow,
17
+  buildTvSeriesSink,
18
+  filterByPersonIdFlow,
19
+  filterByPersonNameFlow,
20
+  filterByTvSeriePrimaryTitleFlow
21
+}
13
 import org.slf4j.LoggerFactory
22
 import org.slf4j.LoggerFactory
14
 import org.springframework.stereotype.Component
23
 import org.springframework.stereotype.Component
15
 
24
 
18
 import scala.concurrent.{Await, Future}
27
 import scala.concurrent.{Await, Future}
19
 import scala.language.postfixOps
28
 import scala.language.postfixOps
20
 import scala.util.{Failure, Success}
29
 import scala.util.{Failure, Success}
21
-import scala.collection.immutable.Iterable
22
 
30
 
23
 @Component
31
 @Component
24
 class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessing {
32
 class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessing {
139
     tconst
147
     tconst
140
   }
148
   }
141
 
149
 
142
-  def getListOfPersonsIDByTvSerieID(tconst: String): List[Option[String]]={
150
+  private def getListOfPersonsIDByTvSerieID(tconst: String): List[Option[String]]={
143
     val source: Source[Map[String, String], _] = buildSource(inputFile = titlePrincipalsBasics)
151
     val source: Source[Map[String, String], _] = buildSource(inputFile = titlePrincipalsBasics)
144
-    var tvSeries: Iterable[TvSeries]=null
145
 
152
 
146
     val startTime : Long = System.currentTimeMillis()
153
     val startTime : Long = System.currentTimeMillis()
147
     val res: Future[IndexedSeq[Option[String]]] = source
154
     val res: Future[IndexedSeq[Option[String]]] = source
151
       .map((rowMap: Map[String, String])=>{
158
       .map((rowMap: Map[String, String])=>{
152
         rowMap.get(key = "nconst")
159
         rowMap.get(key = "nconst")
153
       })
160
       })
154
-      .withAttributes(supervisionStrategy(resumingDecider))
155
       .runWith(Sink.collection)
161
       .runWith(Sink.collection)
156
       .andThen {
162
       .andThen {
157
         case Success(value) =>
163
         case Success(value) =>
162
         case Failure(error: Error) => logger.error(s"$error")
168
         case Failure(error: Error) => logger.error(s"$error")
163
       }
169
       }
164
 
170
 
165
-    Await.result(res,5 minutes)
171
+    Await.result(res, 3 minutes)
166
     val listPersonsID: List[Option[String]] = res.value.get.get.toList
172
     val listPersonsID: List[Option[String]] = res.value.get.get.toList
167
 
173
 
168
     listPersonsID
174
     listPersonsID
169
 
175
 
170
   }
176
   }
171
 
177
 
178
+  private def getListOfPersonsForTvSerie(nconstList: List[Option[String]]): List[Person] = {
179
+    val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
180
+
181
+    val startTime: Long = System.currentTimeMillis()
182
+
183
+    val res : Future[IndexedSeq[Person]]= source
184
+      .filter((rowMap: Map[String, String])=>{
185
+        nconstList.contains(rowMap.getOrElse(key = "nconst",default = ""))
186
+      })
187
+      .map((rowMap: Map[String, String])=>{
188
+        val person: Person = buildPersonModel(rowMap)
189
+        person
190
+      })
191
+      .runWith(Sink.collection)
192
+      .andThen {
193
+        case Failure(exception) => logger.error(s"$exception")
194
+        case Success(value) =>
195
+          val elapsedTime: Long = (System.currentTimeMillis()-startTime)/1000
196
+          logger.info(s"$value")
197
+          logger.info(s"END: Successfully, elapsed time: $elapsedTime")
198
+      }
199
+
200
+    Await.result(res, 4 minutes)
201
+    val listPersons: List[Person] = res.value.get.get.toList
202
+
203
+    listPersons
204
+
205
+  }
206
+
207
+  override def generalize(tvSeriePrimaryTitle: String): Unit={
208
+
209
+    val tvSerieID: String = getTvSerieIdByPrimaryTitle(primaryTitle = tvSeriePrimaryTitle)
210
+    val listPersonIDs: List[Option[String]] = getListOfPersonsIDByTvSerieID(tconst = tvSerieID)
211
+    getListOfPersonsForTvSerie(nconstList = listPersonIDs)
212
+  }
172
 
213
 
173
 }
214
 }
174
 
215
 

Powered by TurnKey Linux.