2023-04-13 16:58:56 +03:00
|
|
|
package server
|
|
|
|
|
|
2023-04-13 22:32:00 +03:00
|
|
|
import cats.effect._
|
2023-04-23 22:41:38 +03:00
|
|
|
import cats.implicits.toTraverseOps
|
|
|
|
|
import db.DBService
|
|
|
|
|
import fetch.FetchService
|
|
|
|
|
import io.circe.{Json, Printer}
|
2023-04-13 22:32:00 +03:00
|
|
|
import org.http4s._
|
|
|
|
|
import org.http4s.dsl.io._
|
|
|
|
|
import org.http4s.implicits._
|
|
|
|
|
import org.http4s.server.Router
|
|
|
|
|
import org.http4s.server.blaze.BlazeServerBuilder
|
2023-04-23 22:41:38 +03:00
|
|
|
import io.circe.syntax._
|
|
|
|
|
import io.circe.generic.auto._
|
|
|
|
|
import org.http4s.circe.jsonEncoder
|
2023-04-18 17:18:45 +03:00
|
|
|
import parse.{Meteo, Parser}
|
2023-04-13 22:32:00 +03:00
|
|
|
|
2023-04-23 22:41:38 +03:00
|
|
|
import java.time.{LocalDate, LocalDateTime}
|
2023-04-13 22:32:00 +03:00
|
|
|
import java.time.format.DateTimeFormatter
|
|
|
|
|
import scala.util.Try
|
|
|
|
|
|
|
|
|
|
object Server extends IOApp {
|
2023-04-18 17:18:45 +03:00
|
|
|
private val formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm")
|
2023-04-13 22:32:00 +03:00
|
|
|
private val appRoutes = HttpRoutes.of[IO] {
|
2023-04-19 17:34:27 +03:00
|
|
|
// http://localhost:3000/query/20230414_2200-20230501_1230/Liepāja,Rēzekne/tempAvg
|
2023-04-19 14:22:28 +03:00
|
|
|
case GET -> Root / "query" / timestampRange / cities / aggregate =>
|
2023-04-13 22:32:00 +03:00
|
|
|
// TODO proly better to Validated with chained errors
|
2023-04-18 17:18:45 +03:00
|
|
|
val parsedArguments = for {
|
2023-04-13 22:32:00 +03:00
|
|
|
(from, to) <- timestampRange.split("-").toList
|
|
|
|
|
.map(str => Try(LocalDateTime.parse(str, formatter)).toOption) match {
|
|
|
|
|
case List(Some(from), Some(to)) => Some(from, to)
|
|
|
|
|
case _ => None
|
|
|
|
|
}
|
|
|
|
|
cityList <- cities.split(",").toList match {
|
|
|
|
|
case list => Some(list)
|
|
|
|
|
case Nil => None
|
|
|
|
|
}
|
2023-04-18 17:18:45 +03:00
|
|
|
aggregate <- Meteo.stringToAggregateParam(aggregate)
|
|
|
|
|
|
2023-04-13 22:32:00 +03:00
|
|
|
}
|
2023-04-18 17:18:45 +03:00
|
|
|
yield (from, to, cityList, aggregate)
|
2023-04-13 22:32:00 +03:00
|
|
|
|
2023-04-18 17:18:45 +03:00
|
|
|
parsedArguments match {
|
|
|
|
|
case Some((from, to, cityList, aggregate)) => {
|
2023-04-19 17:34:27 +03:00
|
|
|
val res = for {
|
|
|
|
|
lines <- db.DBService.getInRange(from, to)
|
|
|
|
|
parsedData <- parse.Parser.queryData(lines, cityList, aggregate)
|
|
|
|
|
} yield parsedData
|
|
|
|
|
|
|
|
|
|
Ok(res.map(_.toString()))
|
2023-04-18 17:18:45 +03:00
|
|
|
}
|
2023-04-13 22:32:00 +03:00
|
|
|
case _ => BadRequest(s"Invalid request format")
|
|
|
|
|
}
|
2023-04-19 14:22:28 +03:00
|
|
|
|
2023-04-23 22:41:38 +03:00
|
|
|
// http://localhost:3000/fetchDate/20230423
|
|
|
|
|
case GET -> Root / "fetchDate" / dateStr => {
|
|
|
|
|
val dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd")
|
|
|
|
|
val maybeDate = Try(LocalDate.parse(dateStr, dateFormatter)).toEither
|
|
|
|
|
maybeDate match {
|
|
|
|
|
case Right(date) => {
|
|
|
|
|
val result = for {
|
|
|
|
|
fetched <- FetchService.fetchFromDate(date)
|
|
|
|
|
(fetchErrors, successDownloads) = fetched.partitionMap(identity)
|
|
|
|
|
saved <- successDownloads.traverse { case (name, content) => DBService.save(name, content) }
|
|
|
|
|
(saveErrors, successSaves) = saved.partitionMap(identity)
|
|
|
|
|
successes = successDownloads.map(s => s"fetched: ${s._1}") ++ successSaves.map(s => s"saved: $s")
|
|
|
|
|
errors = fetchErrors.map(e => s"FetchError: ${e.getMessage}") ++ saveErrors.map(e => s"SaveError: ${e.getMessage}")
|
|
|
|
|
} yield (successes, errors)
|
|
|
|
|
|
|
|
|
|
result.flatMap { case (successes, errors) =>
|
|
|
|
|
val responseBody = Json.obj(
|
|
|
|
|
"errors" -> errors.asJson,
|
|
|
|
|
"successes" -> successes.asJson
|
|
|
|
|
)
|
|
|
|
|
val printer = Printer.spaces2.copy(dropNullValues = true)
|
|
|
|
|
val prettyJson = printer.print(responseBody)
|
|
|
|
|
Ok(prettyJson)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case Left(_) => BadRequest("Invalid request format")
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-19 14:22:28 +03:00
|
|
|
|
|
|
|
|
case GET -> Root / "show" / "fetched_dates" =>
|
|
|
|
|
???
|
|
|
|
|
|
|
|
|
|
case GET -> Root / "show" / dateRange =>
|
|
|
|
|
???
|
|
|
|
|
|
2023-04-13 22:32:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private val httpApp = Router("/" -> appRoutes).orNotFound
|
|
|
|
|
|
|
|
|
|
override def run(args: List[String]): IO[ExitCode] =
|
|
|
|
|
BlazeServerBuilder[IO]
|
|
|
|
|
.bindHttp(3000, "localhost")
|
|
|
|
|
.withHttpApp(httpApp)
|
|
|
|
|
.serve
|
|
|
|
|
.compile
|
|
|
|
|
.drain
|
|
|
|
|
.as(ExitCode.Success)
|
2023-04-13 16:58:56 +03:00
|
|
|
}
|