Skip to content

Commit

Permalink
Merge pull request #110 from redlink-gmbh/FEATURE-Relative_Study_Start
Browse files Browse the repository at this point in the history
Fix for participant shift when relative study ends in less than 60 minutes
  • Loading branch information
alireza-dhp authored Feb 19, 2024
2 parents bdbd395 + cf3b018 commit fb0c570
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private void updateParticipantStatus(long studyId, int groupId, int participantI

if ("active".equals(newStatus)) {
start = Timestamp.from(
SchedulerUtils.shiftStartIfObservationAlreadyStarted(Instant.now(), listObservations(studyId, groupId, participantId, true))
SchedulerUtils.shiftStartIfObservationAlreadyEnded(Instant.now(), listObservations(studyId, groupId, participantId, true))
);
}

Expand Down
11 changes: 2 additions & 9 deletions src/main/java/io/redlink/more/data/schedule/SchedulerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public static List<Pair<Instant, Instant>> parseToObservationSchedulesForRelativ

List<Pair<Instant, Instant>> events = new ArrayList<>();

start = shiftStartIfNecessary(start);

Pair<Instant, Instant> currentEvt = Pair.of(toInstant(event.getDtstart(), start), toInstant(event.getDtend(), start));

if(event.getRrrule() != null) {
Expand All @@ -57,11 +55,6 @@ public static List<Pair<Instant, Instant>> parseToObservationSchedulesForRelativ
return events;
}

private static Instant shiftStartIfNecessary(Instant start) {
// TODO
return start;
}

private static Instant toInstant(RelativeDate date, Instant start) {
return ZonedDateTime.ofInstant(start.plus(date.getOffset().getValue() - 1L, date.getOffset().getUnit().toTemporalUnit()), date.getZoneId())
.withHour(date.getHours())
Expand Down Expand Up @@ -98,7 +91,7 @@ public static List<Pair<Instant, Instant>> parseToObservationSchedules(ScheduleE
}
}

public static Instant shiftStartIfObservationAlreadyStarted(Instant start, List<Observation> observations) {
public static Instant shiftStartIfObservationAlreadyEnded(Instant start, List<Observation> observations) {
// returns start date, if now event ends before, otherwise start date + 1 day
return observations.stream()
.map(Observation::observationSchedule)
Expand All @@ -107,7 +100,7 @@ public static Instant shiftStartIfObservationAlreadyStarted(Instant start, List<
.filter(relativeDate -> relativeDate.getOffset().getValue() == 1)
.map(relativeDate -> start.atZone(relativeDate.getZoneId()).withHour(relativeDate.getHours()).withMinute(relativeDate.getMinutes()).withSecond(0).withNano(0).toInstant())
.filter(instant -> {
return instant.isBefore(start.plus(1, ChronoUnit.HOURS));
return instant.isBefore(start);
})
.map(instant -> start.atZone(ZoneId.systemDefault()).withHour(0).withMinute(0).plusDays(1).toInstant())
.findFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.Optional;
import java.util.TimeZone;

import static io.redlink.more.data.schedule.SchedulerUtils.shiftStartIfObservationAlreadyStarted;
import static io.redlink.more.data.schedule.SchedulerUtils.shiftStartIfObservationAlreadyEnded;

@Service
public class CalendarService {
Expand All @@ -39,7 +39,7 @@ public Optional<String> getICalendarString(Long studyId) {
iCalEvent.setDateEnd(Date.from(study.endDate().atStartOfDay(TimeZone.getDefault().toZoneId()).toInstant()), false);
ical.addEvent(iCalEvent);

final Instant start = shiftStartIfObservationAlreadyStarted(
final Instant start = shiftStartIfObservationAlreadyEnded(
study.plannedStartDate().atStartOfDay(ZoneId.systemDefault()).toInstant(),
study.observations()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.Arrays;
import java.util.List;

import static io.redlink.more.data.schedule.SchedulerUtils.shiftStartIfObservationAlreadyStarted;
import static io.redlink.more.data.schedule.SchedulerUtils.shiftStartIfObservationAlreadyEnded;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -392,13 +392,13 @@ void testRelativeEventShift() {
when(observationDay1At13.observationSchedule()).thenReturn(day1At13);
when(observationDay2At10.observationSchedule()).thenReturn(day2At10);

Instant s1 = shiftStartIfObservationAlreadyStarted(start, List.of(observationDay1At10, observationDay1At12, observationDay2At10));
Instant s1 = shiftStartIfObservationAlreadyEnded(start, List.of(observationDay1At10, observationDay1At12, observationDay2At10));
Assertions.assertNotEquals(s1.toEpochMilli(), start.toEpochMilli());

Instant s2 = shiftStartIfObservationAlreadyStarted(start, List.of(observationDay1At12, observationDay2At10));
Assertions.assertNotEquals(s2.toEpochMilli(), start.toEpochMilli());
Instant s2 = shiftStartIfObservationAlreadyEnded(start, List.of(observationDay1At12, observationDay2At10));
Assertions.assertEquals(s2.toEpochMilli(), start.toEpochMilli());

Instant s3 = shiftStartIfObservationAlreadyStarted(start, List.of(observationDay1At13, observationDay2At10));
Instant s3 = shiftStartIfObservationAlreadyEnded(start, List.of(observationDay1At13, observationDay2At10));
Assertions.assertEquals(s3.toEpochMilli(), start.toEpochMilli());
}

Expand Down

0 comments on commit fb0c570

Please sign in to comment.