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