Search in sources :

Example 1 with ParticipantSchedule

use of org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantSchedule in project BridgeServer2 by Sage-Bionetworks.

the class Schedule2ServiceTest method getParticipantSchedule.

@Test
public void getParticipantSchedule() {
    RequestContext.set(new RequestContext.Builder().withCallerUserId(TEST_USER_ID).withCallerEnrolledStudies(ImmutableSet.of(TEST_STUDY_ID)).build());
    Account account = Account.create();
    account.setAppId(TEST_APP_ID);
    account.setId(TEST_USER_ID);
    account.setClientTimeZone("America/Chicago");
    Study study = Study.create();
    study.setIdentifier(TEST_STUDY_ID);
    study.setScheduleGuid(SCHEDULE_GUID);
    when(mockStudyService.getStudy(TEST_APP_ID, TEST_STUDY_ID, true)).thenReturn(study);
    Schedule2 schedule = Schedule2Test.createValidSchedule();
    when(mockDao.getSchedule(TEST_APP_ID, SCHEDULE_GUID)).thenReturn(Optional.of(schedule));
    ResourceList<StudyActivityEvent> events = new ResourceList<>(ImmutableList.of());
    when(mockStudyActivityEventService.getRecentStudyActivityEvents(TEST_APP_ID, TEST_STUDY_ID, TEST_USER_ID)).thenReturn(events);
    ParticipantSchedule retValue = service.getParticipantSchedule(TEST_APP_ID, TEST_STUDY_ID, account);
    assertEquals(retValue.getClientTimeZone(), "America/Chicago");
    assertEquals(retValue.getCreatedOn(), CREATED_ON.withZone(DateTimeZone.forID("America/Chicago")));
}
Also used : Account(org.sagebionetworks.bridge.models.accounts.Account) Study(org.sagebionetworks.bridge.models.studies.Study) PagedResourceList(org.sagebionetworks.bridge.models.PagedResourceList) ResourceList(org.sagebionetworks.bridge.models.ResourceList) Schedule2(org.sagebionetworks.bridge.models.schedules2.Schedule2) StudyActivityEvent(org.sagebionetworks.bridge.models.activities.StudyActivityEvent) ParticipantSchedule(org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantSchedule) SessionTest(org.sagebionetworks.bridge.models.schedules2.SessionTest) Test(org.testng.annotations.Test) Schedule2Test(org.sagebionetworks.bridge.models.schedules2.Schedule2Test)

Example 2 with ParticipantSchedule

use of org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantSchedule in project BridgeServer2 by Sage-Bionetworks.

the class ParticipantScheduleGenerator method generate.

public ParticipantSchedule generate(AdherenceState state, Timeline timeline) {
    Multimap<LocalDate, ScheduledSession> chronology = LinkedHashMultimap.create();
    LocalDate earliestDate = LATEST_LOCAL_DATE;
    LocalDate latestDate = EARLIEST_LOCAL_DATE;
    Map<String, DateTime> eventTimestamps = new HashMap<>();
    for (ScheduledSession schSession : timeline.getSchedule()) {
        String eventId = schSession.getStartEventId();
        DateTime eventTimestamp = state.getEventTimestampById(eventId);
        if (eventTimestamp == null) {
            continue;
        }
        LocalDate startDate = eventTimestamp.plusDays(schSession.getStartDay()).toLocalDate();
        LocalDate endDate = eventTimestamp.plusDays(schSession.getEndDay()).toLocalDate();
        eventTimestamps.put(eventId, eventTimestamp);
        ScheduledSession.Builder builder = schSession.toBuilder();
        builder.withStartDate(startDate);
        builder.withEndDate(endDate);
        if (startDate.isBefore(earliestDate)) {
            earliestDate = startDate;
        }
        if (endDate.isAfter(latestDate)) {
            latestDate = endDate;
        }
        for (ScheduledAssessment schAssessment : schSession.getAssessments()) {
            ScheduledAssessment.Builder asmtBuilder = new ScheduledAssessment.Builder().withRefKey(schAssessment.getRefKey()).withInstanceGuid(schAssessment.getInstanceGuid());
            builder.withScheduledAssessment(asmtBuilder.build());
        }
        // null these out, not useful
        schSession.getTimeWindow().setGuid(null);
        builder.withStartDay(null);
        builder.withEndDay(null);
        chronology.put(startDate, builder.build());
    }
    // chronology.size() is the total number of pairs, not the total number of keys (and thus correct).
    List<ScheduledSession> scheduledSessions = Lists.newArrayListWithCapacity(chronology.size());
    for (LocalDate date : chronology.keySet()) {
        scheduledSessions.addAll(chronology.get(date));
    }
    scheduledSessions.sort(SCHEDULED_SESSION_COMPARATOR);
    DateRange range = null;
    if (earliestDate.isBefore(latestDate)) {
        range = new DateRange(earliestDate, latestDate);
    }
    ParticipantSchedule schedule = new ParticipantSchedule();
    schedule.setCreatedOn(state.getNow());
    schedule.setClientTimeZone(state.getClientTimeZone());
    schedule.setDateRange(range);
    schedule.setSchedule(scheduledSessions);
    schedule.setSessions(timeline.getSessions().stream().map(SessionInfo::createScheduleEntry).collect(toList()));
    schedule.setAssessments(timeline.getAssessments());
    schedule.setStudyBursts(timeline.getStudyBursts());
    schedule.setEventTimestamps(eventTimestamps);
    return schedule;
}
Also used : HashMap(java.util.HashMap) SessionInfo(org.sagebionetworks.bridge.models.schedules2.timelines.SessionInfo) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) ScheduledAssessment(org.sagebionetworks.bridge.models.schedules2.timelines.ScheduledAssessment) DateRange(org.sagebionetworks.bridge.models.DateRange) ScheduledSession(org.sagebionetworks.bridge.models.schedules2.timelines.ScheduledSession)

Example 3 with ParticipantSchedule

use of org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantSchedule in project BridgeServer2 by Sage-Bionetworks.

the class StudyParticipantController method getParticipantScheduleForSelf.

@EtagSupport({ // Most recent modification to the schedule
@EtagCacheKey(model = Schedule2.class, keys = { "appId", "studyId" }), // Most recent modification to the participant’s collection of events
@EtagCacheKey(model = StudyActivityEvent.class, keys = { "userId" }), // Most recent modification to the participant’s time zone
@EtagCacheKey(model = DateTimeZone.class, keys = { "userId" }) })
@GetMapping("/v5/studies/{studyId}/participants/self/schedule")
public ParticipantSchedule getParticipantScheduleForSelf(@PathVariable String studyId) {
    UserSession session = getAuthenticatedAndConsentedSession();
    // This produces the desired error (unauthorized rather than 404)
    if (!session.getParticipant().getStudyIds().contains(studyId)) {
        throw new UnauthorizedException("Caller is not enrolled in study '" + studyId + "'");
    }
    AccountId accountId = AccountId.forId(session.getAppId(), session.getId());
    Account account = accountService.getAccount(accountId).orElseThrow(() -> new EntityNotFoundException(Account.class));
    // Even if the call fails, we want to know they tried it
    DateTime timelineRequestedOn = getDateTime();
    RequestInfo requestInfo = getRequestInfoBuilder(session).withTimelineAccessedOn(timelineRequestedOn).build();
    requestInfoService.updateRequestInfo(requestInfo);
    ParticipantSchedule schedule = scheduleService.getParticipantSchedule(session.getAppId(), studyId, account);
    studyActivityEventService.publishEvent(new StudyActivityEvent.Builder().withAppId(session.getAppId()).withStudyId(studyId).withUserId(session.getId()).withObjectType(TIMELINE_RETRIEVED).withTimestamp(timelineRequestedOn).build(), false, true);
    return schedule;
}
Also used : Account(org.sagebionetworks.bridge.models.accounts.Account) AccountId(org.sagebionetworks.bridge.models.accounts.AccountId) UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) UnauthorizedException(org.sagebionetworks.bridge.exceptions.UnauthorizedException) StudyActivityEvent(org.sagebionetworks.bridge.models.activities.StudyActivityEvent) EntityNotFoundException(org.sagebionetworks.bridge.exceptions.EntityNotFoundException) RequestInfo(org.sagebionetworks.bridge.models.RequestInfo) DateTime(org.joda.time.DateTime) ParticipantSchedule(org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantSchedule) GetMapping(org.springframework.web.bind.annotation.GetMapping) EtagSupport(org.sagebionetworks.bridge.spring.util.EtagSupport)

