Skip to content

Commit c375d4f

Browse files
authored
Merge pull request #16 from jpbnetley/feature/library_updates
database cleanup
2 parents 716302a + b6adcc4 commit c375d4f

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

src/main/scala/Util/DataBuilder/Processing.scala

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import org.bson.Document
1010

1111
import scala.concurrent.Await
1212
import scala.concurrent.duration.Duration
13-
import scala.reflect.io.File
14-
import Util.Logging.log
1513
import Util.ErrorHandler._
1614
import Util.Models.OrderedFile
1715

@@ -25,22 +23,26 @@ object Processing {
2523
*/
2624
def csvFiles(csvFiles: List[OrderedFile])
2725
(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 =>
4446
val (errors, _) = result.separate
4547
errors.headOption.map(error).toLeft(())
4648
}

src/main/scala/Util/Database/Database.scala

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import com.mongodb.{MongoClientSettings, MongoCredential, ServerAddress}
33
import monix.eval.Task
44
import org.mongodb.scala.{MongoClient, MongoDatabase}
55
import Util.ErrorHandler._
6+
import cats.implicits._
67

78
import scala.jdk.CollectionConverters._
89

910
case object Database {
1011

12+
private var mongoClient: Option[MongoClient] = None
13+
1114
/** builds mongo settings with auth
1215
*
1316
* @param authUser on the db
@@ -54,20 +57,25 @@ case object Database {
5457
}
5558
}
5659

57-
private val databaseName: String = sys.env.get("mongo_db_name").fold(throw error("environment variables not set for db name"))(identity)
58-
private val settings: MongoClientSettings.Builder = settingsBuilder().fold(throw error("environment variables not set for db"))(identity)
59-
private val mongoClient: MongoClient = MongoClient(settings.build())
60-
private val database: MongoDatabase = mongoClient.getDatabase(databaseName)
61-
6260
/** Returns thee database instance
6361
*
6462
* @return
6563
*/
66-
def getDatabase: Task[MongoDatabase] = Task(database)
64+
def getDatabase: Task[Either[Exception, MongoDatabase]] = Task.eval {
65+
for {
66+
databaseName <- Either.fromOption(sys.env.get("mongo_db_name"), error("environment variables not set for db name"))
67+
settings <- Either.fromOption(settingsBuilder(), error("environment variables not set for db"))
68+
mongoClient = MongoClient(settings.build()).some
69+
mClient <- Either.fromOption(mongoClient, error("mongo client could not be defined"))
70+
} yield mClient.getDatabase(databaseName)
71+
}
6772

6873
/** Closes the mongo connection
6974
*
7075
*/
71-
def close(): Unit = mongoClient.close()
76+
def close(): Unit = mongoClient match {
77+
case Some(db) => db.close()
78+
case None => ()
79+
}
7280

7381
}

0 commit comments

Comments
 (0)