UpdateCoursesService.java

1
package edu.ucsb.cs156.courses.services;
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.repositories.EnrollmentDataPointRepository;
8
import edu.ucsb.cs156.jobs.services.JobContext;
9
import java.util.List;
10
import java.util.Optional;
11
import lombok.extern.slf4j.Slf4j;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.scheduling.annotation.Async;
14
import org.springframework.stereotype.Service;
15
16
@Service
17
@Slf4j
18
public class UpdateCoursesService {
19
  @Autowired ConvertedSectionCollection convertedSectionCollection;
20
  @Autowired EnrollmentDataPointRepository enrollmentDataPointRepository;
21
  @Autowired UCSBAPIQuarterService ucsbapiQuarterService;
22
  @Autowired UpdateCollection updateCollection;
23
24
  /**
25
   * Insert or update each section in the convertedSections collection.
26
   *
27
   * @param fromJob true when called from UpdateCourseDataJob, which fetches a complete
28
   *     (subjectArea, quarter) with level "A". Only then do we (a) record enrollment data points,
29
   *     so enrollment history gets one point per scheduled run rather than one per user search, and
30
   *     (b) save an Update record, which IsStaleService reads as "this subject/quarter is fully
31
   *     loaded". User searches pass false: the main page only ever sends a partial level (L, S, U
32
   *     or G), and GE searches span many subjects, so neither may mark a subject/quarter as loaded.
33
   * @return the Update summarizing this run; saved to the updates collection only when fromJob
34
   */
35
  public synchronized Update upsert(
36
      List<ConvertedSection> convertedSections,
37
      JobContext ctx,
38
      String quarterYYYYQ,
39
      String subjectArea,
40
      boolean fromJob)
41
      throws Exception {
42
43
    int saved = 0, updated = 0, errors = 0;
44
    // Short-circuit so user searches never pay for the registration-pass lookup.
45 1 1. upsert : negated conditional → KILLED
    boolean recordEnrollment =
46 1 1. upsert : negated conditional → KILLED
        fromJob && ucsbapiQuarterService.isQuarterInRegistrationPass(quarterYYYYQ);
47
48
    for (ConvertedSection section : convertedSections) {
49 2 1. upsert : negated conditional → KILLED
2. upsert : removed call to edu/ucsb/cs156/jobs/services/JobContext::checkCancellation → KILLED
      if (ctx != null) ctx.checkCancellation();
50
      try {
51
        String quarter = section.getCourseInfo().getQuarter();
52
        String enrollCode = section.getSection().getEnrollCode();
53
        Optional<ConvertedSection> optionalSection =
54
            convertedSectionCollection.findOneByQuarterAndEnrollCode(quarter, enrollCode);
55 1 1. upsert : negated conditional → KILLED
        if (optionalSection.isPresent()) {
56
          ConvertedSection existingSection = optionalSection.get();
57 1 1. upsert : removed call to edu/ucsb/cs156/courses/documents/ConvertedSection::setCourseInfo → KILLED
          existingSection.setCourseInfo(section.getCourseInfo());
58 1 1. upsert : removed call to edu/ucsb/cs156/courses/documents/ConvertedSection::setSection → KILLED
          existingSection.setSection(section.getSection());
59
          convertedSectionCollection.save(existingSection);
60 1 1. upsert : Changed increment from 1 to -1 → KILLED
          updated++;
61
        } else {
62
          convertedSectionCollection.save(section);
63 1 1. upsert : Changed increment from 1 to -1 → KILLED
          saved++;
64
        }
65 1 1. upsert : negated conditional → KILLED
        if (recordEnrollment) {
66
          enrollmentDataPointRepository.save(section.getEnrollmentDataPoint());
67
        }
68
      } catch (Exception e) {
69 1 1. upsert : Changed increment from 1 to -1 → KILLED
        errors++;
70
      }
71
    }
72
73
    Update update = new Update(null, subjectArea, quarterYYYYQ, saved, updated, errors, null);
74 2 1. upsert : replaced return value with null for edu/ucsb/cs156/courses/services/UpdateCoursesService::upsert → KILLED
2. upsert : negated conditional → KILLED
    return fromJob ? updateCollection.save(update) : update;
75
  }
76
77
  /**
78
   * Fire-and-forget write-through for the live-API search endpoints. The caller returns
79
   * immediately, so the user's search response is never delayed by Mongo writes.
80
   *
81
   * <p>Runs on lib-jobs' "jobsExecutor" (@EnableAsync is also provided by lib-jobs). That pool has
82
   * 2 threads, so this can overlap a job or another search; upsert() is synchronized so the
83
   * find-then-save can't insert duplicate sections. Waiting on that lock is fine for a cache fill.
84
   *
85
   * <p>Must be called from another bean: Spring applies @Async through a proxy, so a call from
86
   * inside this class would run synchronously.
87
   *
88
   * <p>Exceptions are caught and logged here because an exception thrown from an @Async void method
89
   * has no caller to propagate to.
90
   */
91
  @Async("jobsExecutor")
92
  public void upsertAsync(
93
      List<ConvertedSection> convertedSections, String quarterYYYYQ, String subjectArea) {
94
    try {
95
      upsert(convertedSections, null, quarterYYYYQ, subjectArea, false);
96
    } catch (Exception e) {
97
      log.error("Write-through upsert failed for [{} {}]", subjectArea, quarterYYYYQ, e);
98
    }
99
  }
100
}

