Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@ComponentScan(basePackages = { "com.podzilla", "com.Podzilla" })
@EnableScheduling
public class AnalyticsApplication {

public static void main(final String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.transaction.annotation.Transactional;

import com.Podzilla.analytics.api.projections.fulfillment.FulfillmentTimeProjection;
import com.Podzilla.analytics.api.projections.order.OrderFailureRateProjection;
Expand Down Expand Up @@ -164,4 +166,16 @@ List<RevenueSummaryProjection> findRevenueSummaryByPeriod(
List<RevenueByCategoryProjection> findRevenueByCategory(
@Param("startDate") LocalDateTime startDate,
@Param("endDate") LocalDateTime endDate);

@Modifying
@Transactional
@Query("DELETE FROM OrderItem oi "
+ "WHERE oi.order.id IN (SELECT o.id FROM Order o "
+ "WHERE o.orderPlacedTimestamp < :cutoff)")
void deleteOrderItemsOlderThan(@Param("cutoff") LocalDateTime cutoff);

@Modifying
@Transactional
@Query("DELETE FROM Order o WHERE o.orderPlacedTimestamp < :cutoff")
void deleteOrdersOlderThan(@Param("cutoff") LocalDateTime cutoff);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.Podzilla.analytics.scheduling;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.Podzilla.analytics.repositories.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;

@Component
public class CleanupScheduler {

private final OrderRepository orderRepository;

@Autowired
public CleanupScheduler(final OrderRepository orderRepository) {
this.orderRepository = orderRepository;
}

// // Runs at 2:00 AM on the first day of every month
@Scheduled(cron = "0 0 2 1 * ?")
@Transactional
public void monthlyCleanup() {
System.out.println("Starting monthly cleanup");
Copy link

Copilot AI May 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a dedicated logging framework (e.g., SLF4J) instead of System.out.println for better log management.

Copilot uses AI. Check for mistakes.
LocalDateTime cutoff = LocalDateTime.now().minusMonths(1);
// Delete order items first
orderRepository.deleteOrderItemsOlderThan(cutoff);
// Then delete orders
orderRepository.deleteOrdersOlderThan(cutoff);
System.out.println("Monthly cleanup completed");
}
}