diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/WasteController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/WasteController.java index b2eb154fc6dc44bc3da0b96e2f105b2117e4d757..ff28172f2623650fcb510fb0e702bcd8df6143bb 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/WasteController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/WasteController.java @@ -103,4 +103,16 @@ public class WasteController { public ResponseEntity<Double> getLostMoney(@PathVariable("groupId") long groupId){ return wasteService.getLostMoney(groupId).map(ResponseEntity::ok).orElseGet(()->ResponseEntity.notFound().build()); } + + /** + * Retrieves the amount of CO2 emitted annually per person in a specific group. + * + * @param groupId the ID of the group to retrieve the statistic for + * @return a ResponseEntity containing the amount of CO2 emitted annually per person in the group, + * or a ResponseEntity with HTTP status 404 (not found) if the group or data is not found + */ + @GetMapping("/statistic/annuallyCO2/{groupId}") + public ResponseEntity<Double> getCO2Annually(@PathVariable("groupId") long groupId){ + return wasteService.getCO2PerPerson(groupId).map(ResponseEntity::ok).orElseGet(()->ResponseEntity.notFound().build()); + } } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/GroupRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/GroupRepository.java index 6363b44990e2779e83fa589673c684939701231d..e1f3278481f0b498efb7b1b4ba4beb203f3a39ec 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/GroupRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/GroupRepository.java @@ -47,4 +47,7 @@ public interface GroupRepository extends JpaRepository<Group, Long> { Optional<Group> findByLinkCode(String linkCode); + @Query(value = "SELECT count(*) FROM user_group where group_id = :groupId", nativeQuery = true) + int countAllUserInGroup(@Param("groupId") long groupId); + } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/WasteService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/WasteService.java index 3ab784ad7c1d996a79fb2e46c6cfdd01213e1870..dffcfd8e60fe99dc0ee5dbd1747405992dd77318 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/WasteService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/WasteService.java @@ -1,7 +1,9 @@ package ntnu.idatt2016.v233.SmartMat.service.group; import java.nio.channels.FileChannel; +import java.sql.Date; import java.sql.Timestamp; +import java.util.ArrayList; import java.util.List; import lombok.AllArgsConstructor; import ntnu.idatt2016.v233.SmartMat.dto.request.WasteRequest; @@ -100,4 +102,19 @@ public class WasteService { Optional<Group> group = groupRepository.findByGroupId(groupId); return group.map(value -> StatisticUtil.getLostMoneyInLastMonth(wasteRepository.findByGroupId(value).get())); } + + /** + * Calculates the annual average CO2 emissions per person in the specified group. + * + * @param groupId the ID of the group for which to calculate CO2 emissions + * @return an Optional containing the annual average CO2 emissions per person, or empty if the group has no users or does not exist + */ + public Optional<Double> getCO2PerPerson(long groupId){ + Optional<Group> group = groupRepository.findByGroupId(groupId); + int number = groupRepository.countAllUserInGroup(groupId); + if(number == 0 || group.isEmpty()) return Optional.empty(); + List<Waste> wastes = wasteRepository.findByGroupId(group.get()).get(); + return Optional.of(StatisticUtil.getAnnualAverage(wastes,number)); + } + } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtil.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtil.java index 9bc24cb424e394e59165ffb9351771a74a0f4fd4..9c6f63d7dd01cf2dbbec19d354468afd43fdb4e6 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtil.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtil.java @@ -13,9 +13,92 @@ import java.util.List; /** * A utility class for calculating statistics related to waste and CO2 emissions. + * @author Pedro Cardona + * @version 1.0 + * @since 03.05.2023 */ public class StatisticUtil { + private final static double CO2_KG_MEAT = (99.48 + 39.72 + 33.3 + 26.87 + 12.31 + 9.87 )/6.0; + private final static double CO2_KG_DAIRY = (23.88 + 4.67 + 3.15)/3; + private final static double CO2_KG_BAKED = (4.45 + 1.7 + 1.57)/3; + private final static double CO2_KG_VEGETABLE = (2.09 + 0.98 + 0.86 + 0.46)/4; + private final static double CO2_KG_OTHER = (CO2_KG_MEAT + CO2_KG_DAIRY + CO2_KG_BAKED + CO2_KG_VEGETABLE)/4.0; + + + /** + * Calculates the annual average amount of CO2 emissions per person based on a list of waste objects and a number of persons. + * @param wastes a list of waste objects + * @param number the number of persons + * @return the annual average amount of CO2 emissions per person + */ + public static double getAnnualAverage(List<Waste> wastes, int number){ + Timestamp timestamp = new Timestamp(System.currentTimeMillis()); + List<Double> CO2 = new ArrayList<>(); + for(Waste waste: wastes){ + if(waste.getTimestamp().before(timestamp)) timestamp = waste.getTimestamp(); + CO2.add(getCO2ByCategory(waste.getEan().getCategory().getCategoryName()) * getCorrectRelation(waste.getEan().getUnit()) * waste.getAmount()); + } + return getAnnualAveragePerPerson(CO2,number,timestamp); + } + + + /** + * Returns the amount of CO2 emissions per kg for a given category of waste. + * @param category the waste category + * @return the amount of CO2 emissions per kg + */ + private static double getCO2ByCategory(String category){ + switch (category){ + case "meat, fish and chicken" -> { + return CO2_KG_MEAT; + } + case "baked goods and grains" -> { + return CO2_KG_BAKED; + } + case "dairy and egg" ->{ + return CO2_KG_DAIRY; + } + case "fruit and vegetables" ->{ + return CO2_KG_VEGETABLE; + } + default -> { + return CO2_KG_OTHER; + } + } + } + + /** + * Returns the correct CO2 relation for a given unit of waste. + * @param unit the unit of waste + * @return the correct CO2 relation + */ + private static double getCorrectRelation(String unit){ + switch (unit){ + case "l" -> { + return 0.998; + } + case "kg" -> { + return 1.0; + } + case "g" -> { + return 0.001; + } + case "ml" -> { + return 0.000998; + } + case "cl" -> { + return 0.00998; + } + case "dl" -> { + return 0.0998; + } + default -> { + return 0.1; + } + } + } + /** * Get the annual average amount of CO2 emissions per person. * @@ -24,13 +107,13 @@ public class StatisticUtil { * @param firstDate The date on which the group started tracking their CO2 emissions. * @return The annual average amount of CO2 emissions per person. */ - private static double getAnnualAveragePerPerson(List<Double> co2List, int numberOfPerson, Date firstDate) { + private static double getAnnualAveragePerPerson(List<Double> co2List, int numberOfPerson, Timestamp firstDate) { java.util.Date currentDate = new java.util.Date(); long diffInMillis = currentDate.getTime() - firstDate.getTime(); long diffInDays = diffInMillis / (24 * 60 * 60 * 1000); double co2Sum = 0.0; for (double c02 : co2List) co2Sum += c02; - return co2Sum / (((double) diffInDays / 365.0) * (double) numberOfPerson); + return co2Sum / (((double) (diffInDays +1) / 365.0) * (double) numberOfPerson); } /** @@ -106,15 +189,7 @@ public class StatisticUtil { wastes = new ArrayList<>(); } for(Waste waste: wastes){ - switch (waste.getEan().getUnit()){ - case "l" -> sum += waste.getAmount() * 0.998; - case "kg" -> sum += waste.getAmount(); - case "g" -> sum += waste.getAmount() * 0.001; - case "ml" -> sum += waste.getAmount() * 0.000998; - case "cl" -> sum += waste.getAmount() * 0.00998; - case "dl" -> sum += waste.getAmount() * 0.0998; - default -> sum += 0.1 * waste.getAmount(); - } + sum += getCorrectRelation(waste.getEan().getUnit()) * waste.getAmount(); } return sum; } diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtils.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtils.java index 3560688c40d2613caf1228f2cfaf11227920866f..fd7f879b95f0162ffc037c36b0b17fb60b5d9f7c 100644 --- a/src/test/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtils.java +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtils.java @@ -1,11 +1,6 @@ package ntnu.idatt2016.v233.SmartMat.util; public class StatisticUtils { - private final static double CO2_KG_MEAT = (99.48 + 39.72 + 33.3 + 26.87 + 12.31 + 9.87 )/6.0; - private final static double CO2_KG_DAIRY = (23.88 + 4.67 + 3.15)/3; - private final static double CO2_KG_BAKED = (4.45 + 1.7 + 1.57)/3; - private final static double CO2_KG_VEGETABLE = (2.09 + 0.98 + 0.86 + 0.46)/4; - private final static double CO2_KG_OTHER = (CO2_KG_MEAT + CO2_KG_DAIRY + CO2_KG_BAKED + CO2_KG_VEGETABLE)/4.0; }