Search in sources :

Example 21 with TimeWindow

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

the class SessionValidator method validate.

@Override
public void validate(Object obj, Errors errors) {
    Session session = (Session) obj;
    if (isBlank(session.getGuid())) {
        errors.rejectValue(GUID_FIELD, CANNOT_BE_BLANK);
    }
    if (isBlank(session.getName())) {
        errors.rejectValue(NAME_FIELD, CANNOT_BE_BLANK);
    } else if (!session.getName().matches(BRIDGE_RELAXED_ID_PATTERN)) {
        errors.rejectValue(NAME_FIELD, BRIDGE_RELAXED_ID_ERROR);
    }
    validateStringLength(errors, 255, session.getName(), NAME_FIELD);
    if (session.getStartEventIds().isEmpty() && session.getStudyBurstIds().isEmpty()) {
        errors.rejectValue("", MUST_DEFINE_TRIGGER_ERROR);
    }
    validateStringLength(errors, 32, session.getSymbol(), SYMBOL_FIELD);
    if (session.getSymbol() != null && isBlank(session.getSymbol())) {
        errors.rejectValue(SYMBOL_FIELD, CANNOT_BE_BLANK);
    }
    // Not catching duplicates, here and in study bursts
    Set<String> uniqueStartEventIds = new HashSet<>();
    for (int i = 0; i < session.getStartEventIds().size(); i++) {
        String eventId = session.getStartEventIds().get(i);
        errors.pushNestedPath(START_EVENT_IDS_FIELD + '[' + i + ']');
        if (uniqueStartEventIds.contains(eventId)) {
            errors.rejectValue("", CANNOT_BE_DUPLICATE);
        }
        uniqueStartEventIds.add(eventId);
        if (eventId == null) {
            errors.rejectValue("", INVALID_EVENT_ID);
        } else if (isBlank(eventId)) {
            errors.rejectValue("", CANNOT_BE_BLANK);
        }
        errors.popNestedPath();
    }
    Set<String> uniqueStudyBurstIds = new HashSet<>();
    for (int i = 0; i < session.getStudyBurstIds().size(); i++) {
        String burstId = session.getStudyBurstIds().get(i);
        errors.pushNestedPath(STUDY_BURST_IDS_FIELD + '[' + i + ']');
        if (uniqueStudyBurstIds.contains(burstId)) {
            errors.rejectValue("", CANNOT_BE_DUPLICATE);
        }
        uniqueStudyBurstIds.add(burstId);
        if (burstId == null) {
            errors.rejectValue("", CANNOT_BE_NULL);
        } else if (isBlank(burstId)) {
            errors.rejectValue("", CANNOT_BE_BLANK);
        }
        if (!studyBurstIds.contains(burstId)) {
            errors.rejectValue("", UNDEFINED_STUDY_BURST);
        }
        errors.popNestedPath();
    }
    if (session.getOccurrences() != null && session.getOccurrences() < 1) {
        errors.rejectValue(OCCURRENCES_FIELD, LESS_THAN_ONE_ERROR);
    }
    validateFixedLengthPeriod(errors, session.getDelay(), DELAY_FIELD, false);
    if (scheduleDuration != null && session.getDelay() != null) {
        if (periodInMinutes(session.getDelay()) > periodInMinutes(scheduleDuration)) {
            errors.rejectValue(DELAY_FIELD, DELAY_LONGER_THAN_SCHEDULE_DURATION_ERROR);
        }
    }
    validateFixedLengthLongPeriod(errors, session.getInterval(), INTERVAL_FIELD, false);
    if (session.getPerformanceOrder() == null) {
        errors.rejectValue(PERFORMANCE_ORDER_FIELD, CANNOT_BE_NULL);
    }
    if (!session.getLabels().isEmpty()) {
        validateLabels(errors, session.getLabels());
    }
    validateJsonLength(errors, TEXT_SIZE, session.getLabels(), LABELS_FIELD);
    if (session.getTimeWindows().isEmpty()) {
        errors.rejectValue(TIME_WINDOWS_FIELD, CANNOT_BE_NULL_OR_EMPTY);
    } else {
        for (int i = 0; i < session.getTimeWindows().size(); i++) {
            TimeWindow window = session.getTimeWindows().get(i);
            errors.pushNestedPath(TIME_WINDOWS_FIELD + "[" + i + "]");
            if (isBlank(window.getGuid())) {
                errors.rejectValue(GUID_FIELD, CANNOT_BE_BLANK);
            }
            if (window.getStartTime() == null) {
                errors.rejectValue(START_TIME_FIELD, CANNOT_BE_NULL);
            } else if (window.getStartTime().getSecondOfMinute() > 0) {
                errors.rejectValue(START_TIME_FIELD, START_TIME_SECONDS_INVALID_ERROR);
            } else if (window.getStartTime().getMillisOfSecond() > 0) {
                errors.rejectValue(START_TIME_FIELD, START_TIME_MILLIS_INVALID_ERROR);
            }
            validateFixedLengthPeriod(errors, window.getExpiration(), EXPIRATION_FIELD, false);
            if (session.getInterval() != null) {
                if (window.getExpiration() == null) {
                    errors.rejectValue(EXPIRATION_FIELD, EXPIRATION_REQUIRED_ERROR);
                } else {
                    long intervalMin = periodInMinutes(session.getInterval());
                    long expMin = periodInMinutes(window.getExpiration());
                    if (expMin > intervalMin) {
                        errors.rejectValue(EXPIRATION_FIELD, EXPIRATION_LONGER_THAN_INTERVAL_ERROR);
                    }
                }
            }
            if (window.getExpiration() != null) {
                long windowExpiration = periodInMinutes(window.getExpiration());
                if (session.getDelay() != null) {
                    windowExpiration += periodInMinutes(session.getDelay());
                }
                if (scheduleDuration != null) {
                    if (windowExpiration > periodInMinutes(scheduleDuration)) {
                        errors.rejectValue(EXPIRATION_FIELD, WINDOW_EXPIRATION_AFTER_SCHEDULE_DURATION);
                    }
                }
            }
            errors.popNestedPath();
        }
        validateTimeWindowOverlaps(errors, session);
    }
    for (int i = 0; i < session.getAssessments().size(); i++) {
        AssessmentReference asmt = session.getAssessments().get(i);
        errors.pushNestedPath(ASSESSMENTS_FIELD + "[" + i + "]");
        if (isBlank(asmt.getGuid())) {
            errors.rejectValue(GUID_FIELD, CANNOT_BE_BLANK);
        }
        if (isBlank(asmt.getIdentifier())) {
            errors.rejectValue(IDENTIFIER_FIELD, CANNOT_BE_BLANK);
        }
        validateStringLength(errors, 255, asmt.getIdentifier(), IDENTIFIER_FIELD);
        if (isBlank(asmt.getAppId())) {
            errors.rejectValue(APP_ID_FIELD, CANNOT_BE_BLANK);
        }
        validateStringLength(errors, 255, asmt.getTitle(), TITLE_FIELD);
        validateLabels(errors, asmt.getLabels());
        validateJsonLength(errors, TEXT_SIZE, asmt.getLabels(), LABELS_FIELD);
        validateColorScheme(errors, asmt.getColorScheme(), COLOR_SCHEME_FIELD);
        errors.popNestedPath();
    }
    for (int i = 0; i < session.getNotifications().size(); i++) {
        Notification notification = session.getNotifications().get(i);
        errors.pushNestedPath(NOTIFICATIONS_FIELD + "[" + i + "]");
        if (notification.getNotifyAt() == null) {
            errors.rejectValue(NOTIFY_AT_FIELD, CANNOT_BE_NULL);
        }
        validateFixedLengthPeriod(errors, notification.getOffset(), OFFSET_FIELD, false);
        validateFixedLengthPeriod(errors, notification.getInterval(), INTERVAL_FIELD, false);
        if (!session.getTimeWindows().isEmpty()) {
            TimeWindow shortestWindow = shortestTimeWindow(session);
            if (shortestWindow.getExpiration() != null) {
                long winExpMinutes = periodInMinutes(shortestWindow.getExpiration());
                if (periodInMinutes(notification.getOffset()) > winExpMinutes) {
                    errors.rejectValue(OFFSET_FIELD, LONGER_THAN_WINDOW_EXPIRATION_ERROR);
                }
                if (notification.getInterval() != null) {
                    if (winExpMinutes < (24 * 60)) {
                        errors.rejectValue(INTERVAL_FIELD, WINDOW_SHORTER_THAN_DAY_ERROR);
                    } else if (periodInMinutes(notification.getInterval()) > winExpMinutes) {
                        errors.rejectValue(INTERVAL_FIELD, LONGER_THAN_WINDOW_EXPIRATION_ERROR);
                    }
                }
            }
        }
        if (notification.getMessages() == null || notification.getMessages().isEmpty()) {
            errors.rejectValue(MESSAGES_FIELD, CANNOT_BE_NULL_OR_EMPTY);
        } else {
            validateMessages(errors, notification.getMessages());
            validateJsonLength(errors, TEXT_SIZE, notification.getMessages(), MESSAGES_FIELD);
        }
        errors.popNestedPath();
    }
}
Also used : AssessmentReference(org.sagebionetworks.bridge.models.schedules2.AssessmentReference) TimeWindow(org.sagebionetworks.bridge.models.schedules2.TimeWindow) Notification(org.sagebionetworks.bridge.models.schedules2.Notification) Session(org.sagebionetworks.bridge.models.schedules2.Session) HashSet(java.util.HashSet)

