First version of querying data from postgres

This commit is contained in:
Guntis Smaukstelis
2023-12-15 13:46:45 +02:00
parent e89b526a88
commit 3a1f0ac726
9 changed files with 85 additions and 49 deletions
+4
View File
@@ -6,6 +6,8 @@ import cats.implicits.toTraverseOps
import fetch.FileNameService
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.slf4j.Slf4jLogger
import parse.Aggregate
import parse.Aggregate.UserQuery
import java.time.{Instant, LocalDate, LocalDateTime, ZoneId}
@@ -85,6 +87,8 @@ class DataService private(
def getInRange(from: LocalDateTime, to: LocalDateTime): IO[List[String]] = fileService.getInRange(from, to)
def query(userQuery: UserQuery): IO[Map[String, Option[Aggregate.AggregateValue]]] = postgresService.query(userQuery)
def getDates: IO[List[LocalDate]] = fileService.getDates
def getDatesByMonths(monthList: List[LocalDate]): IO[List[LocalDate]] = {
+9 -2
View File
@@ -1,13 +1,16 @@
package db
import cats.data.NonEmptyList
import cats.effect._
import cats.effect.unsafe.implicits.global
import doobie._
import doobie.implicits._
import doobie.postgres.implicits._
import parse.Aggregate.{AggregateKey, UserQuery}
import java.time.{LocalDate, LocalDateTime}
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
object Main {
def transactor[F[_]: Async]: Transactor[F] = Transactor.fromDriverManager[F](
@@ -27,10 +30,14 @@ object Main {
// } yield re
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
val dateTime = LocalDateTime.parse("20231210_1300", formatter)
val from = LocalDateTime.parse("20231212_0000", formatter)
val to = LocalDateTime.parse("20231214_2359", formatter)
val query = UserQuery(NonEmptyList.of("Ainaži", "Rīga", "Kolka", "Vičaki"), "tempAvg", AggregateKey.Max, ChronoUnit.HOURS, from, to)
val result = for {
postgresService <- PostgresService.of(xa)
re <- postgresService.getDateTimeEntries(dateTime)
re <- postgresService.query(query)
} yield re
println(result.unsafeRunSync().toString())
+51 -1
View File
@@ -2,7 +2,7 @@ package db
import cats.data.NonEmptyList
import cats.effect._
import cats.implicits.{catsSyntaxParallelTraverse1, toFoldableOps}
import cats.implicits._
import doobie._
import doobie.implicits._
@@ -11,6 +11,7 @@ import java.time.{LocalDate, LocalDateTime, OffsetDateTime, ZoneId, ZonedDateTim
import doobie.postgres.implicits._
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.slf4j.Slf4jLogger
import parse.Aggregate.{AggregateKey, AggregateValue, DoubleValue, UserQuery}
import parse.{Parser, WeatherStationData}
object PostgresService {
@@ -34,6 +35,55 @@ class PostgresService(transactor: Transactor[IO], log: Logger[IO]) extends DataS
}
}
/*
SELECT city, AVG(tempmax) AS tempmax -- MAX MIN AVG SUM
FROM weather
WHERE city IN ('Rīga', 'Rēzekne', 'Kolka')
AND dateTime BETWEEN '2023-12-10 00:00:00' AND '2023-12-10 23:59:59'
GROUP BY city;
*/
/*
SELECT city, dateTime, tempmax
FROM weather
WHERE city IN ('Rīga', 'Rēzekne', 'Kolka')
AND dateTime BETWEEN '2023-12-10 00:00:00' AND '2023-12-10 23:59:59'
*/
/*
SELECT city, DATE(dateTime) as day, MAX(tempmax) as max_temp
FROM weather
WHERE city IN ('Rīga', 'Rēzekne', 'Kolka')
AND dateTime BETWEEN '2023-12-01' AND '2023-12-31'
GROUP BY city, DATE(dateTime)
ORDER BY city, day;
*/
def query(userQuery: UserQuery): IO[Map[String, Option[AggregateValue]]] = {
if (List(
AggregateKey.Max,
AggregateKey.Min,
AggregateKey.Avg,
AggregateKey.Sum
).contains(userQuery.key)) {
val query =
(fr"SELECT city, ROUND(CAST(" ++ Fragment.const(userQuery.key.toString.toUpperCase) ++ fr"(" ++ Fragment.const(userQuery.field) ++ fr") AS NUMERIC), 1) AS value FROM weather WHERE " ++
Fragments.in(fr"city", userQuery.cities) ++
fr" AND dateTime BETWEEN ${userQuery.from} AND ${userQuery.to} GROUP BY city")
query.query[(String, Option[Double])]
.to[List]
.transact(transactor)
.map { resultList =>
resultList.map {
case (city, maybeValue) =>
city -> maybeValue.map(DoubleValue)
}.toMap: Map[String, Option[AggregateValue]]
}
} else {
???
}
}
def getDatesByMonths(monthList: List[LocalDate]): IO[List[LocalDate]] = {
val monthYearPairs = monthList.map { localDate =>
val month = localDate.atStartOfDay.atZone(rigaZone).getMonthValue