Dashboard made robust

This commit is contained in:
Sidharth Prabhu
2026-04-21 01:10:45 +05:30
parent ee98c1c45e
commit 7b9473266f
13 changed files with 104 additions and 61 deletions

View File

@@ -16,4 +16,9 @@ public class BackendApplication {
SpringApplication.run(BackendApplication.class, args);
}
@jakarta.annotation.PostConstruct
public void init() {
java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("Asia/Kolkata"));
System.out.println("[TIMEZONE] Global JVM TimeZone set to: " + java.util.TimeZone.getDefault().getID());
}
}

View File

@@ -128,6 +128,10 @@ public class OrderController {
@PostMapping
@org.springframework.transaction.annotation.Transactional
public ResponseEntity<?> placeOrder(@RequestBody Order order) {
System.out.println("[REVENUE-TRACE] Incoming Place Order Request -> User: " + order.getUserId() +
" | Total: " + order.getTotalAmount() +
" | Items: " + (order.getItems() != null ? order.getItems().size() : 0));
// 1. Pre-validation and linking
if (order.getItems() == null || order.getItems().isEmpty()) {
return ResponseEntity.badRequest().body(Map.of("success", false, "message", "Order must have items"));
@@ -164,6 +168,9 @@ public class OrderController {
// 3. Complete Order Details
for (OrderItem item : order.getItems()) {
item.setOrder(order);
if (item.getStallName() == null || item.getStallName().isEmpty() || item.getStallName().equals("Unknown Stall")) {
item.setStallName("RIT Canteen");
}
}
LocalDateTime now = LocalDateTime.now();
@@ -189,7 +196,16 @@ public class OrderController {
// 5. Final Save
Order savedOrder = orderRepository.save(order);
System.out.println("Placed Order: " + savedOrder.getId() + " -> Display ID: #" + displayId);
System.out.println("[REVENUE-TRACE] Saved Order: " + savedOrder.getOrderNumber() +
" | Display ID: #" + savedOrder.getDisplayOrderId() +
" | CreatedAt: " + savedOrder.getCreatedAt() +
" | Payment: " + savedOrder.getPaymentMethod());
if (savedOrder.getItems() != null) {
savedOrder.getItems().forEach(item ->
System.out.println("[REVENUE-TRACE] Saved Item: " + item.getProductName() +
" | Stall: " + item.getStallName()));
}
return ResponseEntity.ok(Map.of(
"success", true,

View File

@@ -3,16 +3,16 @@ package com.rit.canteen.sales.model;
public class TrendingItem {
private String name;
private String category;
private long qty;
private String image;
private long orderCount;
private String imageUrl;
public TrendingItem() {}
public TrendingItem(String name, String category, long qty, String image) {
public TrendingItem(String name, String category, long orderCount, String imageUrl) {
this.name = name;
this.category = category;
this.qty = qty;
this.image = image;
this.orderCount = orderCount;
this.imageUrl = imageUrl;
}
public String getName() { return name; }
@@ -21,9 +21,9 @@ public class TrendingItem {
public String getCategory() { return category; }
public void setCategory(String category) { this.category = category; }
public long getQty() { return qty; }
public void setQty(long qty) { this.qty = qty; }
public long getOrderCount() { return orderCount; }
public void setOrderCount(long orderCount) { this.orderCount = orderCount; }
public String getImage() { return image; }
public void setImage(String image) { this.image = image; }
public String getImageUrl() { return imageUrl; }
public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; }
}

View File

@@ -35,8 +35,9 @@ public class DashboardService {
private TokenTransactionRepository tokenTransactionRepository;
public GeneralDashboardData getGeneralDashboardData(LocalDateTime from, LocalDateTime to) {
if (from == null) from = LocalDate.now().atStartOfDay();
if (to == null) to = LocalDate.now().atTime(LocalTime.MAX);
java.time.ZoneId zone = java.time.ZoneId.of("Asia/Kolkata");
if (from == null) from = java.time.LocalDate.now(zone).atStartOfDay();
if (to == null) to = java.time.LocalDate.now(zone).atTime(java.time.LocalTime.MAX);
System.out.println("[DIAGNOSTIC] Final timestamp range for service logic: " + from + " to " + to);
DashboardStats stats = getDashboardStats(from, to);
@@ -47,11 +48,17 @@ public class DashboardService {
List<Map<String, Object>> storeOverview = new ArrayList<>();
for (Object[] row : storeData) {
Map<String, Object> store = new HashMap<>();
store.put("name", row[0] != null ? row[0] : "Unknown Stall");
store.put("sale", row[1] != null ? row[1] : 0);
store.put("orders", row[2] != null ? row[2] : 0);
store.put("taxes", 0); // Placeholder
store.put("purchase", 0); // Placeholder
String stallName = (row[0] != null && !row[0].toString().equals("Unknown Stall")) ? row[0].toString() : "RIT Canteen";
BigDecimal saleVal = row[1] != null ? (BigDecimal) row[1] : BigDecimal.ZERO;
long orderCount = row[2] != null ? ((Number) row[2]).longValue() : 0;
System.out.println("[REVENUE-TRACE] Store Overview Result -> Stall: " + stallName + " | Sales: " + saleVal + " | Orders: " + orderCount);
store.put("name", stallName);
store.put("sale", saleVal);
store.put("orders", orderCount);
store.put("taxes", 0);
store.put("purchase", 0);
storeOverview.add(store);
}
@@ -86,7 +93,7 @@ public class DashboardService {
String imageData = (String) row[3];
// Format image data for frontend
String imageUrl = imageData != null ? (imageData.startsWith("http") ? imageData : (imageData.startsWith("data:") ? imageData : "data:image/png;base64," + imageData)) : "https://images.unsplash.com/photo-1546069901-ba9599a7e63c?w=200&q=80";
String imageUrl = imageData != null ? (imageData.startsWith("http") ? imageData : (imageData.startsWith("data:") ? imageData : "data:image/png;base64," + imageData)) : null;
trendingItems.add(new TrendingItem(name, category, qty, imageUrl));
}
@@ -103,7 +110,7 @@ public class DashboardService {
? BigDecimal.valueOf(stats.getTotalSales()).divide(BigDecimal.valueOf(stats.getActiveOrders()), 2, RoundingMode.HALF_UP)
: BigDecimal.ZERO;
Map<String, String> avgInsight = new HashMap<>();
avgInsight.put("text", "" + avg + " average order value! Either everyone's hungry or just living large 🔥😋");
avgInsight.put("text", "R" + avg + " average order value! Either everyone's hungry or just living large 🔥😋");
avgInsight.put("color", "bg-emerald-50 text-emerald-600 border-emerald-100");
insights.add(avgInsight);
@@ -113,7 +120,7 @@ public class DashboardService {
insights.add(customerInsight);
Map<String, String> revenueInsight = new HashMap<>();
revenueInsight.put("text", "RIT Canteen clocked " + String.format("%,d", stats.getTotalSales()) + " — ka-ching! That's called business booming 💸📈");
revenueInsight.put("text", "RIT Canteen clocked R" + String.format("%,d", stats.getTotalSales()) + " — ka-ching! That's called business booming 💸📈");
revenueInsight.put("color", "bg-blue-50 text-blue-600 border-blue-100");
insights.add(revenueInsight);
} else {
@@ -128,11 +135,13 @@ public class DashboardService {
}
public DashboardStats getDashboardStats(LocalDateTime from, LocalDateTime to) {
LocalDateTime startOfToday = LocalDate.now().atStartOfDay();
LocalDateTime endOfToday = LocalDate.now().atTime(LocalTime.MAX);
System.out.println("[REVENUE-TRACE] Dashboard Request Range: " + from + " to " + to);
java.time.ZoneId zone = java.time.ZoneId.of("Asia/Kolkata");
LocalDateTime startOfToday = LocalDate.now(zone).atStartOfDay();
LocalDateTime endOfToday = LocalDate.now(zone).atTime(LocalTime.MAX);
LocalDateTime startOfYesterday = LocalDate.now().minusDays(1).atStartOfDay();
LocalDateTime endOfYesterday = LocalDate.now().minusDays(1).atTime(LocalTime.MAX);
LocalDateTime startOfYesterday = LocalDate.now(zone).minusDays(1).atStartOfDay();
LocalDateTime endOfYesterday = LocalDate.now(zone).minusDays(1).atTime(LocalTime.MAX);
System.out.println("[DIAGNOSTIC] Fetching Total Revenue...");
BigDecimal totalRevenueRaw = tokenTransactionRepository.sumByType(com.rit.canteen.sales.model.TokenTransaction.TransactionType.TOPUP);
@@ -146,6 +155,8 @@ public class DashboardService {
int activeOrders = (int) orderRepository.countByCreatedAtBetween(from, to);
int dailyCustomers = (int) orderRepository.countUniqueUsersInRange(from, to);
System.out.println("[REVENUE-TRACE] Active Orders in Range: " + activeOrders + " | Unique Customers: " + dailyCustomers);
BigDecimal todayRevenue = tokenTransactionRepository.sumByTypeInRange(com.rit.canteen.sales.model.TokenTransaction.TransactionType.TOPUP, startOfToday, endOfToday);
BigDecimal yesterdayRevenue = tokenTransactionRepository.sumByTypeInRange(com.rit.canteen.sales.model.TokenTransaction.TransactionType.TOPUP, startOfYesterday, endOfYesterday);

View File

@@ -9,6 +9,7 @@ spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.jdbc.time_zone=Asia/Kolkata
# Master Account Credentials
app.master.username=admin