What is a Resource?
A resource represents a sustainability indicator in the system. Each resource tracks a specific metric (like CO2 emissions, temperature, or economic indicators) and maintains metadata about the data being collected. Resources serve as the public-facing entity that client applications interact with, while wrappers handle the behind-the-scenes data collection.Resource Schema
The resource data model is defined inapp/schemas/resource.py:
Field Descriptions
Unique MongoDB ObjectId for the resource
UUID of the associated wrapper that collects data for this resource
Human-readable name of the sustainability indicatorExamples:
- “CO2 Emissions - Manufacturing Sector”
- “Average Temperature - Lisbon”
- “Renewable Energy Percentage”
Classification of the resource. Typically
"sustainability_indicator"Timestamp of the earliest data point collected. Initially
null, updated when first data arrivesTimestamp of the most recent data point collected. Updated continuously as new data arrives
Soft delete flag. Resources are never hard-deleted to maintain data integrity
Resource Lifecycle
1. Creation
Resources are typically created automatically when a wrapper is generated withauto_create_resource: true:
The system validates that the specified
wrapper_id exists before creating a resource. This ensures referential integrity between resources and wrappers.2. Data Collection
Once created, the associated wrapper begins collecting data. As data points flow in:- Wrapper fetches data from its configured source
- Data points are published to RabbitMQ queues
startPeriodandendPeriodare automatically updated- Data is routed to the data service for storage
3. Active Use
While active, resources can be:- Queried via the REST API
- Updated to modify name or type
- Monitored for health and data collection status
- Stopped temporarily by stopping the wrapper
4. Deletion
Resources use soft deletion to preserve historical data:Resource-Wrapper Relationship
Resources and wrappers have a one-to-one relationship: Key Points:- Each resource is linked to exactly one wrapper via
wrapper_id - The wrapper stores the
resource_idfor bidirectional navigation - Resources represent the what (indicator being tracked)
- Wrappers represent the how (data collection mechanism)
CRUD Operations
Create
Read
Update
Partial updates are supported via
ResourcePatch, which allows updating only specific fields without providing all required fields.Delete
Error Handling
The resource service implements comprehensive error handling:Common Error Scenarios
Invalid Resource ID
Invalid Resource ID
Error:
ValueError: Invalid resource IDCause: Provided ID is not a valid MongoDB ObjectIdSolution: Ensure the ID is a 24-character hexadecimal stringWrapper Not Found
Wrapper Not Found
Error:
ValueError: Wrapper with ID 'xyz' does not existCause: Attempting to create a resource for a non-existent wrapperSolution: Create the wrapper first, or verify the wrapper_id is correctDuplicate Resource
Duplicate Resource
Error:
ValueError: Resource with this identifier already existsCause: Database unique constraint violationSolution: Check for existing resources with the same wrapper_idQuerying Resources
The service provides flexible querying capabilities:Data Segments
While resources track metadata, actual data points are stored separately in data segments:Data segments are managed by a separate data service. Resources only track the time range (
startPeriod to endPeriod) of collected data.Best Practices
Always validate wrapper existence
Before creating a resource, ensure the associated wrapper exists and is properly configured.
Use meaningful names
Resource names should clearly describe what is being measured, including relevant context like location or timeframe.
Monitor period updates
Track
startPeriod and endPeriod to ensure data collection is working as expected.Implement soft deletes
Never hard-delete resources. Use the
deleted flag to maintain historical records and data integrity.Next Steps
Wrappers
Learn how wrappers collect data for resources
Data Sources
Understand the different data source types
API Reference
View the complete Resources API documentation
Architecture
Explore the overall system architecture