Search in sources :

Example 11 with TimeWindow

use of com.google.cloud.asset.v1.TimeWindow in project BridgeServer2 by Sage-Bionetworks.

the class Schedule2Service method preValidationCleanup.

/**
 * Set GUIDs on objects that don't have them; clean up event keys or set
 * them to null if they're not valid, so they will fail validation.
 */
void preValidationCleanup(Study study, Schedule2 schedule, Consumer<HasGuid> consumer) {
    checkNotNull(study);
    checkNotNull(schedule);
    StudyActivityEventIdsMap map = new StudyActivityEventIdsMap();
    map.addCustomEvents(study.getCustomEvents());
    map.addStudyBursts(schedule.getStudyBursts());
    for (Session session : schedule.getSessions()) {
        consumer.accept(session);
        session.setSchedule(schedule);
        for (TimeWindow window : session.getTimeWindows()) {
            consumer.accept(window);
        }
        List<String> events = session.getStartEventIds().stream().map(s -> formatActivityEventId(map, s)).collect(toList());
        session.setStartEventIds(events);
    }
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) PagedResourceList(org.sagebionetworks.bridge.models.PagedResourceList) Study(org.sagebionetworks.bridge.models.studies.Study) CAN_EDIT_SCHEDULES(org.sagebionetworks.bridge.AuthUtils.CAN_EDIT_SCHEDULES) INCLUDE_DELETED(org.sagebionetworks.bridge.models.ResourceList.INCLUDE_DELETED) OFFSET_BY(org.sagebionetworks.bridge.models.ResourceList.OFFSET_BY) LoggerFactory(org.slf4j.LoggerFactory) NEGATIVE_OFFSET_ERROR(org.sagebionetworks.bridge.BridgeConstants.NEGATIVE_OFFSET_ERROR) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestContext(org.sagebionetworks.bridge.RequestContext) StringUtils(org.apache.commons.lang3.StringUtils) Timeline(org.sagebionetworks.bridge.models.schedules2.timelines.Timeline) ParticipantScheduleGenerator(org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantScheduleGenerator) Session(org.sagebionetworks.bridge.models.schedules2.Session) ORG_ID(org.sagebionetworks.bridge.AuthEvaluatorField.ORG_ID) Schedule2(org.sagebionetworks.bridge.models.schedules2.Schedule2) PAGE_SIZE(org.sagebionetworks.bridge.models.ResourceList.PAGE_SIZE) API_MAXIMUM_PAGE_SIZE(org.sagebionetworks.bridge.BridgeConstants.API_MAXIMUM_PAGE_SIZE) Account(org.sagebionetworks.bridge.models.accounts.Account) ADMIN(org.sagebionetworks.bridge.Roles.ADMIN) List(java.util.List) Schedule2Dao(org.sagebionetworks.bridge.dao.Schedule2Dao) Optional(java.util.Optional) BridgeUtils(org.sagebionetworks.bridge.BridgeUtils) API_MINIMUM_PAGE_SIZE(org.sagebionetworks.bridge.BridgeConstants.API_MINIMUM_PAGE_SIZE) CAN_READ_STUDIES(org.sagebionetworks.bridge.AuthUtils.CAN_READ_STUDIES) TimeWindow(org.sagebionetworks.bridge.models.schedules2.TimeWindow) Stopwatch(com.google.common.base.Stopwatch) Validate(org.sagebionetworks.bridge.validators.Validate) Scheduler(org.sagebionetworks.bridge.models.schedules2.timelines.Scheduler) INSTANCE(org.sagebionetworks.bridge.validators.Schedule2Validator.INSTANCE) TimelineMetadata(org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadata) ParticipantSchedule(org.sagebionetworks.bridge.models.schedules2.participantschedules.ParticipantSchedule) StudyActivityEventIdsMap(org.sagebionetworks.bridge.models.activities.StudyActivityEventIdsMap) PAGE_SIZE_ERROR(org.sagebionetworks.bridge.BridgeConstants.PAGE_SIZE_ERROR) StudyActivityEvent(org.sagebionetworks.bridge.models.activities.StudyActivityEvent) AdherenceState(org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceState) PublishedEntityException(org.sagebionetworks.bridge.exceptions.PublishedEntityException) Logger(org.slf4j.Logger) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) SUPERADMIN(org.sagebionetworks.bridge.Roles.SUPERADMIN) DateTime(org.joda.time.DateTime) CAN_READ_SCHEDULES(org.sagebionetworks.bridge.AuthUtils.CAN_READ_SCHEDULES) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Collectors.toList(java.util.stream.Collectors.toList) Component(org.springframework.stereotype.Component) STUDY_ID(org.sagebionetworks.bridge.AuthEvaluatorField.STUDY_ID) BridgeUtils.formatActivityEventId(org.sagebionetworks.bridge.BridgeUtils.formatActivityEventId) EntityNotFoundException(org.sagebionetworks.bridge.exceptions.EntityNotFoundException) CAN_CREATE_SCHEDULES(org.sagebionetworks.bridge.AuthUtils.CAN_CREATE_SCHEDULES) HasGuid(org.sagebionetworks.bridge.models.schedules2.HasGuid) StudyActivityEventIdsMap(org.sagebionetworks.bridge.models.activities.StudyActivityEventIdsMap) TimeWindow(org.sagebionetworks.bridge.models.schedules2.TimeWindow) Session(org.sagebionetworks.bridge.models.schedules2.Session)

