feat(server): store and expose last updated date of gtfs data
This commit is contained in:
parent
25e5282ea8
commit
5535034fd7
8 changed files with 430 additions and 2 deletions
|
|
@ -19,6 +19,7 @@ import moe.lava.banksia.Constants
|
|||
import moe.lava.banksia.di.CommonModules
|
||||
import moe.lava.banksia.room.dao.RouteDao
|
||||
import moe.lava.banksia.room.dao.StopDao
|
||||
import moe.lava.banksia.room.dao.VersionMetadataDao
|
||||
import moe.lava.banksia.server.di.ServerModules
|
||||
import moe.lava.banksia.server.gtfs.GtfsHandler
|
||||
import moe.lava.banksia.server.gtfsr.GtfsrService
|
||||
|
|
@ -61,6 +62,22 @@ fun Application.module() {
|
|||
}
|
||||
}
|
||||
|
||||
get("/metadata/{type?}") {
|
||||
val dao by inject<VersionMetadataDao>()
|
||||
val type = call.parameters["type"]
|
||||
if (type == null) {
|
||||
call.respond(dao.getAll().map { it.asModel() })
|
||||
return@get
|
||||
}
|
||||
|
||||
val data = dao.get(type)?.asModel()
|
||||
if (data == null) {
|
||||
call.respond(HttpStatusCode.NotFound)
|
||||
} else {
|
||||
call.respond(data)
|
||||
}
|
||||
}
|
||||
|
||||
get("/routes") {
|
||||
val routes = withContext(context = Dispatchers.IO) {
|
||||
inject<RouteDao>().value.getAll()
|
||||
|
|
|
|||
|
|
@ -29,6 +29,8 @@ import moe.lava.banksia.server.gtfs.structures.GtfsTrip
|
|||
import moe.lava.banksia.util.Point
|
||||
import java.io.File
|
||||
import java.util.zip.ZipFile
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.ExperimentalTime
|
||||
|
||||
class GtfsHandler(
|
||||
private val log: Logger,
|
||||
|
|
@ -38,7 +40,8 @@ class GtfsHandler(
|
|||
private val csv = CsvFormat(StringDeferringConfig(EmptySerializersModule()))
|
||||
private val datasetPath = File("/tmp/banksia", "dataset.zip")
|
||||
|
||||
suspend fun update(datasetUrl: String) {
|
||||
@OptIn(ExperimentalTime::class)
|
||||
suspend fun update(datasetUrl: String, date: Long? = null) {
|
||||
val parentDir = datasetPath.parentFile
|
||||
@Suppress("SimplifyBooleanWithConstants", "KotlinConstantConditions")
|
||||
if (parentDir.exists() && !Constants.devMode)
|
||||
|
|
@ -72,6 +75,8 @@ class GtfsHandler(
|
|||
addTrips(files)
|
||||
addStopTimes(files)
|
||||
|
||||
updateMetadata(date ?: Clock.System.now().epochSeconds)
|
||||
|
||||
@Suppress("KotlinConstantConditions")
|
||||
if (!Constants.devMode) {
|
||||
parentDir.deleteRecursively()
|
||||
|
|
@ -80,6 +85,12 @@ class GtfsHandler(
|
|||
log.info("done!")
|
||||
}
|
||||
|
||||
private suspend fun updateMetadata(date: Long) {
|
||||
val dao = db.versionMetadataDao
|
||||
log.info("updating metadata...")
|
||||
dao.update(date, listOf("routes", "stops", "shapes", "trips", "stop_times"))
|
||||
}
|
||||
|
||||
private suspend fun addRoutes(files: List<File>) {
|
||||
val dao = db.routeDao
|
||||
log.info("parsing routes...")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue