Return of list of time double list instead of just doubles
This commit is contained in:
@@ -4,7 +4,7 @@ import cats.implicits.{catsSyntaxOptionId, toFoldableOps}
|
|||||||
import io.circe.generic.semiauto.deriveEncoder
|
import io.circe.generic.semiauto.deriveEncoder
|
||||||
import io.circe.syntax.EncoderOps
|
import io.circe.syntax.EncoderOps
|
||||||
import io.circe.{Encoder, Json}
|
import io.circe.{Encoder, Json}
|
||||||
import parse.Parser.parseLine
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
object Aggregate {
|
object Aggregate {
|
||||||
|
|
||||||
@@ -46,7 +46,7 @@ object Aggregate {
|
|||||||
|
|
||||||
sealed trait AggregateValue
|
sealed trait AggregateValue
|
||||||
final case class DoubleValue(value: Double) extends 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 StringListList(list: List[List[String]]) extends AggregateValue
|
||||||
final case class DistinctStringList(list: List[String]) extends AggregateValue
|
final case class DistinctStringList(list: List[String]) extends AggregateValue
|
||||||
object AggregateValue {
|
object AggregateValue {
|
||||||
@@ -64,7 +64,7 @@ object Aggregate {
|
|||||||
object AggregateValueImplicits {
|
object AggregateValueImplicits {
|
||||||
implicit val aggregateValueEncoder: Encoder[AggregateValue] = Encoder.instance {
|
implicit val aggregateValueEncoder: Encoder[AggregateValue] = Encoder.instance {
|
||||||
case doubleValue: DoubleValue => doubleValue.asJson
|
case doubleValue: DoubleValue => doubleValue.asJson
|
||||||
case listOptionValue: ListOptionValue => listOptionValue.asJson
|
case timeDoubleList: TimeDoubleList => timeDoubleList.asJson
|
||||||
case stringListList: StringListList => stringListList.asJson
|
case stringListList: StringListList => stringListList.asJson
|
||||||
case distinctStringList: DistinctStringList => distinctStringList.asJson
|
case distinctStringList: DistinctStringList => distinctStringList.asJson
|
||||||
}
|
}
|
||||||
@@ -73,7 +73,7 @@ object Aggregate {
|
|||||||
case DoubleValue(value) if !value.isNaN => Json.fromDouble(value).get
|
case DoubleValue(value) if !value.isNaN => Json.fromDouble(value).get
|
||||||
case _ => Json.Null
|
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): _*)
|
Json.arr(loValue.list.map(_.asJson): _*)
|
||||||
}
|
}
|
||||||
implicit val stringListListEncoder: Encoder[StringListList] = Encoder.instance { sllValue =>
|
implicit val stringListListEncoder: Encoder[StringListList] = Encoder.instance { sllValue =>
|
||||||
@@ -103,7 +103,8 @@ object Aggregate {
|
|||||||
|
|
||||||
def aggregateDoubleValues(
|
def aggregateDoubleValues(
|
||||||
AggKey: AggregateKey,
|
AggKey: AggregateKey,
|
||||||
values: List[Option[Double]]
|
values: List[Option[Double]],
|
||||||
|
timestamps: List[LocalDateTime],
|
||||||
): Option[AggregateValue] = {
|
): Option[AggregateValue] = {
|
||||||
val flatValues = values.flatten
|
val flatValues = values.flatten
|
||||||
AggKey match {
|
AggKey match {
|
||||||
@@ -111,7 +112,7 @@ object Aggregate {
|
|||||||
case AggregateKey.Max => flatValues.maximumOption.map(DoubleValue)
|
case AggregateKey.Max => flatValues.maximumOption.map(DoubleValue)
|
||||||
case AggregateKey.Avg => flatValues.reduceOption(_ + _).map(sum => DoubleValue(sum / flatValues.length))
|
case AggregateKey.Avg => flatValues.reduceOption(_ + _).map(sum => DoubleValue(sum / flatValues.length))
|
||||||
case AggregateKey.Sum => flatValues.sum.some.map(DoubleValue)
|
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
|
case AggregateKey.Distinct => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,15 +14,14 @@ object Main {
|
|||||||
println("================ start parser")
|
println("================ start parser")
|
||||||
|
|
||||||
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
|
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
|
||||||
val from = LocalDateTime.parse("20230414_2200", formatter)
|
val from = LocalDateTime.parse("20230512_2200", formatter)
|
||||||
val to = LocalDateTime.parse("20230501_1230", formatter)
|
val to = LocalDateTime.parse("20230514_1230", formatter)
|
||||||
val userQuery = UserQuery(List("Liepāja", "Rēzekne", "randomstr"), "tempMax", AggregateKey.Max)
|
val userQuery = UserQuery(List("Rīga"), "tempAvg", AggregateKey.List)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
lines <- db.DBService.getInRange(from, to)
|
lines <- db.DBService.getInRange(from, to)
|
||||||
parsed <- IO.pure(Parser.queryData(userQuery, lines))
|
parsed <- IO.pure(Parser.queryData(userQuery, lines))
|
||||||
_ <- IO.println(parsed.asJson)
|
_ <- IO.println(parsed.asJson)
|
||||||
_ <- IO.println(None.asJson)
|
|
||||||
} yield ()
|
} yield ()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ object Parser {
|
|||||||
|
|
||||||
def parseTimestamp(timestampStr: String): Option[LocalDateTime] = {
|
def parseTimestamp(timestampStr: String): Option[LocalDateTime] = {
|
||||||
val formatter = DateTimeFormatter.ofPattern("yyyydd.MM HH:mm")
|
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 {
|
Try(LocalDateTime.parse(s"2023${timestampStr.trim}", formatter)).toEither match {
|
||||||
case Right(timestamp) => Some(timestamp)
|
case Right(timestamp) => Some(timestamp)
|
||||||
case Left(_) => None
|
case Left(_) => None
|
||||||
@@ -49,7 +49,7 @@ object Parser {
|
|||||||
}
|
}
|
||||||
case field => {
|
case field => {
|
||||||
val doubleList = extractDoubleFieldValues(field, weatherStationData.map(_.weather))
|
val doubleList = extractDoubleFieldValues(field, weatherStationData.map(_.weather))
|
||||||
(city -> aggregateDoubleValues(userQuery.key, doubleList))
|
(city -> aggregateDoubleValues(userQuery.key, doubleList, weatherStationData.map(_.timestamp)))
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user