use of org.sagebionetworks.bridge.models.schedules2.adherence.eventstream.EventStreamWindow in project BridgeServer2 by Sage-Bionetworks.
the class StudyAdherenceReportGeneratorTest method timestampBeforeSchedule.
@Test
public void timestampBeforeSchedule() throws Exception {
AdherenceState state = createAdherenceState().withNow(DateTime.parse("2020-01-01T00:00:00.000-08:00")).build();
StudyAdherenceReport report = INSTANCE.generate(state);
for (StudyReportWeek week : report.getWeeks()) {
for (List<EventStreamDay> days : week.getByDayEntries().values()) {
for (EventStreamDay oneDay : days) {
for (EventStreamWindow win : oneDay.getTimeWindows()) {
assertEquals(win.getState(), NOT_YET_AVAILABLE);
}
}
}
}
}
use of org.sagebionetworks.bridge.models.schedules2.adherence.eventstream.EventStreamWindow in project BridgeServer2 by Sage-Bionetworks.
the class StudyAdherenceReportGeneratorTest method timestampAfterSchedule.
@Test
public void timestampAfterSchedule() throws Exception {
AdherenceState state = createAdherenceState().withNow(DateTime.parse("2022-05-01T00:00:00.000-08:00")).build();
StudyAdherenceReport report = INSTANCE.generate(state);
for (StudyReportWeek week : report.getWeeks()) {
for (List<EventStreamDay> days : week.getByDayEntries().values()) {
for (EventStreamDay oneDay : days) {
for (EventStreamWindow win : oneDay.getTimeWindows()) {
assertEquals(win.getState(), EXPIRED);
}
}
}
}
}
use of org.sagebionetworks.bridge.models.schedules2.adherence.eventstream.EventStreamWindow in project BridgeServer2 by Sage-Bionetworks.
the class StudyAdherenceReportGeneratorTest method dayHasNoUnnecessaryFieldsFilledOut.
@Test
public void dayHasNoUnnecessaryFieldsFilledOut() throws Exception {
StudyAdherenceReport report = createReport();
for (StudyReportWeek week : report.getWeeks()) {
for (List<EventStreamDay> days : week.getByDayEntries().values()) {
for (EventStreamDay oneDay : days) {
assertNull(oneDay.getStudyBurstId());
assertNull(oneDay.getStudyBurstNum());
assertNull(oneDay.getSessionName());
assertNull(oneDay.getWeek());
assertNull(oneDay.getStartDay());
for (EventStreamWindow window : oneDay.getTimeWindows()) {
assertNull(window.getEndDay());
}
}
}
}
}
use of org.sagebionetworks.bridge.models.schedules2.adherence.eventstream.EventStreamWindow in project BridgeServer2 by Sage-Bionetworks.
the class EventStreamAdherenceReportGenerator method generate.
public EventStreamAdherenceReport generate(AdherenceState state) {
LocalDate earliestDate = LATEST_LOCAL_DATE;
LocalDate latestDate = EARLIEST_LOCAL_DATE;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
String earliestEventId = null;
for (TimelineMetadata meta : state.getMetadata()) {
if (meta.isTimeWindowPersistent()) {
continue;
}
int startDay = meta.getSessionInstanceStartDay();
int endDay = meta.getSessionInstanceEndDay();
String eventId = meta.getSessionStartEventId();
Integer daysSinceEvent = state.getDaysSinceEventById(eventId);
DateTime timestamp = state.getEventTimestampById(eventId);
LocalDate localDate = (timestamp == null) ? null : timestamp.toLocalDate();
LocalDate startDate = (localDate == null) ? null : localDate.plusDays(startDay);
LocalDate endDate = (localDate == null) ? null : localDate.plusDays(endDay);
// Produce one report for each event ID. Create them lazily as we find each eventId;
EventStream stream = state.getEventStreamById(eventId);
stream.setDaysSinceEvent(daysSinceEvent);
stream.setStudyBurstId(meta.getStudyBurstId());
stream.setStudyBurstNum(meta.getStudyBurstNum());
// Get the adherence information for this session instance and derive the state of the session
AdherenceRecord record = state.getAdherenceRecordByGuid(meta.getSessionInstanceGuid());
SessionCompletionState sessionState = calculateSessionState(record, startDay, endDay, daysSinceEvent);
// Retrieve the event stream. All items in this stream start on the same day, but can end on different days
EventStreamDay eventStreamDay = state.getEventStreamDayByKey(meta);
eventStreamDay.setStartDay(startDay);
eventStreamDay.setStartDate(startDate);
// Create a window entry (windows are flattened in the list of timeline metadata records...all session
// records in the metadata table are actually session window records)
EventStreamWindow windowEntry = new EventStreamWindow();
windowEntry.setSessionInstanceGuid(meta.getSessionInstanceGuid());
windowEntry.setTimeWindowGuid(meta.getTimeWindowGuid());
windowEntry.setEndDay(endDay);
windowEntry.setEndDate(endDate);
windowEntry.setState(sessionState);
eventStreamDay.addTimeWindow(windowEntry);
if (startDate != null && startDate.isBefore(earliestDate)) {
earliestDate = startDate;
earliestEventId = eventStreamDay.getStartEventId();
}
if (endDate != null && endDate.isAfter(latestDate)) {
latestDate = endDate;
}
if (min > startDay) {
min = startDay;
}
if (max < endDay) {
max = endDay;
}
}
DayRange dayRange = null;
if (min <= max) {
dayRange = new DayRange(min, max);
}
DateRange dateRange = null;
if (earliestDate.isBefore(latestDate)) {
dateRange = new DateRange(earliestDate, latestDate);
}
EventStreamAdherenceReport report = new EventStreamAdherenceReport();
report.setTimestamp(state.getNow());
report.setClientTimeZone(state.getClientTimeZone());
report.setAdherencePercent(state.calculateAdherencePercentage());
report.setDayRangeOfAllStreams(dayRange);
report.setDateRangeOfAllStreams(dateRange);
report.setEarliestEventId(earliestEventId);
for (String eventId : state.getStreamEventIds()) {
report.getStreams().add(state.getEventStreamById(eventId));
}
report.setProgression(calculateProgress(state, report.getStreams()));
return report;
}
use of org.sagebionetworks.bridge.models.schedules2.adherence.eventstream.EventStreamWindow in project BridgeServer2 by Sage-Bionetworks.
the class StudyAdherenceReportGenerator method generate.
public StudyAdherenceReport generate(AdherenceState state) {
EventStreamAdherenceReport eventReport = EventStreamAdherenceReportGenerator.INSTANCE.generate(state);
LocalDate studyStartDate = getStudyStartDate(state, eventReport);
EventStream studyStream = new EventStream();
Set<String> unsetEventIds = new HashSet<>();
Set<String> unscheduledSessions = new HashSet<>();
Map<String, DateTime> eventTimestamps = new HashMap<>();
LocalDate todayLocal = state.getNow().toLocalDate();
// Remap all the event streams to one event stream — the study event stream
for (EventStream stream : eventReport.getStreams()) {
for (List<EventStreamDay> days : stream.getByDayEntries().values()) {
for (EventStreamDay oneDay : days) {
if (oneDay.getStartDate() != null) {
// Map this stream into the study stream
Integer numDays = Days.daysBetween(studyStartDate, oneDay.getStartDate()).getDays();
studyStream.addEntry(numDays, oneDay);
String eventId = oneDay.getStartEventId();
eventTimestamps.put(eventId, state.getEventTimestampById(eventId));
} else {
// Note for the report the events and sessions that are not applicable to this user
unsetEventIds.add(oneDay.getStartEventId());
unscheduledSessions.add(oneDay.getSessionName());
}
}
}
}
// study-wide progress
ParticipantStudyProgress progression = calculateProgress(state, ImmutableList.of(studyStream));
// Break this study stream down into weeks. TreeMap sorts the weeks by week number
Map<Integer, StudyReportWeek> weekMap = new TreeMap<>();
for (Map.Entry<Integer, List<EventStreamDay>> entry : studyStream.getByDayEntries().entrySet()) {
int week = entry.getKey() / 7;
// Day of week can be negative if the week was negative... just flip it
int dayOfWeek = Math.abs(entry.getKey() % 7);
StudyReportWeek oneWeek = weekMap.get(week);
if (oneWeek == null) {
oneWeek = new StudyReportWeek();
oneWeek.setStartDate(studyStartDate.plusDays(week * 7));
oneWeek.setWeekInStudy(week + 1);
weekMap.put(week, oneWeek);
}
List<EventStreamDay> days = oneWeek.getByDayEntries().get(dayOfWeek);
days.addAll(entry.getValue());
}
;
Collection<StudyReportWeek> weeks = weekMap.values();
StudyReportWeek currentWeek = null;
for (StudyReportWeek oneWeek : weeks) {
calculateRowsAndLabels(oneWeek, todayLocal);
// If there’s a day in this week that is “today”, set a flag for all day entries on that day
LocalDate firstDayOfWeek = oneWeek.getStartDate();
LocalDate lastDayOfWeek = oneWeek.getStartDate().plusDays(7);
if (BridgeUtils.isLocalDateInRange(firstDayOfWeek, lastDayOfWeek, todayLocal)) {
currentWeek = oneWeek;
for (List<EventStreamDay> days : oneWeek.getByDayEntries().values()) {
for (EventStreamDay oneDay : days) {
if (oneDay.getStartDate().isEqual(todayLocal)) {
days.forEach(day -> day.setToday(true));
}
}
}
}
// current one) but not the weeks after todayLocal.
if (BridgeUtils.isLocalDateInRange(oneWeek.getStartDate(), null, todayLocal)) {
int weekAdh = AdherenceUtils.calculateAdherencePercentage(oneWeek.getByDayEntries());
oneWeek.setAdherencePercent(weekAdh);
}
}
Integer adherence = null;
if (progression != ParticipantStudyProgress.NO_SCHEDULE) {
adherence = calculateAdherencePercentage(ImmutableList.of(studyStream));
}
NextActivity nextActivity = null;
if (currentWeek == null) {
nextActivity = getNextActivity(weeks, todayLocal);
}
DateRange dateRange = null;
if (eventReport.getDateRangeOfAllStreams() != null) {
dateRange = new DateRange(studyStartDate, eventReport.getDateRangeOfAllStreams().getEndDate());
}
for (StudyReportWeek oneWeek : weeks) {
for (List<EventStreamDay> days : oneWeek.getByDayEntries().values()) {
for (EventStreamDay oneDay : days) {
oneDay.setStudyBurstId(null);
oneDay.setStudyBurstNum(null);
oneDay.setSessionName(null);
oneDay.setWeek(null);
oneDay.setStartDay(null);
for (EventStreamWindow window : oneDay.getTimeWindows()) {
window.setEndDay(null);
// This cannot be removed, or the window will be removed from persisted collection
// window.setTimeWindowGuid(null);
}
}
}
}
StudyAdherenceReport report = new StudyAdherenceReport();
report.setDateRange(dateRange);
report.setWeeks(weeks);
report.setCurrentWeek(currentWeek);
report.setNextActivity(nextActivity);
report.setProgression(progression);
report.setAdherencePercent(adherence);
report.setEventTimestamps(eventTimestamps);
report.setUnsetEventIds(unsetEventIds);
report.setUnscheduledSessions(unscheduledSessions);
return report;
}
Aggregations