From 9241277b7f14556e31e4af75fc916bef87cfbcc0 Mon Sep 17 00:00:00 2001 From: Guntis Smaukstelis Date: Sat, 20 May 2023 19:19:38 +0300 Subject: [PATCH] Return of list of time double list instead of just doubles --- src/main/scala/parse/Aggregate.scala | 13 +++++++------ src/main/scala/parse/Main.scala | 7 +++---- src/main/scala/parse/Parser.scala | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/scala/parse/Aggregate.scala b/src/main/scala/parse/Aggregate.scala index 928095d..93d6dca 100644 --- a/src/main/scala/parse/Aggregate.scala +++ b/src/main/scala/parse/Aggregate.scala @@ -4,7 +4,7 @@ import cats.implicits.{catsSyntaxOptionId, toFoldableOps} import io.circe.generic.semiauto.deriveEncoder import io.circe.syntax.EncoderOps import io.circe.{Encoder, Json} -import parse.Parser.parseLine +import java.time.LocalDateTime object Aggregate { @@ -46,7 +46,7 @@ object Aggregate { sealed trait AggregateValue final case class DoubleValue(value: Double) extends AggregateValue - final case class ListOptionValue(list: List[Option[Double]]) extends AggregateValue + final case class TimeDoubleList(list: List[(LocalDateTime, Option[Double])]) extends AggregateValue final case class StringListList(list: List[List[String]]) extends AggregateValue final case class DistinctStringList(list: List[String]) extends AggregateValue object AggregateValue { @@ -64,7 +64,7 @@ object Aggregate { object AggregateValueImplicits { implicit val aggregateValueEncoder: Encoder[AggregateValue] = Encoder.instance { case doubleValue: DoubleValue => doubleValue.asJson - case listOptionValue: ListOptionValue => listOptionValue.asJson + case timeDoubleList: TimeDoubleList => timeDoubleList.asJson case stringListList: StringListList => stringListList.asJson case distinctStringList: DistinctStringList => distinctStringList.asJson } @@ -73,7 +73,7 @@ object Aggregate { case DoubleValue(value) if !value.isNaN => Json.fromDouble(value).get case _ => Json.Null } - implicit val listOptionValueEncoder: Encoder[ListOptionValue] = Encoder.instance { loValue => + implicit val listOptionValueEncoder: Encoder[TimeDoubleList] = Encoder.instance { loValue => Json.arr(loValue.list.map(_.asJson): _*) } implicit val stringListListEncoder: Encoder[StringListList] = Encoder.instance { sllValue => @@ -103,7 +103,8 @@ object Aggregate { def aggregateDoubleValues( AggKey: AggregateKey, - values: List[Option[Double]] + values: List[Option[Double]], + timestamps: List[LocalDateTime], ): Option[AggregateValue] = { val flatValues = values.flatten AggKey match { @@ -111,7 +112,7 @@ object Aggregate { case AggregateKey.Max => flatValues.maximumOption.map(DoubleValue) case AggregateKey.Avg => flatValues.reduceOption(_ + _).map(sum => DoubleValue(sum / flatValues.length)) case AggregateKey.Sum => flatValues.sum.some.map(DoubleValue) - case AggregateKey.List => ListOptionValue(values).some + case AggregateKey.List => TimeDoubleList(timestamps.zip(values)).some case AggregateKey.Distinct => None } } diff --git a/src/main/scala/parse/Main.scala b/src/main/scala/parse/Main.scala index 2d80612..69cfbc4 100644 --- a/src/main/scala/parse/Main.scala +++ b/src/main/scala/parse/Main.scala @@ -14,15 +14,14 @@ object Main { println("================ start parser") val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm") - val from = LocalDateTime.parse("20230414_2200", formatter) - val to = LocalDateTime.parse("20230501_1230", formatter) - val userQuery = UserQuery(List("Liepāja", "Rēzekne", "randomstr"), "tempMax", AggregateKey.Max) + val from = LocalDateTime.parse("20230512_2200", formatter) + val to = LocalDateTime.parse("20230514_1230", formatter) + val userQuery = UserQuery(List("Rīga"), "tempAvg", AggregateKey.List) for { lines <- db.DBService.getInRange(from, to) parsed <- IO.pure(Parser.queryData(userQuery, lines)) _ <- IO.println(parsed.asJson) - _ <- IO.println(None.asJson) } yield () } diff --git a/src/main/scala/parse/Parser.scala b/src/main/scala/parse/Parser.scala index 76a5481..2aaf3cd 100644 --- a/src/main/scala/parse/Parser.scala +++ b/src/main/scala/parse/Parser.scala @@ -12,7 +12,7 @@ object Parser { def parseTimestamp(timestampStr: String): Option[LocalDateTime] = { val formatter = DateTimeFormatter.ofPattern("yyyydd.MM HH:mm") - // TODO figure out what to do with hardcoded year. Proly fetched data should be also modified to inlcude year + // TODO figure out what to do with hardcoded year. Proly fetched data should be also modified to include year Try(LocalDateTime.parse(s"2023${timestampStr.trim}", formatter)).toEither match { case Right(timestamp) => Some(timestamp) case Left(_) => None @@ -49,7 +49,7 @@ object Parser { } case field => { val doubleList = extractDoubleFieldValues(field, weatherStationData.map(_.weather)) - (city -> aggregateDoubleValues(userQuery.key, doubleList)) + (city -> aggregateDoubleValues(userQuery.key, doubleList, weatherStationData.map(_.timestamp))) } }