Agency alloctor
Design & Implementation
Module Information
Module Name:
openg2p-g2p-bridge-agency-allocatorLocation:
/openg2p-g2p-bridge-agency-allocator/Primary Implementation:
AgencyAllocatorRefImpl
Interface Definition
File: interface/agency_allocator_interface.py
class AgencyAllocator(BaseService):
def allocate_agency(
self,
small_geo_list: List[Dict],
benefit_code: Dict,
program: Dict,
) -> List[Dict]:
"""
Allocates agencies to geographic zones.
Args:
small_geo_list: List of dicts with keys:
- batch_control_geo_id
- administrative_zone_id_small
- administrative_zone_mnemonic_small
benefit_code: Dict with keys:
- id
- mnemonic
program: Dict with keys:
- id
- mnemonic
Returns:
List of dicts with keys:
- batch_control_geo_id
- administrative_zone_id_small
- administrative_zone_mnemonic_small
- benefit_code_id
- program_id
- agency_id
- agency_mnemonic
- agency_name
- agency_admin_name
- agency_admin_email
- agency_admin_phone
- agency_additional_attributes
"""
raise NotImplementedError()Reference Implementation: AgencyAllocatorRefImpl
File: implementations/agency_allocator_ref_impl.py
Database Models Used
G2PAgency- Agency master dataG2PAgencyProgramBenefitCode- Authorization matrix (which agencies handle which programs/benefits)G2PAdministrativeAreaSmallAgencyRel- Geographic zone to agency mapping
Algorithm
The implementation uses a two-set intersection approach:
Key Characteristics
Random Selection: Uses Python's
random.choice()to select from eligible agenciesException Handling: Raises exception if no eligible agency found for any geo
Database Engines: Uses separate database connections:
db_engine_pbms- PBMS database for agency master data
Logging: Uses Python logging with logger name "agency_allocator_ref_impl"
Data Types in Response
Factory Pattern
File: factory/agency_allocator_factory.py
The factory returns the reference implementation. Custom implementations would be substituted here.
Configuration
File: config.py
Configuration is handled through Settings class (inherits from BaseSettings). Uses standard OpenG2P configuration pattern via environment variables.
Database Dependencies
Tables Required
G2PAgency
Fields: id, agency_mnemonic, name, admin_name, admin_email, admin_mobile
G2PAgencyProgramBenefitCode
Fields: agency_id, program_id, benefit_code_id, additional_info (JSON)
G2PAdministrativeAreaSmallAgencyRel
Fields: g2p_administrative_area_small_id, g2p_agency_id
Connection
Engine key:
db_engine_pbmsUses SQLAlchemy sessionmaker with expire_on_commit=False
Integration Points
Celery Worker Integration
Called from Celery workers in the main bridge via the Factory pattern.
Typical Celery Task Pattern:
Error Handling
Raises Exception if no eligible agency found for a geographic zone
Logs warnings for missing agencies
No granular error codes - uses generic Exception
Implementation Notes
The function processes all geographic zones in a batch, but if ANY zone has no eligible agency, it raises an exception for the entire batch
The algorithm uses set intersection, which is efficient for large datasets
Agency selection is random - no load balancing or priority logic in reference implementation
Additional attributes are pulled from the authorization table, not the agency master
The implementation does NOT handle the case where program/benefit_code parameters contain None values
Last updated
Was this helpful?