implement basic structure for ad hoc suggestions where we send user a notification for better alternatives after user finished trip
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good

- notification logic still missing
- Weather api missing
This commit is contained in:
2026-01-10 17:09:44 +01:00
parent ea3c941b84
commit 1f439338da
8 changed files with 129 additions and 12 deletions
@@ -41,7 +41,7 @@ public class RoutingController {
routingAlternativeRequestDTO.setRoutingPreferencesDTO(preferences);
}
var routeDTOs = routingService.determineAlternativeRoutes(routingAlternativeRequestDTO);
var routeDTOs = routingService.determineAlternativeRoutesOnUserRequest(routingAlternativeRequestDTO);
return ResponseEntity.ok(routeDTOs);
} catch (WebClientRequestException e) {
// nominatim container is not up yet, we return mock
@@ -0,0 +1,32 @@
package SuMoC.backend.routing.config;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* These are the routing properties for alternatives that are calculated as a resonse to an uploaded trip.
* We separate these from properties for OnCall (Explicitly Requested by user) routing properties since
* the assumption is that "unasked for" alternatives have less tolerance for additional travel time etc.
*/
@ConfigurationProperties(prefix = "routing.adhoc")
@Getter
@Setter
public class AdHocRoutingProperties implements RoutingProperties {
/**
* Multiplier of how much more travel time is tolerated for alternative routing.
*/
private double acceptableAdditionalMultiplierRegular = 1.10;
/**
* Multiplier of accepted additional travel time for users that wish more physical activity.
*/
private double acceptableAdditionalMultiplierPrefersPhysical = 1.3;
/**
* Minimum of accepted additional travel time for regular users which is always accepted.
*/
private Integer minimumAcceptedAdditionalSecondsRegular = 5 * 60;
/**
* Minimum of accepted additional travel time for users that wish more physical activity.
*/
private Integer minimumAcceptedAdditionalSecondsPrefersPhysicalActivity = 5 * 60;
}
@@ -4,6 +4,9 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(RoutingProperties.class)
@EnableConfigurationProperties({
OnCallRoutingProperties.class,
AdHocRoutingProperties.class
})
public class RoutingConfig {
}
@@ -3,13 +3,15 @@ package SuMoC.backend.routing.dtos;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import lombok.*;
import java.time.Instant;
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Schema(description = "This DTO represents a request for alternative routes between an origin and a destination.")
public class RoutingAlternativeRequestDTO {
@NotNull
@@ -0,0 +1,18 @@
package SuMoC.backend.suggestion.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "adhocsuggestion")
public class AdHocSuggestionProperties {
/**
* Maximum time after which an uploaded trip is considered recent enough to send
* an alternative routing suggestion for if available.
*/
private Integer recentTripThresholdSeconds = 60 * 60;
}
@@ -0,0 +1,57 @@
package SuMoC.backend.suggestion.services;
import SuMoC.backend.routing.dtos.RoutingAlternativeRequestDTO;
import SuMoC.backend.routing.dtos.RoutingPreferencesDTO;
import SuMoC.backend.routing.services.RoutingService;
import SuMoC.backend.suggestion.config.AdHocSuggestionProperties;
import SuMoC.backend.trip.models.Trip;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.time.Instant;
@Service
@RequiredArgsConstructor
public class AdHocSuggestionService {
private final RoutingService routingService;
private final AdHocSuggestionProperties adHocSuggestionProperties;
/**
* Receives a trip a user has just uploaded, if it is a recent trip and there is a more sustainable alternative,
* schedules a notification to push to the user.
*/
@Async
public void checkNewTripForNotifiedAlternativeSuggestion(Trip newTrip) {
if (!isRecentTrip(newTrip)) return;
var viableAlternatives = routingService.determineAlternativeRoutesAdHoc(alternativeRequestFromTrip(newTrip));
// check if there are viable alternative routings
// if yes, generate notification
// TODO notification generator
// schedule send of notification
// TODO sending of notification
}
private boolean isRecentTrip(Trip trip) {
long secondsSinceTripCompletion = Instant.now().getEpochSecond() - trip.getEndTime().getEpochSecond();
return secondsSinceTripCompletion < adHocSuggestionProperties.getRecentTripThresholdSeconds();
}
private RoutingAlternativeRequestDTO alternativeRequestFromTrip(Trip trip) {
// TODO for now we assume the preferences here, it would be better if we store these per user
var preferences = new RoutingPreferencesDTO();
return RoutingAlternativeRequestDTO.builder()
.originLat(trip.getStartLatitude())
.originLon(trip.getStartLongitude())
.destinationLat(trip.getEndLatitude())
.destinationLon(trip.getEndLongitude())
.arrivalTime(trip.getEndTime())
.departureTime(trip.getStartTime())
.routingPreferencesDTO(preferences).build();
}
}
@@ -1,13 +1,15 @@
package SuMoC.backend.trip;
import SuMoC.backend.suggestion.services.AdHocSuggestionService;
import SuMoC.backend.trip.dtos.NewTripDTO;
import SuMoC.backend.trip.dtos.TripDTO;
import SuMoC.backend.trip.models.Trip;
import SuMoC.backend.user.models.User;
import SuMoC.backend.user.repository.UserRepository;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
@@ -18,15 +20,14 @@ import java.util.List;
@RestController
@RequestMapping("/trip")
@RequiredArgsConstructor
public class TripController {
private final TripService tripService;
private final UserRepository userRepository;
@Autowired
public TripController(TripService tripService, UserRepository userRepository) {
this.tripService = tripService;
this.userRepository = userRepository;
}
private final AdHocSuggestionService adHocSuggestionService;
// TODO extend security context with user id to avoid unnecessary db query
private User getAuthenticatedUser() {
@@ -38,8 +39,11 @@ public class TripController {
@PostMapping("/uploadTrip")
public ResponseEntity<Void> uploadTrip(@Valid @RequestBody NewTripDTO newTripDTO) {
User user = getAuthenticatedUser();
Trip savedTrip = tripService.addTrip(user, newTripDTO);
// let the ad hoc service suggest an alternative more ecofriendly route if appropriate
adHocSuggestionService.checkNewTripForNotifiedAlternativeSuggestion(savedTrip);
tripService.addTrip(user, newTripDTO);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
@@ -43,11 +43,12 @@ public class TripService {
tripRepository.save(trip);
}
public void addTrip(User user, NewTripDTO newTripDTO) {
public Trip addTrip(User user, NewTripDTO newTripDTO) {
Trip trip = tripMapper.toTrip(newTripDTO);
trip.setUser(user);
trip.getTripSegments().forEach(tripSegment -> tripSegment.setTrip(trip));
tripRepository.save(trip);
return trip;
}
public List<TripDTO> getAllTripsOfUser(User user) {