Example 12 with TimeWindow

use of com.google.cloud.asset.v1.TimeWindow in project BridgeServer2 by Sage-Bionetworks.

the class Schedule2ServiceTest method setsBlankGuidsOnUpdate.

@Test
public void setsBlankGuidsOnUpdate() throws Exception {
    App app = App.create();
    when(mockAppService.getApp(TEST_APP_ID)).thenReturn(app);
    when(mockOrganizationService.getOrganizationOpt(TEST_APP_ID, TEST_ORG_ID)).thenReturn(Optional.of(Organization.create()));
    Schedule2 existing = new Schedule2();
    existing.setAppId(TEST_APP_ID);
    existing.setOwnerId(TEST_ORG_ID);
    existing.setCreatedOn(CREATED_ON);
    Schedule2 schedule = Schedule2Test.createValidSchedule();
    Session session1 = SessionTest.createValidSession();
    Session session2 = SessionTest.createValidSession();
    session2.setGuid(null);
    TimeWindow window1 = SessionTest.createValidSession().getTimeWindows().get(0);
    TimeWindow window2 = SessionTest.createValidSession().getTimeWindows().get(0);
    window2.setGuid(null);
    window2.setStartTime(LocalTime.parse("14:00"));
    window2.setExpiration(Period.parse("PT6H"));
    session1.setTimeWindows(ImmutableList.of(window1, window2));
    schedule.setSessions(ImmutableList.of(session1, session2));
    // test valid schedule has GUID set on all the fields. for this test,
    // let's set something else and verify that only blank GUID fields are set,
    // allowing for copies and new items to be added to the schedule.
    doReturn("otherGuid").when(service).generateGuid();
    Study study = Study.create();
    study.setAppId(TEST_APP_ID);
    study.setIdentifier(TEST_STUDY_ID);
    when(mockDao.updateSchedule(any())).thenReturn(schedule);
    service.updateSchedule(study, existing, schedule);
    assertEquals(schedule.getSessions().get(0).getGuid(), SESSION_GUID_1);
    assertEquals(schedule.getSessions().get(0).getTimeWindows().get(0).getGuid(), SESSION_WINDOW_GUID_1);
    assertEquals(schedule.getSessions().get(0).getTimeWindows().get(1).getGuid(), "otherGuid");
    assertEquals(schedule.getSessions().get(1).getGuid(), "otherGuid");
    assertEquals(schedule.getSessions().get(1).getTimeWindows().get(0).getGuid(), SESSION_WINDOW_GUID_1);
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) Study(org.sagebionetworks.bridge.models.studies.Study) Schedule2(org.sagebionetworks.bridge.models.schedules2.Schedule2) TimeWindow(org.sagebionetworks.bridge.models.schedules2.TimeWindow) Session(org.sagebionetworks.bridge.models.schedules2.Session) SessionTest(org.sagebionetworks.bridge.models.schedules2.SessionTest) Test(org.testng.annotations.Test) Schedule2Test(org.sagebionetworks.bridge.models.schedules2.Schedule2Test)

Example 13 with TimeWindow

use of com.google.cloud.asset.v1.TimeWindow in project BridgeServer2 by Sage-Bionetworks.

the class SessionInfo method createTimelineEntry.

public static final SessionInfo createTimelineEntry(Session session) {
    List<String> languages = RequestContext.get().getCallerLanguages();
    Label label = selectByLang(session.getLabels(), languages, new Label("", session.getName()));
    int min = 0;
    for (AssessmentReference ref : session.getAssessments()) {
        if (ref.getMinutesToComplete() != null) {
            min += ref.getMinutesToComplete();
        }
    }
    SessionInfo info = new SessionInfo();
    info.guid = session.getGuid();
    info.label = label.getValue();
    info.symbol = session.getSymbol();
    info.startEventId = Iterables.getFirst(session.getStartEventIds(), null);
    info.performanceOrder = session.getPerformanceOrder();
    info.timeWindowGuids = session.getTimeWindows().stream().map(TimeWindow::getGuid).collect(toList());
    info.notifications = session.getNotifications().stream().map(not -> NotificationInfo.create(not, languages)).collect(toList());
    if (min > 0) {
        info.minutesToComplete = min;
    }
    return info;
}
Also used : Label(org.sagebionetworks.bridge.models.Label) AssessmentReference(org.sagebionetworks.bridge.models.schedules2.AssessmentReference) TimeWindow(org.sagebionetworks.bridge.models.schedules2.TimeWindow)

