Search in sources :

Example 1 with TimelineMetadata

use of org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadata in project BridgeServer2 by Sage-Bionetworks.

the class AdherenceService method updateSessionState.

protected void updateSessionState(String appId, MetadataContainer container, AdherenceRecord asmt) {
    TimelineMetadata asmtMeta = container.getMetadata(asmt.getInstanceGuid());
    String sessionInstanceGuid = asmtMeta.getSessionInstanceGuid();
    List<TimelineMetadata> asmtMetas = scheduleService.getSessionAssessmentMetadata(sessionInstanceGuid);
    Set<String> instanceGuids = asmtMetas.stream().map(TimelineMetadata::getAssessmentInstanceGuid).collect(toSet());
    instanceGuids.add(sessionInstanceGuid);
    PagedResourceList<AdherenceRecord> allRecords = recordDao.getAdherenceRecords(new AdherenceRecordsSearch.Builder().withUserId(asmt.getUserId()).withStudyId(asmt.getStudyId()).withEventTimestamps(ImmutableMap.of(asmtMeta.getSessionStartEventId(), asmt.getEventTimestamp())).withInstanceGuids(instanceGuids).build());
    SessionState state = new SessionState(asmtMetas.size());
    // The session record may have been submitted, it may be persisted, or
    // it may not yet exist, and we take the records in that order.
    AdherenceRecord sessionRecord = container.getRecord(sessionInstanceGuid);
    for (AdherenceRecord oneRecord : allRecords.getItems()) {
        if (sessionInstanceGuid.equals(oneRecord.getInstanceGuid())) {
            // The record was persisted
            if (sessionRecord == null) {
                sessionRecord = oneRecord;
            }
        } else {
            state.add(oneRecord);
        }
    }
    // The record is new and needs to be created
    if (sessionRecord == null) {
        sessionRecord = new AdherenceRecord();
        sessionRecord.setAppId(asmt.getAppId());
        sessionRecord.setUserId(asmt.getUserId());
        sessionRecord.setStudyId(asmt.getStudyId());
        sessionRecord.setInstanceGuid(sessionInstanceGuid);
        sessionRecord.setEventTimestamp(asmt.getEventTimestamp());
    }
    if (state.updateSessionRecord(sessionRecord)) {
        container.addRecord(sessionRecord);
    }
}
Also used : TimelineMetadata(org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadata) SessionState(org.sagebionetworks.bridge.models.schedules2.timelines.SessionState) AdherenceRecord(org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceRecord)

Example 2 with TimelineMetadata

use of org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadata in project BridgeServer2 by Sage-Bionetworks.

the class AdherenceService method generateReport.

private <T> T generateReport(String appId, String studyId, String userId, DateTime createdOn, String clientTimeZone, Function<AdherenceState, T> func) {
    AdherenceState.Builder builder = new AdherenceState.Builder();
    builder.withNow(createdOn);
    builder.withClientTimeZone(clientTimeZone);
    Study study = studyService.getStudy(appId, studyId, true);
    if (study.getScheduleGuid() == null) {
        return func.apply(builder.build());
    }
    List<TimelineMetadata> metadata = scheduleService.getScheduleMetadata(study.getScheduleGuid());
    List<StudyActivityEvent> events = studyActivityEventService.getRecentStudyActivityEvents(appId, studyId, userId).getItems();
    List<AdherenceRecord> adherenceRecords = getAdherenceRecords(appId, new AdherenceRecordsSearch.Builder().withCurrentTimestampsOnly(true).withIncludeRepeats(true).withAdherenceRecordType(AdherenceRecordType.SESSION).withStudyId(studyId).withUserId(userId).build()).getItems();
    builder.withMetadata(metadata);
    builder.withEvents(events);
    builder.withAdherenceRecords(adherenceRecords);
    builder.withStudyStartEventId(study.getStudyStartEventId());
    return func.apply(builder.build());
}
Also used : AdherenceState(org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceState) Study(org.sagebionetworks.bridge.models.studies.Study) TimelineMetadata(org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadata) AdherenceRecord(org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceRecord) StudyActivityEvent(org.sagebionetworks.bridge.models.activities.StudyActivityEvent)

Example 3 with TimelineMetadata

use of org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadata in project BridgeServer2 by Sage-Bionetworks.

the class MetadataContainerTest method addRecord_addPersistentSessionAdherenceRecord.

@Test
public void addRecord_addPersistentSessionAdherenceRecord() {
    AdherenceRecord record = new AdherenceRecord();
    record.setStartedOn(TIMESTAMP);
    record.setInstanceGuid("instanceGuid");
    TimelineMetadata meta = new TimelineMetadata();
    meta.setTimeWindowPersistent(true);
    meta.setGuid("guid");
    when(mockScheduleService.getTimelineMetadata("instanceGuid")).thenReturn(Optional.of(meta));
    MetadataContainer container = createContainer(ImmutableList.of(record));
    container.addRecord(record);
    assertEquals(record, container.getRecord("instanceGuid"));
    assertEquals(meta, container.getMetadata("instanceGuid"));
    assertEquals(record.getInstanceTimestamp(), TIMESTAMP);
    assertTrue(container.getAssessments().isEmpty());
    assertFalse(container.getSessionUpdates().isEmpty());
}
Also used : AdherenceRecord(org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceRecord) Test(org.testng.annotations.Test)