Example 22 with TimeWindow

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

the class Scheduler method calculateTimeline.

public final Timeline calculateTimeline(Schedule2 schedule) {
    Timeline.Builder builder = new Timeline.Builder();
    builder.withDuration(schedule.getDuration());
    builder.withSchedule(schedule);
    calculateLanguageKey(builder);
    Map<String, Set<String>> studyBurstEventsMap = getStudyBurstEventIdsMap(schedule);
    Map<String, StudyBurst> studyBurstsById = schedule.getStudyBursts().stream().collect(Collectors.toMap(StudyBurst::getIdentifier, sb -> sb));
    for (Session session : schedule.getSessions()) {
        if (!session.getAssessments().isEmpty()) {
            List<String> startEventIds = session.getStartEventIds();
            for (String studyBurstId : session.getStudyBurstIds()) {
                startEventIds = addUniqueItemsToList(startEventIds, studyBurstEventsMap.get(studyBurstId));
            }
            for (TimeWindow window : session.getTimeWindows()) {
                scheduleTimeWindowSequence(builder, schedule, session, window, startEventIds, studyBurstsById);
            }
        }
    }
    return builder.build();
}
Also used : TimeWindow(org.sagebionetworks.bridge.models.schedules2.TimeWindow) Multiset(com.google.common.collect.Multiset) LocalTime(org.joda.time.LocalTime) HashMap(java.util.HashMap) Hashing(com.google.common.hash.Hashing) RequestContext(org.sagebionetworks.bridge.RequestContext) COMMA_JOINER(org.sagebionetworks.bridge.BridgeUtils.COMMA_JOINER) HashSet(java.util.HashSet) StudyBurst(org.sagebionetworks.bridge.models.schedules2.StudyBurst) Strings(com.google.common.base.Strings) HashMultiset(com.google.common.collect.HashMultiset) Map(java.util.Map) Hasher(com.google.common.hash.Hasher) Session(org.sagebionetworks.bridge.models.schedules2.Session) ENCODER(org.sagebionetworks.bridge.BridgeUtils.ENCODER) Charsets(com.google.common.base.Charsets) Period(org.joda.time.Period) AssessmentReference(org.sagebionetworks.bridge.models.schedules2.AssessmentReference) Schedule2(org.sagebionetworks.bridge.models.schedules2.Schedule2) Set(java.util.Set) Collectors(java.util.stream.Collectors) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) BridgeUtils.addUniqueItemsToList(org.sagebionetworks.bridge.BridgeUtils.addUniqueItemsToList) STUDY_BURST(org.sagebionetworks.bridge.models.activities.ActivityEventObjectType.STUDY_BURST) HashFunction(com.google.common.hash.HashFunction) HashSet(java.util.HashSet) Set(java.util.Set) StudyBurst(org.sagebionetworks.bridge.models.schedules2.StudyBurst) TimeWindow(org.sagebionetworks.bridge.models.schedules2.TimeWindow) Session(org.sagebionetworks.bridge.models.schedules2.Session)

