From 304a40ae5a75d414258184ae65297bf84d890374 Mon Sep 17 00:00:00 2001 From: kazhamiakin Date: Mon, 7 Aug 2023 13:09:42 +0200 Subject: [PATCH] Change in the validation: validity of legs --- .../pgazienda/dto/TrackDTO.java | 10 +++++- .../service/TrackingDataService.java | 32 +++++++++++++++++-- .../pgazienda/util/TrackUtils.java | 9 ++++-- .../web/rest/AdminResourceITest.java | 3 ++ 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/backend/src/main/java/it/smartcommunitylab/pgazienda/dto/TrackDTO.java b/backend/src/main/java/it/smartcommunitylab/pgazienda/dto/TrackDTO.java index 96dc8eb..bb2bfdc 100644 --- a/backend/src/main/java/it/smartcommunitylab/pgazienda/dto/TrackDTO.java +++ b/backend/src/main/java/it/smartcommunitylab/pgazienda/dto/TrackDTO.java @@ -61,6 +61,7 @@ public static class TrackLegDTO { private Double distance; private long duration; private double co2; + private Boolean valid; private List points; @@ -100,7 +101,14 @@ public double getCo2() { public void setCo2(double co2) { this.co2 = co2; } - + + public Boolean getValid() { + return valid; + } + public void setValid(Boolean valid) { + this.valid = valid; + } + } diff --git a/backend/src/main/java/it/smartcommunitylab/pgazienda/service/TrackingDataService.java b/backend/src/main/java/it/smartcommunitylab/pgazienda/service/TrackingDataService.java index abe098e..6bf3b91 100644 --- a/backend/src/main/java/it/smartcommunitylab/pgazienda/service/TrackingDataService.java +++ b/backend/src/main/java/it/smartcommunitylab/pgazienda/service/TrackingDataService.java @@ -155,14 +155,40 @@ public TrackValidityDTO validate(String campaignId, String playerId, TrackDTO tr .map(l -> new Circle(new double[] {l.getLatitude(), l.getLongitude()}, l.getRadius())) .collect(Collectors.toList()); - // legs with matching means - List matchingLegs = track.getLegs().stream().filter(leg -> campaign.getMeans().indexOf(leg.getMean()) >= 0).collect(Collectors.toList()); + // index of matching leg (first or last of the trip) + int matchingLegIndex = -1; // no locations, no legs with appropriate means, trip does not match any location if (locations.size() == 0) { return TrackValidityDTO.errLocations(); - } else if (matchingLegs.size() == 0 || !TrackUtils.matchLocations(track, locations)) { + } else if ((matchingLegIndex = TrackUtils.matchLocations(track, locations)) < 0) { return TrackValidityDTO.errMatches(); } else { + List validTrack = new LinkedList<>(); + // limit to valid legs towards the location + if (matchingLegIndex == 0) { + for (int i = 0; i < track.getLegs().size(); i++) { + TrackLegDTO leg = track.getLegs().get(i); + if (!Boolean.TRUE.equals(leg.getValid())) break; + validTrack.add(leg); + } + } else { + for (int i = track.getLegs().size() - 1; i >= 0; i--) { + TrackLegDTO leg = track.getLegs().get(i); + if (!Boolean.TRUE.equals(leg.getValid())) break; + validTrack.add(leg); + } + Collections.reverse(validTrack); + } + if (validTrack.size() == 0) { + return TrackValidityDTO.errMatches(); + } + + // legs with matching means + List matchingLegs = validTrack.stream().filter(leg -> campaign.getMeans().indexOf(leg.getMean()) >= 0).collect(Collectors.toList()); + if (matchingLegs.size() == 0) { + return TrackValidityDTO.errMatches(); + } + LocalDate date = toLocalDate(track.getStartTime()); // stat of current date DayStat stat = dayStatRepo.findOneByPlayerIdAndCampaignAndCompanyAndDate(playerId, campaign.getId(), company.getId(), date.toString()); diff --git a/backend/src/main/java/it/smartcommunitylab/pgazienda/util/TrackUtils.java b/backend/src/main/java/it/smartcommunitylab/pgazienda/util/TrackUtils.java index a3fef6d..df129d0 100644 --- a/backend/src/main/java/it/smartcommunitylab/pgazienda/util/TrackUtils.java +++ b/backend/src/main/java/it/smartcommunitylab/pgazienda/util/TrackUtils.java @@ -43,7 +43,7 @@ public static boolean matchLocations(TrackLegDTO ti, List areas) { return matchPoints(areas, arr); } - public static boolean matchLocations(TrackDTO track, List areas) { + public static int matchLocations(TrackDTO track, List areas) { TrackLegDTO startLeg = track.getLegs().get(0); startLeg.getPoints().sort((a,b) -> { return a.getRecorded_at().compareTo(b.getRecorded_at()); @@ -56,9 +56,14 @@ public static boolean matchLocations(TrackDTO track, List areas) { List points = new LinkedList<>(); if (startLeg.getPoints().size() > 5) points.addAll(startLeg.getPoints().subList(0, 5)); else points.addAll(startLeg.getPoints()); + if (matchPoints(areas, points.toArray())) return 0; + + points = new LinkedList<>(); if (endLeg.getPoints().size() > 5) points.addAll(endLeg.getPoints().subList(endLeg.getPoints().size() - 5, endLeg.getPoints().size())); else points.addAll(endLeg.getPoints()); - return matchPoints(areas, points.toArray()); + if (matchPoints(areas, points.toArray())) return track.getLegs().size() - 1; + + return -1; } private static boolean matchPoints(List areas, Object[] arr) { diff --git a/backend/src/test/java/it/smartcommunitylab/pgazienda/web/rest/AdminResourceITest.java b/backend/src/test/java/it/smartcommunitylab/pgazienda/web/rest/AdminResourceITest.java index 2439068..c3affbc 100644 --- a/backend/src/test/java/it/smartcommunitylab/pgazienda/web/rest/AdminResourceITest.java +++ b/backend/src/test/java/it/smartcommunitylab/pgazienda/web/rest/AdminResourceITest.java @@ -214,6 +214,7 @@ public void testValidate() throws Exception { TrackLegDTO leg = new TrackLegDTO(); leg.setDistance(1000d); leg.setMean("bike"); + leg.setValid(true); leg.setId("123456"); leg.setPoints(new LinkedList<>()); for (int i = 0; i < 10; i++) { @@ -262,6 +263,7 @@ public void testInvalidate() throws Exception { leg.setDistance(1000d); leg.setMean("bike"); leg.setId("123456"); + leg.setValid(true); leg.setPoints(new LinkedList<>()); for (int i = 0; i < 10; i++) { TrackPointDTO point = new TrackPointDTO(); @@ -315,6 +317,7 @@ public void testUpdate() throws Exception { leg.setDistance(1000d); leg.setMean("bike"); leg.setId("123456"); + leg.setValid(true); leg.setPoints(new LinkedList<>()); for (int i = 0; i < 10; i++) { TrackPointDTO point = new TrackPointDTO();