1 | package edu.ucsb.cs156.courses.services; | |
2 | ||
3 | import java.io.Reader; | |
4 | import java.io.StringReader; | |
5 | import java.util.ArrayList; | |
6 | import java.util.List; | |
7 | import java.util.Map; | |
8 | import java.util.stream.Collectors; | |
9 | ||
10 | import com.fasterxml.jackson.databind.ObjectMapper; | |
11 | import com.opencsv.CSVReader; | |
12 | ||
13 | import edu.ucsb.cs156.courses.entities.GradeData; | |
14 | import edu.ucsb.cs156.courses.models.Quarter; | |
15 | import edu.ucsb.cs156.courses.models.github.ApiResult; | |
16 | import edu.ucsb.cs156.courses.models.github.TreeElement; | |
17 | ||
18 | import org.springframework.web.client.RestTemplate; | |
19 | ||
20 | import lombok.extern.slf4j.Slf4j; | |
21 | ||
22 | import org.springframework.beans.factory.annotation.Autowired; | |
23 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
24 | import org.springframework.http.HttpEntity; | |
25 | import org.springframework.http.HttpHeaders; | |
26 | import org.springframework.http.HttpMethod; | |
27 | import org.springframework.http.MediaType; | |
28 | import org.springframework.http.ResponseEntity; | |
29 | import org.springframework.stereotype.Service; | |
30 | ||
31 | @Service("UCSBGradeDataService") | |
32 | @Slf4j | |
33 | public class UCSBGradeDataServiceImpl implements UCSBGradeDataService { | |
34 | ||
35 | private final RestTemplate restTemplate; | |
36 | ||
37 | public UCSBGradeDataServiceImpl(RestTemplateBuilder restTemplateBuilder) { | |
38 | restTemplate = restTemplateBuilder.build(); | |
39 | } | |
40 | ||
41 | @Autowired | |
42 | ObjectMapper mapper; | |
43 | ||
44 | public static final String REPO_OWNER_AND_NAME = "ucsb-cs156/UCSB_Grades"; | |
45 | public static final String API_ENDPOINT = "https://api.github.com/repos/" | |
46 | + REPO_OWNER_AND_NAME + | |
47 | "/git/trees/main?recursive=1"; | |
48 | ||
49 | @Override | |
50 | public List<String> getUrls() throws Exception { | |
51 | ||
52 | log.info("getting data from {}", API_ENDPOINT); | |
53 | ||
54 | HttpHeaders headers = new HttpHeaders(); | |
55 |
1
1. getUrls : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(List.of(MediaType.APPLICATION_JSON)); |
56 |
1
1. getUrls : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
57 | ||
58 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
59 | Map<String, String> uriVariables = Map.of("recursive", "1"); | |
60 | ||
61 | ResponseEntity<String> re = restTemplate.exchange(API_ENDPOINT, HttpMethod.GET, entity, String.class, | |
62 | uriVariables); | |
63 | ||
64 | ApiResult apiResult = mapper.readValue(re.getBody(), ApiResult.class); | |
65 | ||
66 | List<TreeElement> treeElements = apiResult.getTree(); | |
67 |
1
1. lambda$getUrls$0 : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBGradeDataServiceImpl::lambda$getUrls$0 → KILLED |
List<String> urls = treeElements.stream().map(treeElement -> treeElement.getPath()) |
68 |
3
1. lambda$getUrls$1 : negated conditional → KILLED 2. lambda$getUrls$1 : negated conditional → KILLED 3. lambda$getUrls$1 : replaced boolean return with true for edu/ucsb/cs156/courses/services/UCSBGradeDataServiceImpl::lambda$getUrls$1 → KILLED |
.filter(path -> (path.startsWith("quarters/") && path.endsWith(".csv"))).collect(Collectors.toList()); |
69 | List<String> rawUrls = urls.stream() | |
70 |
1
1. lambda$getUrls$2 : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBGradeDataServiceImpl::lambda$getUrls$2 → KILLED |
.map(url -> "https://raw.githubusercontent.com/" + REPO_OWNER_AND_NAME + "/main/" + url) |
71 | .collect(Collectors.toList()); | |
72 |
1
1. getUrls : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBGradeDataServiceImpl::getUrls → KILLED |
return rawUrls; |
73 | } | |
74 | ||
75 | @Override | |
76 | public List<GradeData> getGradeData(String url) throws Exception { | |
77 | log.info("getting data from {}", url); | |
78 | HttpHeaders headers = new HttpHeaders(); | |
79 |
1
1. getGradeData : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(List.of(MediaType.APPLICATION_JSON)); |
80 |
1
1. getGradeData : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
81 | ||
82 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
83 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
84 | String csvData = re.getBody(); | |
85 |
1
1. getGradeData : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBGradeDataServiceImpl::getGradeData → KILLED |
return parse(new StringReader(csvData)); |
86 | } | |
87 | ||
88 | @Override | |
89 | public List<GradeData> parse(Reader reader) throws Exception { | |
90 | List<GradeData> gradeDataList = new ArrayList<GradeData>(); | |
91 | log.info("Parsing CSV file with grade data... "); | |
92 | CSVReader csvReader = new CSVReader(reader); | |
93 | csvReader.skip(1); | |
94 | List<String[]> myEntries = csvReader.readAll(); | |
95 | for (String[] row : myEntries) { | |
96 | String yyyyq = Integer.toString(Quarter.qyyToyyyyQ(row[0])); | |
97 | GradeData gradeData = GradeData.builder() | |
98 | .yyyyq(yyyyq) | |
99 | .course(row[2]) | |
100 | .professor(row[3]) | |
101 | .grade(row[4]) | |
102 | .count(Integer.parseInt(row[5])) | |
103 | .build(); | |
104 | log.info("Parsed: " + gradeData.toString()); | |
105 | gradeDataList.add(gradeData); | |
106 | } | |
107 | csvReader.close(); | |
108 |
1
1. parse : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBGradeDataServiceImpl::parse → KILLED |
return gradeDataList; |
109 | } | |
110 | } | |
Mutations | ||
55 |
1.1 |
|
56 |
1.1 |
|
67 |
1.1 |
|
68 |
1.1 2.2 3.3 |
|
70 |
1.1 |
|
72 |
1.1 |
|
79 |
1.1 |
|
80 |
1.1 |
|
85 |
1.1 |
|
108 |
1.1 |