mirror of
https://github.com/2martens/tsw-timetable.git
synced 2026-10-17 07:57:55 +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
|
||||
|
||||
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),
|
||||
|
||||
@@ -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 {
|
||||
|
||||
+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.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,
|
||||
|
||||
+5
-1
@@ -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
|
||||
|
||||
+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.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 {
|
||||
|
||||
+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.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())
|
||||
}
|
||||
|
||||
+6
-3
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
+5
-1
@@ -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 {
|
||||
|
||||
+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.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 {
|
||||
|
||||
Reference in New Issue
Block a user