Skip to content

Commit

Permalink
Change in the validation: validity of legs
Browse files Browse the repository at this point in the history
  • Loading branch information
kazhamiakin committed Aug 7, 2023
1 parent ad05d05 commit 304a40a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static class TrackLegDTO {
private Double distance;
private long duration;
private double co2;
private Boolean valid;

private List<TrackPointDTO> points;

Expand Down Expand Up @@ -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;
}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TrackLegDTO> 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<TrackLegDTO> 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<TrackLegDTO> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static boolean matchLocations(TrackLegDTO ti, List<Shape> areas) {
return matchPoints(areas, arr);
}

public static boolean matchLocations(TrackDTO track, List<Shape> areas) {
public static int matchLocations(TrackDTO track, List<Shape> areas) {
TrackLegDTO startLeg = track.getLegs().get(0);
startLeg.getPoints().sort((a,b) -> {
return a.getRecorded_at().compareTo(b.getRecorded_at());
Expand All @@ -56,9 +56,14 @@ public static boolean matchLocations(TrackDTO track, List<Shape> areas) {
List<TrackPointDTO> 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<Shape> areas, Object[] arr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 304a40a

Please sign in to comment.