Dashboard functionalities extended

This commit is contained in:
Sidharth Prabhu
2026-04-16 11:45:08 +05:30
parent c1a662e185
commit 0e12702fa4
7 changed files with 129 additions and 40 deletions

View File

@@ -6,6 +6,7 @@ import com.rit.canteen.sales.service.DashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@@ -18,8 +19,10 @@ public class DashboardController {
private DashboardService dashboardService;
@GetMapping("/stats")
public GeneralDashboardData getStats() {
return dashboardService.getGeneralDashboardData();
public GeneralDashboardData getStats(
@RequestParam(required = false) @org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME) java.time.LocalDateTime from,
@RequestParam(required = false) @org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME) java.time.LocalDateTime to) {
return dashboardService.getGeneralDashboardData(from, to);
}
@GetMapping("/recent-orders")

View File

@@ -2,14 +2,16 @@ package com.rit.canteen.sales.model;
public class DashboardStats {
private long totalSales;
private long periodRevenue;
private int activeOrders;
private int dailyCustomers;
private double growth;
public DashboardStats() {}
public DashboardStats(long totalSales, int activeOrders, int dailyCustomers, double growth) {
public DashboardStats(long totalSales, long periodRevenue, int activeOrders, int dailyCustomers, double growth) {
this.totalSales = totalSales;
this.periodRevenue = periodRevenue;
this.activeOrders = activeOrders;
this.dailyCustomers = dailyCustomers;
this.growth = growth;
@@ -18,6 +20,9 @@ public class DashboardStats {
public long getTotalSales() { return totalSales; }
public void setTotalSales(long totalSales) { this.totalSales = totalSales; }
public long getPeriodRevenue() { return periodRevenue; }
public void setPeriodRevenue(long periodRevenue) { this.periodRevenue = periodRevenue; }
public int getActiveOrders() { return activeOrders; }
public void setActiveOrders(int activeOrders) { this.activeOrders = activeOrders; }

View File

@@ -14,6 +14,7 @@ import java.util.Optional;
public interface OrderRepository extends JpaRepository<Order, Long>, JpaSpecificationExecutor<Order> {
List<Order> findByUserIdOrderByCreatedAtDesc(Long userId);
long countByCreatedAtGreaterThanEqual(LocalDateTime startOfDay);
long countByCreatedAtBetween(LocalDateTime start, LocalDateTime end);
List<Order> findByIsArchivedFalseAndCreatedAtBefore(LocalDateTime timestamp);
Optional<Order> findByOrderNumber(String orderNumber);
@@ -23,6 +24,9 @@ public interface OrderRepository extends JpaRepository<Order, Long>, JpaSpecific
@Query("SELECT COUNT(DISTINCT o.userId) FROM Order o WHERE o.createdAt >= :startOfDay")
long countUniqueUsersToday(@Param("startOfDay") LocalDateTime startOfDay);
@Query("SELECT COUNT(DISTINCT o.userId) FROM Order o WHERE o.createdAt >= :start AND o.createdAt <= :end")
long countUniqueUsersInRange(@Param("start") LocalDateTime start, @Param("end") LocalDateTime end);
@Query("SELECT SUM(o.totalAmount) FROM Order o WHERE o.createdAt >= :start AND o.createdAt <= :end")
BigDecimal getRevenuePerPeriod(@Param("start") LocalDateTime start, @Param("end") LocalDateTime end);

View File

@@ -20,14 +20,14 @@ public class DashboardService {
@Autowired
private OrderRepository orderRepository;
public GeneralDashboardData getGeneralDashboardData() {
LocalDateTime startOfToday = LocalDate.now().atStartOfDay();
LocalDateTime endOfToday = LocalDate.now().atTime(LocalTime.MAX);
public GeneralDashboardData getGeneralDashboardData(LocalDateTime from, LocalDateTime to) {
if (from == null) from = LocalDate.now().atStartOfDay();
if (to == null) to = LocalDate.now().atTime(LocalTime.MAX);
DashboardStats stats = getDashboardStats();
DashboardStats stats = getDashboardStats(from, to);
// 1. Store Overview
List<Object[]> storeData = orderRepository.getStoreOverview(startOfToday, endOfToday);
List<Object[]> storeData = orderRepository.getStoreOverview(from, to);
List<Map<String, Object>> storeOverview = new ArrayList<>();
for (Object[] row : storeData) {
Map<String, Object> store = new HashMap<>();
@@ -40,7 +40,7 @@ public class DashboardService {
}
// 2. Hourly Sales
List<Object[]> hourlyData = orderRepository.getHourlySales(startOfToday, endOfToday);
List<Object[]> hourlyData = orderRepository.getHourlySales(from, to);
List<Map<String, Object>> hourlySales = new ArrayList<>();
// Initialize 24 hours
for (int i = 0; i < 24; i += 2) {
@@ -72,7 +72,7 @@ public class DashboardService {
return new GeneralDashboardData(stats, storeOverview, hourlySales, insights);
}
public DashboardStats getDashboardStats() {
public DashboardStats getDashboardStats(LocalDateTime from, LocalDateTime to) {
LocalDateTime startOfToday = LocalDate.now().atStartOfDay();
LocalDateTime endOfToday = LocalDate.now().atTime(LocalTime.MAX);
@@ -82,8 +82,11 @@ public class DashboardService {
BigDecimal totalRevenueRaw = orderRepository.getTotalRevenue();
long totalSales = totalRevenueRaw != null ? totalRevenueRaw.longValue() : 0;
int activeOrders = (int) orderRepository.countByCreatedAtGreaterThanEqual(startOfToday);
int dailyCustomers = (int) orderRepository.countUniqueUsersToday(startOfToday);
BigDecimal periodRevenueRaw = orderRepository.getRevenuePerPeriod(from, to);
long periodRevenue = periodRevenueRaw != null ? periodRevenueRaw.longValue() : 0;
int activeOrders = (int) orderRepository.countByCreatedAtBetween(from, to);
int dailyCustomers = (int) orderRepository.countUniqueUsersInRange(from, to);
BigDecimal todayRevenue = orderRepository.getRevenuePerPeriod(startOfToday, endOfToday);
BigDecimal yesterdayRevenue = orderRepository.getRevenuePerPeriod(startOfYesterday, endOfYesterday);
@@ -99,7 +102,7 @@ public class DashboardService {
growth = 100.0;
}
return new DashboardStats(totalSales, activeOrders, dailyCustomers, growth);
return new DashboardStats(totalSales, periodRevenue, activeOrders, dailyCustomers, growth);
}
public List<Order> getRecentOrders() {