Parser accepts querying data, server reads from parser

This commit is contained in:
Guntis Smaukstelis
2023-04-18 17:18:45 +03:00
parent 260b6ef23a
commit 0e60f94835
6 changed files with 94 additions and 26 deletions
+37 -1
View File
@@ -24,9 +24,45 @@ case class MeteoData(
atmPressure: Option[Double],
dewPoint: Option[Double],
airHumidity: Option[Double],
sunshineDuration: Option[Double]
sunshineDuration: Option[Double],
)
sealed trait AggregateMeteo
object AggregateMeteo {
case object tempMax extends AggregateMeteo
case object tempMin extends AggregateMeteo
case object tempAvg extends AggregateMeteo
case object precipitationSum extends AggregateMeteo
case object windSpeedAvg extends AggregateMeteo
case object windGustMax extends AggregateMeteo
case object snowThicknessAvg extends AggregateMeteo
case object dewPointAvg extends AggregateMeteo
case object airHumidityAvg extends AggregateMeteo
}
object Meteo {
def stringToAggregateParam(strParam: String): Option[AggregateMeteo] = strParam match {
case "tempMax" => Some(AggregateMeteo.tempMax)
case "tempMin" => Some(AggregateMeteo.tempMin)
case "tempAvg" => Some(AggregateMeteo.tempAvg)
case "precipitationSum" => Some(AggregateMeteo.precipitationSum)
case "windSpeedAvg" => Some(AggregateMeteo.windSpeedAvg)
case "windGustMax" => Some(AggregateMeteo.windGustMax)
case "snowThicknessAvg" => Some(AggregateMeteo.snowThicknessAvg)
case "dewPointAvg" => Some(AggregateMeteo.dewPointAvg)
case "airHumidityAvg" => Some(AggregateMeteo.airHumidityAvg)
case _ => None
}
}
object MeteoData {
def fromDoubles(data: List[Option[Double]]): Option[MeteoData] = data match {
case List(
+40 -14
View File
@@ -6,6 +6,23 @@ import java.time.format.DateTimeFormatter
import scala.io.Source
import scala.util.Try
/*
import cats.effect.{IO, Resource}
import java.io.File
def readFile(file: File): IO[String] =
IO(scala.io.Source.fromFile(file).mkString).handleErrorWith(_ => IO.pure(""))
def readFiles(dir: File): IO[List[(String, String)]] =
IO(dir.listFiles.toList)
.flatMap(files =>
files.traverse { file =>
readFile(file).map((file.getName, _))
}
)
.handleErrorWith(_ => IO.pure(List.empty))
*/
object Parser {
val data_path = "/Users/guntissmaukstelis/sandbox/hello/data/"
@@ -63,6 +80,26 @@ object Parser {
def readFromVariable(str: String): List[String] = str.split("\n").toList // Data.csv
def queryData(from: LocalDateTime, to: LocalDateTime, cities: List[String], aggregator: AggregateMeteo): Map[String, Double] = {
val res = getFilesInRange(from, to)
.flatMap(readFromFile)
.flatMap(parseLine)
.filter(line => cities.contains(line.city))
.groupBy(_.city)
// res.foreach(println)
aggregator match {
case AggregateMeteo.tempAvg => res.map { case (city, weatherData) => city -> weatherData.flatMap(_.meteo.tempMax).max }
case AggregateMeteo.tempAvg => res.map { case (city, weatherData) => city -> weatherData.flatMap(_.meteo.tempMin).min }
case AggregateMeteo.tempAvg => res.map { case (city, weatherData) => city -> {
val avgList = weatherData.flatMap(_.meteo.tempAvg)
avgList.sum / avgList.length
}}
case AggregateMeteo.precipitationSum => res.map { case (city, weatherData) => city -> weatherData.flatMap(_.meteo.precipitation).sum }
// TODO add here other aggregateParams
}
}
def main(args: Array[String]): Unit = {
println("================ start parser")
@@ -70,20 +107,9 @@ object Parser {
val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
val start = LocalDateTime.parse("20230409_2200", formatter)
val end = LocalDateTime.parse("20230501_1230", formatter)
val files = getFilesInRange(start, end)
val weatherStationData = files
.flatMap(readFromFile)
.flatMap(parseLine)
// weatherStationData.foreach(println)
val liepajaWeather = weatherStationData.filter(_.city == "Liepāja")
val maxTempLiepaja = liepajaWeather.flatMap(_.meteo.tempMax).max
val minTempLiepaja = liepajaWeather.flatMap(_.meteo.tempMin).min
val avgTemps = liepajaWeather.flatMap(_.meteo.tempAvg)
val avgTempLiepaja = avgTemps.sum / avgTemps.size
println(s"Liepaja max: $maxTempLiepaja, min: $minTempLiepaja, avg: $avgTempLiepaja")
// liepaja.foreach(println)
val parsed = queryData(start, end, List("Liepāja", "Rēzekne", "randomstr"), AggregateMeteo.tempAvg)
// parsed.foreach(println)
println(parsed.toString())
}
}