Move debug logic from server to separate file
This commit is contained in:
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,6 @@ import data.DataService
|
|||||||
import db.PostgresService
|
import db.PostgresService
|
||||||
import fetch.csv.FetchService
|
import fetch.csv.FetchService
|
||||||
import fetch.lvgmc
|
import fetch.lvgmc
|
||||||
import fs2.io.file.Files
|
|
||||||
import server.ValidateRoutes.{AggFieldList, AggKey, CityList, DateTimeRange, Granularity, ValidateDate, ValidateDateTime, ValidateInt, ValidateMonths, ValidateZonedDateTime}
|
import server.ValidateRoutes.{AggFieldList, AggKey, CityList, DateTimeRange, Granularity, ValidateDate, ValidateDateTime, ValidateInt, ValidateMonths, ValidateZonedDateTime}
|
||||||
import io.circe.{Encoder, Json, Printer}
|
import io.circe.{Encoder, Json, Printer}
|
||||||
import org.http4s._
|
import org.http4s._
|
||||||
@@ -28,7 +27,6 @@ import org.http4s.circe.jsonEncoder
|
|||||||
import org.typelevel.log4cats.Logger
|
import org.typelevel.log4cats.Logger
|
||||||
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
||||||
import parse.csv.Aggregate
|
import parse.csv.Aggregate
|
||||||
import fs2.io.file.{Files, Path}
|
|
||||||
import org.http4s.headers.`Content-Type`
|
import org.http4s.headers.`Content-Type`
|
||||||
|
|
||||||
import java.time.{ZoneOffset, ZonedDateTime}
|
import java.time.{ZoneOffset, ZonedDateTime}
|
||||||
@@ -44,13 +42,8 @@ object Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Server(postgresService: PostgresService, dataService: DataService, fetch: FetchService, log: Logger[IO]) {
|
class Server(postgresService: PostgresService, dataService: DataService, fetch: FetchService, log: Logger[IO]) {
|
||||||
|
|
||||||
// Define the extension method `pretty` for Json
|
|
||||||
implicit class JsonPrettyPrinter(json: Json) {
|
implicit class JsonPrettyPrinter(json: Json) {
|
||||||
def pretty: String = {
|
def pretty: String = Printer.spaces2.copy(dropNullValues = true).print(json)
|
||||||
val printer = Printer.spaces2.copy(dropNullValues = true)
|
|
||||||
printer.print(json)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private case class ResponseWrapper(result: Map[String, Option[Aggregate.AggregateValue]], query: UserQuery)
|
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] {
|
private val apiRoutes = HttpRoutes.of[IO] {
|
||||||
// http://0.0.0.0:8080/api/show/lvgmc-forecast/Latvija_LTV_pilsetas_tekosa_dn.csv
|
// http://0.0.0.0:8080/api/show/lvgmc-forecast/Latvija_LTV_pilsetas_tekosa_dn.csv
|
||||||
case GET -> Root / "show" / "lvgmc-forecast" / fileName =>
|
case GET -> Root / "show" / "lvgmc-forecast" / fileName =>
|
||||||
println("lvgmc-forecast GET")
|
|
||||||
lvgmc.FetchService.of.flatMap(f => f.fetchFile(fileName)).flatMap(bytes =>
|
lvgmc.FetchService.of.flatMap(f => f.fetchFile(fileName)).flatMap(bytes =>
|
||||||
Ok(bytes).map(_.withContentType(`Content-Type`(MediaType.text.csv)))
|
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
|
// http://0.0.0.0:8080/api/show/grib-all-structure
|
||||||
case GET -> Root / "show" / "grib-all-structure" =>
|
case GET -> Root / "show" / "grib-all-structure" =>
|
||||||
dataService.getAllFileStructure().flatMap(gribList => Ok(gribList.asJson))
|
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" =>
|
case GET -> Root / "grib" / "delete-old-forecasts" =>
|
||||||
dataService.deleteOldForecasts().flatMap(result => Ok(result.asJson))
|
dataService.deleteOldForecasts().flatMap(result => Ok(result.asJson))
|
||||||
|
|
||||||
// http://0.0.0.0:8080/api/show/time
|
// http://0.0.0.0:8080/api/debug/time
|
||||||
case GET -> Root / "show" / "time" =>
|
case GET -> Root / "debug" / "time" =>
|
||||||
val nowUTC = ZonedDateTime.now(ZoneOffset.UTC)
|
val nowUTC = ZonedDateTime.now(ZoneOffset.UTC)
|
||||||
val ageThreshold = nowUTC.minusHours(9)
|
val ageThreshold = nowUTC.minusHours(9)
|
||||||
Ok(ageThreshold.toString)
|
Ok(ageThreshold.toString)
|
||||||
|
|
||||||
// http://0.0.0.0:8080/api/show/folder-structure
|
// http://0.0.0.0:8080/api/debug/folder-structure
|
||||||
case GET -> Root / "show" / "folder-structure" => {
|
case GET -> Root / "debug" / "folder-structure" =>
|
||||||
println("folder-structure")
|
DebugUtils.getFolderStructure.flatMap(response => Ok(response.asJson))
|
||||||
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
|
// 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) =>
|
case GET -> Root / "query" / "city" / CityList(cities) / DateTimeRange(from, to) / Granularity(granularity) / field / AggKey(key) =>
|
||||||
|
|||||||
Reference in New Issue
Block a user