Skip to content

Commit

Permalink
HDX-10213 Humanitarian Needs category column integration - all in one
Browse files Browse the repository at this point in the history
  • Loading branch information
IanHopkinson committed Oct 9, 2024
1 parent 74b310c commit 27f490b
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 108 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ venv/
coverage.xml

*.pg_restore

src/*
5 changes: 1 addition & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
"program": "main.py",
"console": "integratedTerminal",
"justMyCode": true,
"env":{
// "HAPI_DB_NAME": "hapi",
"HAPI_USE_VAT": "false"
}
"env":{"HAPI_USE_VAT": "true"}
}
],

Expand Down
22 changes: 5 additions & 17 deletions hdx_hapi/db/dao/humanitarian_needs_view_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@
apply_reference_period_filter,
)
from hdx_hapi.endpoints.util.util import PaginationParams, ReferencePeriodParameters
from hapi_schema.utils.enums import DisabledMarker, Gender, PopulationGroup, PopulationStatus
from hapi_schema.utils.enums import PopulationStatus


async def humanitarian_needs_view_list(
pagination_parameters: PaginationParams,
ref_period_parameters: ReferencePeriodParameters,
db: AsyncSession,
admin2_ref: Optional[int] = None,
gender: Optional[Gender] = None,
age_range: Optional[str] = None,
disabled_marker: Optional[DisabledMarker] = None,
category: Optional[str] = None,
sector_code: Optional[str] = None,
population_group: Optional[PopulationGroup] = None,
population_status: Optional[PopulationStatus] = None,
population_min: Optional[int] = None,
population_max: Optional[int] = None,
Expand All @@ -42,16 +39,10 @@ async def humanitarian_needs_view_list(
):
query = select(HumanitarianNeedsView)

if gender:
query = query.where(HumanitarianNeedsView.gender == gender)
if age_range:
query = query.where(HumanitarianNeedsView.age_range == age_range)
if disabled_marker:
query = query.where(HumanitarianNeedsView.disabled_marker == disabled_marker)
if category:
query = query.where(HumanitarianNeedsView.category.icontains(category))
if sector_code:
query = query.where(HumanitarianNeedsView.sector_code.icontains(sector_code))
if population_group:
query = query.where(HumanitarianNeedsView.population_group == population_group)
if population_status:
query = query.where(HumanitarianNeedsView.population_status == population_status)

Expand Down Expand Up @@ -85,12 +76,9 @@ async def humanitarian_needs_view_list(
query = apply_pagination(query, pagination_parameters)
query = query.order_by(
HumanitarianNeedsView.admin2_ref,
HumanitarianNeedsView.gender,
HumanitarianNeedsView.age_range,
HumanitarianNeedsView.category,
HumanitarianNeedsView.sector_code,
HumanitarianNeedsView.population_group,
HumanitarianNeedsView.population_status,
HumanitarianNeedsView.disabled_marker,
HumanitarianNeedsView.reference_period_start,
)

Expand Down
8 changes: 1 addition & 7 deletions hdx_hapi/db/models/views/all_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

from hapi_schema.utils.enums import (
CommodityCategory,
DisabledMarker,
EventType,
IPCPhase,
IPCType,
Expand Down Expand Up @@ -283,14 +282,9 @@ class HumanitarianNeedsView(Base):
__table__ = humanitarian_needs_view
resource_hdx_id: Mapped[str] = column_property(humanitarian_needs_view.c.resource_hdx_id)
admin2_ref: Mapped[int] = column_property(humanitarian_needs_view.c.admin2_ref)
gender: Mapped[Gender] = column_property(humanitarian_needs_view.c.gender)
age_range: Mapped[str] = column_property(humanitarian_needs_view.c.age_range)
min_age: Mapped[int] = column_property(humanitarian_needs_view.c.min_age)
max_age: Mapped[int] = column_property(humanitarian_needs_view.c.max_age)
sector_code: Mapped[str] = column_property(humanitarian_needs_view.c.sector_code)
population_group: Mapped[PopulationGroup] = column_property(humanitarian_needs_view.c.population_group)
category: Mapped[str] = column_property(humanitarian_needs_view.c.category)
population_status: Mapped[PopulationStatus] = column_property(humanitarian_needs_view.c.population_status)
disabled_marker: Mapped[DisabledMarker] = column_property(humanitarian_needs_view.c.disabled_marker)
population: Mapped[int] = column_property(humanitarian_needs_view.c.population)
reference_period_start: Mapped[datetime.datetime] = column_property(
humanitarian_needs_view.c.reference_period_start
Expand Down
15 changes: 3 additions & 12 deletions hdx_hapi/endpoints/get_affected_people.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from hdx_hapi.config.config import get_config
from hdx_hapi.config.doc_snippets import (
DOC_DISABLED_MARKER,
DOC_GENDER,
DOC_AGE_RANGE,
DOC_POPULATION_GROUP,
Expand Down Expand Up @@ -36,7 +35,7 @@
from hdx_hapi.services.humanitarian_needs_logic import get_humanitarian_needs_srv
from hdx_hapi.services.refugees_logic import get_refugees_srv
from hdx_hapi.services.sql_alchemy_session import get_db
from hapi_schema.utils.enums import DisabledMarker, Gender, PopulationGroup, PopulationStatus
from hapi_schema.utils.enums import Gender, PopulationGroup, PopulationStatus
from hdx_hapi.endpoints.util.util import (
CommonEndpointParams,
OutputFormat,
Expand Down Expand Up @@ -145,13 +144,8 @@ async def get_humanitarian_needs(
# ref_period_parameters: Annotated[ReferencePeriodParameters, Depends(reference_period_parameters)],
common_parameters: Annotated[CommonEndpointParams, Depends(common_endpoint_parameters)],
db: AsyncSession = Depends(get_db),
gender: Annotated[Optional[Gender], Query(max_length=3, description=f'{DOC_GENDER}')] = None,
age_range: Annotated[Optional[str], Query(max_length=32, description=f'{DOC_AGE_RANGE}')] = None,
disabled_marker: Annotated[Optional[DisabledMarker], Query(description=f'{DOC_DISABLED_MARKER}')] = None,
category: Annotated[Optional[str], Query(max_length=128, description='')] = None,
sector_code: Annotated[Optional[str], Query(max_length=32, description=f'{DOC_SECTOR_CODE}')] = None,
population_group: Annotated[
Optional[PopulationGroup], Query(max_length=32, description=f'{DOC_POPULATION_GROUP}')
] = None,
population_status: Annotated[
Optional[PopulationStatus], Query(max_length=32, description=f'{DOC_POPULATION_STATUS}')
] = None,
Expand Down Expand Up @@ -192,11 +186,8 @@ async def get_humanitarian_needs(
ref_period_parameters=ref_period_parameters,
db=db,
admin2_ref=admin2_ref,
gender=gender,
age_range=age_range,
disabled_marker=disabled_marker,
category=category,
sector_code=sector_code,
population_group=population_group,
population_status=population_status,
population_min=population_min,
population_max=population_max,
Expand Down
29 changes: 5 additions & 24 deletions hdx_hapi/endpoints/models/humanitarian_needs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,18 @@
from typing import Optional

from hdx_hapi.config.doc_snippets import (
DOC_DISABLED_MARKER,
DOC_GENDER,
DOC_POPULATION_GROUP,
DOC_POPULATION_STATUS,
DOC_HDX_RESOURCE_ID,
DOC_AGE_RANGE,
DOC_REFERENCE_PERIOD_START,
DOC_REFERENCE_PERIOD_END,
truncate_query_description,
)
from hdx_hapi.endpoints.models.base import HapiBaseModel, HapiModelWithAdmins
from hapi_schema.utils.enums import Gender, PopulationGroup, PopulationStatus, DisabledMarker
from hapi_schema.utils.enums import PopulationStatus


class HumanitarianNeedsResponse(HapiBaseModel, HapiModelWithAdmins):
resource_hdx_id: str = Field(max_length=36, description=truncate_query_description(DOC_HDX_RESOURCE_ID))
gender: Gender = Field(description=truncate_query_description(DOC_GENDER))
age_range: str = Field(max_length=32, description=truncate_query_description(DOC_AGE_RANGE))
min_age: Optional[int] = Field(
ge=0,
description=(
'The minimum age from `age_range`, set to `null` if `age_range` is "all" and '
'there is no age disaggregation'
),
)
max_age: Optional[int] = Field(
ge=0,
description=(
'The maximum age from `age_range`, set to `null` if `age_range` is "all" and '
'there is no age disaggregation, or if there is no upper limit to the '
'age range'
),
)
disabled_marker: DisabledMarker = Field(description=truncate_query_description(DOC_DISABLED_MARKER))
sector_code: str = Field(
max_length=32,
description=(
Expand All @@ -45,7 +23,10 @@ class HumanitarianNeedsResponse(HapiBaseModel, HapiModelWithAdmins):
'dataset, or created for HDX HAPI'
),
)
population_group: PopulationGroup = Field(description=truncate_query_description(DOC_POPULATION_GROUP))
category: str = Field(
max_length=128,
description='A category combining gender, age range, disability marker and population group information',
)
population_status: PopulationStatus = Field(description=truncate_query_description(DOC_POPULATION_STATUS))
population: int = Field(ge=0, description='The number of people')
reference_period_start: datetime.datetime = Field(description=DOC_REFERENCE_PERIOD_START)
Expand Down
12 changes: 3 additions & 9 deletions hdx_hapi/services/humanitarian_needs_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@
from hdx_hapi.db.dao.humanitarian_needs_view_dao import humanitarian_needs_view_list
from hdx_hapi.services.admin_level_logic import compute_unspecified_values
from hdx_hapi.endpoints.util.util import AdminLevel, PaginationParams, ReferencePeriodParameters
from hapi_schema.utils.enums import DisabledMarker, Gender, PopulationGroup, PopulationStatus
from hapi_schema.utils.enums import PopulationStatus


async def get_humanitarian_needs_srv(
pagination_parameters: PaginationParams,
ref_period_parameters: ReferencePeriodParameters,
db: AsyncSession,
admin2_ref: Optional[int] = None,
gender: Optional[Gender] = None,
age_range: Optional[str] = None,
disabled_marker: Optional[DisabledMarker] = None,
category: Optional[str] = None,
sector_code: Optional[str] = None,
population_group: Optional[PopulationGroup] = None,
population_status: Optional[PopulationStatus] = None,
population_min: Optional[int] = None,
population_max: Optional[int] = None,
Expand All @@ -44,11 +41,8 @@ async def get_humanitarian_needs_srv(
ref_period_parameters=ref_period_parameters,
db=db,
admin2_ref=admin2_ref,
gender=gender,
age_range=age_range,
disabled_marker=disabled_marker,
category=category,
sector_code=sector_code,
population_group=population_group,
population_status=population_status,
population_min=population_min,
population_max=population_max,
Expand Down
20 changes: 10 additions & 10 deletions tests/sample_data/humanitarian_needs.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
-- dummy data
INSERT INTO public.humanitarian_needs
(resource_hdx_id, admin2_ref, gender, age_range, min_age, max_age, sector_code, population_group, population_status, disabled_marker, population, reference_period_start, reference_period_end)
VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab',2, 'all', 'ALL',0,99,'EDU','REF','AFF','y',500000,'2023-01-01 00:00:00', '2025-01-01 00:00:00');
(resource_hdx_id, admin2_ref, category, sector_code, population_status, population, reference_period_start, reference_period_end)
VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab',2, 'All - Disabled - all - AFF','EDU','AFF',500000,'2023-01-01 00:00:00', '2025-01-01 00:00:00');
INSERT INTO public.humanitarian_needs
(resource_hdx_id, admin2_ref, gender, age_range, min_age, max_age, sector_code, population_group, population_status, disabled_marker, population, reference_period_start, reference_period_end)
VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab', 4, 'f', '0-17',0,17,'SHL','IDP','INN','n',1500000,'2023-01-01 00:00:00', '2025-01-01 00:00:00');
(resource_hdx_id, admin2_ref, category, sector_code, population_status, population, reference_period_start, reference_period_end)
VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab',4,'Female - child - INN','SHL','INN',1500000,'2023-01-01 00:00:00', '2025-01-01 00:00:00');
INSERT INTO public.humanitarian_needs
(resource_hdx_id, admin2_ref, gender, age_range, min_age, max_age, sector_code, population_group, population_status, disabled_marker, population, reference_period_start, reference_period_end)
VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab', 6, 'm', '65+',65,100,'WSH','all','all','all',100000,'2023-01-01 00:00:00', '2025-01-01 00:00:00');
(resource_hdx_id, admin2_ref, category, sector_code, population_status, population, reference_period_start, reference_period_end)
VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab', 6,'Male - elderly - all','WSH','all',100000,'2023-01-01 00:00:00', '2025-01-01 00:00:00');
INSERT INTO public.humanitarian_needs
(resource_hdx_id, admin2_ref, gender, age_range, min_age, max_age, sector_code, population_group, population_status, disabled_marker, population, reference_period_start, reference_period_end)
VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab', 4, 'f', '18-64',18,64,'SHL','IDP','INN','n',1500000,'2023-01-01 00:00:00', '2025-01-01 00:00:00');
(resource_hdx_id, admin2_ref, category, sector_code, population_status, population, reference_period_start, reference_period_end)
VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab', 4, 'Female - adult - INN','SHL','INN',1500000,'2023-01-01 00:00:00', '2025-01-01 00:00:00');
INSERT INTO public.humanitarian_needs
(resource_hdx_id, admin2_ref, gender, age_range, min_age, max_age, sector_code, population_group, population_status, disabled_marker, population, reference_period_start, reference_period_end)
VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab', 6, 'm', '18-64',18,64,'WSH','all','all','all',100000,'2023-01-01 00:00:00', '2025-01-01 00:00:00');
(resource_hdx_id, admin2_ref, category, sector_code, population_status, population, reference_period_start, reference_period_end)
VALUES ('17acb541-9431-409a-80a8-50eda7e8ebab', 6,'Male - adult - all','WSH','all',100000,'2023-01-01 00:00:00', '2025-01-01 00:00:00');
13 changes: 2 additions & 11 deletions tests/test_endpoints/endpoint_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from hapi_schema.utils.enums import (
CommodityCategory,
DisabledMarker,
EventType,
Gender,
PopulationGroup,
Expand Down Expand Up @@ -456,11 +455,8 @@
'/api/v1/affected-people/humanitarian-needs': {
'query_parameters': {
'admin2_ref': 2,
'gender': Gender.ALL.value,
'age_range': 'ALL',
'disabled_marker': DisabledMarker.YES.value,
'sector_code': 'EDU',
'population_group': PopulationGroup.REFUGEES.value,
'category': 'All - Disabled - all - AFF',
'population_status': PopulationStatus.AFFECTED.value,
# 'reference_period_start_min': '2020-01-01T00:00:00',
# 'reference_period_start_max': '2026-01-01T00:00:00',
Expand All @@ -479,13 +475,8 @@
'expected_fields': [
'resource_hdx_id',
'admin2_ref',
'gender',
'age_range',
'min_age',
'max_age',
'disabled_marker',
'category',
'sector_code',
'population_group',
'population_status',
'population',
'reference_period_start',
Expand Down
19 changes: 5 additions & 14 deletions tests/test_endpoints/test_humanitarian_needs_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from httpx import AsyncClient
from hdx_hapi.endpoints.models.humanitarian_needs import HumanitarianNeedsResponse
from hapi_schema.utils.enums import PopulationStatus
from main import app
from tests.test_endpoints.endpoint_data import endpoint_data

Expand Down Expand Up @@ -67,14 +68,9 @@ async def test_get_humanitarian_needs_adm_fields(event_loop, refresh_db):

humanitarian_needs_view_adm_specified = HumanitarianNeedsResponse(
resource_hdx_id='17acb541-9431-409a-80a8-50eda7e8ebab',
gender='f',
age_range='1-2',
min_age=1,
max_age=2,
disabled_marker='y',
category='',
sector_code='EDU',
population_group='REF',
population_status='AFF',
population_status=PopulationStatus.AFFECTED,
population=500000,
reference_period_start='2023-01-01 00:00:00',
reference_period_end='2023-03-31 23:59:59',
Expand Down Expand Up @@ -111,14 +107,9 @@ async def test_get_humanitarian_needs_adm_fields(event_loop, refresh_db):

humanitarian_needs_view_adm_unspecified = HumanitarianNeedsResponse(
resource_hdx_id='17acb541-9431-409a-80a8-50eda7e8ebab',
gender='f',
age_range='1-2',
min_age=1,
max_age=2,
disabled_marker='y',
sector_code='EDU',
population_group='REF',
population_status='AFF',
category='',
population_status=PopulationStatus.AFFECTED,
population=500000,
reference_period_start='2023-01-01 00:00:00',
reference_period_end='2023-03-31 23:59:59',
Expand Down

0 comments on commit 27f490b

Please sign in to comment.