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.services.IsStaleService; | |
9 | import edu.ucsb.cs156.courses.services.UCSBCurriculumService; | |
10 | import edu.ucsb.cs156.courses.services.jobs.JobContext; | |
11 | import edu.ucsb.cs156.courses.services.jobs.JobContextConsumer; | |
12 | import java.util.List; | |
13 | import java.util.Optional; | |
14 | import lombok.AllArgsConstructor; | |
15 | import lombok.Builder; | |
16 | import lombok.Getter; | |
17 | import lombok.extern.slf4j.Slf4j; | |
18 | ||
19 | @Builder | |
20 | @Getter | |
21 | @AllArgsConstructor | |
22 | @Slf4j | |
23 | public class UpdateCourseDataJob implements JobContextConsumer { | |
24 | private String start_quarterYYYYQ; | |
25 | private String end_quarterYYYYQ; | |
26 | private List<String> subjects; | |
27 | private UCSBCurriculumService ucsbCurriculumService; | |
28 | private ConvertedSectionCollection convertedSectionCollection; | |
29 | private UpdateCollection updateCollection; | |
30 | private IsStaleService isStaleService; | |
31 | private boolean ifStale; | |
32 | ||
33 | @Override | |
34 | public void accept(JobContext ctx) throws Exception { | |
35 | List<Quarter> quarters = Quarter.quarterList(start_quarterYYYYQ, end_quarterYYYYQ); | |
36 | for (Quarter quarter : quarters) { | |
37 | String quarterYYYYQ = quarter.getYYYYQ(); | |
38 | for (String subjectArea : subjects) { | |
39 | boolean isStale = isStaleService.isStale(subjectArea, quarterYYYYQ); | |
40 |
1
1. accept : negated conditional → KILLED |
if (ifStale) { |
41 |
1
1. accept : negated conditional → KILLED |
if (!isStale) { |
42 | ||
43 |
1
1. accept : removed call to edu/ucsb/cs156/courses/services/jobs/JobContext::log → KILLED |
ctx.log("Data is not stale for [" + subjectArea + " " + quarterYYYYQ + "]"); |
44 | continue; | |
45 | } | |
46 | } | |
47 |
1
1. accept : removed call to edu/ucsb/cs156/courses/jobs/UpdateCourseDataJob::updateCourses → KILLED |
updateCourses(ctx, quarterYYYYQ, subjectArea); |
48 | } | |
49 | } | |
50 | } | |
51 | ||
52 | public Update updateUpdatesCollection( | |
53 | String quarterYYYYQ, String subjectArea, int saved, int updated, int errors) { | |
54 | Update update = new Update(null, subjectArea, quarterYYYYQ, saved, updated, errors, null); | |
55 | Update savedUpdate = updateCollection.save(update); | |
56 |
1
1. updateUpdatesCollection : replaced return value with null for edu/ucsb/cs156/courses/jobs/UpdateCourseDataJob::updateUpdatesCollection → KILLED |
return savedUpdate; |
57 | } | |
58 | ||
59 | public void updateCourses(JobContext ctx, String quarterYYYYQ, String subjectArea) | |
60 | throws Exception { | |
61 |
1
1. updateCourses : removed call to edu/ucsb/cs156/courses/services/jobs/JobContext::log → KILLED |
ctx.log("Updating courses for [" + subjectArea + " " + quarterYYYYQ + "]"); |
62 | ||
63 | List<ConvertedSection> convertedSections = | |
64 | ucsbCurriculumService.getConvertedSections(subjectArea, quarterYYYYQ, "A"); | |
65 | ||
66 |
1
1. updateCourses : removed call to edu/ucsb/cs156/courses/services/jobs/JobContext::log → KILLED |
ctx.log("Found " + convertedSections.size() + " sections"); |
67 |
1
1. updateCourses : removed call to edu/ucsb/cs156/courses/services/jobs/JobContext::log → KILLED |
ctx.log("Storing in MongoDB Collection..."); |
68 | ||
69 | int newSections = 0; | |
70 | int updatedSections = 0; | |
71 | int errors = 0; | |
72 | ||
73 | for (ConvertedSection section : convertedSections) { | |
74 | try { | |
75 | String quarter = section.getCourseInfo().getQuarter(); | |
76 | String enrollCode = section.getSection().getEnrollCode(); | |
77 | Optional<ConvertedSection> optionalSection = | |
78 | convertedSectionCollection.findOneByQuarterAndEnrollCode(quarter, enrollCode); | |
79 |
1
1. updateCourses : negated conditional → KILLED |
if (optionalSection.isPresent()) { |
80 | ConvertedSection existingSection = optionalSection.get(); | |
81 |
1
1. updateCourses : removed call to edu/ucsb/cs156/courses/documents/ConvertedSection::setCourseInfo → KILLED |
existingSection.setCourseInfo(section.getCourseInfo()); |
82 |
1
1. updateCourses : removed call to edu/ucsb/cs156/courses/documents/ConvertedSection::setSection → KILLED |
existingSection.setSection(section.getSection()); |
83 | convertedSectionCollection.save(existingSection); | |
84 |
1
1. updateCourses : Changed increment from 1 to -1 → KILLED |
updatedSections++; |
85 | } else { | |
86 | convertedSectionCollection.save(section); | |
87 |
1
1. updateCourses : Changed increment from 1 to -1 → KILLED |
newSections++; |
88 | } | |
89 | } catch (Exception e) { | |
90 |
1
1. updateCourses : removed call to edu/ucsb/cs156/courses/services/jobs/JobContext::log → KILLED |
ctx.log("Error saving section: " + e.getMessage()); |
91 |
1
1. updateCourses : Changed increment from 1 to -1 → KILLED |
errors++; |
92 | } | |
93 | } | |
94 | ||
95 | Update savedUpdate = | |
96 | updateUpdatesCollection(quarterYYYYQ, subjectArea, newSections, updatedSections, errors); | |
97 | ||
98 |
1
1. updateCourses : removed call to edu/ucsb/cs156/courses/services/jobs/JobContext::log → KILLED |
ctx.log( |
99 | String.format( | |
100 | "%d new sections saved, %d sections updated, %d errors, last update: %s", | |
101 | newSections, updatedSections, errors, savedUpdate.getLastUpdate())); | |
102 |
1
1. updateCourses : removed call to edu/ucsb/cs156/courses/services/jobs/JobContext::log → KILLED |
ctx.log("Saved update: " + savedUpdate); |
103 |
1
1. updateCourses : removed call to edu/ucsb/cs156/courses/services/jobs/JobContext::log → KILLED |
ctx.log("Courses for [" + subjectArea + " " + quarterYYYYQ + "] have been updated"); |
104 | } | |
105 | } | |
Mutations | ||
40 |
1.1 |
|
41 |
1.1 |
|
43 |
1.1 |
|
47 |
1.1 |
|
56 |
1.1 |
|
61 |
1.1 |
|
66 |
1.1 |
|
67 |
1.1 |
|
79 |
1.1 |
|
81 |
1.1 |
|
82 |
1.1 |
|
84 |
1.1 |
|
87 |
1.1 |
|
90 |
1.1 |
|
91 |
1.1 |
|
98 |
1.1 |
|
102 |
1.1 |
|
103 |
1.1 |