diff --git a/src/main/java/com/dedicatedcode/reitti/service/importer/PromotionJobHandler.java b/src/main/java/com/dedicatedcode/reitti/service/importer/PromotionJobHandler.java index a6014640..2dd91e27 100644 --- a/src/main/java/com/dedicatedcode/reitti/service/importer/PromotionJobHandler.java +++ b/src/main/java/com/dedicatedcode/reitti/service/importer/PromotionJobHandler.java @@ -78,14 +78,22 @@ public void execute(JobExecutionContext context) throws JobExecutionException { metadataRepository.updateProgress(jobId, 2, 3, "Scheduling cleanup job"); if (promote > 0) { - this.userNotificationService.newLocationData(user, data.device, timeRange); - this.jobSchedulingService.enqueueTask(locationDataCleanupTask, - new LocationDataCleanupTask.TaskData(user, data.getDevice(), timeRange.start(), timeRange.end()).withParentJobId(data.getParentJobId()), - JobSchedulingService.Metadata.builder() - .user(user) - .jobType(JobType.LOCATION_DATA_CLEANUP) - .friendlyName("Location Data Cleanup") - .build()); + if (timeRange.equals(TimeRange.empty())) { + log.debug("No timerange found for partitionKey [{}], recalculating", partitionKey); + timeRange = this.stagingService.getWholeTimeRange(partitionKey); + } + if (timeRange.equals(TimeRange.empty())) { + log.warn("Still no timerange found for partitionKey [{}], skipping cleanup", partitionKey); + } else { + this.userNotificationService.newLocationData(user, data.device, timeRange); + this.jobSchedulingService.enqueueTask(locationDataCleanupTask, + new LocationDataCleanupTask.TaskData(user, data.getDevice(), timeRange.start(), timeRange.end()).withParentJobId(data.getParentJobId()), + JobSchedulingService.Metadata.builder() + .user(user) + .jobType(JobType.LOCATION_DATA_CLEANUP) + .friendlyName("Location Data Cleanup") + .build()); + } } else { log.debug("No points to promote, timerange was [{}]", timeRange); } diff --git a/src/main/java/com/dedicatedcode/reitti/service/processing/LocationPointStagingService.java b/src/main/java/com/dedicatedcode/reitti/service/processing/LocationPointStagingService.java index c5d2f1a1..3e4da308 100644 --- a/src/main/java/com/dedicatedcode/reitti/service/processing/LocationPointStagingService.java +++ b/src/main/java/com/dedicatedcode/reitti/service/processing/LocationPointStagingService.java @@ -138,6 +138,20 @@ ON CONFLICT (user_id, device_id, timestamp) DO NOTHING return insertedIds.size(); } + public TimeRange getWholeTimeRange(String partitionKey) { + String sql = "SELECT MIN(timestamp) as start_time, MAX(timestamp) as end_time FROM staging_location_points WHERE partition_key = ?"; + return this.jdbcTemplate.queryForObject(sql, (rs, rowNum) -> { + Timestamp start = rs.getTimestamp("start_time"); + Timestamp end = rs.getTimestamp("end_time"); + + if (start == null || end == null) { + return TimeRange.empty(); + } + + return new TimeRange(start.toInstant(), end.toInstant()); + }, partitionKey); + } + public TimeRange getTimeRange(String partitionKey) { String sql = "SELECT MIN(timestamp) as start_time, MAX(timestamp) as end_time FROM staging_location_points WHERE partition_key = ? AND promoted = FALSE"; return this.jdbcTemplate.queryForObject(sql, (rs, rowNum) -> { @@ -145,7 +159,7 @@ public TimeRange getTimeRange(String partitionKey) { Timestamp end = rs.getTimestamp("end_time"); if (start == null || end == null) { - return null; + return TimeRange.empty(); } return new TimeRange(start.toInstant(), end.toInstant());