Geo resolver
Design & Implementation
Module Information
Module Name:
openg2p-g2p-bridge-geo-resolverLocation:
/openg2p-g2p-bridge-geo-resolver/Primary Implementation:
FarmerGeoResolverImpl
Interface Definition
File: interface/geo_resolver_interface.py
class GeoResolver(BaseService):
def resolve_geo(self, batch_beneficiary_list: List[Dict[str, str]]) -> List[Dict[str, str]]:
"""
Resolves geographic zones for beneficiaries.
Args:
batch_beneficiary_list: List of dicts with keys:
- disbursement_id (str)
- beneficiary_id (str)
Returns:
List of dicts with keys:
- disbursement_id
- beneficiary_id
- administrative_zone_id_large
- administrative_zone_mnemonic_large
- administrative_zone_id_small
- administrative_zone_mnemonic_small
"""
raise NotImplementedError()Reference Implementation: FarmerGeoResolverImpl
File: implementations/farmer_resolver_impl.py
Database Models Used
G2PFarmerRegistry- Farmer/beneficiary registry with geographic location data
Algorithm
The implementation uses a direct lookup and mapping approach:
Key Characteristics
Direct Database Lookup: Uses SQLAlchemy's
select()andwhere()within_()operatorMapping-based Processing: Builds dict for O(1) lookups after initial query
Handles Missing Records: Silently skips beneficiaries not found in registry
Database Engine: Uses registry database
db_engine_registry- Registry database for farmer/beneficiary data
Logging: Uses logger name "farmer_geo_resolver_impl"
Return Data Structure
Factory Pattern
File: factory/geo_resolution_factory.py
Database Model Details
G2PFarmerRegistry Fields Used:
link_registry_id- Beneficiary/farmer ID (lookup key)large_area_id- State/Province IDlarge_area_code- State/Province codesmall_area_id- District/Block IDsmall_area_code- District/Block code
Configuration
File: config.py
Standard OpenG2P configuration pattern. Database connection via Settings.
Key Behaviors
No Exception on Missing Beneficiaries: If a beneficiary ID is not found in registry, it's silently skipped
Order Not Preserved: Results may not be in same order as input
No Validation: Does not validate if geographic zones exist/are valid
Logging:
INFO: When resolution starts, how many beneficiaries input, how many farmer details fetched
WARNING: When no farmer details found for provided beneficiary IDs
Error Handling
No Exceptions Raised: Returns empty list if no matches found (doesn't error)
Graceful Degradation: Skips individual missing beneficiaries, returns partial results
Database Errors: Would propagate SQLAlchemy exceptions if database connection fails
Integration Notes
Input Format
The input is a simple list of dicts, typically coming from a disbursement batch:
Output Format
Returns matching records with geographic zone information, typically used for:
Agency allocation (which agency serves this zone)
Warehouse allocation (which warehouse serves this zone)
Reporting and analytics
Performance Considerations
Single Batch Query: All beneficiary lookups done in one DB query (efficient)
Dict-based Lookup: O(1) lookup time per beneficiary after initial query
No N+1 Queries: Uses
in_()clause instead of looping queries
Implementation Limitations/Notes
Silently omits beneficiaries not found in registry - caller must handle partial results
No de-duplication - if beneficiary appears twice in input, appears twice in output
Direct mapping only - no fuzzy matching or fallback strategies
Depends entirely on farmer registry data being present and accurate
No caching - queries database every time (even for same beneficiaries)
Typical Celery Integration
Last updated
Was this helpful?