Skip to content

Commit e10578a

Browse files
authored
Merge pull request #27 from CIAT-DAPA/develop
Add new endpoints for indicators and historical data
2 parents 1d850b6 + 826e92d commit e10578a

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,4 @@ coverage.xml
146146

147147
# Pre-commit
148148
.pre-commit-config.yaml.backup
149+
src/populate_sample_data.py

src/routes/get_climate_historical_indicator.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,46 @@ def get_by_indicator_and_location(
203203
end_date=d.end_date
204204
) for d in data
205205
]
206+
207+
@router.get("/by-location-date-period", response_model=List[ClimateHistoricalIndicatorRecord])
208+
def get_by_location_date_period(
209+
location_id: int = Query(..., description="Location ID"),
210+
start_date: str = Query(..., description="Start date (YYYY-MM-DD)"),
211+
end_date: str = Query(..., description="End date (YYYY-MM-DD)"),
212+
period: str = Query(..., description="Period type (e.g. monthly, yearly)")
213+
):
214+
"""
215+
Returns historical indicator records filtered by:
216+
- Location ID
217+
- Date range (inclusive)
218+
- Period type
219+
220+
Example: /by-location-date-period?location_id=123&start_date=2023-01-01&end_date=2023-12-31&period=monthly
221+
"""
222+
service = ClimateHistoricalIndicatorService()
223+
224+
# Validación adicional para fechas
225+
if start_date > end_date:
226+
raise HTTPException(
227+
status_code=400,
228+
detail="Start date cannot be after end date"
229+
)
230+
231+
period_upper = period.upper()
232+
data = service.get_by_location_date_period(location_id, start_date, end_date, period_upper)
233+
234+
return [
235+
ClimateHistoricalIndicatorRecord(
236+
id=d.id,
237+
indicator_id=d.indicator_id,
238+
indicator_name=d.indicator.name if d.indicator else None,
239+
indicator_short_name=d.indicator.short_name if d.indicator else None,
240+
indicator_unit=d.indicator.unit if d.indicator else None,
241+
location_id=d.location_id,
242+
location_name=d.location.name if d.location else None,
243+
value=d.value,
244+
period=d.period,
245+
start_date=d.start_date,
246+
end_date=d.end_date
247+
) for d in data
248+
]

src/routes/get_locations_by_id.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def get_locations_by_id(
3535
"admin1_ext_id": location.admin_2.admin_1.ext_id if location.admin_2 and location.admin_2.admin_1 and location.admin_2.admin_1.ext_id else None,
3636
"country_id": location.admin_2.admin_1.country.id if location.admin_2 and location.admin_2.admin_1 and location.admin_2.admin_1.country else None,
3737
"country_name": location.admin_2.admin_1.country.name if location.admin_2 and location.admin_2.admin_1 and location.admin_2.admin_1.country else None,
38-
"country_iso2": location.admin_2.admin_1.country.iso2 if location.admin_2 and location.admin_2.admin_1 and location.admin_2.admin_1.country else None
38+
"country_iso2": location.admin_2.admin_1.country.iso2 if location.admin_2 and location.admin_2.admin_1 and location.admin_2.admin_1.country else None,
39+
"source": location.source.name if location.source else None
3940
}
4041

4142
result.append(flat_loc)

src/routes/get_mng_indicators.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,33 @@ def get_by_type(
114114
updated_at=d.updated_at
115115
) for d in data
116116
]
117+
@router.get("/by-id", response_model=List[Indicator])
118+
def get_by_id(
119+
id: int = Query(..., description="Indicator ID")
120+
):
121+
"""
122+
Returns indicators filtered by ID.
123+
- **id**: ID of the indicator to filter by.
124+
"""
125+
service = MngIndicatorService()
126+
127+
try:
128+
data = service.get_by_id(id)
129+
except Exception as e:
130+
raise HTTPException(status_code=400, detail=f"Invalid indicator ID: {id}")
131+
return [
132+
Indicator(
133+
id=d.id,
134+
name=d.name,
135+
short_name=d.short_name,
136+
unit=d.unit,
137+
type=d.type,
138+
description=d.description,
139+
enable=d.enable,
140+
registered_at=d.registered_at,
141+
updated_at=d.updated_at
142+
) for d in data
143+
]
117144

118145
@router.get("/all-enabled", response_model=List[Indicator])
119146
def get_all_enabled():

0 commit comments

Comments
 (0)