| 1 | package edu.ucsb.cs156.happiercows.jobs; | |
| 2 | import edu.ucsb.cs156.happiercows.entities.Commons; | |
| 3 | import edu.ucsb.cs156.happiercows.entities.CommonsPlus; | |
| 4 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 5 | import edu.ucsb.cs156.happiercows.entities.UserCommons; | |
| 6 | import edu.ucsb.cs156.happiercows.repositories.CommonsRepository; | |
| 7 | import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository; | |
| 8 | import edu.ucsb.cs156.happiercows.repositories.UserRepository; | |
| 9 | import edu.ucsb.cs156.happiercows.services.jobs.JobContext; | |
| 10 | import edu.ucsb.cs156.happiercows.services.jobs.JobContextConsumer; | |
| 11 | import edu.ucsb.cs156.happiercows.services.CommonsPlusBuilderService; | |
| 12 | import edu.ucsb.cs156.happiercows.strategies.CowHealthUpdateStrategy; | |
| 13 | import lombok.AllArgsConstructor; | |
| 14 | import lombok.Getter; | |
| 15 | ||
| 16 | @AllArgsConstructor | |
| 17 | public class UpdateCowHealthJob implements JobContextConsumer { | |
| 18 | ||
| 19 | @Getter | |
| 20 | private CommonsRepository commonsRepository; | |
| 21 | @Getter | |
| 22 | private UserCommonsRepository userCommonsRepository; | |
| 23 | @Getter | |
| 24 | private UserRepository userRepository; | |
| 25 | @Getter | |
| 26 | private CommonsPlusBuilderService commonsPlusBuilderService; | |
| 27 | ||
| 28 | @Override | |
| 29 | public void accept(JobContext ctx) throws Exception { | |
| 30 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("Updating cow health..."); |
| 31 | Iterable<Commons> allCommons = commonsRepository.findAll(); | |
| 32 | Iterable<CommonsPlus> allCommonsPlus = commonsPlusBuilderService.convertToCommonsPlus(allCommons); | |
| 33 | ||
| 34 | for (CommonsPlus commonsPlus : allCommonsPlus) { | |
| 35 | Commons commons = commonsPlus.getCommons(); | |
| 36 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::runUpdateJobInCommons → KILLED |
runUpdateJobInCommons(commons, commonsPlus, commonsPlusBuilderService, commonsRepository, userCommonsRepository, ctx); |
| 37 | | |
| 38 | } | |
| 39 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("Cow health has been updated!"); |
| 40 | } | |
| 41 | ||
| 42 | public static double calculateNewCowHealthUsingStrategy( | |
| 43 | CowHealthUpdateStrategy strategy, | |
| 44 | CommonsPlus commonsPlus, | |
| 45 | UserCommons userCommons, | |
| 46 | int totalCows | |
| 47 | ) { | |
| 48 | var health = strategy.calculateNewCowHealth(commonsPlus, userCommons, totalCows); | |
| 49 |
1
1. calculateNewCowHealthUsingStrategy : replaced double return with 0.0d for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateNewCowHealthUsingStrategy → KILLED |
return Math.max(0, Math.min(health, 100)); |
| 50 | } | |
| 51 | ||
| 52 | public static void calculateCowDeaths(UserCommons userCommons, JobContext ctx) { | |
| 53 |
1
1. calculateCowDeaths : negated conditional → KILLED |
if (userCommons.getCowHealth() == 0.0) { |
| 54 |
2
1. calculateCowDeaths : Replaced integer addition with subtraction → KILLED 2. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowDeaths → KILLED |
userCommons.setCowDeaths(userCommons.getCowDeaths() + userCommons.getNumOfCows()); |
| 55 |
1
1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setNumOfCows → KILLED |
userCommons.setNumOfCows(0); |
| 56 |
1
1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED |
userCommons.setCowHealth(100.0); |
| 57 | ||
| 58 |
1
1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log(" " + userCommons.getCowDeaths() + " cows for this user died." ); |
| 59 | } | |
| 60 | } | |
| 61 | ||
| 62 | public static void runUpdateJobInCommons(Commons commons, CommonsPlus commonsPlus, CommonsPlusBuilderService commonsPlusBuilderService, CommonsRepository commonsRepository, UserCommonsRepository userCommonsRepository, JobContext ctx){ | |
| 63 |
1
1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("Commons " + commons.getName() + ", degradationRate: " + commons.getDegradationRate() + ", effectiveCapacity: " + commonsPlus.getEffectiveCapacity()); |
| 64 | ||
| 65 |
1
1. lambda$runUpdateJobInCommons$0 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$runUpdateJobInCommons$0 → KILLED |
int numUsers = commonsRepository.getNumUsers(commons.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumUsers(" + commons.getId() + ")")); |
| 66 | ||
| 67 |
1
1. runUpdateJobInCommons : negated conditional → KILLED |
if (numUsers==0) { |
| 68 |
1
1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("No users in this commons, skipping"); |
| 69 | return; | |
| 70 | } | |
| 71 | | |
| 72 | int carryingCapacity = commonsPlus.getEffectiveCapacity(); | |
| 73 | Iterable<UserCommons> allUserCommons = userCommonsRepository.findByCommonsId(commons.getId()); | |
| 74 | ||
| 75 |
1
1. lambda$runUpdateJobInCommons$1 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$runUpdateJobInCommons$1 → KILLED |
Integer totalCows = commonsRepository.getNumCows(commons.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumCows(" + commons.getId() + ")")); |
| 76 | ||
| 77 |
2
1. runUpdateJobInCommons : changed conditional boundary → KILLED 2. runUpdateJobInCommons : negated conditional → KILLED |
var isAboveCapacity = totalCows > carryingCapacity; |
| 78 |
1
1. runUpdateJobInCommons : negated conditional → KILLED |
var cowHealthUpdateStrategy = isAboveCapacity ? commons.getAboveCapacityHealthUpdateStrategy() : commons.getBelowCapacityHealthUpdateStrategy(); |
| 79 | ||
| 80 | for (UserCommons userCommons : allUserCommons) { | |
| 81 | User user = userCommons.getUser(); | |
| 82 | ||
| 83 | var newCowHealth = calculateNewCowHealthUsingStrategy(cowHealthUpdateStrategy, commonsPlusBuilderService.toCommonsPlus(commons), userCommons, totalCows); | |
| 84 |
1
1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("User: " + user.getFullName() + ", numCows: " + userCommons.getNumOfCows() + ", cowHealth: " + userCommons.getCowHealth()); |
| 85 | ||
| 86 | double oldHealth = userCommons.getCowHealth(); | |
| 87 |
1
1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED |
userCommons.setCowHealth(newCowHealth); |
| 88 |
1
1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateCowDeaths → KILLED |
calculateCowDeaths(userCommons, ctx); |
| 89 | ||
| 90 |
1
1. runUpdateJobInCommons : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log(" old cow health: " + oldHealth + ", new cow health: " + userCommons.getCowHealth()); |
| 91 | userCommonsRepository.save(userCommons); | |
| 92 | } | |
| 93 | } | |
| 94 | } | |
Mutations | ||
| 30 |
1.1 |
|
| 36 |
1.1 |
|
| 39 |
1.1 |
|
| 49 |
1.1 |
|
| 53 |
1.1 |
|
| 54 |
1.1 2.2 |
|
| 55 |
1.1 |
|
| 56 |
1.1 |
|
| 58 |
1.1 |
|
| 63 |
1.1 |
|
| 65 |
1.1 |
|
| 67 |
1.1 |
|
| 68 |
1.1 |
|
| 75 |
1.1 |
|
| 77 |
1.1 2.2 |
|
| 78 |
1.1 |
|
| 84 |
1.1 |
|
| 87 |
1.1 |
|
| 88 |
1.1 |
|
| 90 |
1.1 |