Skip to content

Commit

Permalink
Merge pull request #13 from jpbnetley/feature/library_updates
Browse files Browse the repository at this point in the history
updated implicit functions.
  • Loading branch information
jpbnetley authored Apr 15, 2020
2 parents 63fc1be + c1158fb commit 8368b29
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
9 changes: 5 additions & 4 deletions src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import Util.UserPrompt._
import cats.data.EitherT
import cats.effect.ExitCode
import monix.eval.{Task, TaskApp}
import Util.ImplicitConversions._

object Main extends TaskApp {
override def run(args: List[String]): Task[ExitCode] = {

implicit val database: Database.type = Database

(for {
userInput <- EitherT(promptUser())
csvFiles <- EitherT(FileHelper.getCsvFiles(userInput.folderPath, userInput.skipFiles))
zippedFiles = csvFiles.zipWithIndex
_ <- EitherT(Processing.csvFiles(zippedFiles))
userInput <- EitherT(promptUser())
csvFiles <- EitherT(FileHelper.getCsvFiles(userInput.folderPath, userInput.skipFiles))
orderedFiles = csvFiles.zipWithIndex.toOrderedFile()
_ <- EitherT(Processing.csvFiles(orderedFiles))
} yield {
database.close()
ExitCode.Success
Expand Down
13 changes: 7 additions & 6 deletions src/main/scala/Util/DataBuilder/Processing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import scala.concurrent.duration.Duration
import scala.reflect.io.File
import Util.Logging.log
import Util.ErrorHandler._
import Util.Models.OrderedFile

object Processing {

Expand All @@ -22,22 +23,22 @@ object Processing {
* @param database to insert into
* @return Unit
*/
def csvFiles(csvFiles: List[(File, Int)])
def csvFiles(csvFiles: List[OrderedFile])
(implicit database: Database.type): Task[Either[Exception, Unit]] = {
Task.wander(csvFiles) { case (file, index) =>
println(s"Processing file ${index + 1} of ${csvFiles.length} file name: ${file.name}")
Task.wander(csvFiles) { orderedFile =>
println(s"Processing file ${orderedFile.index + 1} of ${csvFiles.length} file name: ${orderedFile.file.name}")
(for {
fileLines <- EitherT(FileHelper.extractCsvFileLines(file))
fileLines <- EitherT(FileHelper.extractCsvFileLines(orderedFile.file))
headers = fileLines.headOption.map(_.split(',').toList)
lineItems = fileLines.drop(1)
collectionName = file.name.replace(".csv", "").toLowerCase
collectionName = orderedFile.file.name.replace(".csv", "").toLowerCase
documentResult <- EitherT(buildMongoDocuments(headers, lineItems))
db <- EitherT.right[Exception](database.getDatabase)
dbInsert <- EitherT.rightT[Task, Exception](db.getCollection[Document](collectionName).insertMany(documentResult))
} yield {
println(s"Inserting into db: $dbInsert")
Await.result(dbInsert.toFuture(), Duration.Inf)
println(s"Done processing file ${index + 1}")
println(s"Done processing file ${orderedFile.index + 1}")
}).value
}.map { result =>
val (errors, _) = result.separate
Expand Down
16 changes: 16 additions & 0 deletions src/main/scala/Util/ImplicitConversions.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package Util

import Util.Models.OrderedFile

import scala.reflect.io.File

object ImplicitConversions {
implicit class listFiles(files : List[(File, Int)]) {
def toOrderedFile(): List[OrderedFile] = {
files.map { case (file, index) =>
OrderedFile(index, file)
}
}
}
}

5 changes: 5 additions & 0 deletions src/main/scala/Util/Models/OrderedFile.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package Util.Models

import scala.reflect.io.File

final case class OrderedFile(index: Int, file: File)

0 comments on commit 8368b29

Please sign in to comment.