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 55cd41d..208f073 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 @@ -1,6 +1,8 @@ package de.twomartens.timetable.bahnApi.mapper 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.NonEmptyString import org.mapstruct.* @@ -15,8 +17,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, hourAtDay: HourAtDay): BahnTimetable { return BahnTimetable( + userId, + routeId, dto.eva, hourAtDay, NonEmptyString(dto.station), 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 9fbb416..edc8bb2 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,6 +1,8 @@ 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.UserId import de.twomartens.timetable.types.HourAtDay import org.springframework.stereotype.Component import java.time.LocalDateTime @@ -18,10 +20,11 @@ class TaskFactory { countTasks(existingTasks) } - fun createTaskAndUpdateCounter(eva: Eva, hourAtDay: HourAtDay): ScheduledFetchTask { + fun createTaskAndUpdateCounter(userId: UserId, routeId: RouteId, eva: Eva, + hourAtDay: HourAtDay): ScheduledFetchTask { val fetchDateTime = calculateNextAvailableFetchDateTime(hourAtDay) incrementTaskCountForHourAtDay(hourAtDay) - return ScheduledFetchTask(eva, hourAtDay, fetchDateTime) + return ScheduledFetchTask(userId, routeId, 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 7b8d97d..6ca7064 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 @@ -2,6 +2,8 @@ package de.twomartens.timetable.bahnApi.model.db import de.twomartens.timetable.bahnApi.model.Eva 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.NonEmptyString import org.bson.types.ObjectId @@ -13,8 +15,10 @@ import org.springframework.data.mongodb.core.mapping.Document import java.time.Instant @Document -@CompoundIndex(def = "{'eva': 1, 'hourAtDay': 1}", unique = true) +@CompoundIndex(def = "{'userId': 1, 'routeId': 1, 'eva': 1, 'hourAtDay': 1}", unique = true) data class BahnTimetable( + var userId: UserId, + var routeId: RouteId, var eva: Eva, var hourAtDay: HourAtDay, var station: NonEmptyString, 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 6c39bc3..fc0ea50 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,6 +1,8 @@ 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.UserId import de.twomartens.timetable.types.HourAtDay import org.bson.types.ObjectId import org.springframework.data.annotation.CreatedDate @@ -12,8 +14,10 @@ import java.time.Instant import java.time.LocalDateTime @Document -@CompoundIndex(def = "{'eva': 1, 'fetchedDateTime': 1}", unique = true) +@CompoundIndex(def = "{'userId': 1, 'routeId': 1, 'eva': 1, 'fetchedDateTime': 1}", unique = true) data class ScheduledFetchTask( + var userId: UserId, + var routeId: RouteId, var eva: Eva, var fetchedDateTime: HourAtDay, var scheduledExecutionDateTime: LocalDateTime 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 c87bd89..e518a8f 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,6 +6,8 @@ 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.UserId import de.twomartens.timetable.model.db.Station import de.twomartens.timetable.model.repository.StationRepository import de.twomartens.timetable.types.HourAtDay @@ -87,8 +89,9 @@ open class BahnDatabaseService( .execute() } - fun storeTimetable(timetable: BahnTimetable, hourAtDay: HourAtDay) { - bahnTimetableRepository.save(bahnTimetableMapper.mapToDB(timetable, hourAtDay)) + fun storeTimetable(timetable: BahnTimetable, userId: UserId, routeId: RouteId, + hourAtDay: HourAtDay) { + bahnTimetableRepository.save(bahnTimetableMapper.mapToDB(timetable, userId, routeId, hourAtDay)) } companion object { 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 660afba..19fef80 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,6 +6,8 @@ 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.UserId import de.twomartens.timetable.types.HourAtDay import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler @@ -24,8 +26,9 @@ class FetchTaskScheduler( private val bahnDatabaseService: BahnDatabaseService, private val scheduledFetchTaskRepository: ScheduledFetchTaskRepository ) { - fun scheduleStoreTask(timetable: BahnTimetable, hourAtDay: HourAtDay) { - val storeTask = StoreTimetableTask(timetable, hourAtDay, bahnDatabaseService) + fun scheduleStoreTask(timetable: BahnTimetable, userId: UserId, routeId: RouteId, + hourAtDay: HourAtDay) { + val storeTask = StoreTimetableTask(timetable, userId, routeId, hourAtDay, bahnDatabaseService) threadPoolTaskExecutor.execute(storeTask) } @@ -78,8 +81,8 @@ class FetchTaskScheduler( scheduledFetchTask: ScheduledFetchTask, zonedExecutionTime: ZonedDateTime ) { - val timetableTask = FetchTimetableTask(scheduledFetchTask.eva, - scheduledFetchTask.fetchedDateTime, + val timetableTask = FetchTimetableTask(scheduledFetchTask.userId, scheduledFetchTask.routeId, + scheduledFetchTask.eva, scheduledFetchTask.fetchedDateTime, bahnApiService, this) threadPoolTaskScheduler.schedule(timetableTask, zonedExecutionTime.toInstant()) } 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 c69107b..0ea4678 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 @@ -88,16 +88,19 @@ class ScheduledTaskService( val stationId = it.id val eva = Eva.of(stationId) 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) for (hour in 0..23) { 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) } for (hour in 0..3) { 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) } } 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 dcb8d96..a0f6228 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,12 +3,16 @@ 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.UserId import de.twomartens.timetable.types.HourAtDay import mu.KotlinLogging import org.springframework.scheduling.annotation.Async @Async open class FetchTimetableTask( + private val userId: UserId, + private val routeId: RouteId, private val eva: Eva, private val hourAtDay: HourAtDay, private val bahnApiService: BahnApiService, @@ -18,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, hourAtDay) + scheduler.scheduleStoreTask(timetable, userId, routeId, 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 1c1e800..d4f653e 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,6 +2,8 @@ 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.UserId import de.twomartens.timetable.types.HourAtDay import mu.KotlinLogging import org.springframework.scheduling.annotation.Async @@ -9,12 +11,14 @@ import org.springframework.scheduling.annotation.Async @Async open class StoreTimetableTask( private val timetable: BahnTimetable, + private val userId: UserId, + private val routeId: RouteId, 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, hourAtDay) + bahnDatabaseService.storeTimetable(timetable, userId, routeId, hourAtDay) } companion object {