Query and display weather data for all country
This commit is contained in:
@@ -14,7 +14,7 @@ object DBConnection {
|
||||
private def getDbPassword = sys.env.getOrElse("POSTGRES_PASSWORD", "")
|
||||
|
||||
private def getDatabaseUrl: String =
|
||||
sys.env.getOrElse("DATABASE_URL", s"postgres://${getDbUser}:${getDbPassword}@postgres:5432/${getDbName}")
|
||||
sys.env.getOrElse("DATABASE_URL", s"postgres://${getDbUser}:${getDbPassword}@0.0.0.0:5432/${getDbName}") // host - postgres for docker, 0.0.0.0 outside docker
|
||||
|
||||
private def parseUrl(url: String): Either[String, PostgresConfig] = {
|
||||
Try {
|
||||
|
||||
@@ -7,7 +7,7 @@ import doobie._
|
||||
import doobie.implicits._
|
||||
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.time.{LocalDate, LocalDateTime, OffsetDateTime, ZoneId, ZonedDateTime}
|
||||
import java.time.{LocalDate, LocalDateTime, OffsetDateTime}
|
||||
import doobie.postgres.implicits._
|
||||
import org.typelevel.log4cats.Logger
|
||||
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
||||
@@ -23,8 +23,6 @@ object PostgresService {
|
||||
}
|
||||
|
||||
class PostgresService(transactor: Transactor[IO], log: Logger[IO]) {
|
||||
// in pgAdmin run: ```SET TIMEZONE = 'Europe/Riga';```
|
||||
|
||||
def save(fileName: String, content: String): IO[String] = {
|
||||
val strLines = content.split(System.lineSeparator()).toList
|
||||
val weatherStationData = strLines.flatMap(Parser.parseLine)
|
||||
@@ -36,6 +34,30 @@ class PostgresService(transactor: Transactor[IO], log: Logger[IO]) {
|
||||
}
|
||||
}
|
||||
|
||||
def queryCountry(from: LocalDateTime, to: LocalDateTime, fieldList: NonEmptyList[String]): IO[Map[String, (Option[Double], Option[Double], Option[Double], Option[Double])]] = {
|
||||
def queryForField(field: String, from: LocalDateTime, to: LocalDateTime): ConnectionIO[(String, (Option[Double], Option[Double], Option[Double], Option[Double]))] =
|
||||
{
|
||||
val query =
|
||||
fr"SELECT " ++
|
||||
fr" ROUND(CAST(MIN(" ++ Fragment.const(field) ++ fr") AS NUMERIC), 1), " ++
|
||||
fr" ROUND(CAST(MAX(" ++ Fragment.const(field) ++ fr") AS NUMERIC), 1), " ++
|
||||
fr" ROUND(CAST(AVG(" ++ Fragment.const(field) ++ fr") AS NUMERIC), 1), " ++
|
||||
fr" ROUND(CAST(SUM(" ++ Fragment.const(field) ++ fr") AS NUMERIC), 1)" ++
|
||||
fr" FROM weather" ++
|
||||
fr" WHERE dateTime BETWEEN $from AND $to"
|
||||
|
||||
query.query[(Option[Double], Option[Double], Option[Double], Option[Double])]
|
||||
.unique
|
||||
.map { case (min, max, avg, sum) =>
|
||||
field -> (min, max, avg, sum)
|
||||
}
|
||||
}
|
||||
|
||||
fieldList.toList.traverse { field =>
|
||||
queryForField(field, from, to).transact(transactor)
|
||||
}.map(_.toMap)
|
||||
}
|
||||
|
||||
def query(userQuery: UserQuery): IO[Map[String, Option[AggregateValue]]] = {
|
||||
if (userQuery.field == "phenomena") { // this handles strings
|
||||
// TODO query list and distinct values from phenomena
|
||||
@@ -171,12 +193,6 @@ class PostgresService(transactor: Transactor[IO], log: Logger[IO]) {
|
||||
.transact(transactor)
|
||||
}
|
||||
|
||||
def readFile(fileName: String): IO[List[String]] = ???
|
||||
|
||||
def getInRange(from: LocalDateTime, to: LocalDateTime): IO[List[String]] = ???
|
||||
|
||||
def getDates: IO[List[LocalDate]] = ???
|
||||
|
||||
def getDateTimeEntries(dateTime: LocalDateTime): IO[List[String]] = {
|
||||
val columnNames = List("City; TempMax; TempMin; TempAvg; Precipitation; WindAvg; WindMax; VisibilityMin; VisibilityAvg; SnowAvg; AtmPressure; DewPoint; Humidity; SunDuration; Phenomena")
|
||||
val result = fr"""
|
||||
@@ -247,38 +263,4 @@ class PostgresService(transactor: Transactor[IO], log: Logger[IO]) {
|
||||
.transact(transactor)
|
||||
}
|
||||
}
|
||||
|
||||
def selectWeatherTable(): IO[List[(String, Option[Double])]] = {
|
||||
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
|
||||
val from = LocalDateTime.parse("20230516_0100", formatter)
|
||||
val to = LocalDateTime.parse("20230516_1500", formatter)
|
||||
val citiesNel = NonEmptyList.of("Rīga", "Rēzekne")
|
||||
val granularity = "HOUR" // "HOUR", "DAY", "MONTH", "YEAR"
|
||||
val columnName = "tempMin"
|
||||
|
||||
val baseQuery =
|
||||
fr"""
|
||||
SELECT
|
||||
city,
|
||||
MIN(""" ++ Fragment.const(columnName) ++ fr""") AS tempMin
|
||||
FROM weather
|
||||
WHERE
|
||||
""" ++ Fragments.in(fr"city", citiesNel) ++ fr"""
|
||||
AND dateTime BETWEEN $from AND $to
|
||||
AND """ ++ Fragment.const(columnName) ++fr""" IS NOT NULL
|
||||
GROUP BY city, EXTRACT(""" ++ Fragment.const(granularity) ++ fr""" FROM dateTime)
|
||||
"""
|
||||
|
||||
baseQuery
|
||||
.query[(String, Option[Double])]
|
||||
.to[List]
|
||||
.transact(transactor)
|
||||
}
|
||||
|
||||
def dropWeatherTable(): IO[Int] = {
|
||||
for {
|
||||
dropTableSql <- getResourceContent("/db/drop_weather_table.sql")
|
||||
result <- Update0(dropTableSql, None).run.transact(transactor)
|
||||
} yield result
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ import cats.implicits.toTraverseOps
|
||||
import com.comcast.ip4s.IpLiteralSyntax
|
||||
import db.PostgresService
|
||||
import fetch.FetchService
|
||||
import parse.{Aggregate}
|
||||
import server.ValidateRoutes.{AggKey, CityList, DateTimeRange, Granularity, ValidateDate, ValidateDateTime, ValidateMonths}
|
||||
import parse.Aggregate
|
||||
import server.ValidateRoutes.{AggFieldList, AggKey, CityList, DateTimeRange, Granularity, ValidateDate, ValidateDateTime, ValidateMonths}
|
||||
import io.circe.{Json, Printer}
|
||||
import org.http4s._
|
||||
import org.http4s.dsl.io._
|
||||
@@ -50,14 +50,19 @@ class Server(postgresService: PostgresService, fetch: FetchService, log: Logger[
|
||||
|
||||
private val apiRoutes = HttpRoutes.of[IO] {
|
||||
|
||||
// http://0.0.0.0:8080/api/query/20230414_2200-20230501_1230/Liepāja,Rēzekne/tempMax/max
|
||||
case GET -> Root / "query" / DateTimeRange(from, to) / Granularity(granularity) / CityList(cities) / field / AggKey(key) =>
|
||||
// http://0.0.0.0:8080/api/query/city/Liepāja,Rēzekne/20230414_2200-20230501_1230/hour/tempMax/max
|
||||
case GET -> Root / "query" / "city" / CityList(cities) / DateTimeRange(from, to) / Granularity(granularity) / field / AggKey(key) =>
|
||||
val userQuery = UserQuery(cities, field, key, granularity, from, to)
|
||||
|
||||
postgresService.query(userQuery)
|
||||
.map(result => ResponseWrapper(result, userQuery))
|
||||
.flatMap(responseWrapper => Ok(responseWrapper.asJson.pretty))
|
||||
|
||||
// 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)
|
||||
.flatMap(result => Ok(result.asJson.pretty))
|
||||
|
||||
// http://0.0.0.0:8080/api/fetch/date/20230514
|
||||
case GET -> Root / "fetch" / "date" / ValidateDate(date) =>
|
||||
val result = for {
|
||||
|
||||
@@ -2,6 +2,7 @@ package server
|
||||
|
||||
import cats.data.NonEmptyList
|
||||
import parse.Aggregate.AggregateKey
|
||||
import parse.WeatherData
|
||||
|
||||
import java.time.{LocalDate, LocalDateTime}
|
||||
import java.time.format.DateTimeFormatter
|
||||
@@ -56,6 +57,14 @@ object ValidateRoutes {
|
||||
}
|
||||
}
|
||||
|
||||
object AggFieldList {
|
||||
def unapply(str: String): Option[NonEmptyList[String]] = {
|
||||
val weatherFields = WeatherData.getKeys
|
||||
val filteredList = str.split(",").toList.filter(weatherFields.contains)
|
||||
NonEmptyList.fromList(filteredList)
|
||||
}
|
||||
}
|
||||
|
||||
object AggKey {
|
||||
def unapply(str: String): Option[AggregateKey] = {
|
||||
AggregateKey.fromString(str)
|
||||
|
||||
Reference in New Issue
Block a user