| 1 | package edu.ucsb.cs156.dining.services; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.core.type.TypeReference; | |
| 5 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 6 | import edu.ucsb.cs156.dining.entities.MenuItem; | |
| 7 | import edu.ucsb.cs156.dining.models.Entree; | |
| 8 | import edu.ucsb.cs156.dining.repositories.MenuItemRepository; | |
| 9 | import jakarta.transaction.Transactional; | |
| 10 | import java.time.Duration; | |
| 11 | import java.util.List; | |
| 12 | import java.util.Map; | |
| 13 | import java.util.function.Function; | |
| 14 | import java.util.stream.Collectors; | |
| 15 | import lombok.extern.slf4j.Slf4j; | |
| 16 | import org.springframework.beans.factory.annotation.Autowired; | |
| 17 | import org.springframework.beans.factory.annotation.Value; | |
| 18 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
| 19 | import org.springframework.cache.annotation.Cacheable; | |
| 20 | import org.springframework.http.HttpEntity; | |
| 21 | import org.springframework.http.HttpHeaders; | |
| 22 | import org.springframework.http.HttpMethod; | |
| 23 | import org.springframework.http.MediaType; | |
| 24 | import org.springframework.http.ResponseEntity; | |
| 25 | import org.springframework.stereotype.Service; | |
| 26 | import org.springframework.web.client.RestTemplate; | |
| 27 | ||
| 28 | @Service | |
| 29 | @Slf4j | |
| 30 | public class UCSBDiningMenuItemsService { | |
| 31 | ||
| 32 | @Autowired private ObjectMapper objectMapper; | |
| 33 | ||
| 34 | @Value("${app.ucsb.api.consumer_key}") | |
| 35 | private String apiKey; | |
| 36 | ||
| 37 | @Value("${app.ucsb.api.host}") | |
| 38 | private String apiHost; | |
| 39 | ||
| 40 | @Value("${app.name}") | |
| 41 | private String appName; | |
| 42 | ||
| 43 | @Value("${app.hostname}") | |
| 44 | private String appHostname; | |
| 45 | ||
| 46 | /* | |
| 47 | * A RestTemplate built with no timeout blocks its calling thread forever on a hung external | |
| 48 | * call. That's a real incident lib-jobs' single-threaded jobsExecutor hit on another app | |
| 49 | * (proj-scaffold): a job stuck this way permanently wedged the executor, with no way to recover | |
| 50 | * short of restarting the app (see lib-jobs DESIGN.md 9 -- cooperative job cancellation only | |
| 51 | * helps a job that reaches another checkpoint, which a truly hung thread never will). Generous | |
| 52 | * but finite: long enough to never trip on legitimate slowness, short enough to guarantee a job | |
| 53 | * can't hang forever. | |
| 54 | */ | |
| 55 | private static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(10); | |
| 56 | private static final Duration READ_TIMEOUT = Duration.ofSeconds(60); | |
| 57 | ||
| 58 | private RestTemplate restTemplate = new RestTemplate(); | |
| 59 | @Autowired private MenuItemRepository menuItemRepository; | |
| 60 | ||
| 61 | public UCSBDiningMenuItemsService(RestTemplateBuilder restTemplateBuilder) throws Exception { | |
| 62 | restTemplate = | |
| 63 | restTemplateBuilder.connectTimeout(CONNECT_TIMEOUT).readTimeout(READ_TIMEOUT).build(); | |
| 64 | } | |
| 65 | ||
| 66 | public static final String ALL_MEAL_ITEMS_AT_A_DINING_COMMON_ENDPOINT = | |
| 67 | "{apiHost}/dining/menu/v1/{date-time}/{dining-common-code}/{meal-code}"; | |
| 68 | ||
| 69 | /** | |
| 70 | * Create a List of Entree from json representation | |
| 71 | * | |
| 72 | * @param dateTime String of date in iso format | |
| 73 | * @param diningCommonCode String of dining common | |
| 74 | * @param mealCode String of meal code | |
| 75 | * @return a list of menu items | |
| 76 | */ | |
| 77 | @Cacheable("menuItem") | |
| 78 | public List<Entree> get(String dateTime, String diningCommonCode, String mealCode) | |
| 79 | throws JsonProcessingException { | |
| 80 | ||
| 81 | HttpHeaders headers = new HttpHeaders(); | |
| 82 |
1
1. get : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(List.of(MediaType.APPLICATION_JSON)); |
| 83 |
1
1. get : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 84 |
1
1. get : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 85 |
1
1. get : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("X-Requesting-App", this.appName + "." + this.appHostname); |
| 86 | ||
| 87 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 88 | String url = ALL_MEAL_ITEMS_AT_A_DINING_COMMON_ENDPOINT; | |
| 89 | url = url.replace("{apiHost}", apiHost); | |
| 90 | url = url.replace("{date-time}", dateTime); | |
| 91 | url = url.replace("{dining-common-code}", diningCommonCode); | |
| 92 | url = url.replace("{meal-code}", mealCode); | |
| 93 | ||
| 94 | log.info( | |
| 95 | "Fetching menu items for date: {}, dining common: {}, meal: {}", | |
| 96 | dateTime, | |
| 97 | diningCommonCode, | |
| 98 | mealCode); | |
| 99 | ||
| 100 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 101 | String retBody = re.getBody(); | |
| 102 | ||
| 103 | List<Entree> menuItems = objectMapper.readValue(retBody, new TypeReference<List<Entree>>() {}); | |
| 104 | ||
| 105 |
1
1. get : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/services/UCSBDiningMenuItemsService::get → KILLED |
return menuItems; |
| 106 | } | |
| 107 | ||
| 108 | @Transactional | |
| 109 | public List<MenuItem> getOrCreateMenuItems( | |
| 110 | String diningCommonCode, String mealCode, List<Entree> entrees) { | |
| 111 | List<MenuItem> existingItems = | |
| 112 | menuItemRepository.findExistingEntrees(diningCommonCode, mealCode, entrees); | |
| 113 | Map<String, MenuItem> existingItemsMap = | |
| 114 | existingItems.stream() | |
| 115 | .collect( | |
| 116 | Collectors.toMap( | |
| 117 |
1
1. lambda$getOrCreateMenuItems$0 : replaced return value with "" for edu/ucsb/cs156/dining/services/UCSBDiningMenuItemsService::lambda$getOrCreateMenuItems$0 → KILLED |
(item) -> item.getStation() + ":" + item.getName(), |
| 118 | Function.identity(), | |
| 119 |
1
1. lambda$getOrCreateMenuItems$1 : replaced return value with null for edu/ucsb/cs156/dining/services/UCSBDiningMenuItemsService::lambda$getOrCreateMenuItems$1 → KILLED |
(first, second) -> first)); // tolerate duplicate rows already in the DB |
| 120 | ||
| 121 | for (Entree selected : entrees) { | |
| 122 | String key = selected.getStation() + ":" + selected.getName(); | |
| 123 |
1
1. getOrCreateMenuItems : negated conditional → KILLED |
if (existingItemsMap.containsKey(key)) { |
| 124 | continue; | |
| 125 | } | |
| 126 | MenuItem created = | |
| 127 | MenuItem.builder() | |
| 128 | .diningCommonsCode(diningCommonCode) | |
| 129 | .mealCode(mealCode) | |
| 130 | .station(selected.getStation()) | |
| 131 | .name(selected.getName()) | |
| 132 | .build(); | |
| 133 | menuItemRepository.save(created); | |
| 134 | existingItems.add(created); | |
| 135 | existingItemsMap.put(key, created); // don't save the same item twice from one list | |
| 136 | } | |
| 137 | ||
| 138 |
1
1. getOrCreateMenuItems : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/services/UCSBDiningMenuItemsService::getOrCreateMenuItems → KILLED |
return existingItems; |
| 139 | } | |
| 140 | } | |
Mutations | ||
| 82 |
1.1 |
|
| 83 |
1.1 |
|
| 84 |
1.1 |
|
| 85 |
1.1 |
|
| 105 |
1.1 |
|
| 117 |
1.1 |
|
| 119 |
1.1 |
|
| 123 |
1.1 |
|
| 138 |
1.1 |