mirror of
https://github.com/2martens/tsw-timetable.git
synced 2026-10-17 16:06:51 +02:00
change: Add userId, routeId to uniquely identify timetables
This commit is contained in:
+5
@@ -1,6 +1,8 @@
|
|||||||
package de.twomartens.timetable.bahnApi.mapper
|
package de.twomartens.timetable.bahnApi.mapper
|
||||||
|
|
||||||
import de.twomartens.timetable.bahnApi.model.db.BahnTimetable
|
import de.twomartens.timetable.bahnApi.model.db.BahnTimetable
|
||||||
|
import de.twomartens.timetable.model.common.RouteId
|
||||||
|
import de.twomartens.timetable.model.common.UserId
|
||||||
import de.twomartens.timetable.types.HourAtDay
|
import de.twomartens.timetable.types.HourAtDay
|
||||||
import de.twomartens.timetable.types.NonEmptyString
|
import de.twomartens.timetable.types.NonEmptyString
|
||||||
import org.mapstruct.*
|
import org.mapstruct.*
|
||||||
@@ -15,8 +17,11 @@ interface BahnTimetableMapper {
|
|||||||
@Mapping(target = "created", ignore = true)
|
@Mapping(target = "created", ignore = true)
|
||||||
@Mapping(target = "lastModified", ignore = true)
|
@Mapping(target = "lastModified", ignore = true)
|
||||||
fun mapToDB(dto: de.twomartens.timetable.bahnApi.model.dto.BahnTimetable,
|
fun mapToDB(dto: de.twomartens.timetable.bahnApi.model.dto.BahnTimetable,
|
||||||
|
userId: UserId, routeId: RouteId,
|
||||||
hourAtDay: HourAtDay): BahnTimetable {
|
hourAtDay: HourAtDay): BahnTimetable {
|
||||||
return BahnTimetable(
|
return BahnTimetable(
|
||||||
|
userId,
|
||||||
|
routeId,
|
||||||
dto.eva,
|
dto.eva,
|
||||||
hourAtDay,
|
hourAtDay,
|
||||||
NonEmptyString(dto.station),
|
NonEmptyString(dto.station),
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package de.twomartens.timetable.bahnApi.model
|
package de.twomartens.timetable.bahnApi.model
|
||||||
|
|
||||||
import de.twomartens.timetable.bahnApi.model.db.ScheduledFetchTask
|
import de.twomartens.timetable.bahnApi.model.db.ScheduledFetchTask
|
||||||
|
import de.twomartens.timetable.model.common.RouteId
|
||||||
|
import de.twomartens.timetable.model.common.UserId
|
||||||
import de.twomartens.timetable.types.HourAtDay
|
import de.twomartens.timetable.types.HourAtDay
|
||||||
import org.springframework.stereotype.Component
|
import org.springframework.stereotype.Component
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
@@ -18,10 +20,11 @@ class TaskFactory {
|
|||||||
countTasks(existingTasks)
|
countTasks(existingTasks)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createTaskAndUpdateCounter(eva: Eva, hourAtDay: HourAtDay): ScheduledFetchTask {
|
fun createTaskAndUpdateCounter(userId: UserId, routeId: RouteId, eva: Eva,
|
||||||
|
hourAtDay: HourAtDay): ScheduledFetchTask {
|
||||||
val fetchDateTime = calculateNextAvailableFetchDateTime(hourAtDay)
|
val fetchDateTime = calculateNextAvailableFetchDateTime(hourAtDay)
|
||||||
incrementTaskCountForHourAtDay(hourAtDay)
|
incrementTaskCountForHourAtDay(hourAtDay)
|
||||||
return ScheduledFetchTask(eva, hourAtDay, fetchDateTime)
|
return ScheduledFetchTask(userId, routeId, eva, hourAtDay, fetchDateTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun calculateNextAvailableFetchDateTime(hourAtDay: HourAtDay): LocalDateTime {
|
private fun calculateNextAvailableFetchDateTime(hourAtDay: HourAtDay): LocalDateTime {
|
||||||
|
|||||||
+5
-1
@@ -2,6 +2,8 @@ package de.twomartens.timetable.bahnApi.model.db
|
|||||||
|
|
||||||
import de.twomartens.timetable.bahnApi.model.Eva
|
import de.twomartens.timetable.bahnApi.model.Eva
|
||||||
import de.twomartens.timetable.bahnApi.model.dto.BahnStationStop
|
import de.twomartens.timetable.bahnApi.model.dto.BahnStationStop
|
||||||
|
import de.twomartens.timetable.model.common.RouteId
|
||||||
|
import de.twomartens.timetable.model.common.UserId
|
||||||
import de.twomartens.timetable.types.HourAtDay
|
import de.twomartens.timetable.types.HourAtDay
|
||||||
import de.twomartens.timetable.types.NonEmptyString
|
import de.twomartens.timetable.types.NonEmptyString
|
||||||
import org.bson.types.ObjectId
|
import org.bson.types.ObjectId
|
||||||
@@ -13,8 +15,10 @@ import org.springframework.data.mongodb.core.mapping.Document
|
|||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
|
|
||||||
@Document
|
@Document
|
||||||
@CompoundIndex(def = "{'eva': 1, 'hourAtDay': 1}", unique = true)
|
@CompoundIndex(def = "{'userId': 1, 'routeId': 1, 'eva': 1, 'hourAtDay': 1}", unique = true)
|
||||||
data class BahnTimetable(
|
data class BahnTimetable(
|
||||||
|
var userId: UserId,
|
||||||
|
var routeId: RouteId,
|
||||||
var eva: Eva,
|
var eva: Eva,
|
||||||
var hourAtDay: HourAtDay,
|
var hourAtDay: HourAtDay,
|
||||||
var station: NonEmptyString,
|
var station: NonEmptyString,
|
||||||
|
|||||||
+5
-1
@@ -1,6 +1,8 @@
|
|||||||
package de.twomartens.timetable.bahnApi.model.db
|
package de.twomartens.timetable.bahnApi.model.db
|
||||||
|
|
||||||
import de.twomartens.timetable.bahnApi.model.Eva
|
import de.twomartens.timetable.bahnApi.model.Eva
|
||||||
|
import de.twomartens.timetable.model.common.RouteId
|
||||||
|
import de.twomartens.timetable.model.common.UserId
|
||||||
import de.twomartens.timetable.types.HourAtDay
|
import de.twomartens.timetable.types.HourAtDay
|
||||||
import org.bson.types.ObjectId
|
import org.bson.types.ObjectId
|
||||||
import org.springframework.data.annotation.CreatedDate
|
import org.springframework.data.annotation.CreatedDate
|
||||||
@@ -12,8 +14,10 @@ import java.time.Instant
|
|||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
@Document
|
@Document
|
||||||
@CompoundIndex(def = "{'eva': 1, 'fetchedDateTime': 1}", unique = true)
|
@CompoundIndex(def = "{'userId': 1, 'routeId': 1, 'eva': 1, 'fetchedDateTime': 1}", unique = true)
|
||||||
data class ScheduledFetchTask(
|
data class ScheduledFetchTask(
|
||||||
|
var userId: UserId,
|
||||||
|
var routeId: RouteId,
|
||||||
var eva: Eva,
|
var eva: Eva,
|
||||||
var fetchedDateTime: HourAtDay,
|
var fetchedDateTime: HourAtDay,
|
||||||
var scheduledExecutionDateTime: LocalDateTime
|
var scheduledExecutionDateTime: LocalDateTime
|
||||||
|
|||||||
+5
-2
@@ -6,6 +6,8 @@ import de.twomartens.timetable.bahnApi.model.dto.BahnStation
|
|||||||
import de.twomartens.timetable.bahnApi.model.dto.BahnTimetable
|
import de.twomartens.timetable.bahnApi.model.dto.BahnTimetable
|
||||||
import de.twomartens.timetable.bahnApi.repository.BahnStationRepository
|
import de.twomartens.timetable.bahnApi.repository.BahnStationRepository
|
||||||
import de.twomartens.timetable.bahnApi.repository.BahnTimetableRepository
|
import de.twomartens.timetable.bahnApi.repository.BahnTimetableRepository
|
||||||
|
import de.twomartens.timetable.model.common.RouteId
|
||||||
|
import de.twomartens.timetable.model.common.UserId
|
||||||
import de.twomartens.timetable.model.db.Station
|
import de.twomartens.timetable.model.db.Station
|
||||||
import de.twomartens.timetable.model.repository.StationRepository
|
import de.twomartens.timetable.model.repository.StationRepository
|
||||||
import de.twomartens.timetable.types.HourAtDay
|
import de.twomartens.timetable.types.HourAtDay
|
||||||
@@ -87,8 +89,9 @@ open class BahnDatabaseService(
|
|||||||
.execute()
|
.execute()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun storeTimetable(timetable: BahnTimetable, hourAtDay: HourAtDay) {
|
fun storeTimetable(timetable: BahnTimetable, userId: UserId, routeId: RouteId,
|
||||||
bahnTimetableRepository.save(bahnTimetableMapper.mapToDB(timetable, hourAtDay))
|
hourAtDay: HourAtDay) {
|
||||||
|
bahnTimetableRepository.save(bahnTimetableMapper.mapToDB(timetable, userId, routeId, hourAtDay))
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
+7
-4
@@ -6,6 +6,8 @@ import de.twomartens.timetable.bahnApi.repository.ScheduledFetchTaskRepository
|
|||||||
import de.twomartens.timetable.bahnApi.tasks.DeleteScheduledTask
|
import de.twomartens.timetable.bahnApi.tasks.DeleteScheduledTask
|
||||||
import de.twomartens.timetable.bahnApi.tasks.FetchTimetableTask
|
import de.twomartens.timetable.bahnApi.tasks.FetchTimetableTask
|
||||||
import de.twomartens.timetable.bahnApi.tasks.StoreTimetableTask
|
import de.twomartens.timetable.bahnApi.tasks.StoreTimetableTask
|
||||||
|
import de.twomartens.timetable.model.common.RouteId
|
||||||
|
import de.twomartens.timetable.model.common.UserId
|
||||||
import de.twomartens.timetable.types.HourAtDay
|
import de.twomartens.timetable.types.HourAtDay
|
||||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
|
||||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
|
||||||
@@ -24,8 +26,9 @@ class FetchTaskScheduler(
|
|||||||
private val bahnDatabaseService: BahnDatabaseService,
|
private val bahnDatabaseService: BahnDatabaseService,
|
||||||
private val scheduledFetchTaskRepository: ScheduledFetchTaskRepository
|
private val scheduledFetchTaskRepository: ScheduledFetchTaskRepository
|
||||||
) {
|
) {
|
||||||
fun scheduleStoreTask(timetable: BahnTimetable, hourAtDay: HourAtDay) {
|
fun scheduleStoreTask(timetable: BahnTimetable, userId: UserId, routeId: RouteId,
|
||||||
val storeTask = StoreTimetableTask(timetable, hourAtDay, bahnDatabaseService)
|
hourAtDay: HourAtDay) {
|
||||||
|
val storeTask = StoreTimetableTask(timetable, userId, routeId, hourAtDay, bahnDatabaseService)
|
||||||
threadPoolTaskExecutor.execute(storeTask)
|
threadPoolTaskExecutor.execute(storeTask)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,8 +81,8 @@ class FetchTaskScheduler(
|
|||||||
scheduledFetchTask: ScheduledFetchTask,
|
scheduledFetchTask: ScheduledFetchTask,
|
||||||
zonedExecutionTime: ZonedDateTime
|
zonedExecutionTime: ZonedDateTime
|
||||||
) {
|
) {
|
||||||
val timetableTask = FetchTimetableTask(scheduledFetchTask.eva,
|
val timetableTask = FetchTimetableTask(scheduledFetchTask.userId, scheduledFetchTask.routeId,
|
||||||
scheduledFetchTask.fetchedDateTime,
|
scheduledFetchTask.eva, scheduledFetchTask.fetchedDateTime,
|
||||||
bahnApiService, this)
|
bahnApiService, this)
|
||||||
threadPoolTaskScheduler.schedule(timetableTask, zonedExecutionTime.toInstant())
|
threadPoolTaskScheduler.schedule(timetableTask, zonedExecutionTime.toInstant())
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-3
@@ -88,16 +88,19 @@ class ScheduledTaskService(
|
|||||||
val stationId = it.id
|
val stationId = it.id
|
||||||
val eva = Eva.of(stationId)
|
val eva = Eva.of(stationId)
|
||||||
var hourAtDay = HourAtDay.of(Hour.of(23), fetchDates.previousDay)
|
var hourAtDay = HourAtDay.of(Hour.of(23), fetchDates.previousDay)
|
||||||
var newTask = taskFactory.createTaskAndUpdateCounter(eva, hourAtDay)
|
var newTask = taskFactory.createTaskAndUpdateCounter(tswRoute.userId,
|
||||||
|
tswRoute.routeId, eva, hourAtDay)
|
||||||
newTasks.add(newTask)
|
newTasks.add(newTask)
|
||||||
for (hour in 0..23) {
|
for (hour in 0..23) {
|
||||||
hourAtDay = HourAtDay.of(Hour.of(hour), fetchDates.fetchDate)
|
hourAtDay = HourAtDay.of(Hour.of(hour), fetchDates.fetchDate)
|
||||||
newTask = taskFactory.createTaskAndUpdateCounter(eva, hourAtDay)
|
newTask = taskFactory.createTaskAndUpdateCounter(tswRoute.userId,
|
||||||
|
tswRoute.routeId, eva, hourAtDay)
|
||||||
newTasks.add(newTask)
|
newTasks.add(newTask)
|
||||||
}
|
}
|
||||||
for (hour in 0..3) {
|
for (hour in 0..3) {
|
||||||
hourAtDay = HourAtDay.of(Hour.of(hour), fetchDates.nextDate)
|
hourAtDay = HourAtDay.of(Hour.of(hour), fetchDates.nextDate)
|
||||||
newTask = taskFactory.createTaskAndUpdateCounter(eva, hourAtDay)
|
newTask = taskFactory.createTaskAndUpdateCounter(tswRoute.userId,
|
||||||
|
tswRoute.routeId, eva, hourAtDay)
|
||||||
newTasks.add(newTask)
|
newTasks.add(newTask)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-1
@@ -3,12 +3,16 @@ package de.twomartens.timetable.bahnApi.tasks
|
|||||||
import de.twomartens.timetable.bahnApi.model.Eva
|
import de.twomartens.timetable.bahnApi.model.Eva
|
||||||
import de.twomartens.timetable.bahnApi.service.BahnApiService
|
import de.twomartens.timetable.bahnApi.service.BahnApiService
|
||||||
import de.twomartens.timetable.bahnApi.service.FetchTaskScheduler
|
import de.twomartens.timetable.bahnApi.service.FetchTaskScheduler
|
||||||
|
import de.twomartens.timetable.model.common.RouteId
|
||||||
|
import de.twomartens.timetable.model.common.UserId
|
||||||
import de.twomartens.timetable.types.HourAtDay
|
import de.twomartens.timetable.types.HourAtDay
|
||||||
import mu.KotlinLogging
|
import mu.KotlinLogging
|
||||||
import org.springframework.scheduling.annotation.Async
|
import org.springframework.scheduling.annotation.Async
|
||||||
|
|
||||||
@Async
|
@Async
|
||||||
open class FetchTimetableTask(
|
open class FetchTimetableTask(
|
||||||
|
private val userId: UserId,
|
||||||
|
private val routeId: RouteId,
|
||||||
private val eva: Eva,
|
private val eva: Eva,
|
||||||
private val hourAtDay: HourAtDay,
|
private val hourAtDay: HourAtDay,
|
||||||
private val bahnApiService: BahnApiService,
|
private val bahnApiService: BahnApiService,
|
||||||
@@ -18,7 +22,7 @@ open class FetchTimetableTask(
|
|||||||
"Fetch timetable: [eva: $eva], [date: ${hourAtDay.date}], [hour: ${hourAtDay.hour}]"
|
"Fetch timetable: [eva: $eva], [date: ${hourAtDay.date}], [hour: ${hourAtDay.hour}]"
|
||||||
}
|
}
|
||||||
val timetable = bahnApiService.fetchTimetable(eva, hourAtDay)
|
val timetable = bahnApiService.fetchTimetable(eva, hourAtDay)
|
||||||
scheduler.scheduleStoreTask(timetable, hourAtDay)
|
scheduler.scheduleStoreTask(timetable, userId, routeId, hourAtDay)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
+5
-1
@@ -2,6 +2,8 @@ package de.twomartens.timetable.bahnApi.tasks
|
|||||||
|
|
||||||
import de.twomartens.timetable.bahnApi.model.dto.BahnTimetable
|
import de.twomartens.timetable.bahnApi.model.dto.BahnTimetable
|
||||||
import de.twomartens.timetable.bahnApi.service.BahnDatabaseService
|
import de.twomartens.timetable.bahnApi.service.BahnDatabaseService
|
||||||
|
import de.twomartens.timetable.model.common.RouteId
|
||||||
|
import de.twomartens.timetable.model.common.UserId
|
||||||
import de.twomartens.timetable.types.HourAtDay
|
import de.twomartens.timetable.types.HourAtDay
|
||||||
import mu.KotlinLogging
|
import mu.KotlinLogging
|
||||||
import org.springframework.scheduling.annotation.Async
|
import org.springframework.scheduling.annotation.Async
|
||||||
@@ -9,12 +11,14 @@ import org.springframework.scheduling.annotation.Async
|
|||||||
@Async
|
@Async
|
||||||
open class StoreTimetableTask(
|
open class StoreTimetableTask(
|
||||||
private val timetable: BahnTimetable,
|
private val timetable: BahnTimetable,
|
||||||
|
private val userId: UserId,
|
||||||
|
private val routeId: RouteId,
|
||||||
private val hourAtDay: HourAtDay,
|
private val hourAtDay: HourAtDay,
|
||||||
private val bahnDatabaseService: BahnDatabaseService
|
private val bahnDatabaseService: BahnDatabaseService
|
||||||
) : Runnable {
|
) : Runnable {
|
||||||
override fun run() {
|
override fun run() {
|
||||||
log.info { "Store timetable: [eva: ${timetable.eva}], [station: ${timetable.station}], [hour: ${hourAtDay}]" }
|
log.info { "Store timetable: [eva: ${timetable.eva}], [station: ${timetable.station}], [hour: ${hourAtDay}]" }
|
||||||
bahnDatabaseService.storeTimetable(timetable, hourAtDay)
|
bahnDatabaseService.storeTimetable(timetable, userId, routeId, hourAtDay)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
Reference in New Issue
Block a user