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