Small fixes and cleanup
This commit is contained in:
@@ -21,17 +21,17 @@ object DBService {
|
|||||||
class DBService(log: Logger[IO]) {
|
class DBService(log: Logger[IO]) {
|
||||||
private val dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
|
private val dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
|
||||||
private val dataPath = "./data"
|
private val dataPath = "./data"
|
||||||
|
private val nonDuplicatedLines = 34 // takes only first 34 lines of data as rest after 'Zosēni' is duplicated
|
||||||
|
|
||||||
// takes only first 34 lines of data as rest after 'Zosēni' is duplicated
|
|
||||||
def readFile(fileName: String): IO[List[String]] = {
|
def readFile(fileName: String): IO[List[String]] = {
|
||||||
val file = new File(dataPath, fileName)
|
val file = new File(dataPath, fileName)
|
||||||
val sourceResource = Resource.fromAutoCloseable(IO(Source.fromFile(file)))
|
val sourceResource = Resource.fromAutoCloseable(IO(Source.fromFile(file)))
|
||||||
sourceResource.use(source => IO(source.getLines().take(34).toList)).handleErrorWith(_ => IO.pure(List.empty))
|
sourceResource.use(source => IO(source.getLines().take(nonDuplicatedLines).toList)).handleError(_ => List.empty)
|
||||||
}
|
}
|
||||||
|
|
||||||
private def readFileNames(path: String): IO[List[String]] =
|
private def readFileNames(path: String): IO[List[String]] =
|
||||||
IO(new File(path).listFiles.toList.map(_.getName))
|
IO(new File(path).listFiles.toList.map(_.getName))
|
||||||
.handleErrorWith(_ => IO.pure(List.empty))
|
.handleError(_ => List.empty)
|
||||||
|
|
||||||
private def inRange(fileName: String, from: LocalDateTime, to: LocalDateTime): Boolean = {
|
private def inRange(fileName: String, from: LocalDateTime, to: LocalDateTime): Boolean = {
|
||||||
def fileToDateTime(fileName: String): Option[LocalDateTime] = {
|
def fileToDateTime(fileName: String): Option[LocalDateTime] = {
|
||||||
@@ -58,11 +58,9 @@ class DBService(log: Logger[IO]) {
|
|||||||
val path = Paths.get(s"$dataPath/$fileName")
|
val path = Paths.get(s"$dataPath/$fileName")
|
||||||
IO(Files.writeString(path, content))
|
IO(Files.writeString(path, content))
|
||||||
.redeemWith(
|
.redeemWith(
|
||||||
error => IO(Left(error))
|
error => IO(Left(error)) // <* log.error(s"Write file '$fileName' failed with error: ${error.getMessage}")
|
||||||
// .flatTap(_ => log.error(s"Write file '$fileName' failed with error: ${error.getMessage}"))
|
|
||||||
,
|
,
|
||||||
_ => IO(Right(fileName))
|
_ => IO(Right(fileName)) // <* log.info(s"write: $fileName")
|
||||||
// .flatTap(_ => log.info(s"write: $fileName"))
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,10 +69,10 @@ class DBService(log: Logger[IO]) {
|
|||||||
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd")
|
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd")
|
||||||
for {
|
for {
|
||||||
fileNames <- readFileNames(dataPath)
|
fileNames <- readFileNames(dataPath)
|
||||||
datesStr <- IO.pure(fileNames.map(_.take(8)).distinct) // take yyyyMMdd
|
datesStr <- IO(fileNames.map(_.take(8)).distinct) // take yyyyMMdd
|
||||||
dates <- IO.pure(datesStr.flatMap(str => {
|
dates <- datesStr.traverse { str =>
|
||||||
Try(LocalDate.parse(str, formatter)).toOption
|
IO(LocalDate.parse(str, formatter)).option
|
||||||
}))
|
}.map(_.flatten)
|
||||||
} yield dates.sorted
|
} yield dates.sorted
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,11 +42,9 @@ class FetchService(fileNameService: FileNameService, log: Logger[IO]) {
|
|||||||
val request = Request[IO](Method.GET, url).withHeaders(Authorization(basicCredentials))
|
val request = Request[IO](Method.GET, url).withHeaders(Authorization(basicCredentials))
|
||||||
|
|
||||||
client.expect[String](request).redeemWith(
|
client.expect[String](request).redeemWith(
|
||||||
error => IO(Left(error))
|
error => IO(Left(error)) // <* log.error(s"Request failed to url: $url with error: ${error.getMessage}")
|
||||||
// .flatTap(_ => log.error(s"Request failed to url: $url with error: ${error.getMessage}"))
|
|
||||||
,
|
,
|
||||||
fileContent => IO(Right((fileName, fileContent)))
|
fileContent => IO(Right((fileName, fileContent))) // <* log.info(s"Fetched: $fileName")
|
||||||
// .flatTap(_ => log.info(s"Fetched: $fileName"))
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ object Main {
|
|||||||
log <- Slf4jLogger.create[IO]
|
log <- Slf4jLogger.create[IO]
|
||||||
dbService <- DBService.of
|
dbService <- DBService.of
|
||||||
lines <- dbService.getInRange(from, to)
|
lines <- dbService.getInRange(from, to)
|
||||||
parsed <- IO.pure(Parser.queryData(userQuery, lines))
|
parsed <- IO(Parser.queryData(userQuery, lines))
|
||||||
_ <- log.info(parsed.asJson.toString)
|
_ <- log.info(parsed.asJson.toString)
|
||||||
} yield ()
|
} yield ()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user