From 52180544e9bfe80c87a63ee0aa44073764fb0e83 Mon Sep 17 00:00:00 2001 From: Guntis Smaukstelis Date: Thu, 6 Mar 2025 00:00:21 +0200 Subject: [PATCH] Add debugging rest methods for folders and time --- README.md | 2 +- src/main/scala/server/Server.scala | 50 +++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 72986c6..11a07e9 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Rename `.env-sample` to `.env` and fill in credentials ## Run ``` -docker-compose up --build +docker-compose up --build --no-cache --force-recreate ``` ## Web diff --git a/src/main/scala/server/Server.scala b/src/main/scala/server/Server.scala index f5f10dd..44670cc 100644 --- a/src/main/scala/server/Server.scala +++ b/src/main/scala/server/Server.scala @@ -6,8 +6,9 @@ import com.comcast.ip4s.IpLiteralSyntax import data.DataService import db.PostgresService import fetch.csv.FetchService +import fs2.io.file.Files import server.ValidateRoutes.{AggFieldList, AggKey, CityList, DateTimeRange, Granularity, ValidateDate, ValidateDateTime, ValidateInt, ValidateMonths, ValidateZonedDateTime} -import io.circe.{Json, Printer} +import io.circe.{Encoder, Json, Printer} import org.http4s._ import org.http4s.dsl.io._ import org.http4s.implicits._ @@ -17,6 +18,7 @@ import org.http4s.server.middleware.CORSConfig import org.http4s.server.staticcontent.FileService import org.http4s.ember.server.EmberServerBuilder import io.circe.generic.auto._ +import io.circe.generic.semiauto.deriveEncoder import io.circe.syntax._ import parse.csv.Aggregate.AggregateValueImplicits.aggregateValueEncoder import parse.csv.Aggregate.userQueryEncoder @@ -25,7 +27,9 @@ import org.http4s.circe.jsonEncoder import org.typelevel.log4cats.Logger import org.typelevel.log4cats.slf4j.Slf4jLogger import parse.csv.Aggregate +import fs2.io.file.{Files, Path} +import java.time.{ZoneOffset, ZonedDateTime} import scala.concurrent.duration.DurationInt @@ -71,6 +75,50 @@ class Server(postgresService: PostgresService, dataService: DataService, fetch: case GET -> Root / "grib" / "delete-old-forecasts" => dataService.deleteOldForecasts().flatMap(result => Ok(result.asJson)) + // http://0.0.0.0:8080/api/show/time + case GET -> Root / "show" / "time" => + val nowUTC = ZonedDateTime.now(ZoneOffset.UTC) + val ageThreshold = nowUTC.minusHours(9) + Ok(ageThreshold.toString) + + // http://0.0.0.0:8080/api/show/folder-structure + case GET -> Root / "show" / "folder-structure" => { + println("folder-structure") + case class FileInfo(name: String, isDirectory: Boolean) + case class DirectoryStructure(path: String, files: List[FileInfo]) + case class FolderStructureResponse( + current: DirectoryStructure, + data: DirectoryStructure, + grib: DirectoryStructure + ) + + implicit val folderStructureResponseEncoder: Encoder[FolderStructureResponse] = deriveEncoder[FolderStructureResponse] + + def getDirectoryStructure(path: Path): IO[DirectoryStructure] = { + for { + _ <- IO.println(path) + absolutePath <- IO(path.toString) + files <- Files[IO].list(path).compile.toList + fileInfos <- files.traverse { filePath => + Files[IO].isDirectory(filePath).map { isDir => + FileInfo(filePath.fileName.toString, isDir) + } + } + } yield DirectoryStructure(absolutePath, fileInfos) + } + + val currentPath = Path(".") + val dataPath = Path("data") + val gribPath = Path("data/grib") + + for { + current <- getDirectoryStructure(currentPath) + data <- getDirectoryStructure(dataPath) + grib <- getDirectoryStructure(gribPath) + response = FolderStructureResponse(current, data, grib) + result <- Ok(response.asJson) + } yield result + } // http://0.0.0.0:8080/api/query/city/Liepāja,Rēzekne/20230414_2200-20230501_1230/hour/tempMax/max