Move debug logic from server to separate file

This commit is contained in:
Guntis Smaukstelis
2025-03-16 13:33:52 +02:00
parent a5aeddd351
commit 329776b67f
2 changed files with 47 additions and 52 deletions
+41
View File
@@ -0,0 +1,41 @@
package server
import cats.effect.IO
import cats.implicits.toTraverseOps
import fs2.io.file.{Files, Path}
object DebugUtils {
case class FileInfo(name: String, isDirectory: Boolean)
case class DirectoryStructure(path: String, files: List[FileInfo])
case class FolderStructureResponse(
current: DirectoryStructure,
data: DirectoryStructure,
grib: DirectoryStructure
)
private 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)
}
def getFolderStructure: IO[FolderStructureResponse] = {
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)
} yield response
}
}
+6 -52
View File
@@ -7,7 +7,6 @@ import data.DataService
import db.PostgresService
import fetch.csv.FetchService
import fetch.lvgmc
import fs2.io.file.Files
import server.ValidateRoutes.{AggFieldList, AggKey, CityList, DateTimeRange, Granularity, ValidateDate, ValidateDateTime, ValidateInt, ValidateMonths, ValidateZonedDateTime}
import io.circe.{Encoder, Json, Printer}
import org.http4s._
@@ -28,7 +27,6 @@ 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 org.http4s.headers.`Content-Type`
import java.time.{ZoneOffset, ZonedDateTime}
@@ -44,13 +42,8 @@ object Server {
}
class Server(postgresService: PostgresService, dataService: DataService, fetch: FetchService, log: Logger[IO]) {
// Define the extension method `pretty` for Json
implicit class JsonPrettyPrinter(json: Json) {
def pretty: String = {
val printer = Printer.spaces2.copy(dropNullValues = true)
printer.print(json)
}
def pretty: String = Printer.spaces2.copy(dropNullValues = true).print(json)
}
private case class ResponseWrapper(result: Map[String, Option[Aggregate.AggregateValue]], query: UserQuery)
@@ -58,13 +51,10 @@ class Server(postgresService: PostgresService, dataService: DataService, fetch:
private val apiRoutes = HttpRoutes.of[IO] {
// http://0.0.0.0:8080/api/show/lvgmc-forecast/Latvija_LTV_pilsetas_tekosa_dn.csv
case GET -> Root / "show" / "lvgmc-forecast" / fileName =>
println("lvgmc-forecast GET")
lvgmc.FetchService.of.flatMap(f => f.fetchFile(fileName)).flatMap(bytes =>
Ok(bytes).map(_.withContentType(`Content-Type`(MediaType.text.csv)))
)
case GET -> Root / "show" / "gribName" => Ok("{\"fileName\":\"TODO replace this fake name\"}")
// http://0.0.0.0:8080/api/show/grib-all-structure
case GET -> Root / "show" / "grib-all-structure" =>
dataService.getAllFileStructure().flatMap(gribList => Ok(gribList.asJson))
@@ -84,51 +74,15 @@ 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" =>
// http://0.0.0.0:8080/api/debug/time
case GET -> Root / "debug" / "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/debug/folder-structure
case GET -> Root / "debug" / "folder-structure" =>
DebugUtils.getFolderStructure.flatMap(response => Ok(response.asJson))
// http://0.0.0.0:8080/api/query/city/Liepāja,Rēzekne/20230414_2200-20230501_1230/hour/tempMax/max
case GET -> Root / "query" / "city" / CityList(cities) / DateTimeRange(from, to) / Granularity(granularity) / field / AggKey(key) =>