Problem: The Data.gov.in API was returning parsing errors due to unsupported >= operator in date filters.
Error:
parsing_exception: [match] query does not support [>=]
Solution:
- Removed the unsupported
filters[Arrival_Date][>=]syntax - Implemented client-side filtering for recent data instead of server-side
- Added fallback mechanism for when date range queries fail
Problem: The API doesn't support date range queries, making "recent data" filtering impossible at the API level.
Solution:
- Fetch all available data from the API
- Apply client-side filtering for the last 7 days
- Implemented efficient date parsing and filtering logic
Problem: API errors were not handled gracefully, causing the application to fail completely.
Solution:
- Added retry mechanism for parsing errors
- Implemented fallback to simpler API requests when complex filters fail
- Added comprehensive error logging for debugging
Problem: Limited Chennai market data availability.
Solution:
- Enhanced MANDI_LOCATIONS with more Tamil Nadu markets including:
- Koyambedu Market (Chennai's main wholesale market)
- Multiple districts across Tamil Nadu
- Added debug function to analyze available data
// Before: Unsupported API syntax
params.append('filters[Arrival_Date][>=]', recentDate);
// After: Client-side filtering
if (filters.onlyRecentData !== false && !filters.date) {
// Fetch all data and filter on client side
const sevenDaysAgo = new Date();
sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);
transformedData = transformedData.filter(item => {
const itemDate = new Date(item.arrival_date);
return itemDate >= sevenDaysAgo;
});
}// Retry with simpler parameters if complex query fails
if (error.message.includes('parsing_exception')) {
const simpleParams = new URLSearchParams({
'api-key': API_KEY,
format: 'json',
limit: filters.limit.toString(),
});
// Retry with minimal filters only
}- Added 10+ Tamil Nadu markets to MANDI_LOCATIONS
- Included major markets like Koyambedu, Salem, Coimbatore
- Added debug function to analyze API data structure
✅ filters[State] - Works correctly
✅ filters[District] - Works correctly
✅ filters[Commodity] - Works correctly
✅ filters[Arrival_Date] - Works for exact dates only
❌ filters[Arrival_Date][>=] - Date range operators
❌ filters[Arrival_Date][<=] - Date range operators
❌ Complex nested filters
- Use exact date filters when possible
- Implement client-side filtering for date ranges
- Keep API queries simple to avoid parsing errors
- Use retry logic for robustness
The fixes ensure that:
- No more parsing exception errors
- Recent data filtering works via client-side logic
- Fallback mechanisms prevent complete failures
- Enhanced Tamil Nadu market coverage
- Client-side filtering may increase data transfer
- Consider implementing pagination for large datasets
- Cache recent API responses to reduce repeated calls
- Use specific filters (state, commodity) to limit data volume
- Implement proper caching mechanism
- Add API response optimization
- Consider alternative data sources for better Chennai coverage
- Add data validation and sanitization