Spin x,y axis on CatMonthModel

This commit is contained in:
Tim
2025-06-15 20:45:56 +02:00
parent 4ea84ac0fd
commit f6996aea70
4 changed files with 23 additions and 26 deletions

View File

@@ -1,6 +1,6 @@
package de.htwsaar.webshop.controller;
import de.htwsaar.webshop.model.MonthlyCatModel;
import de.htwsaar.webshop.model.CatMonthModel;
import de.htwsaar.webshop.service.SessionService;
import de.htwsaar.webshop.service.StatisticsService;
import jakarta.servlet.http.HttpServletRequest;
@@ -32,9 +32,9 @@ public class StatisticsController {
}
@RequestMapping(value = STATISTICS_VOLUME, method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<MonthlyCatModel<Integer>> getMonthlySalesVolume(HttpServletRequest request,
@RequestParam(value = PARAM_SESSION) UUID session,
@RequestParam(value = PARAM_EMAIL) String email) {
public ResponseEntity<CatMonthModel<Integer>> getMonthlySalesVolume(HttpServletRequest request,
@RequestParam(value = PARAM_SESSION) UUID session,
@RequestParam(value = PARAM_EMAIL) String email) {
logRequest(request);
if (!sessionService.isAdmin(session, email)) {
log.warn("Invalid session requesting Admin {}", session);
@@ -44,9 +44,9 @@ public class StatisticsController {
}
@RequestMapping(value = STATISTICS_REVENUE, method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<MonthlyCatModel<Integer>> getMonthlyRevenue(HttpServletRequest request,
@RequestParam(value = PARAM_SESSION) UUID token,
@RequestParam(value = PARAM_EMAIL) String email) {
public ResponseEntity<CatMonthModel<Integer>> getMonthlyRevenue(HttpServletRequest request,
@RequestParam(value = PARAM_SESSION) UUID token,
@RequestParam(value = PARAM_EMAIL) String email) {
logRequest(request);
if (!sessionService.isAdmin(token, email)) {
log.warn("Invalid session requesting Admin {}", token);

View File

@@ -9,7 +9,6 @@ import java.util.Map;
@Getter
@Setter
@AllArgsConstructor
public class MonthlyCatModel<T extends Number> {
Map<Long, Map<String, T>> monthCategoryMap;
public class CatMonthModel<T extends Number> {
Map<String, Map<Long, T>> catMonthMap;
}

View File

@@ -1,10 +1,10 @@
package de.htwsaar.webshop.service;
import de.htwsaar.webshop.model.MonthlyCatModel;
import de.htwsaar.webshop.model.CatMonthModel;
public interface StatisticsService {
MonthlyCatModel<Integer> getSalesVolume();
CatMonthModel<Integer> getSalesVolume();
MonthlyCatModel<Integer> getSalesRevenue();
CatMonthModel<Integer> getSalesRevenue();
}

View File

@@ -1,6 +1,6 @@
package de.htwsaar.webshop.service.impl;
import de.htwsaar.webshop.model.MonthlyCatModel;
import de.htwsaar.webshop.model.CatMonthModel;
import de.htwsaar.webshop.repository.entities.OrderItem;
import de.htwsaar.webshop.service.OrderService;
import de.htwsaar.webshop.service.StatisticsService;
@@ -25,32 +25,30 @@ public class StatisticsServiceImpl implements StatisticsService {
}
//returns Map<unix milli timestamp, Map<Category, T>>
private <T extends Number> MonthlyCatModel<T> getMonthCategoryMap(Function<OrderItem, T> mappingFunction,
BinaryOperator<T> reduceFunction,
T defaultValue) {
Map<Long, Map<String, T>> map = new HashMap<>();
private <T extends Number> CatMonthModel<T> getMonthCategoryMap(Function<OrderItem, T> mappingFunction,
BinaryOperator<T> reduceFunction,
T defaultValue) {
Map<String, Map<Long, T>> map = new HashMap<>();
orderService.getTimeSortedOrders(TimeUtil.nowMonthsAgo(12), 12).forEach( (k,v) -> {
log.info("Month {} has {}", k, v.size());
map.putIfAbsent(k, new HashMap<>());
v.forEach( (order) ->
order.getOrderItems().forEach((item) -> {
//log.info(" OrderItem {} has cat {}", item, item.getArticle().getCategory());
map.get(k).putIfAbsent(item.getArticle().getCategory(), defaultValue);
map.get(k).computeIfPresent(item.getArticle().getCategory(), (cat,left ) -> reduceFunction.apply(left, mappingFunction.apply(item)));
map.putIfAbsent(item.getArticle().getCategory(), new HashMap<>());
map.get(item.getArticle().getCategory()).putIfAbsent(k, defaultValue);
map.get(item.getArticle().getCategory()).computeIfPresent(k, (timestamp,left ) -> reduceFunction.apply(left, mappingFunction.apply(item)));
})
);
log.info("Month has {} cats", map.get(k).size());
});
return new MonthlyCatModel<>(map);
return new CatMonthModel<>(map);
}
@Override
public MonthlyCatModel<Integer> getSalesVolume() {
public CatMonthModel<Integer> getSalesVolume() {
return getMonthCategoryMap(OrderItem::getAmount, Integer::sum, 0);
}
@Override
public MonthlyCatModel<Integer> getSalesRevenue() {
public CatMonthModel<Integer> getSalesRevenue() {
return getMonthCategoryMap(item -> item.getArticle().getPrice100(), Integer::sum, 0);
}
}