Parser accepts querying data, server reads from parser
This commit is contained in:
@@ -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())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user