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