Overview
The Resource Service is a FastAPI-based microservice that manages sustainability indicators through an automated wrapper generation and execution system. It leverages AI to dynamically create data collection wrappers and manages their lifecycle from creation to continuous execution.Core Components
The system is built on several key architectural components that work together to provide a complete data collection and management solution.1. FastAPI Application Layer
The main application entry point (app/main.py) orchestrates the service lifecycle:
The application uses an
asynccontextmanager lifespan to handle startup and shutdown events, ensuring proper initialization and cleanup of resources.- CORS configuration for cross-origin requests
- API route registration
- Lifecycle management (startup/shutdown)
- Dependency injection setup
2. Database Layer (MongoDB)
MongoDB stores all persistent data including resources, wrappers, and execution metadata. Primary Collections:resources - Resource Metadata
resources - Resource Metadata
Stores information about sustainability indicators:
wrapper_id: Associated wrapper identifiername: Resource display nametype: Resource classificationstartPeriod: Earliest data point timestampendPeriod: Latest data point timestampdeleted: Soft delete flag
generated_wrappers - Wrapper State
generated_wrappers - Wrapper State
Manages wrapper lifecycle and configuration:
wrapper_id: Unique identifier (UUID)status: Current execution stategenerated_code: AI-generated Python codesource_type: Data source (API, CSV, XLSX)metadata: Indicator metadataexecution_log: Historical eventshigh_water_mark/low_water_mark: Checkpoint timestamps
3. Message Queue (RabbitMQ)
RabbitMQ provides asynchronous task processing and inter-service communication.| Queue Name | Purpose | Message Format |
|---|---|---|
wrapper_creation_queue | Async wrapper generation tasks | {"wrapper_id": "uuid"} |
resource_data | Data points from wrappers | {"resource_id": "id", "points": [...]} |
resource_deleted | Resource deletion notifications | {"resource_id": "id", "wrapper_id": "uuid"} |
collected_data | Raw collected data | Source-specific formats |
The RabbitMQ client uses a channel pool pattern (default 5 channels) to efficiently handle concurrent message publishing and consumption.
4. AI Wrapper Generator
TheWrapperGenerator service uses Google’s Gemini AI to create customized data collection wrappers.
- Extract sample data from source (CSV/XLSX/API)
- Build contextual prompt with indicator metadata
- Call Gemini with tool support (for API sources)
- Validate generated Python code
- Retry on linting errors
- Save to
/app/generated_wrappers/{wrapper_id}.py
5. Wrapper Process Manager
Manages the execution lifecycle of generated wrappers as separate Python processes. Capabilities:- Launch wrappers as background processes
- Monitor process health and status
- Stop running wrappers
- Resume execution after service restart
- Track execution metrics
6. Resource Service
Provides CRUD operations for resources with wrapper integration.Component Interaction Flow
Wrapper Creation Flow
Data Collection Flow
Configuration
The service uses Pydantic Settings for configuration management:Docker Deployment
The service is containerized with Docker Compose:resource_generated_wrappers: Stores generated wrapper Python filesresource_wrapper_logs: Execution logs for debuggingresource_prompts: AI prompt/response debug dataresource_db: MongoDB data directory
Health Monitoring
The service includes comprehensive health checks:The service automatically restarts continuous wrappers that were executing before a service restart, ensuring no data collection gaps.
API Structure
Routes are organized by domain:Next Steps
Resources
Learn about resource structure and management
Wrappers
Understand wrapper generation and execution
Data Sources
Explore supported data source types
API Reference
View complete API documentation