| 1 | package edu.ucsb.cs156.courses.controllers; | |
| 2 | ||
| 3 | import com.opencsv.bean.StatefulBeanToCsv; | |
| 4 | import com.opencsv.exceptions.CsvDataTypeMismatchException; | |
| 5 | import com.opencsv.exceptions.CsvRequiredFieldEmptyException; | |
| 6 | import edu.ucsb.cs156.courses.entities.EnrollmentDataPoint; | |
| 7 | import edu.ucsb.cs156.courses.models.EnrollmentCSV; | |
| 8 | import edu.ucsb.cs156.courses.repositories.EnrollmentDataPointRepository; | |
| 9 | import edu.ucsb.cs156.courses.services.EnrollmentCSVService; | |
| 10 | import edu.ucsb.cs156.courses.utilities.CourseUtilities; | |
| 11 | import io.swagger.v3.oas.annotations.Operation; | |
| 12 | import io.swagger.v3.oas.annotations.Parameter; | |
| 13 | import io.swagger.v3.oas.annotations.media.Content; | |
| 14 | import io.swagger.v3.oas.annotations.media.Schema; | |
| 15 | import io.swagger.v3.oas.annotations.responses.ApiResponse; | |
| 16 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 17 | import java.io.IOException; | |
| 18 | import java.io.OutputStreamWriter; | |
| 19 | import java.io.Writer; | |
| 20 | import java.nio.charset.StandardCharsets; | |
| 21 | import java.util.List; | |
| 22 | import java.util.stream.Collectors; | |
| 23 | import lombok.extern.slf4j.Slf4j; | |
| 24 | import org.springframework.beans.factory.annotation.Autowired; | |
| 25 | import org.springframework.data.util.Streamable; | |
| 26 | import org.springframework.http.HttpHeaders; | |
| 27 | import org.springframework.http.MediaType; | |
| 28 | import org.springframework.http.ResponseEntity; | |
| 29 | import org.springframework.web.bind.annotation.GetMapping; | |
| 30 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 31 | import org.springframework.web.bind.annotation.RequestParam; | |
| 32 | import org.springframework.web.bind.annotation.RestController; | |
| 33 | import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody; | |
| 34 | ||
| 35 | @Slf4j | |
| 36 | @Tag(name = "API for enrollment data") | |
| 37 | @RequestMapping("/api/enrollment") | |
| 38 | @RestController | |
| 39 | public class EnrollmentController extends ApiController { | |
| 40 | ||
| 41 | @Autowired EnrollmentDataPointRepository enrollmentDataPointRepository; | |
| 42 | @Autowired private EnrollmentCSVService enrollmentCSVService; | |
| 43 | ||
| 44 | @Operation(summary = "Get enrollment history for a course or section") | |
| 45 | @GetMapping(value = "/search", produces = "application/json") | |
| 46 | public Iterable<EnrollmentDataPoint> search( | |
| 47 | @Parameter( | |
| 48 | name = "startQtr", | |
| 49 | description = | |
| 50 | "starting quarter in yyyyq format, e.g. 20231 for W23, 20232 for S23, etc. (1=Winter, 2=Spring, 3=Summer, 4=Fall)", | |
| 51 | example = "20231", | |
| 52 | required = true) | |
| 53 | @RequestParam | |
| 54 | String startQtr, | |
| 55 | @Parameter( | |
| 56 | name = "endQtr", | |
| 57 | description = | |
| 58 | "ending quarter in yyyyq format, e.g. 20231 for W23, 20232 for S23, etc. (1=Winter, 2=Spring, 3=Summer, 4=Fall)", | |
| 59 | example = "20231", | |
| 60 | required = true) | |
| 61 | @RequestParam | |
| 62 | String endQtr, | |
| 63 | @Parameter( | |
| 64 | name = "subjectArea", | |
| 65 | description = "simplified area name, e.g. CMPSC for computer science", | |
| 66 | example = "CMPSC", | |
| 67 | required = true) | |
| 68 | @RequestParam | |
| 69 | String subjectArea, | |
| 70 | @Parameter( | |
| 71 | name = "courseNumber", | |
| 72 | description = "the specific course number, e.g. 130A for CS130A", | |
| 73 | example = "130A", | |
| 74 | required = true) | |
| 75 | @RequestParam | |
| 76 | String courseNumber, | |
| 77 | @Parameter(name = "enrollCd", description = "enroll code", example = "08268") | |
| 78 | @RequestParam(required = false) | |
| 79 | String enrollCd, | |
| 80 | @Parameter(name = "section", description = "section number", example = "0100") | |
| 81 | @RequestParam(required = false) | |
| 82 | String section) { | |
| 83 | String courseId = CourseUtilities.makeFormattedCourseId(subjectArea, courseNumber); | |
| 84 |
1
1. search : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/controllers/EnrollmentController::search → KILLED |
return enrollmentDataPointRepository.findByQuarterRangeAndCourseIdAndOptionalFilters( |
| 85 | startQtr, endQtr, courseId, enrollCd, section); | |
| 86 | } | |
| 87 | ||
| 88 | @Operation( | |
| 89 | summary = "Download Enrollment Data as CSV File", | |
| 90 | description = "Returns a CSV file as a response", | |
| 91 | responses = { | |
| 92 | @ApiResponse( | |
| 93 | responseCode = "200", | |
| 94 | description = "CSV file", | |
| 95 | content = | |
| 96 | @Content( | |
| 97 | mediaType = "text/csv", | |
| 98 | schema = @Schema(type = "string", format = "binary"))), | |
| 99 | @ApiResponse(responseCode = "500", description = "Internal Server Error") | |
| 100 | }) | |
| 101 | @GetMapping(value = "/csv/quarter", produces = "text/csv") | |
| 102 | public ResponseEntity<StreamingResponseBody> csvForQuarter( | |
| 103 | @Parameter(name = "yyyyq", description = "quarter in yyyyq format", example = "20252") | |
| 104 | @RequestParam | |
| 105 | String yyyyq) | |
| 106 | throws IOException { | |
| 107 | ||
| 108 | StreamingResponseBody stream = | |
| 109 | (outputStream) -> { | |
| 110 | Iterable<EnrollmentDataPoint> iterable = enrollmentDataPointRepository.findByYyyyq(yyyyq); | |
| 111 | List<EnrollmentCSV> list = | |
| 112 | Streamable.of(iterable).toList().stream() | |
| 113 |
1
1. lambda$csvForQuarter$0 : replaced return value with null for edu/ucsb/cs156/courses/controllers/EnrollmentController::lambda$csvForQuarter$0 → KILLED |
.map(enrollmentDataPoint -> EnrollmentCSV.fromEntity(enrollmentDataPoint)) |
| 114 | .collect(Collectors.toList()); | |
| 115 | ||
| 116 | try (Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) { | |
| 117 | try { | |
| 118 | StatefulBeanToCsv<EnrollmentCSV> beanToCsvWriter = | |
| 119 | enrollmentCSVService.getStatefulBeanToCSV(writer); | |
| 120 |
1
1. lambda$csvForQuarter$1 : removed call to com/opencsv/bean/StatefulBeanToCsv::write → KILLED |
beanToCsvWriter.write(list); |
| 121 | } catch (CsvDataTypeMismatchException | CsvRequiredFieldEmptyException e) { | |
| 122 | log.error("Error writing CSV file", e); | |
| 123 | throw new IOException("Error writing CSV file: " + e.getMessage()); | |
| 124 | } | |
| 125 | } | |
| 126 | }; | |
| 127 | ||
| 128 |
1
1. csvForQuarter : replaced return value with null for edu/ucsb/cs156/courses/controllers/EnrollmentController::csvForQuarter → KILLED |
return ResponseEntity.ok() |
| 129 | .contentType(MediaType.parseMediaType("text/csv; charset=UTF-8")) | |
| 130 | .header( | |
| 131 | HttpHeaders.CONTENT_DISPOSITION, | |
| 132 | String.format("attachment;filename=enrollment_%s.csv", yyyyq)) | |
| 133 | .header(HttpHeaders.CONTENT_TYPE, "text/csv; charset=UTF-8") | |
| 134 | .header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION) | |
| 135 | .body(stream); | |
| 136 | } | |
| 137 | } | |
Mutations | ||
| 84 |
1.1 |
|
| 113 |
1.1 |
|
| 120 |
1.1 |
|
| 128 |
1.1 |