| 1 | package edu.ucsb.cs156.jobs.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.jobs.entities.Job; | |
| 4 | import edu.ucsb.cs156.jobs.entities.JobLog; | |
| 5 | import edu.ucsb.cs156.jobs.errors.EntityNotFoundException; | |
| 6 | import edu.ucsb.cs156.jobs.repositories.JobsRepository; | |
| 7 | import edu.ucsb.cs156.jobs.services.JobService; | |
| 8 | import io.swagger.v3.oas.annotations.Operation; | |
| 9 | import io.swagger.v3.oas.annotations.Parameter; | |
| 10 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 11 | import java.util.Arrays; | |
| 12 | import java.util.List; | |
| 13 | import java.util.Map; | |
| 14 | import lombok.extern.slf4j.Slf4j; | |
| 15 | import org.springframework.beans.factory.annotation.Autowired; | |
| 16 | import org.springframework.data.domain.Page; | |
| 17 | import org.springframework.data.domain.PageRequest; | |
| 18 | import org.springframework.data.domain.Sort.Direction; | |
| 19 | import org.springframework.data.jpa.domain.Specification; | |
| 20 | import org.springframework.http.HttpStatus; | |
| 21 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 22 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 23 | import org.springframework.web.bind.annotation.ExceptionHandler; | |
| 24 | import org.springframework.web.bind.annotation.GetMapping; | |
| 25 | import org.springframework.web.bind.annotation.PathVariable; | |
| 26 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 27 | import org.springframework.web.bind.annotation.RequestParam; | |
| 28 | import org.springframework.web.bind.annotation.ResponseStatus; | |
| 29 | import org.springframework.web.bind.annotation.RestController; | |
| 30 | ||
| 31 | /** | |
| 32 | * Admin REST API for job records: list (all or paginated, filterable, sortable by any field), fetch | |
| 33 | * one, fetch logs (full or incremental tail), delete one or all. Launch endpoints stay in each | |
| 34 | * app's own controllers. Requires {@code ROLE_ADMIN}, which consuming apps must support via method | |
| 35 | * security ({@code @EnableMethodSecurity}). | |
| 36 | */ | |
| 37 | @Tag(name = "Jobs") | |
| 38 | @RequestMapping("/api/jobs") | |
| 39 | @RestController | |
| 40 | @Slf4j | |
| 41 | public class JobsController { | |
| 42 | public static final List<String> ALLOWED_SORT_FIELDS = | |
| 43 | Arrays.asList( | |
| 44 | "id", | |
| 45 | "jobName", | |
| 46 | "status", | |
| 47 | "createdByEmail", | |
| 48 | "scopeType", | |
| 49 | "scopeId", | |
| 50 | "createdAt", | |
| 51 | "updatedAt"); | |
| 52 | ||
| 53 | /** Number of trailing log lines included as a preview on list/paginated responses. */ | |
| 54 | public static final int LOG_PREVIEW_LINES = 10; | |
| 55 | ||
| 56 | @Autowired private JobsRepository jobsRepository; | |
| 57 | ||
| 58 | @Autowired private JobService jobService; | |
| 59 | ||
| 60 | @Operation(summary = "List all jobs") | |
| 61 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 62 | @GetMapping("/all") | |
| 63 | public Iterable<Job> allJobs() { | |
| 64 | Iterable<Job> jobs = jobsRepository.findAllByOrderByIdDesc(); | |
| 65 |
1
1. allJobs : removed call to java/lang/Iterable::forEach → KILLED |
jobs.forEach(this::populateLogPreview); |
| 66 |
1
1. allJobs : replaced return value with Collections.emptyList for edu/ucsb/cs156/jobs/controllers/JobsController::allJobs → KILLED |
return jobs; |
| 67 | } | |
| 68 | ||
| 69 | @Operation( | |
| 70 | summary = "Get a paginated, optionally filtered, list of jobs, sortable by any allowed field") | |
| 71 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 72 | @GetMapping(value = "/paginated", produces = "application/json") | |
| 73 | public Page<Job> paginatedJobs( | |
| 74 | @Parameter(name = "page", description = "what page of the data", example = "0") @RequestParam | |
| 75 | int page, | |
| 76 | @Parameter(name = "pageSize", description = "size of each page", example = "10") @RequestParam | |
| 77 | int pageSize, | |
| 78 | @Parameter(name = "sortField", description = "sort field", example = "createdAt") | |
| 79 | @RequestParam(defaultValue = "status") | |
| 80 | String sortField, | |
| 81 | @Parameter(name = "sortDirection", description = "sort direction", example = "ASC") | |
| 82 | @RequestParam(defaultValue = "DESC") | |
| 83 | String sortDirection, | |
| 84 | @Parameter(name = "status", description = "exact match, e.g. \"running\"") | |
| 85 | @RequestParam(required = false) | |
| 86 | String status, | |
| 87 | @Parameter(name = "jobName", description = "case-insensitive substring match") | |
| 88 | @RequestParam(required = false) | |
| 89 | String jobName, | |
| 90 | @Parameter(name = "createdByEmail", description = "case-insensitive substring match") | |
| 91 | @RequestParam(required = false) | |
| 92 | String createdByEmail, | |
| 93 | @Parameter(name = "scopeType", description = "exact match, e.g. \"course\"") | |
| 94 | @RequestParam(required = false) | |
| 95 | String scopeType, | |
| 96 | @Parameter(name = "scopeId", description = "exact match") @RequestParam(required = false) | |
| 97 | Long scopeId) { | |
| 98 | ||
| 99 |
1
1. paginatedJobs : negated conditional → KILLED |
if (!ALLOWED_SORT_FIELDS.contains(sortField)) { |
| 100 | throw new IllegalArgumentException( | |
| 101 | String.format( | |
| 102 | "%s is not a valid sort field. Valid values are %s", sortField, ALLOWED_SORT_FIELDS)); | |
| 103 | } | |
| 104 | ||
| 105 | List<String> allowedSortDirections = Arrays.asList("ASC", "DESC"); | |
| 106 |
1
1. paginatedJobs : negated conditional → KILLED |
if (!allowedSortDirections.contains(sortDirection)) { |
| 107 | throw new IllegalArgumentException( | |
| 108 | String.format( | |
| 109 | "%s is not a valid sort direction. Valid values are %s", | |
| 110 | sortDirection, allowedSortDirections)); | |
| 111 | } | |
| 112 | ||
| 113 | Direction sortDirectionObject = Direction.DESC; | |
| 114 |
1
1. paginatedJobs : negated conditional → KILLED |
if (sortDirection.equals("ASC")) { |
| 115 | sortDirectionObject = Direction.ASC; | |
| 116 | } | |
| 117 | ||
| 118 | Specification<Job> spec = Specification.where(null); | |
| 119 |
2
1. paginatedJobs : negated conditional → KILLED 2. paginatedJobs : negated conditional → KILLED |
if (status != null && !status.isEmpty()) { |
| 120 |
1
1. lambda$paginatedJobs$e0ae5173$1 : replaced return value with null for edu/ucsb/cs156/jobs/controllers/JobsController::lambda$paginatedJobs$e0ae5173$1 → KILLED |
spec = spec.and((root, query, cb) -> cb.equal(root.get("status"), status)); |
| 121 | } | |
| 122 |
2
1. paginatedJobs : negated conditional → KILLED 2. paginatedJobs : negated conditional → KILLED |
if (jobName != null && !jobName.isEmpty()) { |
| 123 | String pattern = "%" + jobName.toLowerCase() + "%"; | |
| 124 |
1
1. lambda$paginatedJobs$30c6c2c3$1 : replaced return value with null for edu/ucsb/cs156/jobs/controllers/JobsController::lambda$paginatedJobs$30c6c2c3$1 → KILLED |
spec = spec.and((root, query, cb) -> cb.like(cb.lower(root.get("jobName")), pattern)); |
| 125 | } | |
| 126 |
2
1. paginatedJobs : negated conditional → KILLED 2. paginatedJobs : negated conditional → KILLED |
if (createdByEmail != null && !createdByEmail.isEmpty()) { |
| 127 | String pattern = "%" + createdByEmail.toLowerCase() + "%"; | |
| 128 |
1
1. lambda$paginatedJobs$30c6c2c3$2 : replaced return value with null for edu/ucsb/cs156/jobs/controllers/JobsController::lambda$paginatedJobs$30c6c2c3$2 → KILLED |
spec = spec.and((root, query, cb) -> cb.like(cb.lower(root.get("createdByEmail")), pattern)); |
| 129 | } | |
| 130 |
2
1. paginatedJobs : negated conditional → KILLED 2. paginatedJobs : negated conditional → KILLED |
if (scopeType != null && !scopeType.isEmpty()) { |
| 131 |
1
1. lambda$paginatedJobs$42e6cca5$1 : replaced return value with null for edu/ucsb/cs156/jobs/controllers/JobsController::lambda$paginatedJobs$42e6cca5$1 → KILLED |
spec = spec.and((root, query, cb) -> cb.equal(root.get("scopeType"), scopeType)); |
| 132 | } | |
| 133 |
1
1. paginatedJobs : negated conditional → KILLED |
if (scopeId != null) { |
| 134 |
1
1. lambda$paginatedJobs$67dbb88f$1 : replaced return value with null for edu/ucsb/cs156/jobs/controllers/JobsController::lambda$paginatedJobs$67dbb88f$1 → KILLED |
spec = spec.and((root, query, cb) -> cb.equal(root.get("scopeId"), scopeId)); |
| 135 | } | |
| 136 | ||
| 137 | PageRequest pageRequest = PageRequest.of(page, pageSize, sortDirectionObject, sortField); | |
| 138 | Page<Job> result = jobsRepository.findAll(spec, pageRequest); | |
| 139 |
1
1. paginatedJobs : removed call to java/util/List::forEach → KILLED |
result.getContent().forEach(this::populateLogPreview); |
| 140 |
1
1. paginatedJobs : replaced return value with null for edu/ucsb/cs156/jobs/controllers/JobsController::paginatedJobs → KILLED |
return result; |
| 141 | } | |
| 142 | ||
| 143 | @Operation(summary = "Get a specific job by ID if it is in the database") | |
| 144 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 145 | @GetMapping("") | |
| 146 | public Job getJobById( | |
| 147 | @Parameter(name = "id", description = "ID of the job") @RequestParam Long id) { | |
| 148 | Job job = | |
| 149 |
1
1. lambda$getJobById$0 : replaced return value with null for edu/ucsb/cs156/jobs/controllers/JobsController::lambda$getJobById$0 → KILLED |
jobsRepository.findById(id).orElseThrow(() -> new EntityNotFoundException(Job.class, id)); |
| 150 |
1
1. getJobById : removed call to edu/ucsb/cs156/jobs/entities/Job::setLog → KILLED |
job.setLog(jobService.getJobLogs(id)); |
| 151 |
1
1. getJobById : replaced return value with null for edu/ucsb/cs156/jobs/controllers/JobsController::getJobById → KILLED |
return job; |
| 152 | } | |
| 153 | ||
| 154 | @Operation(summary = "Get long job logs") | |
| 155 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 156 | @GetMapping("/logs/{id}") | |
| 157 | public String getJobLogs(@Parameter(name = "id", description = "Job ID") @PathVariable Long id) { | |
| 158 |
1
1. getJobLogs : replaced return value with "" for edu/ucsb/cs156/jobs/controllers/JobsController::getJobLogs → KILLED |
return jobService.getJobLogs(id); |
| 159 | } | |
| 160 | ||
| 161 | @Operation( | |
| 162 | summary = | |
| 163 | "Get log lines written since afterId, for incremental live-tailing (poll with the " | |
| 164 | + "highest id you've already seen)") | |
| 165 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 166 | @GetMapping("/logs/{id}/tail") | |
| 167 | public List<JobLog> getJobLogTail( | |
| 168 | @Parameter(name = "id", description = "Job ID") @PathVariable Long id, | |
| 169 | @Parameter(name = "afterId", description = "only return lines with id greater than this") | |
| 170 | @RequestParam(defaultValue = "0") | |
| 171 | Long afterId) { | |
| 172 |
1
1. getJobLogTail : replaced return value with Collections.emptyList for edu/ucsb/cs156/jobs/controllers/JobsController::getJobLogTail → KILLED |
return jobService.getJobLogTail(id, afterId); |
| 173 | } | |
| 174 | ||
| 175 | @Operation(summary = "Delete specific job record") | |
| 176 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 177 | @DeleteMapping("") | |
| 178 | public Map<String, String> deleteJob(@Parameter(name = "id") @RequestParam Long id) { | |
| 179 |
1
1. deleteJob : negated conditional → KILLED |
if (!jobsRepository.existsById(id)) { |
| 180 |
1
1. deleteJob : replaced return value with Collections.emptyMap for edu/ucsb/cs156/jobs/controllers/JobsController::deleteJob → KILLED |
return Map.of("message", String.format("Job with id %d not found", id)); |
| 181 | } | |
| 182 |
1
1. deleteJob : removed call to edu/ucsb/cs156/jobs/repositories/JobsRepository::deleteById → KILLED |
jobsRepository.deleteById(id); |
| 183 |
1
1. deleteJob : replaced return value with Collections.emptyMap for edu/ucsb/cs156/jobs/controllers/JobsController::deleteJob → KILLED |
return Map.of("message", String.format("Job with id %d deleted", id)); |
| 184 | } | |
| 185 | ||
| 186 | @Operation(summary = "Delete all job records") | |
| 187 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 188 | @DeleteMapping("/all") | |
| 189 | public Map<String, String> deleteAllJobs() { | |
| 190 |
1
1. deleteAllJobs : removed call to edu/ucsb/cs156/jobs/repositories/JobsRepository::deleteAll → KILLED |
jobsRepository.deleteAll(); |
| 191 |
1
1. deleteAllJobs : replaced return value with Collections.emptyMap for edu/ucsb/cs156/jobs/controllers/JobsController::deleteAllJobs → KILLED |
return Map.of("message", "All jobs deleted"); |
| 192 | } | |
| 193 | ||
| 194 | private void populateLogPreview(Job job) { | |
| 195 |
1
1. populateLogPreview : removed call to edu/ucsb/cs156/jobs/entities/Job::setLog → KILLED |
job.setLog(jobService.getJobLogPreview(job.getId())); |
| 196 | } | |
| 197 | ||
| 198 | /** | |
| 199 | * The apps map these exceptions in an app-level base controller; the library controller cannot | |
| 200 | * extend that, so it carries its own handlers with the same response shape. | |
| 201 | */ | |
| 202 | @ExceptionHandler({EntityNotFoundException.class}) | |
| 203 | @ResponseStatus(HttpStatus.NOT_FOUND) | |
| 204 | public Object handleEntityNotFoundException(Throwable e) { | |
| 205 |
1
1. handleEntityNotFoundException : replaced return value with null for edu/ucsb/cs156/jobs/controllers/JobsController::handleEntityNotFoundException → KILLED |
return Map.of( |
| 206 | "type", e.getClass().getSimpleName(), | |
| 207 | "message", e.getMessage()); | |
| 208 | } | |
| 209 | ||
| 210 | @ExceptionHandler({IllegalArgumentException.class}) | |
| 211 | @ResponseStatus(HttpStatus.BAD_REQUEST) | |
| 212 | public Object handleIllegalArgument(Throwable e) { | |
| 213 |
1
1. handleIllegalArgument : replaced return value with null for edu/ucsb/cs156/jobs/controllers/JobsController::handleIllegalArgument → KILLED |
return Map.of( |
| 214 | "type", e.getClass().getSimpleName(), | |
| 215 | "message", e.getMessage()); | |
| 216 | } | |
| 217 | } | |
Mutations | ||
| 65 |
1.1 |
|
| 66 |
1.1 |
|
| 99 |
1.1 |
|
| 106 |
1.1 |
|
| 114 |
1.1 |
|
| 119 |
1.1 2.2 |
|
| 120 |
1.1 |
|
| 122 |
1.1 2.2 |
|
| 124 |
1.1 |
|
| 126 |
1.1 2.2 |
|
| 128 |
1.1 |
|
| 130 |
1.1 2.2 |
|
| 131 |
1.1 |
|
| 133 |
1.1 |
|
| 134 |
1.1 |
|
| 139 |
1.1 |
|
| 140 |
1.1 |
|
| 149 |
1.1 |
|
| 150 |
1.1 |
|
| 151 |
1.1 |
|
| 158 |
1.1 |
|
| 172 |
1.1 |
|
| 179 |
1.1 |
|
| 180 |
1.1 |
|
| 182 |
1.1 |
|
| 183 |
1.1 |
|
| 190 |
1.1 |
|
| 191 |
1.1 |
|
| 195 |
1.1 |
|
| 205 |
1.1 |
|
| 213 |
1.1 |