Example 23 with TimeWindow

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

the class SessionValidatorTest method timeWindows_expirationExceedsScheduleDuration.

@Test
public void timeWindows_expirationExceedsScheduleDuration() {
    // 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("P8WT1H"));
    session.setTimeWindows(ImmutableList.of(timeWindow));
    session.setInterval(Period.parse("P8W"));
    session.setDelay(null);
    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)

Example 24 with TimeWindow

use of com.google.cloud.asset.v1.TimeWindow in project java-asset by googleapis.

the class BatchGetAssetsHistoryExample method main.

// Export assets for a project.
// @param args path where the results will be exported to.
public static void main(String... args) throws Exception {
    // Asset names, e.g.: "//storage.googleapis.com/[BUCKET_NAME]"
    String[] assetNames = args[0].split(",");
    try (AssetServiceClient client = AssetServiceClient.create()) {
        ProjectName parent = ProjectName.of(projectId);
        ContentType contentType = ContentType.CONTENT_TYPE_UNSPECIFIED;
        TimeWindow readTimeWindow = TimeWindow.newBuilder().build();
        BatchGetAssetsHistoryRequest request = BatchGetAssetsHistoryRequest.newBuilder().setParent(parent.toString()).addAllAssetNames(Arrays.asList(assetNames)).setContentType(contentType).setReadTimeWindow(readTimeWindow).build();
        BatchGetAssetsHistoryResponse response = client.batchGetAssetsHistory(request);
        System.out.println(response);
    }
}
Also used : ContentType(com.google.cloud.asset.v1.ContentType) ProjectName(com.google.cloud.asset.v1.ProjectName) AssetServiceClient(com.google.cloud.asset.v1.AssetServiceClient) BatchGetAssetsHistoryRequest(com.google.cloud.asset.v1.BatchGetAssetsHistoryRequest) BatchGetAssetsHistoryResponse(com.google.cloud.asset.v1.BatchGetAssetsHistoryResponse) TimeWindow(com.google.cloud.asset.v1.TimeWindow)

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