Skip to content
Snippets Groups Projects
Commit 901dffca authored by Pedro Pablo Cardona Arroyave's avatar Pedro Pablo Cardona Arroyave
Browse files

The endpoint to get the annuakky CO2 per person was added

parent c0a9d87b
No related branches found
No related tags found
No related merge requests found
...@@ -103,4 +103,16 @@ public class WasteController { ...@@ -103,4 +103,16 @@ public class WasteController {
public ResponseEntity<Double> getLostMoney(@PathVariable("groupId") long groupId){ public ResponseEntity<Double> getLostMoney(@PathVariable("groupId") long groupId){
return wasteService.getLostMoney(groupId).map(ResponseEntity::ok).orElseGet(()->ResponseEntity.notFound().build()); 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());
}
} }
...@@ -47,4 +47,7 @@ public interface GroupRepository extends JpaRepository<Group, Long> { ...@@ -47,4 +47,7 @@ public interface GroupRepository extends JpaRepository<Group, Long> {
Optional<Group> findByLinkCode(String linkCode); Optional<Group> findByLinkCode(String linkCode);
@Query(value = "SELECT count(*) FROM user_group where group_id = :groupId", nativeQuery = true)
int countAllUserInGroup(@Param("groupId") long groupId);
} }
package ntnu.idatt2016.v233.SmartMat.service.group; package ntnu.idatt2016.v233.SmartMat.service.group;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.sql.Date;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.dto.request.WasteRequest; import ntnu.idatt2016.v233.SmartMat.dto.request.WasteRequest;
...@@ -100,4 +102,19 @@ public class WasteService { ...@@ -100,4 +102,19 @@ public class WasteService {
Optional<Group> group = groupRepository.findByGroupId(groupId); Optional<Group> group = groupRepository.findByGroupId(groupId);
return group.map(value -> StatisticUtil.getLostMoneyInLastMonth(wasteRepository.findByGroupId(value).get())); 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));
}
} }
...@@ -13,9 +13,92 @@ import java.util.List; ...@@ -13,9 +13,92 @@ import java.util.List;
/** /**
* A utility class for calculating statistics related to waste and CO2 emissions. * 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 { 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. * Get the annual average amount of CO2 emissions per person.
* *
...@@ -24,13 +107,13 @@ public class StatisticUtil { ...@@ -24,13 +107,13 @@ public class StatisticUtil {
* @param firstDate The date on which the group started tracking their CO2 emissions. * @param firstDate The date on which the group started tracking their CO2 emissions.
* @return The annual average amount of CO2 emissions per person. * @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(); java.util.Date currentDate = new java.util.Date();
long diffInMillis = currentDate.getTime() - firstDate.getTime(); long diffInMillis = currentDate.getTime() - firstDate.getTime();
long diffInDays = diffInMillis / (24 * 60 * 60 * 1000); long diffInDays = diffInMillis / (24 * 60 * 60 * 1000);
double co2Sum = 0.0; double co2Sum = 0.0;
for (double c02 : co2List) co2Sum += c02; 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 { ...@@ -106,15 +189,7 @@ public class StatisticUtil {
wastes = new ArrayList<>(); wastes = new ArrayList<>();
} }
for(Waste waste: wastes){ for(Waste waste: wastes){
switch (waste.getEan().getUnit()){ sum += getCorrectRelation(waste.getEan().getUnit()) * waste.getAmount();
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();
}
} }
return sum; return sum;
} }
......
package ntnu.idatt2016.v233.SmartMat.util; package ntnu.idatt2016.v233.SmartMat.util;
public class StatisticUtils { 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;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment