| 1 | package edu.ucsb.cs156.dining.jobs; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.databind.JsonNode; | |
| 5 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 6 | import edu.ucsb.cs156.dining.models.DiningCommons; | |
| 7 | import edu.ucsb.cs156.dining.models.Entree; | |
| 8 | import edu.ucsb.cs156.dining.services.DiningCommonsService; | |
| 9 | import edu.ucsb.cs156.dining.services.UCSBDiningMenuItemsService; | |
| 10 | import edu.ucsb.cs156.dining.services.UCSBDiningMenuService; | |
| 11 | import edu.ucsb.cs156.jobs.services.JobContext; | |
| 12 | import edu.ucsb.cs156.jobs.services.JobContextConsumer; | |
| 13 | import java.time.LocalDate; | |
| 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 | import org.springframework.web.client.RestClientException; | |
| 20 | ||
| 21 | /** | |
| 22 | * Downloads the menus for every dining commons and meal on each day in [startDate, endDate] from | |
| 23 | * the UCSB Dining API, and saves any MenuItems not already in the database. Backs off exponentially | |
| 24 | * when the API responds with 429 Too Many Requests. | |
| 25 | */ | |
| 26 | @Builder | |
| 27 | @Getter | |
| 28 | public class LoadMenuItemsJob implements JobContextConsumer { | |
| 29 | ||
| 30 | /** Abstraction over Thread.sleep so tests don't have to actually wait. */ | |
| 31 | @FunctionalInterface | |
| 32 | public interface Sleeper { | |
| 33 | void sleep(long ms) throws InterruptedException; | |
| 34 | } | |
| 35 | ||
| 36 | private DiningCommonsService diningCommonsService; | |
| 37 | private UCSBDiningMenuService menuService; | |
| 38 | private UCSBDiningMenuItemsService menuItemsService; | |
| 39 | private ObjectMapper objectMapper; | |
| 40 | private LocalDate startDate; | |
| 41 | private LocalDate endDate; | |
| 42 | ||
| 43 | @Builder.Default private Sleeper sleeper = Thread::sleep; | |
| 44 | @Builder.Default private int maxRetries = 6; | |
| 45 | @Builder.Default private long initialBackoffMs = 1000; | |
| 46 | ||
| 47 | @Override | |
| 48 | public void accept(JobContext ctx) throws Exception { | |
| 49 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Loading menu items from " + startDate + " to " + endDate); |
| 50 | ||
| 51 |
1
1. lambda$accept$0 : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob::lambda$accept$0 → KILLED |
List<DiningCommons> commons = withBackoff(ctx, () -> diningCommonsService.get()); |
| 52 | ||
| 53 | int count = 0; | |
| 54 |
1
1. accept : negated conditional → KILLED |
for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) { |
| 55 | for (DiningCommons dc : commons) { | |
| 56 |
1
1. accept : Replaced integer addition with subtraction → KILLED |
count += loadDay(ctx, date.toString(), dc.getCode()); |
| 57 | } | |
| 58 | } | |
| 59 | ||
| 60 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Done loading menu items; " + count + " menu items processed."); |
| 61 | } | |
| 62 | ||
| 63 | private int loadDay(JobContext ctx, String date, String commonsCode) throws Exception { | |
| 64 |
1
1. lambda$loadDay$0 : replaced return value with "" for edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob::lambda$loadDay$0 → KILLED |
String mealsJson = withBackoff(ctx, () -> menuService.getJSON(date, commonsCode)); |
| 65 |
1
1. loadDay : negated conditional → KILLED |
if (mealsJson == null) { |
| 66 | // getJSON returns null when the dining commons is closed that day | |
| 67 | return 0; | |
| 68 | } | |
| 69 | ||
| 70 | int count = 0; | |
| 71 | for (JsonNode meal : objectMapper.readTree(mealsJson)) { | |
| 72 | String mealCode = meal.get("code").asText(); | |
| 73 | try { | |
| 74 | List<Entree> entrees = | |
| 75 |
1
1. lambda$loadDay$1 : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob::lambda$loadDay$1 → KILLED |
withBackoff(ctx, () -> menuItemsService.get(date, commonsCode, mealCode)); |
| 76 |
1
1. loadDay : negated conditional → KILLED |
if (!entrees.isEmpty()) { |
| 77 |
1
1. loadDay : Replaced integer addition with subtraction → KILLED |
count += menuItemsService.getOrCreateMenuItems(commonsCode, mealCode, entrees).size(); |
| 78 | } | |
| 79 | } catch (HttpClientErrorException.TooManyRequests e) { | |
| 80 | throw e; | |
| 81 | } catch (RestClientException | JsonProcessingException e) { | |
| 82 | ctx.log("Skipping " + commonsCode + " " + mealCode + " on " + date + ": " + e.getMessage()); | |
| 83 | } | |
| 84 | } | |
| 85 |
1
1. loadDay : replaced int return with 0 for edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob::loadDay → KILLED |
return count; |
| 86 | } | |
| 87 | ||
| 88 | /** Runs call, retrying with exponential backoff if the API responds 429 Too Many Requests. */ | |
| 89 | <T> T withBackoff(JobContext ctx, Callable<T> call) throws Exception { | |
| 90 | long delayMs = initialBackoffMs; | |
| 91 |
1
1. withBackoff : Changed increment from 1 to -1 → TIMED_OUT |
for (int attempt = 0; ; attempt++) { |
| 92 | try { | |
| 93 |
1
1. withBackoff : replaced return value with null for edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob::withBackoff → KILLED |
return call.call(); |
| 94 | } catch (HttpClientErrorException.TooManyRequests e) { | |
| 95 |
2
1. withBackoff : changed conditional boundary → KILLED 2. withBackoff : negated conditional → KILLED |
if (attempt >= maxRetries) { |
| 96 |
1
1. withBackoff : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Still rate limited after " + maxRetries + " retries; giving up."); |
| 97 | throw e; | |
| 98 | } | |
| 99 |
1
1. withBackoff : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Rate limited by UCSB API (429); retrying in " + delayMs + " ms"); |
| 100 |
1
1. withBackoff : removed call to edu/ucsb/cs156/dining/jobs/LoadMenuItemsJob$Sleeper::sleep → KILLED |
sleeper.sleep(delayMs); |
| 101 |
1
1. withBackoff : Replaced long multiplication with division → KILLED |
delayMs *= 2; |
| 102 | } | |
| 103 | } | |
| 104 | } | |
| 105 | } | |
Mutations | ||
| 49 |
1.1 |
|
| 51 |
1.1 |
|
| 54 |
1.1 |
|
| 56 |
1.1 |
|
| 60 |
1.1 |
|
| 64 |
1.1 |
|
| 65 |
1.1 |
|
| 75 |
1.1 |
|
| 76 |
1.1 |
|
| 77 |
1.1 |
|
| 85 |
1.1 |
|
| 91 |
1.1 |
|
| 93 |
1.1 |
|
| 95 |
1.1 2.2 |
|
| 96 |
1.1 |
|
| 99 |
1.1 |
|
| 100 |
1.1 |
|
| 101 |
1.1 |