implement trend detection logic
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good

- for now aimed to detect weekly trends in car distances, easily extendable to all other attributes of the statsm model
- including test
This commit is contained in:
2026-01-07 15:28:35 +01:00
parent ec0615f6df
commit 6e458edd5c
13 changed files with 376 additions and 23 deletions
@@ -0,0 +1,36 @@
package SuMoC.backend.trenddetection;
import SuMoC.backend.trenddetection.enums.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
@AllArgsConstructor
public class TrendDetection {
private Long userId;
private TrendTimespan timespan;
private TrendType type;
private StatAttribute attribute;
private DeltaType deltaType;
private double changeValue;
public TrendConnotation getTrendConnotation() {
if (this.type == TrendType.INCREASING) {
return switch (attribute) {
case CAR_DISTANCE -> TrendConnotation.NEGATIVE;
default -> TrendConnotation.NEUTRAL;
};
} else if (type == TrendType.DECREASING) {
return switch (attribute) {
case CAR_DISTANCE -> TrendConnotation.POSITIVE;
default -> TrendConnotation.NEUTRAL;
};
} else {
return TrendConnotation.NEUTRAL;
}
}
}
@@ -3,6 +3,14 @@ package SuMoC.backend.trenddetection;
import SuMoC.backend.trenddetection.models.WeeklyMobilityStats;
import SuMoC.backend.trenddetection.models.WeeklyMobilityStatsId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface WeeklyMobilityStatsRepository extends JpaRepository<WeeklyMobilityStats, WeeklyMobilityStatsId> {
@Query("SELECT wms FROM WeeklyMobilityStats wms JOIN FETCH wms.user WHERE wms.year = :year AND wms.weekNumber = :weekNumber")
List<WeeklyMobilityStats> findAllByYearAndWeekNumberWithUser(@Param("year") Integer year, @Param("weekNumber") Integer weekNumber);
}
@@ -0,0 +1,21 @@
package SuMoC.backend.trenddetection.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "trenddetection")
public class TrendDetectionProperties {
/**
* Minimum percentage change to detect a positive trend (e.g., 25.0 for 25%)
*/
private double positiveThreshold = 25.0;
/**
* Minimum percentage change to detect a negative trend (e.g., -25.0 for -25%)
*/
private double negativeThreshold = -25.0;
}
@@ -0,0 +1,6 @@
package SuMoC.backend.trenddetection.enums;
public enum DeltaType {
ABSOLUTE,
PERCENTAGE
}
@@ -0,0 +1,8 @@
package SuMoC.backend.trenddetection.enums;
/**
* Represents statistical attributes a detected trend can pertain to.
*/
public enum StatAttribute {
CAR_DISTANCE,
}
@@ -0,0 +1,7 @@
package SuMoC.backend.trenddetection.enums;
public enum TrendConnotation {
POSITIVE,
NEGATIVE,
NEUTRAL
}
@@ -0,0 +1,5 @@
package SuMoC.backend.trenddetection.enums;
public enum TrendTimespan {
WEEKLY,
}
@@ -0,0 +1,7 @@
package SuMoC.backend.trenddetection.enums;
public enum TrendType {
INCREASING,
DECREASING,
STABLE
}
@@ -1,5 +1,6 @@
package SuMoC.backend.trenddetection.models;
import SuMoC.backend.trenddetection.enums.StatAttribute;
import SuMoC.backend.user.models.User;
import jakarta.persistence.*;
import lombok.*;
@@ -77,4 +78,10 @@ public class WeeklyMobilityStats {
@Column(nullable = false)
private Long sumMetersTaxi = 0L;
public Long getValueFor(StatAttribute attribute) {
return switch (attribute) {
case CAR_DISTANCE -> sumMetersCar;
};
}
}
@@ -0,0 +1,84 @@
package SuMoC.backend.trenddetection.services;
import SuMoC.backend.trenddetection.TrendDetection;
import SuMoC.backend.trenddetection.WeeklyMobilityStatsRepository;
import SuMoC.backend.trenddetection.config.TrendDetectionProperties;
import SuMoC.backend.trenddetection.enums.DeltaType;
import SuMoC.backend.trenddetection.enums.StatAttribute;
import SuMoC.backend.trenddetection.enums.TrendTimespan;
import SuMoC.backend.trenddetection.enums.TrendType;
import SuMoC.backend.trenddetection.models.WeeklyMobilityStats;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
public class TrendDetectorService {
private final WeeklyMobilityStatsRepository weeklyMobilityStatsRepository;
private final TrendDetectionProperties trendDetectionProperties;
public List<TrendDetection> detectWeeklyTrends(Integer year, Integer weekNumber, StatAttribute attribute) {
// Fetch stats for current and previous week
List<WeeklyMobilityStats> previousWeekStatsList;
if (weekNumber > 1) {
previousWeekStatsList = weeklyMobilityStatsRepository.findAllByYearAndWeekNumberWithUser(year, weekNumber - 1);
} else {
// we are forgetting about week 53 here, but it's rare enough to ignore for now
previousWeekStatsList = weeklyMobilityStatsRepository.findAllByYearAndWeekNumberWithUser(year - 1, 52);
}
List<WeeklyMobilityStats> currentWeekStatsList = weeklyMobilityStatsRepository.findAllByYearAndWeekNumberWithUser(year, weekNumber);
// Convert to maps for easy userId-based lookup
Map<Long, WeeklyMobilityStats> previousWeekStats = previousWeekStatsList.stream()
.collect(Collectors.toMap(stats -> stats.getUser().getId(), stats -> stats));
Map<Long, WeeklyMobilityStats> currentWeekStats = currentWeekStatsList.stream()
.collect(Collectors.toMap(stats -> stats.getUser().getId(), stats -> stats));
// Detect trends for provided attribute
List<TrendDetection> detectedTrends = new ArrayList<>();
for (Map.Entry<Long, WeeklyMobilityStats> entry : currentWeekStats.entrySet()) {
Long userId = entry.getKey();
WeeklyMobilityStats currentStats = entry.getValue();
WeeklyMobilityStats previousStats = previousWeekStats.get(userId);
if (previousStats != null) {
double currentValue = currentStats.getValueFor(attribute);
double previousValue = previousStats.getValueFor(attribute);
if (previousValue != 0) {
double percentageChange = ((currentValue - previousValue) / previousValue) * 100.0;
if (percentageChange >= trendDetectionProperties.getPositiveThreshold()) {
detectedTrends.add(new TrendDetection(
userId,
TrendTimespan.WEEKLY,
TrendType.INCREASING,
StatAttribute.CAR_DISTANCE,
DeltaType.PERCENTAGE,
percentageChange
));
} else if (percentageChange <= trendDetectionProperties.getNegativeThreshold()) {
detectedTrends.add(new TrendDetection(
userId,
TrendTimespan.WEEKLY,
TrendType.DECREASING,
StatAttribute.CAR_DISTANCE,
DeltaType.PERCENTAGE,
percentageChange
));
}
}
}
}
return detectedTrends;
}
}