| 1 | package edu.ucsb.cs156.dining.jobs; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.databind.JsonNode; | |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | import edu.ucsb.cs156.dining.models.DiningCommons; | |
| 6 | import edu.ucsb.cs156.dining.models.Entree; | |
| 7 | import edu.ucsb.cs156.dining.services.DiningCommonsService; | |
| 8 | import edu.ucsb.cs156.dining.services.UCSBDiningMenuItemsService; | |
| 9 | import edu.ucsb.cs156.dining.services.UCSBDiningMenuService; | |
| 10 | import edu.ucsb.cs156.jobs.services.JobContext; | |
| 11 | import edu.ucsb.cs156.jobs.services.JobContextConsumer; | |
| 12 | import java.time.LocalDate; | |
| 13 | import java.time.YearMonth; | |
| 14 | import java.util.List; | |
| 15 | import java.util.concurrent.Callable; | |
| 16 | import lombok.Builder; | |
| 17 | import lombok.Getter; | |
| 18 | import org.springframework.web.client.HttpClientErrorException; | |
| 19 | ||
| 20 | @Builder | |
| 21 | @Getter | |
| 22 | public class LoadMonthMenuItemsJob implements JobContextConsumer { | |
| 23 | /** Downloads and saves the menu items for every day of a month, backing off on 429s. */ | |
| 24 | private DiningCommonsService diningCommonsService; | |
| 25 | ||
| 26 | private UCSBDiningMenuService menuService; | |
| 27 | private UCSBDiningMenuItemsService menuItemsService; | |
| 28 | private String apiKey; | |
| 29 | private YearMonth month; | |
| 30 | private Sleeper sleeper; | |
| 31 | @Builder.Default private int maxRetries = 5; | |
| 32 | @Builder.Default private long initialBackoffMs = 1000; | |
| 33 | @Builder.Default private ObjectMapper objectMapper = new ObjectMapper(); | |
| 34 | ||
| 35 | @Override | |
| 36 | public void accept(JobContext ctx) throws Exception { | |
| 37 |
2
1. accept : negated conditional → KILLED 2. accept : negated conditional → KILLED |
if (apiKey == null || apiKey.isBlank()) { |
| 38 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("UCSB API key is not set; skipping menu item download."); |
| 39 | return; | |
| 40 | } | |
| 41 | ||
| 42 |
1
1. lambda$accept$0 : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/jobs/LoadMonthMenuItemsJob::lambda$accept$0 → KILLED |
List<DiningCommons> commonsList = withRetry(ctx, () -> diningCommonsService.get()); |
| 43 | ||
| 44 | int itemCount = 0; | |
| 45 | for (LocalDate date = month.atDay(1); | |
| 46 |
1
1. accept : negated conditional → KILLED |
!date.isAfter(month.atEndOfMonth()); |
| 47 | date = date.plusDays(1)) { | |
| 48 | for (DiningCommons commons : commonsList) { | |
| 49 |
1
1. accept : Replaced integer addition with subtraction → KILLED |
itemCount += loadDay(ctx, date.toString(), commons.getCode()); |
| 50 | } | |
| 51 | } | |
| 52 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 53 | "Finished loading menu items for " + month + " (" + itemCount + " menu items processed)"); | |
| 54 | } | |
| 55 | ||
| 56 | private int loadDay(JobContext ctx, String date, String code) throws Exception { | |
| 57 | JsonNode meals; | |
| 58 | try { | |
| 59 |
1
1. lambda$loadDay$0 : replaced return value with "" for edu/ucsb/cs156/dining/jobs/LoadMonthMenuItemsJob::lambda$loadDay$0 → KILLED |
String mealsJson = withRetry(ctx, () -> menuService.getJSON(date, code)); |
| 60 |
1
1. loadDay : negated conditional → KILLED |
if (mealsJson == null) { |
| 61 | return 0; // commons closed that day | |
| 62 | } | |
| 63 | meals = objectMapper.readTree(mealsJson); | |
| 64 | } catch (Exception e) { | |
| 65 | ctx.log("Failed to load meals for " + date + " " + code + ": " + e.getMessage()); | |
| 66 | return 0; | |
| 67 | } | |
| 68 | ||
| 69 | int count = 0; | |
| 70 | for (JsonNode meal : meals) { | |
| 71 | String mealCode = meal.get("code").asText(); | |
| 72 | try { | |
| 73 |
1
1. lambda$loadDay$1 : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/jobs/LoadMonthMenuItemsJob::lambda$loadDay$1 → KILLED |
List<Entree> entrees = withRetry(ctx, () -> menuItemsService.get(date, code, mealCode)); |
| 74 |
1
1. loadDay : Replaced integer addition with subtraction → KILLED |
count += menuItemsService.getOrCreateMenuItems(code, mealCode, entrees).size(); |
| 75 | } catch (Exception e) { | |
| 76 | ctx.log("Failed to load " + date + " " + code + " " + mealCode + ": " + e.getMessage()); | |
| 77 | } | |
| 78 | } | |
| 79 |
1
1. loadDay : replaced int return with 0 for edu/ucsb/cs156/dining/jobs/LoadMonthMenuItemsJob::loadDay → KILLED |
return count; |
| 80 | } | |
| 81 | ||
| 82 | private <T> T withRetry(JobContext ctx, Callable<T> call) throws Exception { | |
| 83 | long backoff = initialBackoffMs; | |
| 84 |
1
1. withRetry : Changed increment from 1 to -1 → TIMED_OUT |
for (int attempt = 0; ; attempt++) { |
| 85 | try { | |
| 86 |
1
1. withRetry : replaced return value with null for edu/ucsb/cs156/dining/jobs/LoadMonthMenuItemsJob::withRetry → KILLED |
return call.call(); |
| 87 | } catch (HttpClientErrorException.TooManyRequests e) { | |
| 88 |
2
1. withRetry : changed conditional boundary → KILLED 2. withRetry : negated conditional → KILLED |
if (attempt >= maxRetries) { |
| 89 | throw e; | |
| 90 | } | |
| 91 |
1
1. withRetry : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Rate limited (429); waiting " + backoff + " ms before retrying"); |
| 92 |
1
1. withRetry : removed call to edu/ucsb/cs156/dining/jobs/Sleeper::sleep → KILLED |
sleeper.sleep(backoff); |
| 93 |
1
1. withRetry : Replaced long multiplication with division → KILLED |
backoff *= 2; |
| 94 | } | |
| 95 | } | |
| 96 | } | |
| 97 | } | |
Mutations | ||
| 37 |
1.1 2.2 |
|
| 38 |
1.1 |
|
| 42 |
1.1 |
|
| 46 |
1.1 |
|
| 49 |
1.1 |
|
| 52 |
1.1 |
|
| 59 |
1.1 |
|
| 60 |
1.1 |
|
| 73 |
1.1 |
|
| 74 |
1.1 |
|
| 79 |
1.1 |
|
| 84 |
1.1 |
|
| 86 |
1.1 |
|
| 88 |
1.1 2.2 |
|
| 91 |
1.1 |
|
| 92 |
1.1 |
|
| 93 |
1.1 |