This implementation provides high-performance H1B visa statistics using PostgreSQL functions in Supabase, replacing client-side calculations with optimized database operations.
get_h1b_statistics()- Comprehensive statistics with filteringget_top_h1b_employers()- Top employers with paginationget_h1b_salary_by_job_title()- Salary analysis by job titleget_h1b_trends()- Time-based trend analysisget_h1b_statistics_by_state()- Geographic statistics
- Database Indexes - Optimized for common query patterns
- Caching - Client-side caching with configurable TTL
- Error Handling - Retry logic and graceful degradation
- Validation - Input validation before database calls
- Custom Hooks - Easy-to-use React hooks for statistics
- Components - Pre-built UI components
- Real-time Updates - Auto-refresh capabilities
- Total applications
- Average/median/min/max salaries
- Certification rates
- Top employers
- Status breakdown
- Trends over time (monthly/quarterly/yearly)
- Geographic distribution by state
- Salary analysis by job title
- Employer performance metrics
# Apply the migration to create functions and indexes
supabase db pushOr manually run the SQL file in Supabase SQL Editor:
-- Copy and paste contents of supabase/migrations/20240125_create_h1b_statistics_functions.sqlRun the test script to verify everything works:
-- Copy and paste contents of test-h1b-functions.sql in Supabase SQL Editorimport { H1BStatisticsComponent } from './components/h1b/H1BStatistics';
import { useH1BStatistics } from './hooks/useH1BStatistics';
// Basic usage
function MyComponent() {
return <H1BStatisticsComponent enableCache={true} />;
}
// With filters
function FilteredStats() {
const filters = {
employer: 'Google',
minSalary: 100000,
status: 'CERTIFIED'
};
return <H1BStatisticsComponent filters={filters} />;
}
// Using hooks directly
function CustomComponent() {
const { statistics, loading, error } = useH1BStatistics({
filters: { employer: 'Microsoft' },
enableCache: true
});
if (loading) return <div>Loading...</div>;
if (error) return <div>Error: {error}</div>;
return <div>Total: {statistics?.totalApplications}</div>;
}SELECT get_h1b_statistics(
p_employer_filter TEXT DEFAULT NULL,
p_status_filter TEXT DEFAULT NULL,
p_job_title_filter TEXT DEFAULT NULL,
p_min_salary NUMERIC DEFAULT NULL,
p_max_salary NUMERIC DEFAULT NULL,
p_search_term TEXT DEFAULT NULL
);SELECT get_top_h1b_employers(
p_limit INTEGER DEFAULT 50,
p_offset INTEGER DEFAULT 0,
p_search_term TEXT DEFAULT NULL
);SELECT get_h1b_trends(
p_start_date DATE DEFAULT NULL,
p_end_date DATE DEFAULT NULL,
p_group_by TEXT DEFAULT 'month' -- 'month', 'quarter', 'year'
);const { statistics, loading, error, refresh } = useH1BStatistics({
filters?: Partial<H1BFilters>,
enableCache?: boolean,
autoRefresh?: boolean,
refreshInterval?: number
});const { employers, loading, error, totalCount } = useTopH1BEmployers(
limit?: number,
searchTerm?: string
);const { trends, loading, error } = useH1BTrends(
startDate?: Date,
endDate?: Date,
groupBy?: 'month' | 'quarter' | 'year'
);// Get statistics with filters
const stats = await H1BStatisticsService.getStatistics(filters);
// Get cached statistics (5-minute cache)
const cachedStats = await H1BStatisticsService.getCachedStatistics(filters);
// Get statistics with retry logic
const reliableStats = await H1BStatisticsService.getStatisticsWithRetry(filters, 3);
// Clear cache
H1BStatisticsService.clearCache();- β Large data transfers from database
- β Client-side processing overhead
- β Slower response times
- β Limited filtering capabilities
- β Minimal data transfer (only results)
- β Database-optimized calculations
- β Fast response times with indexes
- β Advanced filtering and aggregation
- β Caching for repeated queries
const { statistics } = useH1BStatistics();
console.log(`Total applications: ${statistics?.totalApplications}`);const { statistics } = useH1BStatistics({
filters: {
employer: 'Google',
minSalary: 150000,
maxSalary: 300000
}
});const { employers } = useTopH1BEmployers(10);
employers.forEach(emp => {
console.log(`${emp.name}: ${emp.count} applications, $${emp.averageSalary} avg`);
});const { trends } = useH1BTrends(
new Date('2023-01-01'),
new Date('2023-12-31'),
'quarter'
);The system includes comprehensive error handling:
const { statistics, error } = useH1BStatistics({
filters: { employer: 'InvalidEmployer' }
});
if (error) {
// Handle error gracefully
console.error('Statistics error:', error);
}-
Functions not found
- Ensure migration has been applied
- Check function permissions in Supabase
-
Slow performance
- Verify indexes are created
- Check query execution plans
- Consider adding more specific indexes
-
Cache issues
- Clear cache:
H1BStatisticsService.clearCache() - Disable cache temporarily for testing
- Clear cache:
-- Check function performance
EXPLAIN ANALYZE SELECT get_h1b_statistics();
-- Monitor index usage
SELECT * FROM pg_stat_user_indexes WHERE relname = 'h1b_records';Ensure your h1b_applications table has these columns:
case_number(string)case_status(string)employer_name(string)job_title(string)wage_rate_of_pay_from(numeric)wage_rate_of_pay_to(numeric)received_date(date)worksite_state(string)employer_state(string)
- Development: Run migration locally with Supabase CLI
- Production: Apply migration through Supabase dashboard
- Testing: Use provided test script to verify functionality
Monitor function performance in Supabase:
- Go to Database β Functions
- Check execution times and error rates
- Monitor index usage in Database β Indexes
This PostgreSQL-based approach provides significant performance improvements and better scalability for H1B statistics analysis.