Skip to content

Commit

Permalink
#350: Refactor timestamp validation and interval handling.
Browse files Browse the repository at this point in the history
Renamed methods and variables for better clarity and consistency across classes. Adjusted logic to streamline processing, including closing streams explicitly to avoid resource leaks. Updated response messages for improved user feedback.
  • Loading branch information
janoliver20 committed Dec 12, 2024
1 parent 22252f6 commit db40020
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public ResponseEntity<String> storeExternalBulk(String moreApiToken, EndpointDat
ApiRoutingInfo apiRoutingInfo = externalService.getRoutingInfo(moreApiToken);
Integer participantId = Integer.valueOf(endpointDataBulkDTO.getParticipantId());

externalService.allTimestampsInBulkAreValid(apiRoutingInfo.studyId(), apiRoutingInfo.observationId(), participantId, endpointDataBulkDTO);
externalService.assertTimestampsInBulk(apiRoutingInfo.studyId(), apiRoutingInfo.observationId(), participantId, endpointDataBulkDTO);

final RoutingInfo routingInfo = externalService.validateAndCreateRoutingInfo(apiRoutingInfo, participantId);
LoggingUtils.createContext(routingInfo);
Expand All @@ -81,7 +81,7 @@ public ResponseEntity<String> storeExternalBulk(String moreApiToken, EndpointDat
numberOfDiscardedIds, routingInfo.studyId(), routingInfo.participantId());
return ResponseEntity.status(HttpStatus.CONFLICT).body("Study or participant is not active");
}
return ResponseEntity.accepted().body("Data has been successfully processed.");
return ResponseEntity.accepted().body("%d data-points have been successfully processed".formatted(endpointDataBulkDTO.getDataPoints().size()));
} catch (AccessDeniedException e) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("Invalid token!");
} catch (NumberFormatException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public Instant getEnd() {
return end;
}

public boolean containsTimestamp(Instant timestamp) {
return !timestamp.isBefore(start) && !timestamp.isAfter(end);
public boolean contains(Instant instant) {
return !instant.isBefore(start) && !instant.isAfter(end);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,7 @@ private static MapSqlParameterSource toParameterSource(long studyId, int partici

private static MapSqlParameterSource toParameterSource(long studyId, int participantId, ParticipantConsent.ObservationConsent consent) {
return toParameterSource(studyId, participantId)
.addValue("observation_id", consent.observationId())
;
.addValue("observation_id", consent.observationId());
}


Expand All @@ -445,9 +444,11 @@ public List<Interval> getIntervals(Long studyId, Integer participantId, Relative
},
studyId, participantId
)) {
return stream
var intervalList = stream
.flatMap(List::stream)
.collect(Collectors.toList());
stream.close();
return intervalList;
} catch (Exception e) {
throw new BadRequestException("Failed to retrieve intervals for studyId: " + studyId + " and participantId: " + participantId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public RoutingInfo validateAndCreateRoutingInfo(ApiRoutingInfo apiRoutingInfo, I
}

@Cacheable(CachingConfiguration.OBSERVATION_ENDINGS)
public void allTimestampsInBulkAreValid(Long studyId, Integer observationId, Integer participantId, EndpointDataBulkDTO dataBulkDTO) {
public void assertTimestampsInBulk(Long studyId, Integer observationId, Integer participantId, EndpointDataBulkDTO dataBulkDTO) {
Stream<ScheduleEvent> scheduleEvents = Optional.ofNullable(repository.getObservationSchedule(studyId, observationId))
.orElseThrow(() -> BadRequestException.NotFound(studyId, observationId));

Expand All @@ -98,6 +98,7 @@ public void allTimestampsInBulkAreValid(Long studyId, Integer observationId, Int
}
})
.toList();
scheduleEvents.close();

if (intervalList.isEmpty()) {
throw BadRequestException.NotFound(studyId, observationId);
Expand All @@ -106,7 +107,7 @@ public void allTimestampsInBulkAreValid(Long studyId, Integer observationId, Int
boolean allValid = dataBulkDTO.getDataPoints().stream()
.map(ExternalDataDTO::getTimestamp)
.allMatch(timestamp -> intervalList.stream()
.anyMatch(interval -> interval.containsTimestamp(timestamp))
.anyMatch(interval -> interval.contains(timestamp))
);
if (!allValid) {
throw TimeFrameException.InvalidDataPointInterval(dataBulkDTO.getParticipantId(), intervalList);
Expand Down

0 comments on commit db40020

Please sign in to comment.