@@ -10,8 +10,6 @@ import org.bson.Document
10
10
11
11
import scala .concurrent .Await
12
12
import scala .concurrent .duration .Duration
13
- import scala .reflect .io .File
14
- import Util .Logging .log
15
13
import Util .ErrorHandler ._
16
14
import Util .Models .OrderedFile
17
15
@@ -25,22 +23,26 @@ object Processing {
25
23
*/
26
24
def csvFiles (csvFiles : List [OrderedFile ])
27
25
(implicit database : Database .type ): Task [Either [Exception , Unit ]] = {
28
- Task .wander(csvFiles) { orderedFile =>
29
- println(s " Processing file ${orderedFile.index + 1 } of ${csvFiles.length} file name: ${orderedFile.file.name}" )
30
- (for {
31
- fileLines <- EitherT (FileHelper .extractCsvFileLines(orderedFile.file))
32
- headers = fileLines.headOption.map(_.split(',' ).toList)
33
- lineItems = fileLines.drop(1 )
34
- collectionName = orderedFile.file.name.replace(" .csv" , " " ).toLowerCase
35
- documentResult <- EitherT (buildMongoDocuments(headers, lineItems))
36
- db <- EitherT .right[Exception ](database.getDatabase)
37
- dbInsert <- EitherT .rightT[Task , Exception ](db.getCollection[Document ](collectionName).insertMany(documentResult))
38
- } yield {
39
- println(s " Inserting into db: $dbInsert" )
40
- Await .result(dbInsert.toFuture(), Duration .Inf )
41
- println(s " Done processing file ${orderedFile.index + 1 }" )
42
- }).value
43
- }.map { result =>
26
+ val insertion = {
27
+ Task .wander(csvFiles) { orderedFile =>
28
+ println(s " Processing file ${orderedFile.index + 1 } of ${csvFiles.length} file name: ${orderedFile.file.name}" )
29
+ (for {
30
+ fileLines <- EitherT (FileHelper .extractCsvFileLines(orderedFile.file))
31
+ headers = fileLines.headOption.map(_.split(',' ).toList)
32
+ lineItems = fileLines.drop(1 )
33
+ collectionName = orderedFile.file.name.replace(" .csv" , " " ).toLowerCase
34
+ documentResult <- EitherT (buildMongoDocuments(headers, lineItems))
35
+ db <- EitherT (database.getDatabase)
36
+ dbInsert <- EitherT .rightT[Task , Exception ](db.getCollection[Document ](collectionName).insertMany(documentResult))
37
+ } yield {
38
+ println(s " Inserting into db: $dbInsert" )
39
+ Await .result(dbInsert.toFuture(), Duration .Inf )
40
+ println(s " Done processing file ${orderedFile.index + 1 }" )
41
+ }).value
42
+ }
43
+ }
44
+
45
+ insertion.map { result =>
44
46
val (errors, _) = result.separate
45
47
errors.headOption.map(error).toLeft(())
46
48
}
0 commit comments