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

Merge branch '242-add-endpoint-to-get-food-waster-of-the-last-4-months' into 'main'

Resolve "Add endpoint to get food waster of the last 4 months."

Closes #242

See merge request idatt2106-v23-03/backend!183
parents 48f1575a e3ac8d89
No related branches found
No related tags found
No related merge requests found
...@@ -79,4 +79,17 @@ public class WasteController { ...@@ -79,4 +79,17 @@ public class WasteController {
return wasteService.getCakeDiagram(groupId).map(ResponseEntity::ok).orElseGet(()->ResponseEntity.notFound().build()); return wasteService.getCakeDiagram(groupId).map(ResponseEntity::ok).orElseGet(()->ResponseEntity.notFound().build());
} }
/**
* Get the information of the last months of a specific group.
*
* @param groupId the id of the group to get the information for
* @return a ResponseEntity object containing an array of doubles representing the waste for each category
* in the last four months, or a not found response if the group does not exist or has no waste data
*/
@GetMapping("/statistic/lastMonths/{groupId}")
public ResponseEntity<double[]> getInformationOfLastMoths(@PathVariable("groupId") long groupId){
return wasteService.getLastMonth(groupId).map(ResponseEntity::ok).orElseGet(()->ResponseEntity.notFound().build());
}
} }
package ntnu.idatt2016.v233.SmartMat.service.group; package ntnu.idatt2016.v233.SmartMat.service.group;
import java.nio.channels.FileChannel;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.List; import java.util.List;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
...@@ -77,4 +78,15 @@ public class WasteService { ...@@ -77,4 +78,15 @@ public class WasteService {
return group.map(value -> StatisticUtil.getNumberOfWasteByCategoryName(wasteRepository.findByGroupId(value).get())); return group.map(value -> StatisticUtil.getNumberOfWasteByCategoryName(wasteRepository.findByGroupId(value).get()));
} }
/**
* Retrieve an optional array of doubles representing the amount of waste produced in each of the last 4 months for a given group.
*
* @param groupId a long representing the id of the group whose waste production statistics are to be retrieved
* @return an optional array of doubles representing the amount of waste produced in each of the last 4 months for the given group,
* or an empty optional if the group could not be found or no waste was produced in the last 4 months
*/
public Optional<double[]> getLastMonth(long groupId) {
Optional<Group> group = groupRepository.findByGroupId(groupId);
return group.map(value -> StatisticUtil.getNumberOfWasteByLastMonth(wasteRepository.findByGroupId(value).get()));
}
} }
...@@ -3,6 +3,9 @@ package ntnu.idatt2016.v233.SmartMat.util; ...@@ -3,6 +3,9 @@ package ntnu.idatt2016.v233.SmartMat.util;
import ntnu.idatt2016.v233.SmartMat.entity.Waste; import ntnu.idatt2016.v233.SmartMat.entity.Waste;
import java.sql.Date; import java.sql.Date;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -47,4 +50,71 @@ public class StatisticUtil { ...@@ -47,4 +50,71 @@ public class StatisticUtil {
} }
return numberOfWastes; return numberOfWastes;
} }
/**
* Calculates the sum of waste for each of the four previous months.
* The amount of waste is converted to kilograms if the unit is in liters, grams, milliliters, centiliters, or deciliters.
*
* @param wastes the list of wastes to calculate the sum from
* @return an array of four doubles representing the sum of waste for each of the last four months,
* in the same order as the months are counted backwards from the current month
*/
public static double[] getNumberOfWasteByLastMonth(List<Waste> wastes){
double[] result = new double[4];
HashMap<Integer,List<Waste>> hashMap = new HashMap<>();
LocalDate localDate = LocalDate.now();
int currentMonth = localDate.getMonthValue();
int currentYear = localDate.getYear();
for(Waste waste : wastes){
LocalDate localDate1 = waste.getTimestamp().toLocalDateTime().toLocalDate();
if(currentMonth == localDate1.getMonthValue() && currentYear == localDate1.getYear()){
hashMap.computeIfAbsent(0, k -> new ArrayList<>());
hashMap.get(0).add(waste);
}
if(Math.abs((currentMonth-1) % 12) == localDate1.getMonthValue() && currentYear == localDate1.getYear()){
hashMap.computeIfAbsent(1, k -> new ArrayList<>());
hashMap.get(1).add(waste);
}
if(Math.abs((currentMonth-2) % 12) == localDate1.getMonthValue() && currentYear == localDate1.getYear()){
hashMap.computeIfAbsent(2, k -> new ArrayList<>());
hashMap.get(2).add(waste);
}
if(Math.abs((currentMonth-3) % 12) == localDate1.getMonthValue() && currentYear == localDate1.getYear()){
hashMap.computeIfAbsent(3, k -> new ArrayList<>());
hashMap.get(3).add(waste);
}
}
for(int i = 0; i < 4; i++){
result[i] = getSumOfWaste(hashMap.get(i));
}
return result;
}
/**
* Calculates the sum of waste for a list of wastes, and converts the amounts to kilograms if the unit is in liters,
* grams, milliliters, centiliters, or deciliters.
*
* @param wastes the list of wastes to calculate the sum from
* @return the sum of waste in kilograms
*/
private static double getSumOfWaste(List<Waste> wastes){
double sum = 0.0;
if(wastes == null){
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;
}
}
return sum;
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment