@@ -48,16 +48,16 @@ public class UpsertOccurredObservationsCron {
4848 this .calendarService = calendarService ;
4949 }
5050
51- @ Scheduled (cron = "0 */5 * * * ?" ) //every 5min
52- protected void upsertOccurredObservations (){
51+ @ Scheduled (cron = "0 */5 * * * ?" ) //every 5min
52+ protected void upsertOccurredObservations () {
5353 try (var stream = studyService .getStudiesByStates (Study .Status .ACTIVE_STATES )) {
5454 stream .forEach (this ::upsertOccurredObservations );
5555 }
5656 }
5757
5858 private void upsertOccurredObservations (Study study ) {
5959 try (var ctx = LoggingUtils .createContext ()) {
60- if (study == null || !Study .Status .ACTIVE_STATES .contains (study .getStudyState ())) {
60+ if (study == null || !Study .Status .ACTIVE_STATES .contains (study .getStudyState ())) {
6161 return ; //nothing to do
6262 }
6363 ctx .putStudy (study );
@@ -69,22 +69,22 @@ private void upsertOccurredObservations(Study study) {
6969 Instant current = Instant .now ().plus (1 , ChronoUnit .MINUTES ).truncatedTo (ChronoUnit .MINUTES );
7070 LOGGER .debug ("Upsert Occurred Observations for Study(id: {}) " , study .getStudyId ());
7171 participants .stream ()
72- .filter (participant -> participant .getStatus () == Participant .Status .ACTIVE )
73- .forEach (participant -> {
74- Instant lastOccurredObservation = occurredObservationService .getLatestStartTime (study .getStudyId (), participant .getParticipantId ());
75- LOGGER .debug ("Upsert Occurred Observations for Study(id: {}) Participant(id: {}) and timeperiode(start:{}, end:{})" , study .getStudyId (), participant .getParticipantId (), lastOccurredObservation , current );
76- ctx .putParticipant (participant );
77- //NOTE: from, to are currently not supported
78- var timeline = calendarService .getTimeline (study , participant , null , null , null , null , null );
79- for (ObservationTimelineEvent event : timeline .observationTimelineEvents ()){
80- var start = event .start ().truncatedTo (ChronoUnit .MINUTES );
81- if ((lastOccurredObservation == null || start .isAfter (lastOccurredObservation )) && start .isBefore (current )){
82- LOGGER .trace ("upsert occurred observation for study: {}, observation: {}, participant: {}, start: {}, end: {}" ,
83- study .getStudyId (), event .observationId (), participant .getParticipantId (), event .start (), event .end ());
84- occurredObservationService .upsert (study .getStudyId (), event .observationId (), participant .getParticipantId (), event .start (), event .end ());
85- } //else outside of time range ... ignore
86- }
87- });
72+ .filter (participant -> participant .getStatus () == Participant .Status .ACTIVE )
73+ .forEach (participant -> {
74+ Instant lastOccurredObservation = occurredObservationService .getLatestStartTime (study .getStudyId (), participant .getParticipantId ());
75+ LOGGER .debug ("Upsert Occurred Observations for Study(id: {}) Participant(id: {}) and timeperiode(start:{}, end:{})" , study .getStudyId (), participant .getParticipantId (), lastOccurredObservation , current );
76+ ctx .putParticipant (participant );
77+ //NOTE: from, to are currently not supported
78+ var timeline = calendarService .getTimeline (study , participant , null , null , null , null , null );
79+ for (ObservationTimelineEvent event : timeline .observationTimelineEvents ()) {
80+ var start = event .start ().truncatedTo (ChronoUnit .MINUTES );
81+ if ((lastOccurredObservation == null || start .isAfter (lastOccurredObservation )) && start .isBefore (current )) {
82+ LOGGER .trace ("upsert occurred observation for study: {}, observation: {}, participant: {}, start: {}, end: {}" ,
83+ study .getStudyId (), event .observationId (), participant .getParticipantId (), event .start (), event .end ());
84+ occurredObservationService .upsert (study .getStudyId (), event .observationId (), participant .getParticipantId (), event .start (), event .end ());
85+ } //else outside of time range ... ignore
86+ }
87+ });
8888 } catch (Exception e ) {
8989 LOGGER .warn ("Cannot execute Trigger-Job: {}" , e .getMessage (), e );
9090 }
0 commit comments