| 1 | package edu.ucsb.cs156.courses.jobs; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.courses.collections.UpdateCollection; | |
| 4 | import edu.ucsb.cs156.courses.documents.ConvertedSection; | |
| 5 | import edu.ucsb.cs156.courses.documents.Update; | |
| 6 | import edu.ucsb.cs156.courses.models.Quarter; | |
| 7 | import edu.ucsb.cs156.courses.repositories.EnrollmentDataPointRepository; | |
| 8 | import edu.ucsb.cs156.courses.services.CourseCacheService; | |
| 9 | import edu.ucsb.cs156.courses.services.IsStaleService; | |
| 10 | import edu.ucsb.cs156.courses.services.UCSBAPIQuarterService; | |
| 11 | import edu.ucsb.cs156.courses.services.UCSBCurriculumService; | |
| 12 | import edu.ucsb.cs156.jobs.services.JobContext; | |
| 13 | import edu.ucsb.cs156.jobs.services.JobContextConsumer; | |
| 14 | import edu.ucsb.cs156.jobs.services.JobRateLimit; | |
| 15 | import java.util.List; | |
| 16 | import lombok.AllArgsConstructor; | |
| 17 | import lombok.Builder; | |
| 18 | import lombok.Getter; | |
| 19 | import lombok.extern.slf4j.Slf4j; | |
| 20 | ||
| 21 | @Builder | |
| 22 | @Getter | |
| 23 | @AllArgsConstructor | |
| 24 | @Slf4j | |
| 25 | public class UpdateCourseDataJob implements JobContextConsumer { | |
| 26 | ||
| 27 | private String start_quarterYYYYQ; | |
| 28 | private String end_quarterYYYYQ; | |
| 29 | private List<String> subjects; | |
| 30 | private UCSBCurriculumService ucsbCurriculumService; | |
| 31 | private CourseCacheService courseCacheService; | |
| 32 | private UpdateCollection updateCollection; | |
| 33 | private IsStaleService isStaleService; | |
| 34 | private boolean ifStale; | |
| 35 | private EnrollmentDataPointRepository enrollmentDataPointRepository; | |
| 36 | private UCSBAPIQuarterService ucsbapiQuarterService; | |
| 37 | private JobRateLimit jobRateLimit; | |
| 38 | ||
| 39 | @Override | |
| 40 | public void accept(JobContext ctx) throws Exception { | |
| 41 | ||
| 42 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 43 | String.format( | |
| 44 | "Updating courses from %s to %s for %d subjects", | |
| 45 | start_quarterYYYYQ, end_quarterYYYYQ, subjects.size())); | |
| 46 | List<Quarter> quarters = Quarter.quarterList(start_quarterYYYYQ, end_quarterYYYYQ); | |
| 47 | for (Quarter quarter : quarters) { | |
| 48 | String quarterYYYYQ = quarter.getYYYYQ(); | |
| 49 | for (String subjectArea : subjects) { | |
| 50 | // Most (subjectArea, quarterYYYYQ) pairs are not stale on a typical re-run and hit the | |
| 51 | // `continue` below with no ctx.log() call at all -- checkCancellation() gives this loop | |
| 52 | // its own cancellation checkpoint independent of whether an iteration happens to log | |
| 53 | // anything (see UpdateCourseDataJobTests for why a bare log()-only check isn't enough). | |
| 54 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::checkCancellation → KILLED |
ctx.checkCancellation(); |
| 55 | boolean isStale = isStaleService.isStale(subjectArea, quarterYYYYQ); | |
| 56 |
1
1. accept : negated conditional → KILLED |
if (ifStale) { |
| 57 |
1
1. accept : negated conditional → KILLED |
if (!isStale) { |
| 58 | continue; | |
| 59 | } | |
| 60 | } | |
| 61 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobRateLimit::sleep → KILLED |
jobRateLimit.sleep(); |
| 62 |
1
1. accept : removed call to edu/ucsb/cs156/courses/jobs/UpdateCourseDataJob::updateCourses → KILLED |
updateCourses(ctx, quarterYYYYQ, subjectArea); |
| 63 | } | |
| 64 | } | |
| 65 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Finished updating courses"); |
| 66 | } | |
| 67 | ||
| 68 | public Update updateUpdatesCollection( | |
| 69 | String quarterYYYYQ, String subjectArea, int saved, int updated, int errors) { | |
| 70 | Update update = new Update(null, subjectArea, quarterYYYYQ, saved, updated, errors, null); | |
| 71 | Update savedUpdate = updateCollection.save(update); | |
| 72 |
1
1. updateUpdatesCollection : replaced return value with null for edu/ucsb/cs156/courses/jobs/UpdateCourseDataJob::updateUpdatesCollection → KILLED |
return savedUpdate; |
| 73 | } | |
| 74 | ||
| 75 | public void updateCourses(JobContext ctx, String quarterYYYYQ, String subjectArea) | |
| 76 | throws Exception { | |
| 77 |
1
1. updateCourses : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Updating courses for [" + subjectArea + " " + quarterYYYYQ + "]"); |
| 78 | ||
| 79 | List<ConvertedSection> convertedSections = | |
| 80 | ucsbCurriculumService.getConvertedSections(subjectArea, quarterYYYYQ, "A"); | |
| 81 | ||
| 82 | int newSections = 0; | |
| 83 | int updatedSections = 0; | |
| 84 | int errors = 0; | |
| 85 | boolean isInRegistrationPass = ucsbapiQuarterService.isQuarterInRegistrationPass(quarterYYYYQ); | |
| 86 | ||
| 87 | boolean cacheReady = false; | |
| 88 |
1
1. updateCourses : negated conditional → KILLED |
if (!convertedSections.isEmpty()) { |
| 89 |
1
1. updateCourses : removed call to edu/ucsb/cs156/jobs/services/JobContext::checkCancellation → KILLED |
ctx.checkCancellation(); |
| 90 | try { | |
| 91 |
1
1. updateCourses : removed call to edu/ucsb/cs156/courses/services/CourseCacheService::prepareIndex → KILLED |
courseCacheService.prepareIndex(); |
| 92 | cacheReady = true; | |
| 93 | } catch (Exception e) { | |
| 94 | log.error( | |
| 95 | "Unable to prepare course index for subject {} quarter {}", | |
| 96 | subjectArea, | |
| 97 | quarterYYYYQ, | |
| 98 | e); | |
| 99 | } | |
| 100 | } | |
| 101 | ||
| 102 | for (ConvertedSection section : convertedSections) { | |
| 103 | // No branch of this loop ever calls ctx.log() -- all logging happens once, after the loop, | |
| 104 | // in the summary line below. Without its own checkpoint, this loop has zero opportunities | |
| 105 | // to notice a cancellation request no matter how many sections it processes. | |
| 106 |
1
1. updateCourses : removed call to edu/ucsb/cs156/jobs/services/JobContext::checkCancellation → KILLED |
ctx.checkCancellation(); |
| 107 |
1
1. updateCourses : negated conditional → KILLED |
if (!cacheReady) { |
| 108 |
1
1. updateCourses : Changed increment from 1 to -1 → KILLED |
errors++; |
| 109 | continue; | |
| 110 | } | |
| 111 | try { | |
| 112 |
1
1. updateCourses : negated conditional → KILLED |
if (courseCacheService.upsertSection(section)) { |
| 113 |
1
1. updateCourses : Changed increment from 1 to -1 → KILLED |
newSections++; |
| 114 | } else { | |
| 115 |
1
1. updateCourses : Changed increment from 1 to -1 → KILLED |
updatedSections++; |
| 116 | } | |
| 117 |
1
1. updateCourses : negated conditional → KILLED |
if (isInRegistrationPass) { |
| 118 | enrollmentDataPointRepository.save(section.getEnrollmentDataPoint()); | |
| 119 | } | |
| 120 | } catch (Exception e) { | |
| 121 |
1
1. updateCourses : Changed increment from 1 to -1 → KILLED |
errors++; |
| 122 | log.error( | |
| 123 | "Unable to store section for subject {} quarter {}", subjectArea, quarterYYYYQ, e); | |
| 124 | } | |
| 125 | } | |
| 126 | ||
| 127 |
2
1. updateCourses : negated conditional → KILLED 2. updateCourses : negated conditional → KILLED |
if (!cacheReady && !convertedSections.isEmpty()) { |
| 128 |
1
1. updateCourses : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 129 | String.format( | |
| 130 | "Skipped update for [%s %s]: course index preparation failed; %d errors; " | |
| 131 | + "freshness timestamp unchanged", | |
| 132 | subjectArea, quarterYYYYQ, errors)); | |
| 133 | return; | |
| 134 | } | |
| 135 | ||
| 136 | Update savedUpdate = | |
| 137 | updateUpdatesCollection(quarterYYYYQ, subjectArea, newSections, updatedSections, errors); | |
| 138 | ||
| 139 |
1
1. updateCourses : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 140 | String.format( | |
| 141 | "%d new sections saved, %d sections updated, %d errors, last update: %s", | |
| 142 | newSections, updatedSections, errors, savedUpdate.getLastUpdate())); | |
| 143 | ||
| 144 |
1
1. updateCourses : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Saved update: " + savedUpdate); |
| 145 | } | |
| 146 | } | |
Mutations | ||
| 42 |
1.1 |
|
| 54 |
1.1 |
|
| 56 |
1.1 |
|
| 57 |
1.1 |
|
| 61 |
1.1 |
|
| 62 |
1.1 |
|
| 65 |
1.1 |
|
| 72 |
1.1 |
|
| 77 |
1.1 |
|
| 88 |
1.1 |
|
| 89 |
1.1 |
|
| 91 |
1.1 |
|
| 106 |
1.1 |
|
| 107 |
1.1 |
|
| 108 |
1.1 |
|
| 112 |
1.1 |
|
| 113 |
1.1 |
|
| 115 |
1.1 |
|
| 117 |
1.1 |
|
| 121 |
1.1 |
|
| 127 |
1.1 2.2 |
|
| 128 |
1.1 |
|
| 139 |
1.1 |
|
| 144 |
1.1 |