2023-04-23 22:41:38 +03:00
|
|
|
package fetch
|
|
|
|
|
|
|
|
|
|
import cats.effect.IO
|
|
|
|
|
import cats.effect.unsafe.implicits.global
|
|
|
|
|
import cats.implicits.toTraverseOps
|
|
|
|
|
import db.DBService
|
2023-05-21 22:37:33 +03:00
|
|
|
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
2023-04-23 22:41:38 +03:00
|
|
|
|
2023-05-20 20:20:14 +03:00
|
|
|
import java.time.LocalDateTime
|
2023-04-23 22:41:38 +03:00
|
|
|
|
|
|
|
|
object Main {
|
2023-05-25 00:06:20 +03:00
|
|
|
// def run: IO[Unit] = {
|
|
|
|
|
// val from = LocalDateTime.of(2023, 4, 28, 10, 0)
|
|
|
|
|
// val to = LocalDateTime.of(2023, 4, 28, 13, 30)
|
|
|
|
|
// for {
|
|
|
|
|
// log <- Slf4jLogger.create[IO]
|
|
|
|
|
// fetch <- FetchService.of
|
|
|
|
|
// // fetchResultEither <- fetch.fetchFromDate(LocalDate.of(2023, 4, 28)).attempt
|
|
|
|
|
// fetchResultEither <- fetch.fetchInRange(from, to).attempt
|
|
|
|
|
// fetchServiceError = fetchResultEither.left.toOption.map(e => s"FetchServiceError: ${e.getMessage}").toList
|
|
|
|
|
// fetchResult = fetchResultEither.getOrElse(List.empty)
|
|
|
|
|
// (fetchErrors, successDownloads) = fetchResult.partitionMap(identity)
|
|
|
|
|
// dbService <- DBService.of
|
|
|
|
|
// saveResults <- successDownloads.traverse { case (name, content) => dbService.save(name, content) }
|
|
|
|
|
// (saveErrors, successSaves) = saveResults.partitionMap(identity)
|
|
|
|
|
// successes = successDownloads.map(s => s"fetched: ${s._1}") ++ successSaves.map(s => s"saved: $s")
|
|
|
|
|
// errors = fetchServiceError ++ fetchErrors.map(e => s"FetchError: ${e.getMessage}") ++ saveErrors.map(e => s"SaveError: ${e.getMessage}")
|
|
|
|
|
// _ <- log.info(s"errors: $errors")
|
|
|
|
|
// _ <- log.info(s"successes: $successes")
|
|
|
|
|
// } yield (successes, errors)
|
|
|
|
|
// }
|
|
|
|
|
|
2023-04-23 22:41:38 +03:00
|
|
|
def run: IO[Unit] = {
|
|
|
|
|
for {
|
2023-05-21 22:37:33 +03:00
|
|
|
fetch <- FetchService.of
|
2023-05-25 00:06:20 +03:00
|
|
|
statefulFetch <- StatefulFetchService.of(fetch)
|
|
|
|
|
fetchResultEither <- statefulFetch.fetchSingleFile("20230524_0030.csv").attempt
|
|
|
|
|
fetchResultEither <- statefulFetch.fetchSingleFile("20230522_0130.csv").attempt
|
2023-05-01 16:43:21 +03:00
|
|
|
fetchServiceError = fetchResultEither.left.toOption.map(e => s"FetchServiceError: ${e.getMessage}").toList
|
2023-05-25 00:06:20 +03:00
|
|
|
fetchResult = fetchResultEither.flatMap(res => res.flatMap(aaa => {
|
|
|
|
|
println(s"fffffff: ${aaa._1}")
|
|
|
|
|
Right(aaa._1)
|
|
|
|
|
}))
|
|
|
|
|
// _ = println(s"${fetchResult.map()}")
|
|
|
|
|
} yield ()
|
2023-04-23 22:41:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def main(args: Array[String]): Unit = {
|
|
|
|
|
run.unsafeRunSync()
|
|
|
|
|
}
|
|
|
|
|
}
|