Server asks data from db and after uses parser
This commit is contained in:
@@ -59,27 +59,28 @@ object Parser {
|
||||
}
|
||||
|
||||
def queryData(
|
||||
from: LocalDateTime,
|
||||
to: LocalDateTime,
|
||||
data: List[String],
|
||||
cities: List[String],
|
||||
aggregator: AggregateMeteo,
|
||||
): IO[Map[String, Double]] = {
|
||||
for {
|
||||
lines <- db.DBService.getInRange(from, to)
|
||||
weatherLines <- IO.pure(aggregateLines(lines, cities, aggregator))
|
||||
|
||||
weatherLines <- IO.pure(aggregateLines(data, cities, aggregator))
|
||||
// _ <- IO.println(weatherLines)
|
||||
} yield weatherLines
|
||||
}
|
||||
|
||||
// TODO remove this. Just for testing
|
||||
private def run: IO[Unit] = {
|
||||
println("================ start parser")
|
||||
|
||||
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
|
||||
val start = LocalDateTime.parse("20230409_2200", formatter)
|
||||
val end = LocalDateTime.parse("20230501_1230", formatter)
|
||||
val from = LocalDateTime.parse("20230414_2200", formatter)
|
||||
val to = LocalDateTime.parse("20230501_1230", formatter)
|
||||
|
||||
for {
|
||||
parsed <- queryData(start, end, List("Liepāja", "Rēzekne", "randomstr"), AggregateMeteo.tempAvg)
|
||||
lines <- db.DBService.getInRange(from, to)
|
||||
parsed <- queryData(lines, List("Liepāja", "Rēzekne", "randomstr"), AggregateMeteo.tempAvg)
|
||||
_ <- IO.println(parsed)
|
||||
} yield ()
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import scala.util.Try
|
||||
object Server extends IOApp {
|
||||
private val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
|
||||
private val appRoutes = HttpRoutes.of[IO] {
|
||||
// http://localhost:3000/query/20230409_2200-20230501_1230/Liepāja,Rēzekne/tempAvg
|
||||
// http://localhost:3000/query/20230414_2200-20230501_1230/Liepāja,Rēzekne/tempAvg
|
||||
case GET -> Root / "query" / timestampRange / cities / aggregate =>
|
||||
// TODO proly better to Validated with chained errors
|
||||
val parsedArguments = for {
|
||||
@@ -35,8 +35,12 @@ object Server extends IOApp {
|
||||
|
||||
parsedArguments match {
|
||||
case Some((from, to, cityList, aggregate)) => {
|
||||
val resultData = Parser.queryData(from, to, cityList, aggregate)
|
||||
Ok(resultData.toString())
|
||||
val res = for {
|
||||
lines <- db.DBService.getInRange(from, to)
|
||||
parsedData <- parse.Parser.queryData(lines, cityList, aggregate)
|
||||
} yield parsedData
|
||||
|
||||
Ok(res.map(_.toString()))
|
||||
}
|
||||
case _ => BadRequest(s"Invalid request format")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user