Query and display city field chart and all city weather data

This commit is contained in:
Guntis Smaukstelis
2024-01-12 17:13:39 +02:00
parent d6707b7bb0
commit 65dfcf052a
7 changed files with 149 additions and 27 deletions
+11 -8
View File
@@ -45,22 +45,25 @@ object Main {
// } yield re
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
val from = LocalDateTime.parse("20231210_0000", formatter)
val to = LocalDateTime.parse("20231214_2359", formatter)
val cities = NonEmptyList.of("Ainaži", "Rīga", "Kolka", "Vičaki")
val from = LocalDateTime.parse("20240110_1000", formatter)
val to = LocalDateTime.parse("20240111_1000", formatter)
// val cities = NonEmptyList.of("Ainaži", "Rīga", "Kolka", "Vičaki")
// val cities = NonEmptyList.of("Rīga")
val query = UserQuery(cities, "tempMax", AggregateKey.List, ChronoUnit.DAYS, from, to)
// val query = UserQuery(cities, "tempMax", AggregateKey.List, ChronoUnit.DAYS, from, to)
val re = for {
postgresService <- PostgresService.of(xa)
re <- postgresService.query(query)
re <- postgresService.queryCityAllFields("Rīga", from, to)
} yield re
val jssson = re.map(result => ResponseWrapper(result, query))
.map(responseWrapper => responseWrapper.asJson.pretty)
// val jssson = re.map(result => ResponseWrapper(result, query))
// .map(responseWrapper => responseWrapper.asJson.pretty)
println(jssson.unsafeRunSync())
println(re.unsafeRunSync())
// println(jssson.unsafeRunSync())
// val result = createWeatherTable(xa).unsafeRunSync()
// val result = insertInWeatherTable(xa).unsafeRunSync()
+29
View File
@@ -71,6 +71,35 @@ class PostgresService(transactor: Transactor[IO], log: Logger[IO]) {
}.map(_.toMap)
}
def queryCityAllFields(city: String, from: LocalDateTime, to: LocalDateTime): IO[Map[String, Option[Double]]] = {
val query =
fr"SELECT MAX(tempMax), MIN(tempMin), AVG(tempAvg), SUM(precipitation), AVG(windAvg), MAX(windMax), MIN(visibilityMin), AVG(visibilityAvg), AVG(snowAvg), AVG(atmPressure), AVG(dewPoint), AVG(humidity), SUM(sunDuration)" ++
fr" FROM weather" ++
fr" WHERE city = $city" ++
fr" AND dateTime BETWEEN $from AND $to"
query.query[(Option[Double], Option[Double], Option[Double], Option[Double], Option[Double], Option[Double], Option[Double], Option[Double], Option[Double], Option[Double], Option[Double], Option[Double], Option[Double])]
.unique
.map { case (tempMax, tempMin, tempAvg, precipitation, windAvg, windMax, visibilityMin, visibilityAvg, snowAvg, atmPressure, dewPoint, humidity, sunDuration) =>
Map(
"tempMax" -> tempMax,
"tempMin" -> tempMin,
"tempAvg" -> tempAvg,
"precipitation" -> precipitation,
"windAvg" -> windAvg,
"windMax" -> windMax,
"visibilityMin" -> visibilityMin,
"visibilityAvg" -> visibilityAvg,
"snowAvg" -> snowAvg,
"atmPressure" -> atmPressure,
"dewPoint" -> dewPoint,
"humidity" -> humidity,
"sunDuration" -> sunDuration
)
}
.transact(transactor)
}
def query(userQuery: UserQuery): IO[Map[String, Option[AggregateValue]]] = {
if (userQuery.field == "phenomena") { // this handles strings
// TODO query list and distinct values from phenomena
+4
View File
@@ -58,6 +58,10 @@ class Server(postgresService: PostgresService, fetch: FetchService, log: Logger[
.map(result => ResponseWrapper(result, userQuery))
.flatMap(responseWrapper => Ok(responseWrapper.asJson.pretty))
// http://0.0.0.0:8080/api/query/city/Kolka/20230414_2200-20230501_1230/allFields
case GET -> Root / "query" / "city" / (city: String) / DateTimeRange(from, to) / "allFields" =>
postgresService.queryCityAllFields(city, from, to).flatMap(result => Ok(result.asJson))
// http://0.0.0.0:8080/api/query/country/20230414_2200-20230501_1230/tempMax,tempMin,tempAvg,precipitation
case GET -> Root / "query" / "country" / DateTimeRange(from, to) / AggFieldList(fieldList) =>
postgresService.queryCountry(from, to, fieldList)