Skip to content

Conversation

@layoutd
Copy link
Contributor

@layoutd layoutd commented Oct 24, 2025

Replaces inefficient date array building with direct timestamp calculation.

Builds on: #182

Problem

For each order, the old code built an array of all dates in the range (e.g., 664 dates for 2 years), then used array_rand(). For 500 orders over 2 years, this created 332,000 unnecessary date strings.

Solution

Calculate random offset directly using timestamps instead of building arrays.

Impact

  • Instant date calculation vs building 664-element arrays per order
  • Eliminates wasted memory allocation
  • Same uniform random distribution
  • Orders are created in chronological order (smaller order ID = earlier order)

Tests

Test 1: Batch generation with date range produces chronological orders

wp wc generate orders 10 --date-start=2024-01-01 --date-end=2024-12-31

Expected: Order dates should be in ascending or equal order (no date inversions where later ID has earlier date)

Test 2: Same-day date range edge case

wp wc generate orders 5 --date-start=2024-06-15 --date-end=2024-06-15

Expected: All orders created with date 2024-06-15 (different times, randomized)

Test 3: Error handling for failed order generation

# Attempt to create orders when no products exist (should skip failed orders gracefully)
wp wc generate orders 5

Expected: Error logged but batch continues; indicates 0 orders generated in the end.

Test 4: Large multi-year batch maintains chronological order

wp wc generate orders 100 --date-start=2023-01-01 --date-end=2024-12-31

Expected: Date range covers the specified period with proper distribution

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements chronological ordering for batch-generated orders by pre-generating and sorting dates before order creation. When date-start is specified in batch mode, orders created with lower IDs will have earlier or equal dates, ensuring a logical temporal sequence.

  • Pre-generates dates for batch operations when date-start is provided
  • Refactors date generation logic to use timestamp-based calculations instead of array enumeration
  • Adds new generate_batch_dates() method to handle batch date generation with chronological sorting

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@layoutd layoutd marked this pull request as ready for review October 30, 2025 01:09
Base automatically changed from add-order-coupon-refund-ratios to trunk October 30, 2025 12:33
@layoutd layoutd force-pushed the optimize-order-date-generation branch from b042261 to b37c787 Compare October 31, 2025 22:07
@layoutd
Copy link
Contributor Author

layoutd commented Oct 31, 2025

Related to Test 3, I've added another PR because I don't think "Success: 0 orders generated…" is a very accurate message. ("Task failed successfully"): #189

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants