Configuration Migration Implementation Guide
๐ Current Statusโ
The configuration migration has been started with the following components implemented:
โ Completed Componentsโ
Configuration Context (
src/contexts/ConfigurationContext.jsx)- Loads configuration data from
/api/Configuration/all - Provides caching with 1-hour expiration
- Includes fallback to cached data on API failure
- Helper methods for accessing configuration data
- Loads configuration data from
Migration Utilities (
src/utils/configurationMigration.jsx)- Backward compatibility layer
- Legacy constant mappings
- Migration status checking
- Configuration validation
Updated Units Context (
src/contexts/UnitsContext.jsx)- Now uses configuration data when available
- Falls back to direct API calls
- Maintains backward compatibility
App Integration (
src/App.jsx)- ConfigurationProvider added to context hierarchy
- Proper provider ordering
Test Component (
src/components/ConfigurationTest.jsx)- Visual verification of configuration loading
- Migration status display
- Data validation
Migration Constants (
src/constants_migrated.jsx)- Bridge file for backward compatibility
- Hook for configuration-based constants
- Legacy constant exports
๐ Next Stepsโ
Phase 1: API Testing (Immediate)โ
Test Configuration Endpoint
# Test the configuration endpoint
curl http://localhost:5000/api/Configuration/allVerify Data Structure
- Check that the API returns the expected JSON structure
- Validate entity types, metrics, and visualization configs
- Ensure constants are properly formatted
Test Frontend Integration
- Navigate to
/aboutpage to see the ConfigurationTest component - Verify configuration loads without errors
- Check migration status
- Navigate to
Phase 2: Component Migration (High Priority)โ
2.1 Update Entity Type Componentsโ
Files to Update:
src/components/EntityTypeDropdown.jsxsrc/components/EntitySearchModal.jsxsrc/components/GenericDropdown.jsx
Migration Pattern:
// Before
import { ALL_ENTITY_TYPES } from '../constants';
// After
import { useConfiguration } from '../contexts/ConfigurationContext';
const MyComponent = () => {
const { getEntityTypeIds } = useConfiguration();
const entityTypes = getEntityTypeIds();
// ...
};
2.2 Update Metric Componentsโ
Files to Update:
src/components/QualityHydraulicsDropdowns.jsxsrc/components/HydrQualChart.jsxsrc/components/ScadaChart.jsx
Migration Pattern:
// Before
import { METRICS, AGGREGATOR_MAP } from '../constants';
// After
import { useConfiguration } from '../contexts/ConfigurationContext';
const MyComponent = () => {
const { getMetrics, getAggregatorsForMetric } = useConfiguration();
const metrics = getMetrics();
const aggregators = getAggregatorsForMetric('Flow');
// ...
};
2.3 Update Visualization Componentsโ
Files to Update:
src/map_utils/esri/HeatmapLayer.jsxsrc/components/EsriMap.jsxsrc/components/Esri3DMap.jsx
Migration Pattern:
// Before
import { HEATMAP_COLOR_STOPS, HEATMAP_RENDERER_CONFIG } from '../constants';
// After
import { useConfiguration } from '../contexts/ConfigurationContext';
const MyComponent = () => {
const { getVisualizationConfigForMetric } = useConfiguration();
const config = getVisualizationConfigForMetric('Flow');
const colorStops = config?.colorStops || [0, 0.2, 0.7, 0.995, 1];
// ...
};
Phase 3: Utility Functions (Medium Priority)โ
3.1 Update Field Name Resolutionโ
Files to Update:
src/utils/- All fetch utilitiessrc/map_utils/esri/- Map utilities
Migration Pattern:
// Before
import { ATTRIBUTE_TO_API_FIELD } from '../constants';
// After
import { useConfiguration } from '../contexts/ConfigurationContext';
const MyComponent = () => {
const { configuration } = useConfiguration();
const fieldName = configuration?.fieldMappings?.[attribute] || attribute;
// ...
};
3.2 Update API Endpoint Mappingsโ
Files to Update:
src/utils/fetchHydraulics.jsxsrc/utils/fetchQualityTimeseries.jsxsrc/utils/fetchSpecies.jsx
Migration Pattern:
// Before
import { API_ENDPOINTS_HYDRAULICS } from '../constants';
// After
import { useConfiguration } from '../contexts/ConfigurationContext';
const MyComponent = () => {
const { configuration } = useConfiguration();
const endpoint = configuration?.apiEndpoints?.hydraulics?.[entityType];
// ...
};
Phase 4: Constants Cleanup (Low Priority)โ
4.1 Remove Hardcoded Constantsโ
Files to Clean:
src/constants.jsx- Remove migrated constants- Keep only non-configuration constants (API endpoints, UI constants, etc.)
4.2 Update Importsโ
Files to Update:
- All files importing from
constants.jsx - Replace with
constants_migrated.jsxor direct configuration usage
๐งช Testing Strategyโ
1. Unit Testingโ
Create tests for the new configuration system:
// src/__tests__/ConfigurationContext.test.jsx
import { render, screen, waitFor } from '@testing-library/react';
import { ConfigurationProvider, useConfiguration } from '../contexts/ConfigurationContext';
const TestComponent = () => {
const { getEntityTypes, loading, error } = useConfiguration();
return (
<div>
{loading && <div>Loading...</div>}
{error && <div>Error: {error}</div>}
<div>Entity Types: {getEntityTypes().length}</div>
</div>
);
};
2. Integration Testingโ
Test the complete configuration flow:
// src/__tests__/ConfigurationIntegration.test.jsx
import { render, screen } from '@testing-library/react';
import ConfigurationTest from '../components/ConfigurationTest';
test('Configuration loads and displays data', async () => {
render(<ConfigurationTest />);
await waitFor(() => {
expect(screen.getByText(/Entity Types/)).toBeInTheDocument();
});
});
3. End-to-End Testingโ
Test with real API:
# Start the backend API
# Start the frontend
# Navigate to /about
# Verify ConfigurationTest component shows data
๐ง Configuration API Structureโ
The expected API response structure:
{
"entityTypes": [
{
"id": "Tank",
"name": "Tank",
"category": "Node",
"properties": ["Id", "Index", "XCoord", "YCoord", "InitVolume"],
"metrics": ["Head", "Demand", "Pressure", "Level", "TankVolume"]
}
],
"metrics": [
{
"id": "Flow",
"name": "Flow",
"category": "Hydraulic",
"aggregators": ["avg", "sum", "min", "max"],
"precision": 2,
"units": "cfs"
}
],
"visualizationConfigs": [
{
"metricId": "Flow",
"colorStops": [0, 0.001, 0.3, 0.4, 1],
"renderer": {
"radius": 100,
"minWeight": 0,
"maxWeight": 1500,
"maxDensity": 0.1,
"minDensity": 0
}
}
],
"constants": {
"aggregators": ["avg", "sum", "min", "max"],
"flowUnits": {
"US": "cfs",
"SI": "lps"
},
"units": {
"Flow": "cfs",
"Pressure": "psi",
"Head": "ft"
}
},
"timestamp": "2024-01-15T10:30:00Z"
}
๐จ Error Handlingโ
Configuration Loading Errorsโ
- API Unavailable: Fall back to cached configuration
- Invalid Data: Use legacy constants as fallback
- Network Errors: Show error message and retry option
Migration Errorsโ
- Missing Configuration: Use legacy constants
- Invalid Migration: Log error and continue with fallback
- Component Errors: Graceful degradation
๐ Performance Considerationsโ
Caching Strategyโ
- Configuration Cache: 1-hour expiration
- Component Cache: Memoized configuration data
- API Cache: HTTP caching headers
Loading Optimizationโ
- Lazy Loading: Load configuration only when needed
- Progressive Enhancement: Start with legacy constants
- Background Refresh: Update configuration in background
๐ Rollback Planโ
If issues arise during migration:
- Immediate Rollback: Revert to
constants.jsx - Partial Rollback: Use
constants_migrated.jsxwith fallbacks - Gradual Rollback: Migrate components back one by one
๐ Success Metricsโ
Functional Metricsโ
- All configuration data loads from API
- No hardcoded constants in migrated components
- Backward compatibility maintained
- No performance regression
Quality Metricsโ
- Configuration loads within 2 seconds
- Error rate < 1%
- Cache hit rate > 80%
- Zero breaking changes
๐ฏ Next Sprint Goalsโ
- Complete Phase 1: API testing and validation
- Start Phase 2: Migrate 3-5 key components
- Create Tests: Unit and integration tests
- Documentation: Update component documentation
๐ Notesโ
- The migration is designed to be backward compatible
- Gradual migration approach allows for safe rollback
- Configuration caching improves performance
- Error boundaries prevent application crashes
- Test component provides visual verification
This implementation provides a solid foundation for the configuration migration while maintaining system stability and allowing for incremental progress.