Limit all dates asking to 3 months range

This commit is contained in:
Guntis Smaukstelis
2023-08-11 16:22:15 +03:00
parent a52eb2c54f
commit f1e68c6dba
7 changed files with 86 additions and 12 deletions
+14
View File
@@ -81,6 +81,20 @@ class DBService(log: Logger[IO]) extends DataServiceTrait {
} yield dates.sorted
}
def getDatesByMonths(monthList: List[LocalDate]): IO[List[LocalDate]] = {
val dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd")
val monthFormatter = DateTimeFormatter.ofPattern("yyyyMM")
val monthStrList = monthList.map(_.format(monthFormatter))
for {
fileNames <- readFileNames(dataPath)
datesStr <- IO(fileNames.map(_.take(8)).distinct) // take yyyyMMdd
filteredDatesStr = datesStr.filter(date => monthStrList.contains(date.take(6)))
dates <- filteredDatesStr.traverse { str =>
IO(LocalDate.parse(str, dateFormatter)).option
}.map(_.flatten)
} yield dates.sorted
}
def getDateFileNames(date: LocalDate): IO[List[String]] = {
val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd")
val dateStr: String = date.format(formatter)
+2
View File
@@ -63,6 +63,8 @@ class DataService private(
def getDates: IO[List[LocalDate]] = dbService.getDates
def getDatesByMonths(monthList: List[LocalDate]): IO[List[LocalDate]] = dbService.getDatesByMonths(monthList)
def getDateFileNames(date: LocalDate): IO[List[String]] = dbService.getDateFileNames(date)
// TODO implement getting full data from state
+7 -1
View File
@@ -6,7 +6,7 @@ import com.comcast.ip4s.IpLiteralSyntax
import db.DataService
import fetch.FetchService
import parse.{Aggregate, Parser, WeatherData}
import server.ValidateRoutes.{AggKey, CityList, DateTimeRange, Granularity, ValidDate}
import server.ValidateRoutes.{AggKey, CityList, DateTimeRange, Granularity, ValidDate, ValidateMonths}
import io.circe.{Json, Printer}
import org.http4s._
import org.http4s.dsl.io._
@@ -89,6 +89,12 @@ class Server(dataService: DataService, fetch: FetchService, log: Logger[IO]) {
Ok(dates.asJson.pretty)
)
// http://0.0.0.0:8080/api/show/months/202304,202305,202306
case GET -> Root / "show" / "months" / ValidateMonths(monthList) =>
dataService.getDatesByMonths(monthList).flatMap(dates =>
Ok(dates.asJson.pretty)
)
// http://0.0.0.0:8080/api/show/date/20230423
case GET -> Root / "show" / "date" / ValidDate(date) =>
dataService.getDateFileNames(date).flatMap(fileNames =>
@@ -27,6 +27,21 @@ object ValidateRoutes {
}
}
object ValidateMonths {
def unapply(str: String): Option[List[LocalDate]] = {
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd")
val monthList = str
.split(",")
.toList
.flatMap(str => Try(LocalDate.parse(str + "01", formatter)).toOption)
monthList match {
case Nil => None
case list => Some(list)
}
}
}
object CityList {
def unapply(str: String): Option[List[String]] = {
str.split(",").toList match {