|
| 1 | +package org.github.tess1o.geopulse.streaming.engine; |
| 2 | + |
| 3 | +import jakarta.enterprise.context.ApplicationScoped; |
| 4 | +import jakarta.inject.Inject; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import org.github.tess1o.geopulse.streaming.config.TimelineConfig; |
| 7 | +import org.github.tess1o.geopulse.streaming.model.domain.GPSPoint; |
| 8 | +import org.github.tess1o.geopulse.streaming.model.domain.ProcessorMode; |
| 9 | +import org.github.tess1o.geopulse.streaming.model.domain.UserState; |
| 10 | + |
| 11 | +import java.time.Duration; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +/** |
| 16 | + * Evaluates whether a data gap can be treated as stay continuity instead of creating a DataGap. |
| 17 | + * |
| 18 | + * This service intentionally performs no event finalization and does not mutate the caller's state. |
| 19 | + * It returns an internal plan that the engine can apply. |
| 20 | + */ |
| 21 | +@Slf4j |
| 22 | +@ApplicationScoped |
| 23 | +class GapStayInferenceService { |
| 24 | + |
| 25 | + private static final Duration MAX_IN_TRIP_LOCAL_EXCURSION_DURATION = Duration.ofMinutes(30); |
| 26 | + private static final double IN_TRIP_LOCAL_EXCURSION_RADIUS_MULTIPLIER = 2; |
| 27 | + |
| 28 | + @Inject |
| 29 | + TripStopHeuristicsService tripStopHeuristicsService; |
| 30 | + |
| 31 | + GapStayInferencePlan tryInfer(GPSPoint currentPoint, UserState userState, |
| 32 | + TimelineConfig config, Duration gapDuration) { |
| 33 | + Boolean enabled = config.getGapStayInferenceEnabled(); |
| 34 | + if (enabled == null || !enabled) { |
| 35 | + log.debug("Gap stay inference is disabled (enabled={})", enabled); |
| 36 | + return GapStayInferencePlan.none(); |
| 37 | + } |
| 38 | + |
| 39 | + if (!userState.hasActivePoints()) { |
| 40 | + log.debug("No active points for gap stay inference comparison"); |
| 41 | + return GapStayInferencePlan.none(); |
| 42 | + } |
| 43 | + |
| 44 | + Integer maxGapHours = config.getGapStayInferenceMaxGapHours(); |
| 45 | + if (maxGapHours != null && maxGapHours > 0) { |
| 46 | + long gapHours = gapDuration.toHours(); |
| 47 | + if (gapHours > maxGapHours) { |
| 48 | + log.debug("Gap duration {}h exceeds max allowed {}h for stay inference", |
| 49 | + gapHours, maxGapHours); |
| 50 | + return GapStayInferencePlan.none(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + ProcessorMode mode = userState.getCurrentMode(); |
| 55 | + if (mode == ProcessorMode.UNKNOWN) { |
| 56 | + log.debug("Gap stay inference not applicable for mode: {}", mode); |
| 57 | + return GapStayInferencePlan.none(); |
| 58 | + } |
| 59 | + |
| 60 | + int stayRadiusMeters = tripStopHeuristicsService.getStayRadiusMeters(config); |
| 61 | + |
| 62 | + if (mode == ProcessorMode.IN_TRIP) { |
| 63 | + GapStayInferencePlan localTripPlan = |
| 64 | + tryInferForShortLocalTrip(currentPoint, userState, stayRadiusMeters, gapDuration); |
| 65 | + if (localTripPlan.isInferred()) { |
| 66 | + return localTripPlan; |
| 67 | + } |
| 68 | + return tryInferFromTripTailArrival(currentPoint, userState, config, stayRadiusMeters, gapDuration); |
| 69 | + } |
| 70 | + |
| 71 | + return tryInferForStayModes(currentPoint, userState, stayRadiusMeters, gapDuration, mode); |
| 72 | + } |
| 73 | + |
| 74 | + private GapStayInferencePlan tryInferForStayModes(GPSPoint currentPoint, UserState userState, |
| 75 | + int stayRadiusMeters, Duration gapDuration, |
| 76 | + ProcessorMode mode) { |
| 77 | + GPSPoint centroid = userState.calculateCentroid(); |
| 78 | + if (centroid == null) { |
| 79 | + log.debug("Could not calculate centroid for gap stay inference"); |
| 80 | + return GapStayInferencePlan.none(); |
| 81 | + } |
| 82 | + |
| 83 | + double distance = centroid.distanceTo(currentPoint); |
| 84 | + if (distance > stayRadiusMeters) { |
| 85 | + log.debug("Distance {}m from centroid exceeds stay radius {}m - creating gap instead", |
| 86 | + String.format("%.1f", distance), stayRadiusMeters); |
| 87 | + return GapStayInferencePlan.none(); |
| 88 | + } |
| 89 | + |
| 90 | + log.info("Gap stay inference conditions met: mode={}, gap={}h, distance={}m (radius={}m)", |
| 91 | + mode, gapDuration.toHours(), String.format("%.1f", distance), stayRadiusMeters); |
| 92 | + return GapStayInferencePlan.continueExistingStay(); |
| 93 | + } |
| 94 | + |
| 95 | + private GapStayInferencePlan tryInferForShortLocalTrip(GPSPoint currentPoint, UserState userState, |
| 96 | + int stayRadiusMeters, Duration gapDuration) { |
| 97 | + List<GPSPoint> activeTripPoints = userState.copyActivePoints(); |
| 98 | + if (activeTripPoints.size() < 2) { |
| 99 | + log.debug("Gap stay inference not applicable for IN_TRIP with fewer than 2 active points"); |
| 100 | + return GapStayInferencePlan.none(); |
| 101 | + } |
| 102 | + |
| 103 | + GPSPoint firstTripPoint = activeTripPoints.get(0); |
| 104 | + GPSPoint lastTripPoint = activeTripPoints.get(activeTripPoints.size() - 1); |
| 105 | + if (firstTripPoint.getTimestamp() == null || lastTripPoint.getTimestamp() == null) { |
| 106 | + log.debug("Gap stay inference not applicable for IN_TRIP with missing timestamps"); |
| 107 | + return GapStayInferencePlan.none(); |
| 108 | + } |
| 109 | + |
| 110 | + Duration pendingTripDuration = Duration.between(firstTripPoint.getTimestamp(), lastTripPoint.getTimestamp()); |
| 111 | + if (pendingTripDuration.compareTo(MAX_IN_TRIP_LOCAL_EXCURSION_DURATION) > 0) { |
| 112 | + log.debug("Pending IN_TRIP duration {} exceeds local excursion limit {} for gap stay inference", |
| 113 | + pendingTripDuration, MAX_IN_TRIP_LOCAL_EXCURSION_DURATION); |
| 114 | + return GapStayInferencePlan.none(); |
| 115 | + } |
| 116 | + |
| 117 | + double resumeDistance = lastTripPoint.distanceTo(currentPoint); |
| 118 | + if (resumeDistance > stayRadiusMeters) { |
| 119 | + log.debug("IN_TRIP resume distance {}m exceeds stay radius {}m - creating gap instead", |
| 120 | + String.format("%.1f", resumeDistance), stayRadiusMeters); |
| 121 | + return GapStayInferencePlan.none(); |
| 122 | + } |
| 123 | + |
| 124 | + double maxDistanceFromTripEnd = 0.0; |
| 125 | + for (GPSPoint tripPoint : activeTripPoints) { |
| 126 | + maxDistanceFromTripEnd = Math.max(maxDistanceFromTripEnd, tripPoint.distanceTo(lastTripPoint)); |
| 127 | + } |
| 128 | + |
| 129 | + double localExcursionLimitMeters = stayRadiusMeters * IN_TRIP_LOCAL_EXCURSION_RADIUS_MULTIPLIER; |
| 130 | + if (maxDistanceFromTripEnd > localExcursionLimitMeters) { |
| 131 | + log.debug("Pending IN_TRIP spread {}m exceeds local excursion limit {}m (radius={}m x {})", |
| 132 | + String.format("%.1f", maxDistanceFromTripEnd), |
| 133 | + String.format("%.1f", localExcursionLimitMeters), |
| 134 | + stayRadiusMeters, |
| 135 | + IN_TRIP_LOCAL_EXCURSION_RADIUS_MULTIPLIER); |
| 136 | + return GapStayInferencePlan.none(); |
| 137 | + } |
| 138 | + |
| 139 | + List<GPSPoint> localPoints = collectPointsWithinRadius(activeTripPoints, lastTripPoint, stayRadiusMeters); |
| 140 | + |
| 141 | + log.info("Gap stay inference conditions met for short local IN_TRIP: gap={}h, pendingTrip={}, " + |
| 142 | + "resumeDistance={}m, spread={}m (radius={}m)", |
| 143 | + gapDuration.toHours(), |
| 144 | + pendingTripDuration, |
| 145 | + String.format("%.1f", resumeDistance), |
| 146 | + String.format("%.1f", maxDistanceFromTripEnd), |
| 147 | + stayRadiusMeters); |
| 148 | + return GapStayInferencePlan.replaceWithConfirmedStay(localPoints); |
| 149 | + } |
| 150 | + |
| 151 | + private GapStayInferencePlan tryInferFromTripTailArrival(GPSPoint currentPoint, UserState userState, |
| 152 | + TimelineConfig config, int stayRadiusMeters, |
| 153 | + Duration gapDuration) { |
| 154 | + List<GPSPoint> activeTripPoints = userState.copyActivePoints(); |
| 155 | + TripStopHeuristicsService.TailArrivalClusterMatch tailMatch = |
| 156 | + tripStopHeuristicsService.findGapTailArrivalClusterMatch(activeTripPoints, currentPoint, config); |
| 157 | + if (!tailMatch.isMatched()) { |
| 158 | + return GapStayInferencePlan.none(); |
| 159 | + } |
| 160 | + int stopClusterStartIndex = tailMatch.getStartIndex(); |
| 161 | + |
| 162 | + List<GPSPoint> tripPoints = new ArrayList<>(activeTripPoints.subList(0, stopClusterStartIndex)); |
| 163 | + List<GPSPoint> stoppedTailPoints = new ArrayList<>(activeTripPoints.subList(stopClusterStartIndex, activeTripPoints.size())); |
| 164 | + |
| 165 | + log.info("Gap stay inference conditions met for IN_TRIP tail arrival: gap={}h, tailPoints={}, tailDuration={}, " + |
| 166 | + "resumeDistance={}m (radius={}m), finalizedTripPoints={}", |
| 167 | + gapDuration.toHours(), |
| 168 | + stoppedTailPoints.size(), |
| 169 | + tailMatch.getTailDuration(), |
| 170 | + String.format("%.1f", tailMatch.getResumeDistanceMeters()), |
| 171 | + tailMatch.getStayRadiusMeters(), |
| 172 | + tripPoints.size()); |
| 173 | + |
| 174 | + return GapStayInferencePlan.finalizeTripAndReplaceWithConfirmedStay(tripPoints, stoppedTailPoints); |
| 175 | + } |
| 176 | + |
| 177 | + private List<GPSPoint> collectPointsWithinRadius(List<GPSPoint> points, GPSPoint anchorPoint, int radiusMeters) { |
| 178 | + List<GPSPoint> localPoints = new ArrayList<>(); |
| 179 | + for (GPSPoint point : points) { |
| 180 | + if (point.distanceTo(anchorPoint) <= radiusMeters) { |
| 181 | + localPoints.add(point); |
| 182 | + } |
| 183 | + } |
| 184 | + if (localPoints.isEmpty()) { |
| 185 | + localPoints.add(anchorPoint); |
| 186 | + } |
| 187 | + return localPoints; |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments