| 1 | package edu.ucsb.cs156.happiercows.jobs; | |
| 2 | ||
| 3 | import java.time.LocalDateTime; | |
| 4 | import org.springframework.stereotype.Service; | |
| 5 | import edu.ucsb.cs156.happiercows.entities.Commons; | |
| 6 | import edu.ucsb.cs156.happiercows.entities.UserCommons; | |
| 7 | import edu.ucsb.cs156.happiercows.entities.CommonStats; | |
| 8 | import edu.ucsb.cs156.happiercows.repositories.CommonStatsRepository; | |
| 9 | import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository; | |
| 10 | import edu.ucsb.cs156.happiercows.repositories.CommonsRepository; | |
| 11 | import edu.ucsb.cs156.happiercows.services.AverageCowHealthService; | |
| 12 | import edu.ucsb.cs156.happiercows.services.jobs.JobContext; | |
| 13 | import edu.ucsb.cs156.happiercows.services.jobs.JobContextConsumer; | |
| 14 | import lombok.RequiredArgsConstructor; | |
| 15 | import lombok.extern.slf4j.Slf4j; | |
| 16 | ||
| 17 | @Service | |
| 18 | @RequiredArgsConstructor | |
| 19 | @Slf4j | |
| 20 | public class CommonStatsJob implements JobContextConsumer { | |
| 21 | ||
| 22 |     private final AverageCowHealthService averageCowHealthService; | |
| 23 |     private final CommonStatsRepository commonStatsRepository; | |
| 24 |     private final UserCommonsRepository userCommonsRepository; | |
| 25 |     private final CommonsRepository commonsRepository; | |
| 26 | ||
| 27 |     @Override | |
| 28 |     public void accept(JobContext ctx) { | |
| 29 | 
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED | 
        ctx.log("Starting to record common stats."); | 
| 30 |         Iterable<Commons> allCommons = commonsRepository.findAll(); | |
| 31 | ||
| 32 |         for (Commons commons : allCommons) { | |
| 33 | 
1
1. accept : removed call to edu/ucsb/cs156/happiercows/jobs/CommonStatsJob::recordCommonStatsForCommon → KILLED | 
                recordCommonStatsForCommon(ctx, commons); | 
| 34 |         } | |
| 35 |     } | |
| 36 | ||
| 37 |     private void recordCommonStatsForCommon(JobContext ctx, Commons commons) { | |
| 38 |         Long commonId = commons.getId(); | |
| 39 | 
1
1. recordCommonStatsForCommon : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED | 
        ctx.log("Starting to record the common stats for commons: " + commons.getName()); | 
| 40 | ||
| 41 |         Double avgHealth = averageCowHealthService.getAverageCowHealth(commonId); | |
| 42 |         Iterable<UserCommons> userCommonsList = userCommonsRepository.findByCommonsId(commonId); | |
| 43 | ||
| 44 |         int numCows = 0; | |
| 45 |         for (UserCommons userCommons : userCommonsList) { | |
| 46 | 
1
1. recordCommonStatsForCommon : Replaced integer addition with subtraction → KILLED | 
            numCows += userCommons.getNumOfCows(); | 
| 47 |         } | |
| 48 | ||
| 49 |         CommonStats stats = CommonStats.builder() | |
| 50 |             .commonsId(commonId) | |
| 51 |             .numCows(numCows) | |
| 52 |             .avgHealth(avgHealth) | |
| 53 |             .timestamp(LocalDateTime.now()) | |
| 54 |             .build(); | |
| 55 | ||
| 56 |         commonStatsRepository.save(stats); | |
| 57 | 
1
1. recordCommonStatsForCommon : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED | 
        ctx.log("Saved stats for commons: " + commons.getName()); | 
| 58 |     } | |
| 59 | } | |
Mutations | ||
| 29 | 
 
 1.1  | 
|
| 33 | 
 
 1.1  | 
|
| 39 | 
 
 1.1  | 
|
| 46 | 
 
 1.1  | 
|
| 57 | 
 
 1.1  |