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

Endpoint for cakeGraph was added

parent 0e058aa1
No related branches found
No related tags found
No related merge requests found
......@@ -67,4 +67,16 @@ public class WasteController {
return wasteService.getWasteOfCategoryByGroupId(groupId, CategoryUtil.getCategoryName(categoryNumber)).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
/**
* Retrieve information about the cake diagram for a given group ID using a GET request.
*
* @param groupId The ID of the group for which to retrieve the cake diagram information.
* @return A ResponseEntity containing an array of doubles representing the cake diagram information if found,
* or a 404 Not Found response if not found.
*/
@GetMapping("/statistic/cakeGraph/{groupId}")
public ResponseEntity<double[]> getInformationOfCakeGraph(@PathVariable("groupId") long groupId){
return wasteService.getCakeDiagram(groupId).map(ResponseEntity::ok).orElseGet(()->ResponseEntity.notFound().build());
}
}
......@@ -10,6 +10,7 @@ import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import ntnu.idatt2016.v233.SmartMat.repository.group.GroupRepository;
import ntnu.idatt2016.v233.SmartMat.repository.group.WasteRepository;
import ntnu.idatt2016.v233.SmartMat.repository.product.ProductRepository;
import ntnu.idatt2016.v233.SmartMat.util.StatisticUtil;
import org.springframework.stereotype.Service;
import java.util.Optional;
......@@ -64,4 +65,16 @@ public class WasteService {
return wasteRepository.findAllWasteOfOneCategoryFromGroup(groupId,categoryName);
}
/**
* Get the cake diagram for a group of waste.
*
* @param groupId The ID of the group for which to retrieve the cake diagram.
* @return An Optional containing an array of doubles representing the cake diagram for the group,
* or an empty Optional if the group is not found.
*/
public Optional<double[]> getCakeDiagram(long groupId){
Optional<Group> group = groupRepository.findByGroupId(groupId);
return group.map(value -> StatisticUtil.getNumberOfWasteByCategoryName(wasteRepository.findByGroupId(value).get()));
}
}
package ntnu.idatt2016.v233.SmartMat.util;
import ntnu.idatt2016.v233.SmartMat.entity.Waste;
import java.sql.Date;
import java.util.List;
/**
* A utility class for calculating statistics related to waste and CO2 emissions.
*/
public class StatisticUtil {
/**
* Get the annual average amount of CO2 emissions per person.
*
* @param co2List The list of CO2 emissions for a group of people.
* @param numberOfPerson The number of people in the group.
* @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) {
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);
}
/**
* Get the number of waste items for each category name in a list of waste items.
*
* @param wastes The list of waste items for which to calculate the number of items by category name.
* @return An array of doubles representing the number of waste items for each category name.
*/
public static double[] getNumberOfWasteByCategoryName(List<Waste> wastes) {
double[] numberOfWastes = new double[5];
for (Waste waste : wastes) {
switch (waste.getEan().getCategory().getCategoryName()) {
case "meat, fish and chicken" -> numberOfWastes[0] += waste.getAmount();
case "baked goods and grains" -> numberOfWastes[1] += waste.getAmount();
case "dairy and egg" -> numberOfWastes[2] += waste.getAmount();
case "fruit and vegetables" -> numberOfWastes[3] += waste.getAmount();
default -> numberOfWastes[4] += waste.getAmount();
}
}
return numberOfWastes;
}
}
\ No newline at end of file
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