Example 4 with ParticipantSchedule

use of org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantSchedule in project BridgeServer2 by Sage-Bionetworks.

the class StudyParticipantControllerTest method getParticipantScheduleForUser.

@Test
public void getParticipantScheduleForUser() {
    RequestContext.set(new RequestContext.Builder().withCallerUserId("id").withOrgSponsoredStudies(ImmutableSet.of(TEST_STUDY_ID)).withCallerRoles(ImmutableSet.of(STUDY_COORDINATOR)).build());
    mockAccountInStudy();
    ParticipantSchedule schedule = new ParticipantSchedule();
    when(mockScheduleService.getParticipantSchedule(eq(TEST_APP_ID), eq(TEST_STUDY_ID), any())).thenReturn(schedule);
    ParticipantSchedule retValue = controller.getParticipantScheduleForUser(TEST_STUDY_ID, TEST_USER_ID);
    assertSame(retValue, schedule);
}
Also used : RequestContext(org.sagebionetworks.bridge.RequestContext) ParticipantSchedule(org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantSchedule) Test(org.testng.annotations.Test)

Example 5 with ParticipantSchedule

use of org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantSchedule in project BridgeServer2 by Sage-Bionetworks.

the class ParticipantScheduleGeneratorTest method test.

@Test
public void test() throws Exception {
    Schedule2 schedule = Schedule2Test.createValidSchedule();
    Timeline timeline = Scheduler.INSTANCE.calculateTimeline(schedule);
    StudyActivityEvent e1 = new StudyActivityEvent.Builder().withEventId("timeline_retrieved").withTimestamp(TIMESTAMP).build();
    AdherenceState state = new AdherenceState.Builder().withClientTimeZone(TIME_ZONE.getID()).withNow(TIMESTAMP.withZone(TIMEZONE_MSK)).withEvents(ImmutableList.of(e1)).build();
    ParticipantSchedule retValue = INSTANCE.generate(state, timeline);
    assertEquals(retValue.getCreatedOn(), TIMESTAMP.withZone(TIME_ZONE));
    assertEquals(retValue.getClientTimeZone(), TIME_ZONE.getID());
    assertEquals(retValue.getDateRange().getStartDate().toString(), "2015-02-02");
    assertEquals(retValue.getDateRange().getEndDate().toString(), "2015-03-16");
    // These values are all copied over from the timeline, so they don't need to be
    // examined very closely. They are all tested in other tests.
    assertEquals(retValue.getSessions().size(), 1);
    assertEquals(retValue.getStudyBursts().size(), 1);
    assertEquals(retValue.getAssessments().size(), 2);
    assertEquals(retValue.getEventTimestamps().size(), 1);
    assertEquals(retValue.getEventTimestamps().get("timeline_retrieved"), TIMESTAMP.withZone(TIME_ZONE));
    // But the schedule is different
    assertEquals(retValue.getSchedule().size(), 7);
    assertScheduledSession(retValue.getSchedule().get(0), "NpLGwpRYGr3cYjJvp945zQ", LocalDate.parse("2015-02-02"), LocalDate.parse("2015-02-02"), "MDoRIcMJpAZ3Xqy_uZAbnw", "AzGvv4ph-7Xzi9VRIrFyWw");
    assertScheduledSession(retValue.getSchedule().get(1), "0dVupumHdJENCi5rzjA1sQ", LocalDate.parse("2015-02-09"), LocalDate.parse("2015-02-09"), "mZTon_L0lXXErPKme-Ojhg", "okkx1iuHoh6MV8I8I9vYYQ");
    assertScheduledSession(retValue.getSchedule().get(2), "JzKooqt2k1rdn7A1E7j1Hg", LocalDate.parse("2015-02-16"), LocalDate.parse("2015-02-16"), "Pp55NkTwC1MWmAhdZWLD7A", "B2hnjJKGkFQ83rLCzkPDmA");
    assertScheduledSession(retValue.getSchedule().get(3), "8arBYlLtL93TdSjhVYDjfg", LocalDate.parse("2015-02-23"), LocalDate.parse("2015-02-23"), "4_qpTNQfPsIwVK9m0ok7YQ", "h-0BJQhWuU34QODRfLjRlg");
    assertScheduledSession(retValue.getSchedule().get(4), "qUvQ7R3FYbl2j-pEZ42ESQ", LocalDate.parse("2015-03-02"), LocalDate.parse("2015-03-02"), "y0deGrdsyat-i3CHxWzzkg", "9HaDixbXUid_O4Dc5m7Zog");
    assertScheduledSession(retValue.getSchedule().get(5), "XuyuOyJF5zs5s4cdqQISCg", LocalDate.parse("2015-03-09"), LocalDate.parse("2015-03-09"), "_IHC7vBzvy62AAOZDXqe6A", "MmMA1dT66qzTSnugAY-reQ");
    assertScheduledSession(retValue.getSchedule().get(6), "ym78tZHtMLaEfvx4-f9OGg", LocalDate.parse("2015-03-16"), LocalDate.parse("2015-03-16"), "xkucPnJH3lz_BAJ5X1DaLw", "mBwW2oM-rXEcNO7LIVi5PQ");
}
Also used : AdherenceState(org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceState) Timeline(org.sagebionetworks.bridge.models.schedules2.timelines.Timeline) Schedule2(org.sagebionetworks.bridge.models.schedules2.Schedule2) StudyActivityEvent(org.sagebionetworks.bridge.models.activities.StudyActivityEvent) ParticipantSchedule(org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantSchedule) Test(org.testng.annotations.Test) Schedule2Test(org.sagebionetworks.bridge.models.schedules2.Schedule2Test)

