Skip to content

Commit

Permalink
General: Improve cleanup service (#9851)
Browse files Browse the repository at this point in the history
  • Loading branch information
coolchock authored Dec 20, 2024
1 parent 47e5ebc commit 98c0bcf
Show file tree
Hide file tree
Showing 28 changed files with 1,646 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ WHERE f.result IN (
""")
int deleteFeedbackForOrphanResults();

/**
* Counts {@link Feedback} entries where the associated {@link Result} has no submission and no participation.
*
* @return the number of entities that would be deleted
*/
@Query("""
SELECT COUNT(f)
FROM Feedback f
WHERE f.result IN (
SELECT r
FROM Result r
WHERE r.submission IS NULL
AND r.participation IS NULL
)
""")
int countFeedbackForOrphanResults();

/**
* Deletes {@link Feedback} entries with a {@code null} result.
*
Expand All @@ -56,6 +73,18 @@ WHERE f.result IN (
""")
int deleteOrphanFeedback();

/**
* Counts {@link Feedback} entries with a {@code null} result.
*
* @return the number of entities that would be deleted
*/
@Query("""
SELECT COUNT(f)
FROM Feedback f
WHERE f.result IS NULL
""")
int countOrphanFeedback();

/**
* Deletes {@link Feedback} entries associated with rated {@link Result} that are not the latest rated result
* for a {@link Participation}, within courses conducted between the specified date range.
Expand Down Expand Up @@ -89,6 +118,36 @@ SELECT MAX(r2.id)
""")
int deleteOldFeedbackThatAreNotLatestRatedResultsWhereCourseDateBetween(@Param("deleteFrom") ZonedDateTime deleteFrom, @Param("deleteTo") ZonedDateTime deleteTo);

/**
* Counts {@link Feedback} entries associated with rated {@link Result} that are not the latest rated result
* for a {@link Participation}, within courses conducted between the specified date range.
*
* @param deleteFrom the start date for selecting courses
* @param deleteTo the end date for selecting courses
* @return the number of entities that would be deleted
*/
@Query("""
SELECT COUNT(f)
FROM Feedback f
WHERE f.result IN (
SELECT r
FROM Result r
LEFT JOIN r.participation p
LEFT JOIN p.exercise e
LEFT JOIN e.course c
WHERE r.id NOT IN (
SELECT MAX(r2.id)
FROM Result r2
WHERE r2.participation.id = p.id
AND r2.rated = TRUE
)
AND r.rated = TRUE
AND c.endDate < :deleteTo
AND c.startDate > :deleteFrom
)
""")
int countOldFeedbackThatAreNotLatestRatedResultsWhereCourseDateBetween(@Param("deleteFrom") ZonedDateTime deleteFrom, @Param("deleteTo") ZonedDateTime deleteTo);

/**
* Deletes non-rated {@link Feedback} entries that are not the latest non-rated result, where the associated course's start and end dates
* are between the specified date range.
Expand Down Expand Up @@ -120,4 +179,33 @@ SELECT MAX(r2.id)
)
""")
int deleteOldNonRatedFeedbackWhereCourseDateBetween(@Param("deleteFrom") ZonedDateTime deleteFrom, @Param("deleteTo") ZonedDateTime deleteTo);

/**
* Counts non-rated {@link Feedback} entries that are not the latest non-rated result, where the associated course's start and end dates
* are between the specified date range.
*
* @param deleteFrom the start date for selecting courses
* @param deleteTo the end date for selecting courses
* @return the number of entities that would be deleted
*/
@Query("""
SELECT COUNT(f)
FROM Feedback f
WHERE f.result IN (
SELECT r
FROM Result r
LEFT JOIN r.participation p
LEFT JOIN p.exercise e
LEFT JOIN e.course c
WHERE r.id NOT IN (
SELECT MAX(r2.id)
FROM Result r2
WHERE r2.participation.id = p.id
)
AND r.rated = FALSE
AND c.endDate < :deleteTo
AND c.startDate > :deleteFrom
)
""")
int countOldNonRatedFeedbackWhereCourseDateBetween(@Param("deleteFrom") ZonedDateTime deleteFrom, @Param("deleteTo") ZonedDateTime deleteTo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ WHERE lft.feedback.id IN (
""")
int deleteLongFeedbackTextForOrphanResult();

/**
* Counts {@link LongFeedbackText} entries linked to {@link Feedback} where the associated
* {@link Result} has no participation and no submission.
*
* @return the number of entities that would be deleted
*/
@Query("""
SELECT COUNT(lft)
FROM LongFeedbackText lft
WHERE lft.feedback.id IN (
SELECT f.id
FROM Feedback f
WHERE f.result.participation IS NULL
AND f.result.submission IS NULL
)
""")
int countLongFeedbackTextForOrphanResult();

/**
* Deletes {@link LongFeedbackText} linked to {@link Feedback} with a {@code null} result.
*
Expand All @@ -61,6 +79,22 @@ WHERE lft.feedback IN (
""")
int deleteLongFeedbackTextForOrphanedFeedback();

/**
* Counts {@link LongFeedbackText} linked to {@link Feedback} with a {@code null} result.
*
* @return the number of entities that would be deleted
*/
@Query("""
SELECT COUNT(lft)
FROM LongFeedbackText lft
WHERE lft.feedback IN (
SELECT f
FROM Feedback f
WHERE f.result IS NULL
)
""")
int countLongFeedbackTextForOrphanedFeedback();

/**
* Deletes {@link LongFeedbackText} entries associated with rated {@link Result} that are not the latest rated result
* for a {@link Participation}, within courses conducted between the specified date range.
Expand Down Expand Up @@ -95,6 +129,37 @@ SELECT MAX(r2.id)
""")
int deleteLongFeedbackTextForRatedResultsWhereCourseDateBetween(@Param("deleteFrom") ZonedDateTime deleteFrom, @Param("deleteTo") ZonedDateTime deleteTo);

/**
* Counts {@link LongFeedbackText} entries associated with rated {@link Result} that are not the latest rated result
* for a {@link Participation}, within courses conducted between the specified date range.
*
* @param deleteFrom the start date for selecting courses
* @param deleteTo the end date for selecting courses
* @return the number of entities that would be deleted
*/
@Query("""
SELECT COUNT(lft)
FROM LongFeedbackText lft
WHERE lft.feedback IN (
SELECT f
FROM Feedback f
LEFT JOIN f.result r
LEFT JOIN r.participation p
LEFT JOIN p.exercise e
LEFT JOIN e.course c
WHERE f.result.id NOT IN (
SELECT MAX(r2.id)
FROM Result r2
WHERE r2.participation.id = p.id
AND r2.rated = TRUE
)
AND c.endDate < :deleteTo
AND c.startDate > :deleteFrom
AND r.rated = TRUE
)
""")
int countLongFeedbackTextForRatedResultsWhereCourseDateBetween(@Param("deleteFrom") ZonedDateTime deleteFrom, @Param("deleteTo") ZonedDateTime deleteTo);

/**
* Deletes {@link LongFeedbackText} entries linked to non-rated {@link Feedback} that are not the latest non-rated result where the associated course's start
* and end dates are between the specified date range.
Expand Down Expand Up @@ -128,4 +193,35 @@ SELECT MAX(r2.id)
)
""")
int deleteLongFeedbackTextForNonRatedResultsWhereCourseDateBetween(@Param("deleteFrom") ZonedDateTime deleteFrom, @Param("deleteTo") ZonedDateTime deleteTo);

/**
* Counts {@link LongFeedbackText} entries linked to non-rated {@link Feedback} that are not the latest non-rated result where the associated course's start
* and end dates are between the specified date range.
*
* @param deleteFrom the start date for selecting courses
* @param deleteTo the end date for selecting courses
* @return the number of entities that would be deleted
*/
@Query("""
SELECT COUNT(lft)
FROM LongFeedbackText lft
WHERE lft.feedback IN (
SELECT f
FROM Feedback f
LEFT JOIN f.result r
LEFT JOIN r.participation p
LEFT JOIN p.exercise e
LEFT JOIN e.course c
WHERE f.result.id NOT IN (
SELECT MAX(r2.id)
FROM Result r2
WHERE r2.participation.id = p.id
AND r2.rated = FALSE
)
AND r.rated = FALSE
AND c.endDate < :deleteTo
AND c.startDate > :deleteFrom
)
""")
int countLongFeedbackTextForNonRatedResultsWhereCourseDateBetween(@Param("deleteFrom") ZonedDateTime deleteFrom, @Param("deleteTo") ZonedDateTime deleteTo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,37 @@ SELECT MAX(r2.id)
""")
int deleteParticipantScoresForNonLatestLastResultsWhereCourseDateBetween(@Param("deleteFrom") ZonedDateTime deleteFrom, @Param("deleteTo") ZonedDateTime deleteTo);

/**
* Counts {@link ParticipantScore} entries where the associated {@link Result} is not the latest rated result
* for a {@link Participation}, within courses conducted between the specified date range.
*
* @param deleteFrom the start date for selecting courses
* @param deleteTo the end date for selecting courses
* @return the number of entities that would be deleted upon execution of the cleanup operation
*
*/
@Query("""
SELECT COUNT(ps)
FROM ParticipantScore ps
WHERE ps.lastResult IN (
SELECT r
FROM Result r
LEFT JOIN r.participation p
LEFT JOIN p.exercise e
LEFT JOIN e.course c
WHERE r.id NOT IN (
SELECT MAX(r2.id)
FROM Result r2
WHERE r2.participation.id = p.id
AND r2.rated = TRUE
)
AND r.rated = TRUE
AND c.endDate < :deleteTo
AND c.startDate > :deleteFrom
)
""")
int countParticipantScoresForNonLatestLastResultsWhereCourseDateBetween(@Param("deleteFrom") ZonedDateTime deleteFrom, @Param("deleteTo") ZonedDateTime deleteTo);

/**
* Deletes {@link ParticipantScore} entries where the associated last rated {@link Result} is not the latest rated result
* for a {@link Participation}, within courses conducted between the specified date range.
Expand Down Expand Up @@ -90,6 +121,36 @@ SELECT MAX(r2.id)
""")
int deleteParticipantScoresForNonLatestLastRatedResultsWhereCourseDateBetween(@Param("deleteFrom") ZonedDateTime deleteFrom, @Param("deleteTo") ZonedDateTime deleteTo);

/**
* Counts {@link ParticipantScore} entries where the associated last rated {@link Result} is not the latest rated result
* for a {@link Participation}, within courses conducted between the specified date range.
*
* @param deleteFrom the start date for selecting courses
* @param deleteTo the end date for selecting courses
* @return the number of entities that would be deleted upon execution of the cleanup operation
*/
@Query("""
SELECT COUNT(ps)
FROM ParticipantScore ps
WHERE ps.lastRatedResult IN (
SELECT r
FROM Result r
LEFT JOIN r.participation p
LEFT JOIN p.exercise e
LEFT JOIN e.course c
WHERE r.id NOT IN (
SELECT MAX(r2.id)
FROM Result r2
WHERE r2.participation.id = p.id
AND r2.rated = TRUE
)
AND r.rated = TRUE
AND c.endDate < :deleteTo
AND c.startDate > :deleteFrom
)
""")
int countParticipantScoresForNonLatestLastRatedResultsWhereCourseDateBetween(@Param("deleteFrom") ZonedDateTime deleteFrom, @Param("deleteTo") ZonedDateTime deleteTo);

/**
* Deletes {@link ParticipantScore} entries where the associated {@link Result} is not the latest result and is non-rated,
* and the course's start and end dates are between the specified date range.
Expand Down Expand Up @@ -122,6 +183,36 @@ SELECT MAX(r2.id)
""")
int deleteParticipantScoresForLatestNonRatedResultsWhereCourseDateBetween(@Param("deleteFrom") ZonedDateTime deleteFrom, @Param("deleteTo") ZonedDateTime deleteTo);

/**
* Counts {@link ParticipantScore} entries where the associated {@link Result} is not the latest result and is non-rated,
* and the course's start and end dates are between the specified date range.
*
* @param deleteFrom the start date for selecting courses
* @param deleteTo the end date for selecting courses
* @return the number of entities that would be deleted upon execution of the cleanup operation
*/
@Query("""
SELECT COUNT(ps)
FROM ParticipantScore ps
WHERE ps.lastResult IN (
SELECT r
FROM Result r
LEFT JOIN r.participation p
LEFT JOIN p.exercise e
LEFT JOIN e.course c
WHERE r.id NOT IN (
SELECT MAX(r2.id)
FROM Result r2
WHERE r2.participation.id = p.id
AND r2.rated = FALSE
)
AND r.rated = FALSE
AND c.endDate < :deleteTo
AND c.startDate > :deleteFrom
)
""")
int countParticipantScoresForLatestNonRatedResultsWhereCourseDateBetween(@Param("deleteFrom") ZonedDateTime deleteFrom, @Param("deleteTo") ZonedDateTime deleteTo);

/**
* Deletes {@link ParticipantScore} entries where the associated {@link Result} is not latest and is non-rated, even though
* it is marked as the last rated result, to prevent potential integrity violations.
Expand Down Expand Up @@ -155,4 +246,34 @@ SELECT MAX(r2.id)
)
""")
int deleteParticipantScoresForNonRatedResultsWhereCourseDateBetween(@Param("deleteFrom") ZonedDateTime deleteFrom, @Param("deleteTo") ZonedDateTime deleteTo);

/**
* Counts {@link ParticipantScore} entries where the associated {@link Result} is not latest and is non-rated, even though
* it is marked as the last rated result.
*
* @param deleteFrom the start date for selecting courses
* @param deleteTo the end date for selecting courses
* @return the number of entities that would be deleted upon execution of the cleanup operation
*/
@Query("""
SELECT COUNT(ps)
FROM ParticipantScore ps
WHERE ps.lastRatedResult IN (
SELECT r
FROM Result r
LEFT JOIN r.participation p
LEFT JOIN p.exercise e
LEFT JOIN e.course c
WHERE r.id NOT IN (
SELECT MAX(r2.id)
FROM Result r2
WHERE r2.participation.id = p.id
AND r2.rated = FALSE
)
AND r.rated = FALSE
AND c.endDate < :deleteTo
AND c.startDate > :deleteFrom
)
""")
int countParticipantScoresForNonRatedResultsWhereCourseDateBetween(@Param("deleteFrom") ZonedDateTime deleteFrom, @Param("deleteTo") ZonedDateTime deleteTo);
}
Loading

0 comments on commit 98c0bcf

Please sign in to comment.