Mutations

45

1.1
Location : upsert
Killed by : edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests]/[method:upsert_not_from_job_never_records_enrollment_or_checks_registration_pass()]
negated conditional → KILLED

46

1.1
Location : upsert
Killed by : edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests]/[method:upsert_inserts_sections_that_are_not_in_the_database()]
negated conditional → KILLED

49

1.1
Location : upsert
Killed by : edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests]/[method:upsert_not_from_job_never_records_enrollment_or_checks_registration_pass()]
negated conditional → KILLED

2.2
Location : upsert
Killed by : edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests]/[method:upsert_checks_cancellation_before_each_section()]
removed call to edu/ucsb/cs156/jobs/services/JobContext::checkCancellation → KILLED

55

1.1
Location : upsert
Killed by : edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests]/[method:upsertAsync_upserts_sections_without_saving_an_update_record()]
negated conditional → KILLED

57

1.1
Location : upsert
Killed by : edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests]/[method:upsert_updates_sections_that_are_already_in_the_database()]
removed call to edu/ucsb/cs156/courses/documents/ConvertedSection::setCourseInfo → KILLED

58

1.1
Location : upsert
Killed by : edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests]/[method:upsert_updates_sections_that_are_already_in_the_database()]
removed call to edu/ucsb/cs156/courses/documents/ConvertedSection::setSection → KILLED

60

1.1
Location : upsert
Killed by : edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests]/[method:upsert_not_from_job_saves_sections_but_does_not_save_an_update_record()]
Changed increment from 1 to -1 → KILLED

63

1.1
Location : upsert
Killed by : edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests]/[method:upsert_inserts_sections_that_are_not_in_the_database()]
Changed increment from 1 to -1 → KILLED

65

1.1
Location : upsert
Killed by : edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests]/[method:upsert_not_from_job_never_records_enrollment_or_checks_registration_pass()]
negated conditional → KILLED

69

1.1
Location : upsert
Killed by : edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests]/[method:upsert_counts_errors_and_keeps_going()]
Changed increment from 1 to -1 → KILLED

74

1.1
Location : upsert
Killed by : edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests]/[method:upsert_works_with_a_non_cancelled_job_context()]
replaced return value with null for edu/ucsb/cs156/courses/services/UpdateCoursesService::upsert → KILLED

2.2
Location : upsert
Killed by : edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.courses.services.UpdateCoursesServiceTests]/[method:upsertAsync_upserts_sections_without_saving_an_update_record()]
negated conditional → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1