| 1 | package edu.ucsb.cs156.courses.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 4 | import edu.ucsb.cs156.courses.collections.ConvertedSectionCollection; | |
| 5 | import edu.ucsb.cs156.courses.entities.Job; | |
| 6 | import edu.ucsb.cs156.courses.jobs.TestJob; | |
| 7 | import edu.ucsb.cs156.courses.jobs.UpdateCourseDataJobFactory; | |
| 8 | import edu.ucsb.cs156.courses.jobs.UploadGradeDataJob; | |
| 9 | import edu.ucsb.cs156.courses.jobs.UploadGradeDataJobFactory; | |
| 10 | import edu.ucsb.cs156.courses.repositories.JobsRepository; | |
| 11 | import edu.ucsb.cs156.courses.services.jobs.JobService; | |
| 12 | import io.swagger.v3.oas.annotations.Operation; | |
| 13 | import io.swagger.v3.oas.annotations.Parameter; | |
| 14 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 15 | import java.util.Map; | |
| 16 | import lombok.extern.slf4j.Slf4j; | |
| 17 | import org.springframework.beans.factory.annotation.Autowired; | |
| 18 | import org.springframework.http.HttpStatus; | |
| 19 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 20 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 21 | import org.springframework.web.bind.annotation.GetMapping; | |
| 22 | import org.springframework.web.bind.annotation.PathVariable; | |
| 23 | import org.springframework.web.bind.annotation.PostMapping; | |
| 24 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 25 | import org.springframework.web.bind.annotation.RequestParam; | |
| 26 | import org.springframework.web.bind.annotation.RestController; | |
| 27 | import org.springframework.web.server.ResponseStatusException; | |
| 28 | ||
| 29 | @Tag(name = "Jobs") | |
| 30 | @RequestMapping("/api/jobs") | |
| 31 | @RestController | |
| 32 | @Slf4j | |
| 33 | public class JobsController extends ApiController { | |
| 34 |   @Autowired private JobsRepository jobsRepository; | |
| 35 | ||
| 36 |   @Autowired private ConvertedSectionCollection convertedSectionCollection; | |
| 37 | ||
| 38 |   @Autowired private JobService jobService; | |
| 39 | ||
| 40 |   @Autowired ObjectMapper mapper; | |
| 41 | ||
| 42 |   @Autowired UpdateCourseDataJobFactory updateCourseDataJobFactory; | |
| 43 | ||
| 44 |   @Autowired UploadGradeDataJobFactory updateGradeDataJobFactory; | |
| 45 | ||
| 46 |   @Operation(summary = "List all jobs") | |
| 47 |   @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 48 |   @GetMapping("/all") | |
| 49 |   public Iterable<Job> allJobs() { | |
| 50 |     Iterable<Job> jobs = jobsRepository.findAll(); | |
| 51 | 
1
1. allJobs : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/controllers/JobsController::allJobs → KILLED | 
    return jobs; | 
| 52 |   } | |
| 53 | ||
| 54 |   @Operation(summary = "Delete all job records") | |
| 55 |   @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 56 |   @DeleteMapping("/all") | |
| 57 |   public Map<String, String> deleteAllJobs() { | |
| 58 | 
1
1. deleteAllJobs : removed call to edu/ucsb/cs156/courses/repositories/JobsRepository::deleteAll → KILLED | 
    jobsRepository.deleteAll(); | 
| 59 | 
1
1. deleteAllJobs : replaced return value with Collections.emptyMap for edu/ucsb/cs156/courses/controllers/JobsController::deleteAllJobs → KILLED | 
    return Map.of("message", "All jobs deleted"); | 
| 60 |   } | |
| 61 | ||
| 62 |   @Operation(summary = "Delete specific job record") | |
| 63 |   @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 64 |   @DeleteMapping("") | |
| 65 |   public Map<String, String> deleteAllJobs(@Parameter(name = "id") @RequestParam Long id) { | |
| 66 | 
1
1. deleteAllJobs : negated conditional → KILLED | 
    if (!jobsRepository.existsById(id)) { | 
| 67 | 
1
1. deleteAllJobs : replaced return value with Collections.emptyMap for edu/ucsb/cs156/courses/controllers/JobsController::deleteAllJobs → KILLED | 
      return Map.of("message", String.format("Job with id %d not found", id)); | 
| 68 |     } | |
| 69 | 
1
1. deleteAllJobs : removed call to edu/ucsb/cs156/courses/repositories/JobsRepository::deleteById → KILLED | 
    jobsRepository.deleteById(id); | 
| 70 | 
1
1. deleteAllJobs : replaced return value with Collections.emptyMap for edu/ucsb/cs156/courses/controllers/JobsController::deleteAllJobs → KILLED | 
    return Map.of("message", String.format("Job with id %d deleted", id)); | 
| 71 |   } | |
| 72 | ||
| 73 |   @Operation(summary = "Launch Test Job (click fail if you want to test exception handling)") | |
| 74 |   @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 75 |   @PostMapping("/launch/testjob") | |
| 76 |   public Job launchTestJob( | |
| 77 |       @Parameter(name = "fail") @RequestParam Boolean fail, | |
| 78 |       @Parameter(name = "sleepMs") @RequestParam Integer sleepMs) { | |
| 79 | ||
| 80 |     TestJob testJob = TestJob.builder().fail(fail).sleepMs(sleepMs).build(); | |
| 81 | 
1
1. launchTestJob : replaced return value with null for edu/ucsb/cs156/courses/controllers/JobsController::launchTestJob → KILLED | 
    return jobService.runAsJob(testJob); | 
| 82 |   } | |
| 83 | ||
| 84 |   @Operation(summary = "Launch Job to Update Course Data") | |
| 85 |   @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 86 |   @PostMapping("/launch/updateCourses") | |
| 87 |   public Job launchUpdateCourseDataJob( | |
| 88 |       @Parameter(name = "quarterYYYYQ", description = "quarter (YYYYQ format)") @RequestParam | |
| 89 |           String quarterYYYYQ, | |
| 90 |       @Parameter(name = "subjectArea") @RequestParam String subjectArea, | |
| 91 |       @Parameter( | |
| 92 |               name = "ifStale", | |
| 93 |               description = "true if job should only update when data is stale") | |
| 94 |           @RequestParam(defaultValue = "true") | |
| 95 |           Boolean ifStale) { | |
| 96 | ||
| 97 |     log.info( | |
| 98 |         "launchUpdateCourseDataJob: quarterYYYYQ={}, subjectArea={}, ifStale={}", | |
| 99 |         quarterYYYYQ, | |
| 100 |         subjectArea, | |
| 101 |         ifStale); | |
| 102 |     var job = | |
| 103 |         updateCourseDataJobFactory.createForSubjectAndQuarterAndIfStale( | |
| 104 |             subjectArea, quarterYYYYQ, ifStale); | |
| 105 | ||
| 106 | 
1
1. launchUpdateCourseDataJob : replaced return value with null for edu/ucsb/cs156/courses/controllers/JobsController::launchUpdateCourseDataJob → KILLED | 
    return jobService.runAsJob(job); | 
| 107 |   } | |
| 108 | ||
| 109 |   @Operation(summary = "Launch Job to Update Course Data using Quarter") | |
| 110 |   @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 111 |   @PostMapping("/launch/updateQuarterCourses") | |
| 112 |   public Job launchUpdateCourseDataWithQuarterJob( | |
| 113 |       @Parameter(name = "quarterYYYYQ", description = "quarter (YYYYQ format)") @RequestParam | |
| 114 |           String quarterYYYYQ, | |
| 115 |       @Parameter( | |
| 116 |               name = "ifStale", | |
| 117 |               description = "true if job should only update when data is stale") | |
| 118 |           @RequestParam(defaultValue = "true") | |
| 119 |           Boolean ifStale) { | |
| 120 | ||
| 121 |     var job = updateCourseDataJobFactory.createForQuarter(quarterYYYYQ); | |
| 122 | ||
| 123 | 
1
1. launchUpdateCourseDataWithQuarterJob : replaced return value with null for edu/ucsb/cs156/courses/controllers/JobsController::launchUpdateCourseDataWithQuarterJob → KILLED | 
    return jobService.runAsJob(job); | 
| 124 |   } | |
| 125 | ||
| 126 |   @Operation(summary = "Launch Job to Update Course Data for range of quarters") | |
| 127 |   @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 128 |   @PostMapping("/launch/updateCoursesRangeOfQuarters") | |
| 129 |   public Job launchUpdateCourseDataRangeOfQuartersJob( | |
| 130 |       @Parameter(name = "start_quarterYYYYQ", description = "start quarter (YYYYQ format)") | |
| 131 |           @RequestParam | |
| 132 |           String start_quarterYYYYQ, | |
| 133 |       @Parameter(name = "end_quarterYYYYQ", description = "end quarter (YYYYQ format)") | |
| 134 |           @RequestParam | |
| 135 |           String end_quarterYYYYQ, | |
| 136 |       @Parameter( | |
| 137 |               name = "ifStale", | |
| 138 |               description = "true if job should only update when data is stale") | |
| 139 |           @RequestParam(defaultValue = "true") | |
| 140 |           Boolean ifStale) { | |
| 141 | ||
| 142 |     var job = | |
| 143 |         updateCourseDataJobFactory.createForQuarterRange(start_quarterYYYYQ, end_quarterYYYYQ); | |
| 144 | ||
| 145 | 
1
1. launchUpdateCourseDataRangeOfQuartersJob : replaced return value with null for edu/ucsb/cs156/courses/controllers/JobsController::launchUpdateCourseDataRangeOfQuartersJob → KILLED | 
    return jobService.runAsJob(job); | 
| 146 |   } | |
| 147 | ||
| 148 |   @Operation( | |
| 149 |       summary = "Launch Job to Update Course Data for a range of quarters for a single subject") | |
| 150 |   @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 151 |   @PostMapping("/launch/updateCoursesRangeOfQuartersSingleSubject") | |
| 152 |   public Job launchUpdateCourseDataRangeOfQuartersSingleSubjectJob( | |
| 153 |       @Parameter(name = "subjectArea", description = "subject area") @RequestParam | |
| 154 |           String subjectArea, | |
| 155 |       @Parameter(name = "start_quarterYYYYQ", description = "start quarter (YYYYQ format)") | |
| 156 |           @RequestParam | |
| 157 |           String start_quarterYYYYQ, | |
| 158 |       @Parameter(name = "end_quarterYYYYQ", description = "end quarter (YYYYQ format)") | |
| 159 |           @RequestParam | |
| 160 |           String end_quarterYYYYQ, | |
| 161 |       @Parameter( | |
| 162 |               name = "ifStale", | |
| 163 |               description = "true if job should only update when data is stale") | |
| 164 |           @RequestParam(defaultValue = "true") | |
| 165 |           Boolean ifStale) { | |
| 166 | ||
| 167 |     var job = | |
| 168 |         updateCourseDataJobFactory.createForSubjectAndQuarterRange( | |
| 169 |             subjectArea, start_quarterYYYYQ, end_quarterYYYYQ); | |
| 170 | ||
| 171 | 
1
1. launchUpdateCourseDataRangeOfQuartersSingleSubjectJob : replaced return value with null for edu/ucsb/cs156/courses/controllers/JobsController::launchUpdateCourseDataRangeOfQuartersSingleSubjectJob → KILLED | 
    return jobService.runAsJob(job); | 
| 172 |   } | |
| 173 | ||
| 174 |   @Operation(summary = "Launch Job to update grade history") | |
| 175 |   @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 176 |   @PostMapping("/launch/uploadGradeData") | |
| 177 |   public Job launchUploadGradeData() { | |
| 178 |     UploadGradeDataJob updateGradeDataJob = updateGradeDataJobFactory.create(); | |
| 179 | 
1
1. launchUploadGradeData : replaced return value with null for edu/ucsb/cs156/courses/controllers/JobsController::launchUploadGradeData → KILLED | 
    return jobService.runAsJob(updateGradeDataJob); | 
| 180 |   } | |
| 181 | ||
| 182 |   @Operation(summary = "Get job logs") | |
| 183 |   @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 184 |   @GetMapping("/logs/{jobId}") | |
| 185 |   public Map<String, String> getJobLog(@Parameter(name = "jobId") @PathVariable Long jobId) { | |
| 186 |     Job job = | |
| 187 |         jobsRepository | |
| 188 |             .findById(jobId) | |
| 189 | 
1
1. lambda$getJobLog$0 : replaced return value with null for edu/ucsb/cs156/courses/controllers/JobsController::lambda$getJobLog$0 → KILLED | 
            .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Job not found")); | 
| 190 | ||
| 191 |     // Assuming 'getLog' method returns the log output as a string | |
| 192 | 
1
1. getJobLog : replaced return value with Collections.emptyMap for edu/ucsb/cs156/courses/controllers/JobsController::getJobLog → KILLED | 
    return Map.of("log", job.getLog()); | 
| 193 |   } | |
| 194 | } | |
Mutations | ||
| 51 | 
 
 1.1  | 
|
| 58 | 
 
 1.1  | 
|
| 59 | 
 
 1.1  | 
|
| 66 | 
 
 1.1  | 
|
| 67 | 
 
 1.1  | 
|
| 69 | 
 
 1.1  | 
|
| 70 | 
 
 1.1  | 
|
| 81 | 
 
 1.1  | 
|
| 106 | 
 
 1.1  | 
|
| 123 | 
 
 1.1  | 
|
| 145 | 
 
 1.1  | 
|
| 171 | 
 
 1.1  | 
|
| 179 | 
 
 1.1  | 
|
| 189 | 
 
 1.1  | 
|
| 192 | 
 
 1.1  |