Example 4 with TimelineMetadata

use of org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadata in project BridgeServer2 by Sage-Bionetworks.

the class MetadataContainerTest method ar.

private AdherenceRecord ar(String asmtInstanceGuid, String sessionInstanceGuid, DateTime startedOn, DateTime finishedOn) {
    AdherenceRecord record = new AdherenceRecord();
    record.setAppId(TEST_APP_ID);
    record.setUserId(TEST_USER_ID);
    record.setStudyId(TEST_STUDY_ID);
    record.setStartedOn(startedOn);
    record.setFinishedOn(finishedOn);
    record.setEventTimestamp(TIMESTAMP);
    TimelineMetadata meta = new TimelineMetadata();
    meta.setAppId(TEST_APP_ID);
    meta.setSessionStartEventId("enrollment");
    if (asmtInstanceGuid == null) {
        // Session
        record.setInstanceGuid(sessionInstanceGuid);
        meta.setGuid(sessionInstanceGuid);
        meta.setSessionInstanceGuid(sessionInstanceGuid);
        when(mockScheduleService.getTimelineMetadata(sessionInstanceGuid)).thenReturn(Optional.of(meta));
    } else {
        // Assessment
        record.setInstanceGuid(asmtInstanceGuid);
        meta.setGuid(asmtInstanceGuid);
        meta.setSessionInstanceGuid(sessionInstanceGuid);
        meta.setAssessmentInstanceGuid(asmtInstanceGuid);
        when(mockScheduleService.getTimelineMetadata(asmtInstanceGuid)).thenReturn(Optional.of(meta));
    }
    return record;
}
Also used : AdherenceRecord(org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceRecord)

Example 5 with TimelineMetadata

use of org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadata in project BridgeServer2 by Sage-Bionetworks.

the class AdherenceServiceTest method deleteAdherenceRecord_persistentTimeWindow.

@Test
public void deleteAdherenceRecord_persistentTimeWindow() {
    AdherenceRecord record = ar(STARTED_ON, FINISHED_ON, "fake-guid", false);
    RequestContext.set(new RequestContext.Builder().withCallerRoles(ImmutableSet.of(RESEARCHER)).build());
    TimelineMetadata timelineMetadata = new TimelineMetadata();
    timelineMetadata.setTimeWindowPersistent(true);
    when(mockScheduleService.getTimelineMetadata(any())).thenReturn(Optional.of(timelineMetadata));
    service.deleteAdherenceRecord(record);
    verify(mockRecordDao).deleteAdherenceRecordPermanently(eq(record));
    assertEquals(record.getInstanceTimestamp(), record.getStartedOn());
}
Also used : TimelineMetadata(org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadata) AdherenceRecord(org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceRecord) RequestContext(org.sagebionetworks.bridge.RequestContext) Test(org.testng.annotations.Test) StudyAdherenceReportGeneratorTest(org.sagebionetworks.bridge.models.schedules2.adherence.study.StudyAdherenceReportGeneratorTest)

Aggregations

TimelineMetadata (org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadata)21 AdherenceRecord (org.sagebionetworks.bridge.models.schedules2.adherence.AdherenceRecord)16 Test (org.testng.annotations.Test)16 Schedule2Test (org.sagebionetworks.bridge.models.schedules2.Schedule2Test)5 SessionTest (org.sagebionetworks.bridge.models.schedules2.SessionTest)5 StudyActivityEvent (org.sagebionetworks.bridge.models.activities.StudyActivityEvent)4 Schedule2 (org.sagebionetworks.bridge.models.schedules2.Schedule2)4 RequestContext (org.sagebionetworks.bridge.RequestContext)3 StudyAdherenceReportGeneratorTest (org.sagebionetworks.bridge.models.schedules2.adherence.study.StudyAdherenceReportGeneratorTest)3 Timeline (org.sagebionetworks.bridge.models.schedules2.timelines.Timeline)3 PreparedStatement (java.sql.PreparedStatement)2 PagedResourceList (org.sagebionetworks.bridge.models.PagedResourceList)2 Session (org.sagebionetworks.bridge.models.schedules2.Session)2 TimeWindow (org.sagebionetworks.bridge.models.schedules2.TimeWindow)2 MetadataContainer (org.sagebionetworks.bridge.models.schedules2.timelines.MetadataContainer)2 TimelineMetadataView (org.sagebionetworks.bridge.models.schedules2.timelines.TimelineMetadataView)2 Study (org.sagebionetworks.bridge.models.studies.Study)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Stopwatch (com.google.common.base.Stopwatch)1 ImmutableList (com.google.common.collect.ImmutableList)1