- Add admin dashboard with sync controls and data browser - Implement RMM, Auvik, and Addigy organization mappings - Add chunked ticket sync with progress tracking - Implement entity sync service with rate limiting - Add analytics engine and performance optimizer - Create data browser for all PSA entities - Add navigation components and UI improvements - Implement background processing and sync services - Add comprehensive documentation and migration scripts - Update configuration items with multi-system support - Enhance contact management and purchase history - Add issue type assignment and LLM analyzer - Improve error handling and logging utilities
5.2 KiB
5.2 KiB
Chunked Ticket Sync Implementation
Overview
This implementation adds monthly chunking for ticket synchronization to prevent timeouts and API failures when syncing large date ranges.
Problem Solved
- Previous Issue: Syncing tickets over large date ranges (e.g., 2+ years) would often timeout or fail, causing the entire sync to fail
- Solution: Break the sync into monthly chunks, process each chunk independently, and continue even if individual chunks fail
Key Features
1. Monthly Chunking Logic
- Location:
/opt/stacks/pulse/lib/services/entity-sync.ts - Method:
syncTicketsChunked() - Automatically calculates monthly date ranges based on
yearsBackparameter - Processes each month independently with its own API request
- Continues processing even if individual chunks fail
- Aggregates results across all chunks
2. Progress Tracking
- Location:
/opt/stacks/pulse/lib/types/sync.ts - Added
ChunkProgressinterface for detailed tracking - Extended
SyncProgressinterface with chunk-specific fields:currentChunk: Current chunk being processedtotalChunks: Total number of chunkschunkDescription: Human-readable description (e.g., "Jan 2024")
3. Animated Progress Component
- Location:
/opt/stacks/pulse/components/admin/ChunkedSyncProgress.tsx - Displays real-time progress with animated progress bar
- Shows current chunk being processed
- Lists failed chunks with error messages
- Provides completion summary
4. API Endpoint
- Location:
/opt/stacks/pulse/app/api/sync/tickets-chunked/route.ts - Endpoint:
POST /api/sync/tickets-chunked - Parameters:
yearsBack: Number of years to sync (default: 2)triggeredBy: User identifier (default: 'api')
5. UI Integration
- Location:
/opt/stacks/pulse/components/admin/SyncControlPanel.tsx - Added "Chunked Tickets" button with distinctive blue styling
- Integrated progress component that appears during sync
- Disabled other sync buttons while chunked sync is running
Usage
From UI
- Navigate to the Admin Sync page
- Select desired date range (e.g., "Last 2 Years")
- Click "Chunked Tickets" button
- Monitor progress in the animated progress card
- View completion summary or failed chunks
From API
curl -X POST http://localhost:3000/api/sync/tickets-chunked \
-H "Content-Type: application/json" \
-d '{"yearsBack": 2, "triggeredBy": "admin"}'
Technical Details
Chunking Algorithm
// Splits date range into monthly chunks
private calculateMonthlyChunks(yearsBack: number) {
const now = new Date();
const startDate = new Date(now);
startDate.setFullYear(now.getFullYear() - yearsBack);
// Creates array of {startDate, endDate} for each month
// Example for 2 years: ~24 chunks
}
Error Handling
- Each chunk is wrapped in try-catch
- Failed chunks are logged but don't stop the sync
- Failed chunk descriptions are collected and displayed
- Partial success is possible (some chunks succeed, others fail)
Date Filtering
- Uses Autotask API
createDatefield - Filters:
createDate >= chunkStart AND createDate < chunkEnd - Ensures no overlap or gaps between chunks
Benefits
- Reliability: Individual chunk failures don't break entire sync
- Progress Visibility: Users can see exactly which months are being processed
- Timeout Prevention: Smaller API requests are less likely to timeout
- Partial Recovery: Can resume from failed chunks without re-syncing everything
- Better UX: Animated progress bar provides feedback during long operations
Future Enhancements
Recommended Improvements
- WebSocket/SSE Integration: Real-time progress updates instead of simulated progress
- Chunk Retry Logic: Automatically retry failed chunks with exponential backoff
- Configurable Chunk Size: Allow users to choose weekly, monthly, or quarterly chunks
- Resume Capability: Save progress and resume from last successful chunk
- Parallel Processing: Process multiple chunks concurrently (with rate limiting)
- Database Tracking: Store chunk progress in database for persistence
Code Locations for Future Work
- WebSocket Handler: Create
/app/api/sync/tickets-chunked/stream/route.ts - Progress Store: Add Redis or database table for chunk progress
- Retry Logic: Enhance
syncTicketsChunked()method inentity-sync.ts
Testing
Manual Testing Steps
- Set date range to "Last 2 Years" or "Last 5 Years"
- Click "Chunked Tickets" button
- Verify progress bar animates smoothly
- Check console logs for chunk-by-chunk progress
- Verify sync history shows completed records
- Test with intentional API failures to verify error handling
Expected Behavior
- Progress bar should animate from 0% to 100%
- Each chunk should log:
[tickets] Processing chunk X/Y: Month Year - Failed chunks should be listed in red error box
- Completion should show total records processed
Notes
- Current implementation uses simulated progress updates (5-second timeout)
- For production use, implement real-time progress tracking via WebSocket or polling
- Chunked sync is independent of regular sync operations
- Can be run alongside other entity syncs