-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Refactor Pyris Event handling
- Loading branch information
Showing
11 changed files
with
227 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/de/tum/cit/aet/artemis/iris/service/pyris/PyrisEventPublisher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package de.tum.cit.aet.artemis.iris.service.pyris; | ||
|
||
import static de.tum.cit.aet.artemis.core.config.Constants.PROFILE_IRIS; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.context.ApplicationEvent; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Service; | ||
|
||
import de.tum.cit.aet.artemis.iris.domain.settings.event.IrisEventType; | ||
import de.tum.cit.aet.artemis.iris.service.pyris.event.CompetencyJolSetEvent; | ||
import de.tum.cit.aet.artemis.iris.service.pyris.event.NewResultEvent; | ||
import de.tum.cit.aet.artemis.iris.service.pyris.event.PyrisEvent; | ||
import de.tum.cit.aet.artemis.iris.service.settings.IrisSettingsService; | ||
import de.tum.cit.aet.artemis.programming.domain.ProgrammingSubmission; | ||
|
||
/** | ||
* Service for publishing Pyris events. | ||
*/ | ||
@Service | ||
@Profile(PROFILE_IRIS) | ||
public class PyrisEventPublisher { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(PyrisEventPublisher.class); | ||
|
||
private final ApplicationEventPublisher eventPublisher; | ||
|
||
private final IrisSettingsService irisSettingsService; | ||
|
||
public PyrisEventPublisher(ApplicationEventPublisher eventPublisher, IrisSettingsService irisSettingsService) { | ||
this.eventPublisher = eventPublisher; | ||
this.irisSettingsService = irisSettingsService; | ||
} | ||
|
||
/** | ||
* Publishes the given event. | ||
* | ||
* @param event the event to publish | ||
*/ | ||
public void publishEvent(PyrisEvent event) { | ||
isEventSupportedElseThrow(event); | ||
if (!isEventEnabled(event)) { | ||
log.debug("Skipping event publication as conditions are not met: {}", event.getClass().getSimpleName()); | ||
return; | ||
} | ||
try { | ||
eventPublisher.publishEvent(event); | ||
} | ||
catch (Exception e) { | ||
log.error("Failed to publish event: {}", event, e); | ||
throw e; | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the given event is supported and throws an exception if it is not. | ||
* | ||
* @param event - the event to check | ||
*/ | ||
private void isEventSupportedElseThrow(ApplicationEvent event) { | ||
if (!isEventSupported(event)) { | ||
throw new UnsupportedPyrisEventException("Event not supported: " + event); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the given event is supported. | ||
* | ||
* @param event the event to publish | ||
*/ | ||
private boolean isEventSupported(ApplicationEvent event) { | ||
return event instanceof PyrisEvent; | ||
} | ||
|
||
private boolean isEventEnabled(PyrisEvent event) { | ||
return switch (event) { | ||
case NewResultEvent newResultEvent -> { | ||
var result = newResultEvent.getResult(); | ||
var submission = result.getSubmission(); | ||
if (submission instanceof ProgrammingSubmission programmingSubmission) { | ||
var programmingExercise = programmingSubmission.getParticipation().getExercise(); | ||
if (programmingSubmission.isBuildFailed()) { | ||
yield irisSettingsService.isActivatedFor(IrisEventType.BUILD_FAILED, programmingExercise); | ||
} | ||
else { | ||
yield irisSettingsService.isActivatedFor(IrisEventType.PROGRESS_STALLED, programmingExercise); | ||
} | ||
} | ||
else { | ||
yield false; | ||
} | ||
} | ||
case CompetencyJolSetEvent competencyJolSetEvent -> { | ||
var competencyJol = competencyJolSetEvent.getCompetencyJol(); | ||
var course = competencyJol.getCompetency().getCourse(); | ||
yield irisSettingsService.isActivatedFor(IrisEventType.JOL, course); | ||
} | ||
default -> { | ||
log.warn("Unknown event type: {}", event.getClass().getSimpleName()); | ||
yield false; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 16 additions & 9 deletions
25
src/main/java/de/tum/cit/aet/artemis/iris/service/pyris/event/CompetencyJolSetEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
package de.tum.cit.aet.artemis.iris.service.pyris.event; | ||
|
||
import java.util.Optional; | ||
|
||
import de.tum.cit.aet.artemis.atlas.domain.competency.CompetencyJol; | ||
import de.tum.cit.aet.artemis.iris.service.session.IrisCourseChatSessionService; | ||
import de.tum.cit.aet.artemis.core.domain.User; | ||
|
||
public class CompetencyJolSetEvent extends PyrisEvent<IrisCourseChatSessionService, CompetencyJol> { | ||
public class CompetencyJolSetEvent extends PyrisEvent { | ||
|
||
private final CompetencyJol eventObject; | ||
private final CompetencyJol competencyJol; | ||
|
||
public CompetencyJolSetEvent(CompetencyJol eventObject) { | ||
if (eventObject == null) { | ||
throw new IllegalArgumentException("Event object cannot be null"); | ||
public CompetencyJolSetEvent(Object source, CompetencyJol competencyJol) { | ||
super(source); | ||
if (competencyJol == null) { | ||
throw new IllegalArgumentException("CompetencyJol cannot be null"); | ||
} | ||
this.eventObject = eventObject; | ||
this.competencyJol = competencyJol; | ||
} | ||
|
||
public CompetencyJol getCompetencyJol() { | ||
return competencyJol; | ||
} | ||
|
||
@Override | ||
public void handleEvent(IrisCourseChatSessionService service) { | ||
service.onJudgementOfLearningSet(eventObject); | ||
public Optional<User> getUser() { | ||
return Optional.ofNullable(competencyJol.getUser()); | ||
} | ||
} |
40 changes: 19 additions & 21 deletions
40
src/main/java/de/tum/cit/aet/artemis/iris/service/pyris/event/NewResultEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,32 @@ | ||
package de.tum.cit.aet.artemis.iris.service.pyris.event; | ||
|
||
import java.util.Optional; | ||
|
||
import de.tum.cit.aet.artemis.assessment.domain.Result; | ||
import de.tum.cit.aet.artemis.iris.service.session.IrisExerciseChatSessionService; | ||
import de.tum.cit.aet.artemis.programming.domain.ProgrammingSubmission; | ||
import de.tum.cit.aet.artemis.core.domain.User; | ||
import de.tum.cit.aet.artemis.programming.domain.ProgrammingExerciseStudentParticipation; | ||
|
||
public class NewResultEvent extends PyrisEvent<IrisExerciseChatSessionService, Result> { | ||
public class NewResultEvent extends PyrisEvent { | ||
|
||
private final Result eventObject; | ||
private final Result result; | ||
|
||
public NewResultEvent(Result eventObject) { | ||
if (eventObject == null) { | ||
throw new IllegalArgumentException("Event object cannot be null"); | ||
public NewResultEvent(Object source, Result result) { | ||
super(source); | ||
if (result == null) { | ||
throw new IllegalArgumentException("Result cannot be null"); | ||
} | ||
this.eventObject = eventObject; | ||
this.result = result; | ||
} | ||
|
||
public Result getResult() { | ||
return result; | ||
} | ||
|
||
@Override | ||
public void handleEvent(IrisExerciseChatSessionService service) { | ||
if (service == null) { | ||
throw new IllegalArgumentException("Service cannot be null"); | ||
} | ||
var submission = eventObject.getSubmission(); | ||
// We only care about programming submissions | ||
if (submission instanceof ProgrammingSubmission programmingSubmission) { | ||
if (programmingSubmission.isBuildFailed()) { | ||
service.onBuildFailure(eventObject); | ||
} | ||
else { | ||
service.onNewResult(eventObject); | ||
} | ||
public Optional<User> getUser() { | ||
if (result.getSubmission().getParticipation() instanceof ProgrammingExerciseStudentParticipation studentParticipation) { | ||
return studentParticipation.getStudent(); | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
22 changes: 16 additions & 6 deletions
22
src/main/java/de/tum/cit/aet/artemis/iris/service/pyris/event/PyrisEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
package de.tum.cit.aet.artemis.iris.service.pyris.event; | ||
|
||
import de.tum.cit.aet.artemis.iris.domain.session.IrisChatSession; | ||
import de.tum.cit.aet.artemis.iris.service.session.AbstractIrisChatSessionService; | ||
import java.util.Optional; | ||
|
||
public abstract class PyrisEvent<S extends AbstractIrisChatSessionService<? extends IrisChatSession>, T> { | ||
import org.springframework.context.ApplicationEvent; | ||
|
||
import de.tum.cit.aet.artemis.core.domain.User; | ||
|
||
/** | ||
* Base class for Pyris events. | ||
*/ | ||
public abstract class PyrisEvent extends ApplicationEvent { | ||
|
||
public PyrisEvent(Object source) { | ||
super(source); | ||
} | ||
|
||
/** | ||
* Handles the event using the given service. | ||
* Returns the user associated with this event. | ||
* | ||
* @param service The service to handle the event for | ||
* @return the user | ||
*/ | ||
public abstract void handleEvent(S service); | ||
public abstract Optional<User> getUser(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.