diff --git a/src/main/scala/db/PostgresService.scala b/src/main/scala/db/PostgresService.scala index 418323c..4ea66f9 100644 --- a/src/main/scala/db/PostgresService.scala +++ b/src/main/scala/db/PostgresService.scala @@ -29,10 +29,17 @@ object PostgresService { class PostgresService(transactor: Transactor[IO], log: Logger[IO]) { def queryLatestTemperatures(cities: NonEmptyList[String]): IO[List[(String, LocalDateTime, Option[Double])]] = { + // The open-data source publishes fields on different schedules within + // the same hour (e.g. snow arrives well before the hourly temperature + // aggregate), so the single newest row for a city can have a real + // dateTime but a still-null tempAvg. Skip straight to DISTINCT ON's + // "most recent" pick by requiring tempAvg itself to be present, so a + // fresh partial row never hides an older row that actually has data. val query = fr"SELECT DISTINCT ON (city) city, dateTime, tempAvg" ++ fr" FROM weather" ++ fr" WHERE " ++ Fragments.in(fr"city", cities) ++ + fr" AND tempAvg IS NOT NULL" ++ fr" ORDER BY city, dateTime DESC" query.query[(String, LocalDateTime, Option[Double])]