Server returns all dates with data and data in particular day
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import cats.effect.unsafe.implicits.global
|
|
||||||
import cats.effect.{IO, Resource}
|
import cats.effect.{IO, Resource}
|
||||||
import cats.implicits.toTraverseOps
|
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")
|
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd")
|
||||||
for {
|
for {
|
||||||
fileNames <- readFileNames(dataPath)
|
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 => {
|
dates <- IO.pure(datesStr.flatMap(str => {
|
||||||
Try(LocalDate.parse(str, formatter)).toOption
|
Try(LocalDate.parse(str, formatter)).toOption
|
||||||
}))
|
}))
|
||||||
} yield dates
|
} yield dates.sorted
|
||||||
}
|
}
|
||||||
|
|
||||||
def fetchDates(date: LocalDate): IO[List[String]] = {
|
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)
|
||||||
// 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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -52,8 +52,8 @@ object Server extends IOApp {
|
|||||||
case _ => BadRequest(s"Invalid request format")
|
case _ => BadRequest(s"Invalid request format")
|
||||||
}
|
}
|
||||||
|
|
||||||
// http://localhost:3000/fetchDate/20230423
|
// http://localhost:3000/fetch/date/20230423
|
||||||
case GET -> Root / "fetchDate" / dateStr => {
|
case GET -> Root / "fetch" / "date" / dateStr => {
|
||||||
val dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd")
|
val dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd")
|
||||||
val maybeDate = Try(LocalDate.parse(dateStr, dateFormatter)).toEither
|
val maybeDate = Try(LocalDate.parse(dateStr, dateFormatter)).toEither
|
||||||
maybeDate match {
|
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 =>
|
case GET -> Root / "show" / dateRange =>
|
||||||
???
|
???
|
||||||
|
|||||||
Reference in New Issue
Block a user