| 1 | package edu.ucsb.cs156.happiercows.jobs; | |
| 2 | ||
| 3 | import java.util.Optional; | |
| 4 | ||
| 5 | ||
| 6 | import java.util.Iterator; | |
| 7 | import edu.ucsb.cs156.happiercows.services.jobs.JobContext; | |
| 8 | import edu.ucsb.cs156.happiercows.services.jobs.JobContextConsumer; | |
| 9 | import edu.ucsb.cs156.happiercows.entities.Commons; | |
| 10 | import edu.ucsb.cs156.happiercows.entities.UserCommons; | |
| 11 | import edu.ucsb.cs156.happiercows.entities.CommonsPlus; | |
| 12 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 13 | import edu.ucsb.cs156.happiercows.repositories.CommonsRepository; | |
| 14 | import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository; | |
| 15 | import edu.ucsb.cs156.happiercows.repositories.UserRepository; | |
| 16 | import lombok.Getter; | |
| 17 | import lombok.extern.slf4j.Slf4j; | |
| 18 | import lombok.AllArgsConstructor; | |
| 19 | ||
| 20 | @AllArgsConstructor | |
| 21 | public class UpdateCowHealthJob implements JobContextConsumer { | |
| 22 | ||
| 23 | @Getter | |
| 24 | private CommonsRepository commonsRepository; | |
| 25 | @Getter | |
| 26 | private UserCommonsRepository userCommonsRepository; | |
| 27 | @Getter | |
| 28 | private UserRepository userRepository; | |
| 29 | ||
| 30 | @Override | |
| 31 | public void accept(JobContext ctx) throws Exception { | |
| 32 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("Updating cow health..."); |
| 33 | ||
| 34 | Iterable<Commons> allCommons = commonsRepository.findAll(); | |
| 35 | ||
| 36 | for (Commons commons : allCommons) { | |
| 37 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("Commons " + commons.getName() + ", degradationRate: " + commons.getDegradationRate() + ", carryingCapacity: " + commons.getCarryingCapacity()); |
| 38 | ||
| 39 | int carryingCapacity = commons.getCarryingCapacity(); | |
| 40 | double degradationRate = commons.getDegradationRate(); | |
| 41 | Iterable<UserCommons> allUserCommons = userCommonsRepository.findByCommonsId(commons.getId()); | |
| 42 | ||
| 43 |
1
1. lambda$accept$0 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$accept$0 → KILLED |
Integer totalCows = commonsRepository.getNumCows(commons.getId()).orElseThrow(()->new RuntimeException("Error calling getNumCows(" + commons.getId() + ")")); |
| 44 | ||
| 45 | for (UserCommons userCommons : allUserCommons) { | |
| 46 |
1
1. lambda$accept$1 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$accept$1 → KILLED |
User user = userRepository.findById(userCommons.getUserId()).orElseThrow(()->new RuntimeException("Error calling userRepository.findById(" + userCommons.getUserId() + ")")); |
| 47 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("User: " + user.getFullName() + ", numCows: " + userCommons.getNumOfCows() + ", cowHealth: " + userCommons.getCowHealth()); |
| 48 | ||
| 49 | double newCowHealth = calculateNewCowHealth(userCommons.getCowHealth(), userCommons.getNumOfCows(), totalCows, carryingCapacity, degradationRate); | |
| 50 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log(" old cow health: " + userCommons.getCowHealth() + ", new cow health: " + newCowHealth); |
| 51 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED |
userCommons.setCowHealth(newCowHealth); |
| 52 | userCommonsRepository.save(userCommons); | |
| 53 | } | |
| 54 | } | |
| 55 | ||
| 56 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("Cow health has been updated!"); |
| 57 | } | |
| 58 | ||
| 59 | public static double calculateNewCowHealth( | |
| 60 | double oldCowHealth, | |
| 61 | int numCows, | |
| 62 | int totalCows, | |
| 63 | int carryingCapacity, | |
| 64 | double degradationRate) { | |
| 65 |
2
1. calculateNewCowHealth : changed conditional boundary → KILLED 2. calculateNewCowHealth : negated conditional → KILLED |
if (totalCows <= carryingCapacity) { |
| 66 | // increase cow health but do not exceed 100 | |
| 67 |
2
1. calculateNewCowHealth : Replaced double addition with subtraction → KILLED 2. calculateNewCowHealth : replaced double return with 0.0d for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateNewCowHealth → KILLED |
return Math.min(100, oldCowHealth + (degradationRate)); |
| 68 | } else { | |
| 69 | // decrease cow health, don't go lower than 0 | |
| 70 |
4
1. calculateNewCowHealth : Replaced integer subtraction with addition → KILLED 2. calculateNewCowHealth : Replaced double multiplication with division → KILLED 3. calculateNewCowHealth : Replaced double subtraction with addition → KILLED 4. calculateNewCowHealth : replaced double return with 0.0d for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateNewCowHealth → KILLED |
return Math.max(0, oldCowHealth - Math.min((totalCows - carryingCapacity) * degradationRate, 100)); |
| 71 | } | |
| 72 | } | |
| 73 | ||
| 74 | } | |
Mutations | ||
| 32 |
1.1 |
|
| 37 |
1.1 |
|
| 43 |
1.1 |
|
| 46 |
1.1 |
|
| 47 |
1.1 |
|
| 50 |
1.1 |
|
| 51 |
1.1 |
|
| 56 |
1.1 |
|
| 65 |
1.1 2.2 |
|
| 67 |
1.1 2.2 |
|
| 70 |
1.1 2.2 3.3 4.4 |