| 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 |
1
1. updateUpdatesCollection : negated conditional → KILLED |
Update update = |
| 72 | new Update(null, subjectArea, quarterYYYYQ, saved, updated, errors, null, errors == 0); | |
| 73 | Update savedUpdate = updateCollection.save(update); | |
| 74 |
1
1. updateUpdatesCollection : replaced return value with null for edu/ucsb/cs156/courses/jobs/UpdateCourseDataJob::updateUpdatesCollection → KILLED |
return savedUpdate; |
| 75 | } | |
| 76 | ||
| 77 | public void updateCourses(JobContext ctx, String quarterYYYYQ, String subjectArea) | |
| 78 | throws Exception { | |
| 79 |
1
1. updateCourses : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Updating courses for [" + subjectArea + " " + quarterYYYYQ + "]"); |
| 80 | ||
| 81 | List<ConvertedSection> convertedSections = | |
| 82 | ucsbCurriculumService.getConvertedSections(subjectArea, quarterYYYYQ, "A"); | |
| 83 | ||
| 84 | int newSections = 0; | |
| 85 | int updatedSections = 0; | |
| 86 | int errors = 0; | |
| 87 | boolean isInRegistrationPass = ucsbapiQuarterService.isQuarterInRegistrationPass(quarterYYYYQ); | |
| 88 | ||
| 89 | for (ConvertedSection section : convertedSections) { | |
| 90 |
1
1. updateCourses : removed call to edu/ucsb/cs156/jobs/services/JobContext::checkCancellation → KILLED |
ctx.checkCancellation(); |
| 91 | String enrollCode = "unknown"; | |
| 92 | try { | |
| 93 | enrollCode = section.getSection().getEnrollCode(); | |
| 94 | String quarter = section.getCourseInfo().getQuarter(); | |
| 95 | Optional<ConvertedSection> optionalSection = | |
| 96 | convertedSectionCollection.findOneByQuarterAndEnrollCode(quarter, enrollCode); | |
| 97 |
1
1. updateCourses : negated conditional → KILLED |
if (optionalSection.isPresent()) { |
| 98 | ConvertedSection existingSection = optionalSection.get(); | |
| 99 |
1
1. updateCourses : removed call to edu/ucsb/cs156/courses/documents/ConvertedSection::setCourseInfo → KILLED |
existingSection.setCourseInfo(section.getCourseInfo()); |
| 100 |
1
1. updateCourses : removed call to edu/ucsb/cs156/courses/documents/ConvertedSection::setSection → KILLED |
existingSection.setSection(section.getSection()); |
| 101 | convertedSectionCollection.save(existingSection); | |
| 102 |
1
1. updateCourses : Changed increment from 1 to -1 → KILLED |
updatedSections++; |
| 103 | } else { | |
| 104 | convertedSectionCollection.save(section); | |
| 105 |
1
1. updateCourses : Changed increment from 1 to -1 → KILLED |
newSections++; |
| 106 | } | |
| 107 |
2
1. updateCourses : negated conditional → KILLED 2. updateCourses : negated conditional → KILLED |
if (isInRegistrationPass && section.getSection().getEnrolledTotal() != null) { |
| 108 | enrollmentDataPointRepository.save(section.getEnrollmentDataPoint()); | |
| 109 | } | |
| 110 | } catch (Exception e) { | |
| 111 |
1
1. updateCourses : Changed increment from 1 to -1 → KILLED |
errors++; |
| 112 | String message = | |
| 113 | String.format( | |
| 114 | "Failed updating subject=%s quarter=%s enrollCode=%s: %s", | |
| 115 | subjectArea, quarterYYYYQ, enrollCode, e); | |
| 116 |
1
1. updateCourses : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log(message); |
| 117 | log.error(message, e); | |
| 118 | } | |
| 119 | } | |
| 120 | ||
| 121 | Update savedUpdate = | |
| 122 | updateUpdatesCollection(quarterYYYYQ, subjectArea, newSections, updatedSections, errors); | |
| 123 | ||
| 124 |
1
1. updateCourses : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 125 | String.format( | |
| 126 | "%d new sections saved, %d sections updated, %d errors, last update: %s", | |
| 127 | newSections, updatedSections, errors, savedUpdate.getLastUpdate())); | |
| 128 | ||
| 129 |
1
1. updateCourses : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Saved update: " + savedUpdate); |
| 130 | } | |
| 131 | } | |
Mutations | ||
| 43 |
1.1 |
|
| 55 |
1.1 |
|
| 57 |
1.1 |
|
| 58 |
1.1 |
|
| 62 |
1.1 |
|
| 63 |
1.1 |
|
| 66 |
1.1 |
|
| 71 |
1.1 |
|
| 74 |
1.1 |
|
| 79 |
1.1 |
|
| 90 |
1.1 |
|
| 97 |
1.1 |
|
| 99 |
1.1 |
|
| 100 |
1.1 |
|
| 102 |
1.1 |
|
| 105 |
1.1 |
|
| 107 |
1.1 2.2 |
|
| 111 |
1.1 |
|
| 116 |
1.1 |
|
| 124 |
1.1 |
|
| 129 |
1.1 |