From b75424b4993bffbd8a0d048a03d5bbf116d56b05 Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Thu, 18 Sep 2025 17:15:42 +0200 Subject: [PATCH] change: Remove routeId from BahnTimetable --- .../timetable/bahnApi/mapper/BahnTimetableMapper.kt | 4 +--- .../twomartens/timetable/bahnApi/model/TaskFactory.kt | 8 +++----- .../timetable/bahnApi/model/db/BahnTimetable.kt | 4 +--- .../timetable/bahnApi/model/db/ScheduledFetchTask.kt | 4 +--- .../timetable/bahnApi/service/BahnDatabaseService.kt | 6 ++---- .../timetable/bahnApi/service/FetchTaskScheduler.kt | 10 ++++------ .../timetable/bahnApi/service/ScheduledTaskService.kt | 6 +++--- .../timetable/bahnApi/tasks/FetchTimetableTask.kt | 4 +--- .../timetable/bahnApi/tasks/StoreTimetableTask.kt | 4 +--- 9 files changed, 17 insertions(+), 33 deletions(-) diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/mapper/BahnTimetableMapper.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/mapper/BahnTimetableMapper.kt index ba02a3d..d29eaa0 100644 --- a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/mapper/BahnTimetableMapper.kt +++ b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/mapper/BahnTimetableMapper.kt @@ -3,7 +3,6 @@ package de.twomartens.timetable.bahnApi.mapper import de.twomartens.timetable.bahnApi.model.db.BahnStationStop import de.twomartens.timetable.bahnApi.model.db.BahnStopEvent import de.twomartens.timetable.bahnApi.model.db.BahnTimetable -import de.twomartens.timetable.model.common.RouteId import de.twomartens.timetable.model.common.TimetableId import de.twomartens.timetable.model.common.UserId import de.twomartens.timetable.types.HourAtDay @@ -22,12 +21,11 @@ interface BahnTimetableMapper { @Mapping(target = "created", ignore = true) @Mapping(target = "lastModified", ignore = true) fun mapToDB(dto: de.twomartens.timetable.bahnApi.model.dto.BahnTimetable, - userId: UserId, routeId: RouteId, tswTimetableId: TimetableId, + userId: UserId, tswTimetableId: TimetableId, hourAtDay: HourAtDay, clock: Clock): BahnTimetable { return BahnTimetable( userId, - routeId, tswTimetableId, dto.eva, ZonedDateTime.of(hourAtDay.dateTime, clock.zone), diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/model/TaskFactory.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/model/TaskFactory.kt index e6a28b2..09203c4 100644 --- a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/model/TaskFactory.kt +++ b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/model/TaskFactory.kt @@ -1,7 +1,6 @@ package de.twomartens.timetable.bahnApi.model import de.twomartens.timetable.bahnApi.model.db.ScheduledFetchTask -import de.twomartens.timetable.model.common.RouteId import de.twomartens.timetable.model.common.TimetableId import de.twomartens.timetable.model.common.UserId import de.twomartens.timetable.types.HourAtDay @@ -21,12 +20,11 @@ class TaskFactory { countTasks(existingTasks) } - fun createTaskAndUpdateCounter(userId: UserId, routeId: RouteId, - tswTimetableId: TimetableId, eva: Eva, - hourAtDay: HourAtDay): ScheduledFetchTask { + fun createTaskAndUpdateCounter(userId: UserId, tswTimetableId: TimetableId, + eva: Eva, hourAtDay: HourAtDay): ScheduledFetchTask { val fetchDateTime = calculateNextAvailableFetchDateTime(hourAtDay) incrementTaskCountForHourAtDay(hourAtDay) - return ScheduledFetchTask(userId, routeId, tswTimetableId, eva, hourAtDay, fetchDateTime) + return ScheduledFetchTask(userId, tswTimetableId, eva, hourAtDay, fetchDateTime) } private fun calculateNextAvailableFetchDateTime(hourAtDay: HourAtDay): LocalDateTime { diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/BahnTimetable.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/BahnTimetable.kt index 21098e0..a1b5c79 100644 --- a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/BahnTimetable.kt +++ b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/BahnTimetable.kt @@ -1,7 +1,6 @@ package de.twomartens.timetable.bahnApi.model.db import de.twomartens.timetable.bahnApi.model.Eva -import de.twomartens.timetable.model.common.RouteId import de.twomartens.timetable.model.common.TimetableId import de.twomartens.timetable.model.common.UserId import de.twomartens.timetable.types.NonEmptyString @@ -15,11 +14,10 @@ import java.time.Instant import java.time.ZonedDateTime @Document -@CompoundIndex(def = "{'userId': 1, 'routeId': 1, 'tswTimetableId': 1, 'eva': 1, 'hourAtDay': 1}", +@CompoundIndex(def = "{'userId': 1, 'tswTimetableId': 1, 'eva': 1, 'hourAtDay': 1}", unique = true) data class BahnTimetable( var userId: UserId, - var routeId: RouteId, var tswTimetableId: TimetableId, var eva: Eva, var hourAtDay: ZonedDateTime, diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/ScheduledFetchTask.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/ScheduledFetchTask.kt index 702b107..2528943 100644 --- a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/ScheduledFetchTask.kt +++ b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/model/db/ScheduledFetchTask.kt @@ -1,7 +1,6 @@ package de.twomartens.timetable.bahnApi.model.db import de.twomartens.timetable.bahnApi.model.Eva -import de.twomartens.timetable.model.common.RouteId import de.twomartens.timetable.model.common.TimetableId import de.twomartens.timetable.model.common.UserId import de.twomartens.timetable.types.HourAtDay @@ -15,10 +14,9 @@ import java.time.Instant import java.time.LocalDateTime @Document -@CompoundIndex(def = "{'userId': 1, 'routeId': 1, 'tswTimetableId': 1, 'eva': 1, 'fetchedDateTime': 1}") +@CompoundIndex(def = "{'userId': 1, 'tswTimetableId': 1, 'eva': 1, 'fetchedDateTime': 1}") data class ScheduledFetchTask( var userId: UserId, - var routeId: RouteId, var tswTimetableId: TimetableId, var eva: Eva, var fetchedDateTime: HourAtDay, diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/BahnDatabaseService.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/BahnDatabaseService.kt index f62c6e0..318d0bf 100644 --- a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/BahnDatabaseService.kt +++ b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/BahnDatabaseService.kt @@ -6,7 +6,6 @@ import de.twomartens.timetable.bahnApi.model.dto.BahnStation import de.twomartens.timetable.bahnApi.model.dto.BahnTimetable import de.twomartens.timetable.bahnApi.repository.BahnStationRepository import de.twomartens.timetable.bahnApi.repository.BahnTimetableRepository -import de.twomartens.timetable.model.common.RouteId import de.twomartens.timetable.model.common.TimetableId import de.twomartens.timetable.model.common.UserId import de.twomartens.timetable.model.db.Station @@ -93,11 +92,10 @@ open class BahnDatabaseService( .execute() } - fun storeTimetable(timetable: BahnTimetable, userId: UserId, routeId: RouteId, - tswTimetableId: TimetableId, + fun storeTimetable(timetable: BahnTimetable, userId: UserId, tswTimetableId: TimetableId, hourAtDay: HourAtDay) { val matcher = bahnTimetableRepository.getExampleMatcher() - val dbTimetable = bahnTimetableMapper.mapToDB(timetable, userId, routeId, + val dbTimetable = bahnTimetableMapper.mapToDB(timetable, userId, tswTimetableId, hourAtDay, clock) val existingTimetable = bahnTimetableRepository.findOne(Example.of(dbTimetable, matcher)) existingTimetable diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/FetchTaskScheduler.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/FetchTaskScheduler.kt index f88b441..896f009 100644 --- a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/FetchTaskScheduler.kt +++ b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/FetchTaskScheduler.kt @@ -6,7 +6,6 @@ import de.twomartens.timetable.bahnApi.repository.ScheduledFetchTaskRepository import de.twomartens.timetable.bahnApi.tasks.DeleteScheduledTask import de.twomartens.timetable.bahnApi.tasks.FetchTimetableTask import de.twomartens.timetable.bahnApi.tasks.StoreTimetableTask -import de.twomartens.timetable.model.common.RouteId import de.twomartens.timetable.model.common.TimetableId import de.twomartens.timetable.model.common.UserId import de.twomartens.timetable.types.HourAtDay @@ -27,10 +26,9 @@ class FetchTaskScheduler( private val bahnDatabaseService: BahnDatabaseService, private val scheduledFetchTaskRepository: ScheduledFetchTaskRepository ) { - fun scheduleStoreTask(timetable: BahnTimetable, userId: UserId, routeId: RouteId, - tswTimetableId: TimetableId, + fun scheduleStoreTask(timetable: BahnTimetable, userId: UserId, tswTimetableId: TimetableId, hourAtDay: HourAtDay) { - val storeTask = StoreTimetableTask(timetable, userId, routeId, tswTimetableId, + val storeTask = StoreTimetableTask(timetable, userId, tswTimetableId, hourAtDay, bahnDatabaseService) threadPoolTaskExecutor.execute(storeTask) } @@ -76,7 +74,7 @@ class FetchTaskScheduler( scheduledFetchTask: ScheduledFetchTask, zonedExecutionTime: ZonedDateTime ) { - val deleteTask = DeleteScheduledTask(scheduledFetchTaskRepository, scheduledFetchTask) + val deleteTask = DeleteScheduledTask(scheduledFetchTaskRepository, scheduledFetchTask, eventPublisher) threadPoolTaskScheduler.schedule(deleteTask, zonedExecutionTime.toInstant()) } @@ -84,7 +82,7 @@ class FetchTaskScheduler( scheduledFetchTask: ScheduledFetchTask, zonedExecutionTime: ZonedDateTime ) { - val timetableTask = FetchTimetableTask(scheduledFetchTask.userId, scheduledFetchTask.routeId, + val timetableTask = FetchTimetableTask(scheduledFetchTask.userId, scheduledFetchTask.tswTimetableId, scheduledFetchTask.eva, scheduledFetchTask.fetchedDateTime, bahnApiService, this) diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/ScheduledTaskService.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/ScheduledTaskService.kt index b1f3b54..7e9073d 100644 --- a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/ScheduledTaskService.kt +++ b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/service/ScheduledTaskService.kt @@ -83,18 +83,18 @@ class ScheduledTaskService( val eva = Eva.of(stationId) var hourAtDay = HourAtDay.of(Hour.of(23), fetchDates.previousDay) var newTask = taskFactory.createTaskAndUpdateCounter(tswRoute.userId, - tswRoute.routeId, tswTimetableId, eva, hourAtDay) + tswTimetableId, eva, hourAtDay) newTasks.add(newTask) for (hour in 0..23) { hourAtDay = HourAtDay.of(Hour.of(hour), fetchDates.fetchDate) newTask = taskFactory.createTaskAndUpdateCounter(tswRoute.userId, - tswRoute.routeId, tswTimetableId, eva, hourAtDay) + tswTimetableId, eva, hourAtDay) newTasks.add(newTask) } for (hour in 0..3) { hourAtDay = HourAtDay.of(Hour.of(hour), fetchDates.nextDate) newTask = taskFactory.createTaskAndUpdateCounter(tswRoute.userId, - tswRoute.routeId, tswTimetableId, eva, hourAtDay) + tswTimetableId, eva, hourAtDay) newTasks.add(newTask) } } diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/tasks/FetchTimetableTask.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/tasks/FetchTimetableTask.kt index c1988d7..b216c52 100644 --- a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/tasks/FetchTimetableTask.kt +++ b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/tasks/FetchTimetableTask.kt @@ -3,7 +3,6 @@ package de.twomartens.timetable.bahnApi.tasks import de.twomartens.timetable.bahnApi.model.Eva import de.twomartens.timetable.bahnApi.service.BahnApiService import de.twomartens.timetable.bahnApi.service.FetchTaskScheduler -import de.twomartens.timetable.model.common.RouteId import de.twomartens.timetable.model.common.TimetableId import de.twomartens.timetable.model.common.UserId import de.twomartens.timetable.types.HourAtDay @@ -13,7 +12,6 @@ import org.springframework.scheduling.annotation.Async @Async open class FetchTimetableTask( private val userId: UserId, - private val routeId: RouteId, private val tswTimetableId: TimetableId, private val eva: Eva, private val hourAtDay: HourAtDay, @@ -24,7 +22,7 @@ open class FetchTimetableTask( "Fetch timetable: [eva: $eva], [date: ${hourAtDay.date}], [hour: ${hourAtDay.hour}]" } val timetable = bahnApiService.fetchTimetable(eva, hourAtDay) - scheduler.scheduleStoreTask(timetable, userId, routeId, tswTimetableId, hourAtDay) + scheduler.scheduleStoreTask(timetable, userId, tswTimetableId, hourAtDay) } companion object { diff --git a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/tasks/StoreTimetableTask.kt b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/tasks/StoreTimetableTask.kt index a517fd6..05318d2 100644 --- a/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/tasks/StoreTimetableTask.kt +++ b/module-bahnApi/src/main/kotlin/de/twomartens/timetable/bahnApi/tasks/StoreTimetableTask.kt @@ -2,7 +2,6 @@ package de.twomartens.timetable.bahnApi.tasks import de.twomartens.timetable.bahnApi.model.dto.BahnTimetable import de.twomartens.timetable.bahnApi.service.BahnDatabaseService -import de.twomartens.timetable.model.common.RouteId import de.twomartens.timetable.model.common.TimetableId import de.twomartens.timetable.model.common.UserId import de.twomartens.timetable.types.HourAtDay @@ -13,14 +12,13 @@ import org.springframework.scheduling.annotation.Async open class StoreTimetableTask( private val timetable: BahnTimetable, private val userId: UserId, - private val routeId: RouteId, private val tswTimetableId: TimetableId, private val hourAtDay: HourAtDay, private val bahnDatabaseService: BahnDatabaseService ) : Runnable { override fun run() { log.info { "Store timetable: [eva: ${timetable.eva}], [station: ${timetable.station}], [hour: ${hourAtDay}]" } - bahnDatabaseService.storeTimetable(timetable, userId, routeId, tswTimetableId, hourAtDay) + bahnDatabaseService.storeTimetable(timetable, userId, tswTimetableId, hourAtDay) } companion object {