Example 14 with TimeWindow

use of com.google.cloud.asset.v1.TimeWindow in project BridgeServer2 by Sage-Bionetworks.

the class SessionValidator method validateTimeWindowOverlaps.

private void validateTimeWindowOverlaps(Errors errors, Session session) {
    // no windows to overlap, or session doesn't repeat
    if (session.getTimeWindows().size() < 2) {
        return;
    }
    // windows are not required to be in time order, so sort them
    List<TimeWindow> windowsInOrder = Lists.newArrayList(session.getTimeWindows());
    windowsInOrder.sort(START_TIME_COMPARATOR);
    long minuteInDay = 0;
    for (TimeWindow window : windowsInOrder) {
        LocalTime startTime = window.getStartTime();
        long winStartMinute = localTimeInMinutes(startTime);
        if (winStartMinute < minuteInDay) {
            addOverlapError(errors, session, window);
        }
        minuteInDay = winStartMinute + periodInMinutes(window.getExpiration());
    }
    // with the first window in that sequence
    if (session.getInterval() != null) {
        TimeWindow longestWindow = longestTimeWindow(session);
        long intervalInMinutes = periodInMinutes(session.getInterval());
        long windowInMinutes = localTimeInMinutes(longestWindow.getStartTime()) + periodInMinutes(longestWindow.getExpiration());
        if (windowInMinutes > intervalInMinutes) {
            addOverlapError(errors, session, longestWindow);
        }
    }
}
Also used : LocalTime(org.joda.time.LocalTime) TimeWindow(org.sagebionetworks.bridge.models.schedules2.TimeWindow)

Example 15 with TimeWindow

use of com.google.cloud.asset.v1.TimeWindow in project BridgeServer2 by Sage-Bionetworks.

the class SessionValidatorTest method timeWindows_expirationPlusDelayExceedsScheduleDuration.

@Test
public void timeWindows_expirationPlusDelayExceedsScheduleDuration() {
    // Schedule duration is 8 weeks.
    Session session = createValidSession();
    TimeWindow timeWindow = new TimeWindow();
    timeWindow.setGuid(SESSION_GUID_1);
    timeWindow.setStartTime(LocalTime.parse("08:00"));
    timeWindow.setExpiration(Period.parse("P4W"));
    session.setTimeWindows(ImmutableList.of(timeWindow));
    session.setInterval(Period.parse("P4W"));
    session.setDelay(Period.parse("P4WT1H"));
    assertValidatorMessage(INSTANCE, session, TIME_WINDOWS_FIELD + "[0].expiration", WINDOW_EXPIRATION_AFTER_SCHEDULE_DURATION);
}
Also used : TimeWindow(org.sagebionetworks.bridge.models.schedules2.TimeWindow) Session(org.sagebionetworks.bridge.models.schedules2.Session) SessionTest.createValidSession(org.sagebionetworks.bridge.models.schedules2.SessionTest.createValidSession) SessionTest(org.sagebionetworks.bridge.models.schedules2.SessionTest) Test(org.testng.annotations.Test)

Aggregations

TimeWindow (org.sagebionetworks.bridge.models.schedules2.TimeWindow)23 Session (org.sagebionetworks.bridge.models.schedules2.Session)21 Test (org.testng.annotations.Test)13 Schedule2 (org.sagebionetworks.bridge.models.schedules2.Schedule2)8 SessionTest (org.sagebionetworks.bridge.models.schedules2.SessionTest)6 SessionTest.createValidSession (org.sagebionetworks.bridge.models.schedules2.SessionTest.createValidSession)6 AssessmentReference (org.sagebionetworks.bridge.models.schedules2.AssessmentReference)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Collectors.toList (java.util.stream.Collectors.toList)2 LocalTime (org.joda.time.LocalTime)2 RequestContext (org.sagebionetworks.bridge.RequestContext)2 Label (org.sagebionetworks.bridge.models.Label)2 StudyBurst (org.sagebionetworks.bridge.models.schedules2.StudyBurst)2 Study (org.sagebionetworks.bridge.models.studies.Study)2 AssetServiceClient (com.google.cloud.asset.v1.AssetServiceClient)1 BatchGetAssetsHistoryRequest (com.google.cloud.asset.v1.BatchGetAssetsHistoryRequest)1 BatchGetAssetsHistoryResponse (com.google.cloud.asset.v1.BatchGetAssetsHistoryResponse)1 ContentType (com.google.cloud.asset.v1.ContentType)1