Aggregations

ParticipantSchedule (org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantSchedule)9 Test (org.testng.annotations.Test)7 StudyActivityEvent (org.sagebionetworks.bridge.models.activities.StudyActivityEvent)6 Schedule2 (org.sagebionetworks.bridge.models.schedules2.Schedule2)5 Schedule2Test (org.sagebionetworks.bridge.models.schedules2.Schedule2Test)5 Timeline (org.sagebionetworks.bridge.models.schedules2.timelines.Timeline)4 Account (org.sagebionetworks.bridge.models.accounts.Account)3 AdherenceState (org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceState)3 Study (org.sagebionetworks.bridge.models.studies.Study)3 DateTime (org.joda.time.DateTime)2 DateRange (org.sagebionetworks.bridge.models.DateRange)2 PagedResourceList (org.sagebionetworks.bridge.models.PagedResourceList)2 RequestInfo (org.sagebionetworks.bridge.models.RequestInfo)2 ResourceList (org.sagebionetworks.bridge.models.ResourceList)2 SessionTest (org.sagebionetworks.bridge.models.schedules2.SessionTest)2 ScheduledSession (org.sagebionetworks.bridge.models.schedules2.timelines.ScheduledSession)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Stopwatch (com.google.common.base.Stopwatch)1 HashMap (java.util.HashMap)1 LocalDate (org.joda.time.LocalDate)1