From 37a90c58ad77abec1870cb060bbdcb39666c01d3 Mon Sep 17 00:00:00 2001 From: Guntis Smaukstelis Date: Mon, 24 Apr 2023 00:01:06 +0300 Subject: [PATCH] Server returns all dates with data and data in particular day --- src/main/scala/db/DBService.scala | 38 ++++++-------------------- src/main/scala/db/Main.scala | 44 ++++++++++++++++++++++++++++++ src/main/scala/server/Server.scala | 28 ++++++++++++++++--- 3 files changed, 76 insertions(+), 34 deletions(-) create mode 100644 src/main/scala/db/Main.scala diff --git a/src/main/scala/db/DBService.scala b/src/main/scala/db/DBService.scala index a8985a5..328b0e9 100644 --- a/src/main/scala/db/DBService.scala +++ b/src/main/scala/db/DBService.scala @@ -1,6 +1,5 @@ package db -import cats.effect.unsafe.implicits.global import cats.effect.{IO, Resource} import cats.implicits.toTraverseOps @@ -58,42 +57,21 @@ object DBService { ) } - def getDates(): IO[Set[LocalDate]] = { + // dates in which we have saved data + def getDates(): IO[List[LocalDate]] = { val formatter = DateTimeFormatter.ofPattern("yyyyMMdd") for { fileNames <- readFileNames(dataPath) - datesStr <- IO.pure(fileNames.map(_.take(8)).toSet) + datesStr <- IO.pure(fileNames.map(_.take(8)).distinct) // take yyyyMMdd dates <- IO.pure(datesStr.flatMap(str => { Try(LocalDate.parse(str, formatter)).toOption })) - } yield dates + } yield dates.sorted } - def fetchDates(date: LocalDate): IO[List[String]] = { - ??? - } - - // TODO remove this. Just testing - private def testGetInRange: IO[Unit] = { - val from = LocalDateTime.parse("20230414_2200", dateFormatter) - val to = LocalDateTime.parse("20230501_1230", dateFormatter) - - for { - lines <- getInRange(from, to) - _ <- lines.traverse(IO.println) - } yield () - } - - private def testGetDates: IO[Unit] = { - for { - dates <- getDates() - _ <- IO.println(dates) - } yield () - } - - def main(args: Array[String]): Unit = { - println("----------------> db main") -// testGetInRange.unsafeRunSync() - testGetDates.unsafeRunSync() + def getDateFileNames(date: LocalDate): IO[List[String]] = { + val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd") + val dateStr: String = date.format(formatter) + readFileNames(dataPath).map(_.filter(_.startsWith(dateStr)).sorted) } } diff --git a/src/main/scala/db/Main.scala b/src/main/scala/db/Main.scala new file mode 100644 index 0000000..6a3b36e --- /dev/null +++ b/src/main/scala/db/Main.scala @@ -0,0 +1,44 @@ +package db + +import cats.effect.IO +import cats.effect.unsafe.implicits.global +import cats.implicits.toTraverseOps +import db.DBService + +import java.time.{LocalDate, LocalDateTime} +import java.time.format.DateTimeFormatter + +object Main { + private val dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm") + + private def testGetInRange: IO[Unit] = { + val from = LocalDateTime.parse("20230414_2200", dateFormatter) + val to = LocalDateTime.parse("20230501_1230", dateFormatter) + + for { + lines <- DBService.getInRange(from, to) + _ <- lines.traverse(IO.println) + } yield () + } + + private def testGetDates: IO[Unit] = { + for { + dates <- DBService.getDates() + _ <- IO.println(dates) + } yield () + } + + private def testGetDate: IO[Unit] = { + for { + dates <- DBService.getDateFileNames(LocalDate.of(2023, 4, 23)) + _ <- IO.println(dates) + } yield () + } + + def main(args: Array[String]): Unit = { + println("----------------> db main") + // testGetInRange.unsafeRunSync() + // testGetDates.unsafeRunSync() + testGetDate.unsafeRunSync() + } +} diff --git a/src/main/scala/server/Server.scala b/src/main/scala/server/Server.scala index 130abbc..0479bc3 100644 --- a/src/main/scala/server/Server.scala +++ b/src/main/scala/server/Server.scala @@ -52,8 +52,8 @@ object Server extends IOApp { case _ => BadRequest(s"Invalid request format") } - // http://localhost:3000/fetchDate/20230423 - case GET -> Root / "fetchDate" / dateStr => { + // http://localhost:3000/fetch/date/20230423 + case GET -> Root / "fetch" / "date" / dateStr => { val dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd") val maybeDate = Try(LocalDate.parse(dateStr, dateFormatter)).toEither maybeDate match { @@ -81,8 +81,28 @@ object Server extends IOApp { } } - case GET -> Root / "show" / "fetched_dates" => - ??? + // http://localhost:3000/show/all_dates + case GET -> Root / "show" / "all_dates" => + DBService.getDates().flatMap(dates => { + val printer = Printer.spaces2.copy(dropNullValues = true) + val prettyJson = printer.print(dates.asJson) + Ok(prettyJson) + }) + + // http://localhost:3000/show/date/20230423 + case GET -> Root / "show" / "date" / dateStr => + val dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd") + val maybeDate = Try(LocalDate.parse(dateStr, dateFormatter)).toEither + maybeDate match { + case Right(date) => { + DBService.getDateFileNames(date).flatMap(fileNames => { + val printer = Printer.spaces2.copy(dropNullValues = true) + val prettyJson = printer.print(fileNames.asJson) + Ok(prettyJson) + }) + } + case Left(_) => BadRequest("Invalid request format") + } case GET -> Root / "show" / dateRange => ???