Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/hdx 9818 prepare for new schema #109

Merged
merged 83 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
3e68e84
Checks for packages that are missing an __init__.py file
alexandru-m-g May 14, 2024
62dcf8f
HDX-9818 small mixpanel refactoring
alexandru-m-g May 14, 2024
98e38ba
HDX-9818 replacing old views + disabling all routes except location a…
alexandru-m-g May 14, 2024
baca987
HDX-9818 database init changes for tests
alexandru-m-g May 14, 2024
6388c3e
HDX-9821 adapting the location/admin code to work with new schema
alexandru-m-g May 14, 2024
eb1a463
HDX-9819 Add new test data for revised resource and dataset tables
IanHopkinson May 15, 2024
7a1abf2
HDX-9819 Dataset endpoint tests now passing
IanHopkinson May 15, 2024
c349ee8
HDX-9820 adapted sector view
danmihaila May 15, 2024
b7b80be
HDX-9820 adapted org_type view
danmihaila May 15, 2024
6136c07
HDX-9820 adapted org view
danmihaila May 15, 2024
1a18b63
HDX-9821 reference period query params for location/admins
alexandru-m-g May 15, 2024
0b2867b
HDX-9818 adding the views for conflict events and poverty rate
alexandru-m-g May 15, 2024
a749e8b
HDX-9819 Updated dataset to include the provider link
IanHopkinson May 16, 2024
4cdb01e
HDX-9819 Completed implementation of the resource endpoint for the ne…
IanHopkinson May 16, 2024
e78ae7b
HDX-9819 Fix for the linter
IanHopkinson May 16, 2024
8473816
Merge pull request #91 from OCHA-DAP/feature/HDX-9820-hum-response
alexandru-m-g May 16, 2024
c45ca54
Merge pull request #92 from OCHA-DAP/feature/HDX-9821-location-admin-…
alexandru-m-g May 16, 2024
163c035
HDX-9819 Add code to calculate organization hdx api urls
IanHopkinson May 16, 2024
d3859e2
Merge branch 'feature/HDX-9818-prepare-for-new-schema' into HDX-9819_…
IanHopkinson May 16, 2024
30440db
Merge pull request #93 from OCHA-DAP/HDX-9819_dataset_resource_new_sc…
IanHopkinson May 16, 2024
cfdaa65
Adding back original sample data and check on case insensitive search…
alexandru-m-g May 16, 2024
161a7ee
HDX-9818 put sample data in their own folder
alexandru-m-g May 16, 2024
713e16f
Better typing for util functions
alexandru-m-g May 16, 2024
03e2833
HDX-9829 Initial commit to allow import of work so far on the HDX-981…
IanHopkinson May 17, 2024
c95496a
Merge branch 'feature/HDX-9819_dataset_resource_new_schema' into feat…
IanHopkinson May 17, 2024
e510683
HDX-9829 Get test fixtures to consistency
IanHopkinson May 17, 2024
2896670
Merge pull request #95 from OCHA-DAP/feature/better-typing-for-util-f…
alexandru-m-g May 17, 2024
8366014
HDX-9829 Population tests now all passing
IanHopkinson May 18, 2024
42caee8
HDX-9830 adpating 3w to new schema
alexandru-m-g May 17, 2024
fd2ef0a
HDX-9829 Sort of test for the get_populations route since API docs is…
IanHopkinson May 18, 2024
09ff437
HDX-9829 Working my way from route to service to populations_view_lis…
IanHopkinson May 19, 2024
a9f7ac8
HDX-9830 new funding endpoint and logic
alexandru-m-g May 18, 2024
200b8a1
HDX-9818 refactoring pydantic models with admins
alexandru-m-g May 18, 2024
0c1fe96
HDX-9818 extracting common admin query filter
alexandru-m-g May 19, 2024
535015f
HDX-9836 new conflict data endpoint and logic
alexandru-m-g May 20, 2024
45abe74
HDX-9829 Update of utils with new reference and location related clas…
IanHopkinson May 20, 2024
1863233
HDX-9829 Docs endpoint now working but tests failing
IanHopkinson May 20, 2024
9de62e3
HDX-9829 Get the tests to pass - issue with max_age but also not appr…
IanHopkinson May 20, 2024
abac7e5
HDX-9829 Change population route to population-social from themes
IanHopkinson May 20, 2024
6cc2147
HDX-9829 Tidy up the test fixtures
IanHopkinson May 20, 2024
fec614f
HDX-9829 Add diagnostics to the failing test
IanHopkinson May 20, 2024
cc33702
HDX-9829 Fix linter issues
IanHopkinson May 20, 2024
2a2e400
Merge branch 'feature/HDX-9818-prepare-for-new-schema' into feature/H…
IanHopkinson May 20, 2024
42a2d3e
HDX-9829 Delete the test_route_calls test since it wasn't a proper te…
IanHopkinson May 20, 2024
cf52a08
Merge branch 'feature/HDX-9829_population' of https://github.com/OCHA…
IanHopkinson May 20, 2024
2c135eb
Merge pull request #96 from OCHA-DAP/feature/HDX-9830-3w-and-funding
danmihaila May 20, 2024
9da2e51
Merge pull request #97 from OCHA-DAP/feature/HDX-9836-conflict-events
danmihaila May 20, 2024
a7cd03b
HDX-9831 - initial hum needs
danmihaila May 20, 2024
d6421e1
HDX-9831 - hum needs
danmihaila May 21, 2024
8d9d0aa
HDX-9829 Change gender field to Gender enumeration for the population…
IanHopkinson May 21, 2024
b9e8a9c
HDX-9829 Update sample sql names
IanHopkinson May 21, 2024
0377d9d
Merge branch 'feature/HDX-9818-prepare-for-new-schema' into feature/H…
IanHopkinson May 21, 2024
0567c48
HDX-9829 Fix the failing test
IanHopkinson May 21, 2024
066c405
Merge branch 'feature/HDX-9829_population' of https://github.com/OCHA…
IanHopkinson May 21, 2024
9e85d66
HDX-9829 Fix linter issues
IanHopkinson May 21, 2024
80a5534
HDX-9831 - hum needs + tests
danmihaila May 21, 2024
45b3241
HDX-9829 use correct inheritance for response which fixes tests.
IanHopkinson May 21, 2024
e49f656
HDX-9831 - fix some tests
danmihaila May 21, 2024
88c6f09
HDX-9831 - improve pydantic validation
danmihaila May 21, 2024
aa984bd
HDX-9831 - improve pydantic validation
danmihaila May 21, 2024
e086bf7
HDX-9829 Remove duplicate - EntityWithLocationAdmin
IanHopkinson May 21, 2024
26a1972
Merge pull request #98 from OCHA-DAP/feature/HDX-9829_population
alexandru-m-g May 21, 2024
4babdff
HDX-9831 - cleanup code
danmihaila May 21, 2024
31bf223
Merge branch 'feature/HDX-9818-prepare-for-new-schema' into feature/H…
danmihaila May 21, 2024
89198f6
HDX-9846 - refugees
danmihaila May 21, 2024
81abe01
HDX-9844 national risk endpoint
alexandru-m-g May 21, 2024
db27e83
Merge pull request #99 from OCHA-DAP/feature/HDX-9831-hum-needs
danmihaila May 22, 2024
9acd282
Merge pull request #100 from OCHA-DAP/feature/HDX-9846-refugees
danmihaila May 22, 2024
7b5b6dc
HDX-9846 - fix linter
danmihaila May 22, 2024
981ffc9
Merge pull request #103 from OCHA-DAP/feature/HDX-9831-hum-needs-1
danmihaila May 22, 2024
9c3133d
Merge branch 'feature/HDX-9818-prepare-for-new-schema' into feature/H…
danmihaila May 22, 2024
53c6766
Merge pull request #101 from OCHA-DAP/feature/HDX-9844-national-risk
danmihaila May 22, 2024
fbe2ca4
fix for api call tracking test
alexandru-m-g May 21, 2024
f912661
HDX-9834 remove unneeded endpoints + logic + tests
alexandru-m-g May 21, 2024
6b08db8
updating schema to 0.8.3 in preparation for the food prices endpoints
alexandru-m-g May 21, 2024
0d2d971
Merge pull request #102 from OCHA-DAP/feature/HDX-9834-remove-tests-u…
danmihaila May 22, 2024
5886d84
views adapted to schema 0.8.3
alexandru-m-g May 22, 2024
7c7c7a3
update urls for tests
danmihaila May 22, 2024
ec1ddc1
Merge pull request #104 from OCHA-DAP/feature/HDX-9818-prepare-for-ne…
danmihaila May 22, 2024
67cec9b
HDX-9852 use app_identifier instead of identifier throughout the API
alexandru-m-g May 22, 2024
6744406
Merge pull request #108 from OCHA-DAP/feature/HDX-9852-app-identifier
danmihaila May 23, 2024
b9df25f
update all views and new sql alchemy schema version
danmihaila May 23, 2024
30d016c
Merge pull request #110 from OCHA-DAP/feature/HDX-9818-dan
danmihaila May 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added hdx_hapi/config/__init__.py
Empty file.
16 changes: 10 additions & 6 deletions hdx_hapi/config/config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from dataclasses import dataclass
from mixpanel import Mixpanel
import logging
import os

from dataclasses import dataclass
from mixpanel import Mixpanel

from hdx_hapi.config.helper import create_pg_uri_from_env_without_protocol

logger = logging.getLogger(__name__)
Expand All @@ -18,6 +19,7 @@ class Config:
HDX_DATASET_URL: str
HDX_DATASET_API_URL: str
HDX_ORGANIZATION_URL: str
HDX_ORGANIZATION_API_URL: str

HDX_RESOURCE_URL: str
HDX_RESOURCE_API_URL: str
Expand All @@ -28,7 +30,7 @@ class Config:

HAPI_IDENTIFIER_FILTERING: bool

HDX_MIXPANEL_TOKEN: str
MIXPANEL: Mixpanel


CONFIG = None
Expand All @@ -41,6 +43,7 @@ def get_config() -> Config:

sql_alchemy_asyncypg_db_uri = f'postgresql+asyncpg://{db_uri_without_protocol}'
sql_alchemy_psycopg2_db_uri = f'postgresql+psycopg2://{db_uri_without_protocol}'
mixpanel_token = os.getenv('HDX_MIXPANEL_TOKEN', '')
CONFIG = Config(
SQL_ALCHEMY_ASYNCPG_DB_URI=sql_alchemy_asyncypg_db_uri,
SQL_ALCHEMY_PSYCOPG2_DB_URI=sql_alchemy_psycopg2_db_uri,
Expand All @@ -52,14 +55,15 @@ def get_config() -> Config:
'HDX_RESOURCE_API_URL', '{domain}/api/action/resource_show?id={resource_id}'
),
HDX_ORGANIZATION_URL=os.getenv('HDX_ORGANIZATION_URL', '{domain}/organization/{org_id}'),
HDX_ORGANIZATION_API_URL=os.getenv(
'HDX_ORGANIZATION_API_URL', '{domain}/api/action/organization_show?id={org_id}'
),
HAPI_READTHEDOCS_OVERVIEW_URL=os.getenv(
'HAPI_READTHEDOCS_OVERVIEW_URL', 'https://hdx-hapi.readthedocs.io/en/latest/'
),
HAPI_SERVER_URL=os.getenv('HAPI_SERVER_URL', None),
HAPI_IDENTIFIER_FILTERING=os.getenv('HAPI_IDENTIFIER_FILTERING', 'True').lower() == 'true',
HDX_MIXPANEL_TOKEN=os.getenv('HDX_MIXPANEL_TOKEN', ''),
MIXPANEL=Mixpanel(mixpanel_token) if mixpanel_token else None,
)

return CONFIG

mixpanel = Mixpanel(get_config().HDX_MIXPANEL_TOKEN)
3 changes: 2 additions & 1 deletion hdx_hapi/config/doc_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
DOC_ADMIN2_CODE = 'Filter the response by the 2nd subnational administrative divisions. The admin2 codes refer to the p-codes in the <a href="https://data.humdata.org/dashboards/cod?">Common Operational Datasets</a>.'
DOC_ADMIN2_NAME = 'Filter the response by the 1st subnational administrative divisions. The admin2 names refer to the <a href="https://data.humdata.org/dashboards/cod?">Common Operational Datasets</a>.'
DOC_AGE_RANGE_SUMMARY = 'Get the list of age ranges used for disaggregating population data'
DOC_AGE_RANGE_CODE = 'Filter the response by the age range. These are expressed as [start year]-[end year]. The end year is assumed to be inclusive, though that is not always explicit in the source data.'
DOC_AGE_RANGE = 'Filter the response by the age range. These are expressed as [start year]-[end year]. The end year is assumed to be inclusive, though that is not always explicit in the source data.'
DOC_GENDER_SUMMARY = 'Get the list of gender codes used for disaggregating population data'
DOC_GENDER_CODE = 'Filter the response by the gender code.'
DOC_GENDER = 'Filter the response by the gender.'
DOC_GENDER_DESCRIPTION = 'Filter the response by the gender description.'
DOC_HDX_DATASET_ID = 'Filter the response by the dataset ID, which is a unique and fixed identifier of a Dataset on HDX. A URL in the pattern of `https://data.humdata.org/dataset/[dataset id]` will load the dataset page on HDX.'
DOC_HDX_DATASET_NAME = 'Filter the response by the URL-safe name of the dataset as displayed on HDX. This name is unique but can change. A URL in the pattern of `https://data.humdata.org/dataset/[dataset name]` will load the dataset page on HDX.'
Expand Down
22 changes: 6 additions & 16 deletions hdx_hapi/db/dao/admin1_view_dao.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
from datetime import datetime
import logging

from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select

from hdx_hapi.db.models.views.db_admin1_view import Admin1View
from hdx_hapi.db.dao.util.util import apply_pagination, case_insensitive_filter
from hdx_hapi.endpoints.util.util import PaginationParams
from hdx_hapi.db.models.views.all_views import Admin1View
from hdx_hapi.db.dao.util.util import apply_pagination, apply_reference_period_filter, case_insensitive_filter
from hdx_hapi.endpoints.util.util import PaginationParams, ReferencePeriodParameters


logger = logging.getLogger(__name__)


async def admin1_view_list(
pagination_parameters: PaginationParams,
ref_period_parameters: ReferencePeriodParameters,
db: AsyncSession,
code: str = None,
name: str = None,
hapi_updated_date_min: datetime = None,
hapi_updated_date_max: datetime = None,
hapi_replaced_date_min: datetime = None,
hapi_replaced_date_max: datetime = None,
location_code: str = None,
location_name: str = None,
):
Expand All @@ -37,19 +33,13 @@ async def admin1_view_list(
query = case_insensitive_filter(query, Admin1View.code, code)
if name:
query = query.where(Admin1View.name.icontains(name))
if hapi_updated_date_min:
query = query.where(Admin1View.hapi_updated_date >= hapi_updated_date_min)
if hapi_updated_date_max:
query = query.where(Admin1View.hapi_updated_date < hapi_updated_date_max)
if hapi_replaced_date_min:
query = query.where(Admin1View.hapi_replaced_date >= hapi_replaced_date_min)
if hapi_replaced_date_max:
query = query.where(Admin1View.hapi_replaced_date < hapi_replaced_date_max)
if location_code:
query = case_insensitive_filter(query, Admin1View.location_code, location_code)
if location_name:
query = query.where(Admin1View.location_name.icontains(location_name))

query = apply_reference_period_filter(query, ref_period_parameters, Admin1View)

query = apply_pagination(query, pagination_parameters)

logger.debug(f'Executing SQL query: {query}')
Expand Down
22 changes: 6 additions & 16 deletions hdx_hapi/db/dao/admin2_view_dao.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
from datetime import datetime
import logging

from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select

from hdx_hapi.db.models.views.db_admin2_view import Admin2View
from hdx_hapi.db.dao.util.util import apply_pagination, case_insensitive_filter
from hdx_hapi.endpoints.util.util import PaginationParams
from hdx_hapi.db.models.views.all_views import Admin2View
from hdx_hapi.db.dao.util.util import apply_pagination, apply_reference_period_filter, case_insensitive_filter
from hdx_hapi.endpoints.util.util import PaginationParams, ReferencePeriodParameters


logger = logging.getLogger(__name__)


async def admin2_view_list(
pagination_parameters: PaginationParams,
ref_period_parameters: ReferencePeriodParameters,
db: AsyncSession,
code: str = None,
name: str = None,
hapi_updated_date_min: datetime = None,
hapi_updated_date_max: datetime = None,
hapi_replaced_date_min: datetime = None,
hapi_replaced_date_max: datetime = None,
admin1_code: str = None,
admin1_name: str = None,
location_code: str = None,
Expand All @@ -39,14 +35,6 @@ async def admin2_view_list(
query = case_insensitive_filter(query, Admin2View.code, code)
if name:
query = query.where(Admin2View.name.icontains(name))
if hapi_updated_date_min:
query = query.where(Admin2View.hapi_updated_date >= hapi_updated_date_min)
if hapi_updated_date_max:
query = query.where(Admin2View.hapi_updated_date < hapi_updated_date_max)
if hapi_replaced_date_min:
query = query.where(Admin2View.hapi_replaced_date >= hapi_replaced_date_min)
if hapi_replaced_date_max:
query = query.where(Admin2View.hapi_replaced_date < hapi_replaced_date_max)
if admin1_code:
query = case_insensitive_filter(query, Admin2View.admin1_code, admin1_code)
if admin1_name:
Expand All @@ -56,6 +44,8 @@ async def admin2_view_list(
if location_name:
query = query.where(Admin2View.location_name.icontains(location_name))

query = apply_reference_period_filter(query, ref_period_parameters, Admin2View)

query = apply_pagination(query, pagination_parameters)

logger.debug(f'Executing SQL query: {query}')
Expand Down
32 changes: 0 additions & 32 deletions hdx_hapi/db/dao/age_range_view_dao.py

This file was deleted.

68 changes: 68 additions & 0 deletions hdx_hapi/db/dao/conflict_event_view_dao.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import logging
from typing import Optional, Sequence

from hapi_schema.utils.enums import EventType
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select

from hdx_hapi.db.models.views.all_views import ConflictEventView
from hdx_hapi.db.dao.util.util import (
apply_location_admin_filter,
apply_pagination,
apply_reference_period_filter,
)
from hdx_hapi.endpoints.util.util import PaginationParams, ReferencePeriodParameters


logger = logging.getLogger(__name__)


async def conflict_event_view_list(
pagination_parameters: PaginationParams,
ref_period_parameters: ReferencePeriodParameters,
db: AsyncSession,
event_type: Optional[EventType] = None,
location_ref: Optional[int] = None,
location_code: Optional[str] = None,
location_name: Optional[str] = None,
admin1_ref: Optional[int] = None,
admin1_code: Optional[str] = None,
admin1_name: Optional[str] = None,
admin1_is_unspecified: Optional[bool] = None,
admin2_ref: Optional[int] = None,
admin2_code: Optional[str] = None,
admin2_name: Optional[str] = None,
admin2_is_unspecified: Optional[bool] = None,
) -> Sequence[ConflictEventView]:
query = select(ConflictEventView)
if event_type:
query = query.where(ConflictEventView.event_type == event_type)

query = apply_location_admin_filter(
query,
ConflictEventView,
location_ref,
location_code,
location_name,
admin1_ref,
admin1_code,
admin1_name,
admin1_is_unspecified,
admin2_ref,
admin2_code,
admin2_name,
admin2_is_unspecified,
)

query = apply_reference_period_filter(query, ref_period_parameters, ConflictEventView)

query = apply_pagination(query, pagination_parameters)

logger.debug(f'Executing SQL query: {query}')

result = await db.execute(query)
conflict_events = result.scalars().all()

logger.info(f'Retrieved {len(conflict_events)} rows from the database')

return conflict_events
2 changes: 1 addition & 1 deletion hdx_hapi/db/dao/dataset_view_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select

from hdx_hapi.db.models.views.db_dataset_view import DatasetView
from hdx_hapi.db.models.views.all_views import DatasetView
from hdx_hapi.db.dao.util.util import apply_pagination, case_insensitive_filter
from hdx_hapi.endpoints.util.util import PaginationParams

Expand Down
39 changes: 18 additions & 21 deletions hdx_hapi/db/dao/food_security_view_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sqlalchemy import select

from hdx_hapi.db.models.views.db_food_security_view import FoodSecurityView
from hdx_hapi.db.dao.util.util import apply_pagination, case_insensitive_filter
from hdx_hapi.db.dao.util.util import apply_location_admin_filter, apply_pagination, case_insensitive_filter
from hdx_hapi.endpoints.util.util import PaginationParams


Expand All @@ -29,6 +29,7 @@ async def food_security_view_list(
admin2_name: str = None,
admin2_is_unspecified: bool = None,
admin1_ref: int = None,
admin2_ref: int = None,
):
query = select(FoodSecurityView)

Expand All @@ -50,26 +51,22 @@ async def food_security_view_list(
query = query.where(FoodSecurityView.hapi_replaced_date >= hapi_replaced_date_min)
if hapi_replaced_date_max:
query = query.where(FoodSecurityView.hapi_replaced_date < hapi_replaced_date_max)
if location_code:
query = case_insensitive_filter(query, FoodSecurityView.location_code, location_code)
if location_name:
query = query.where(FoodSecurityView.location_name.icontains(location_name))
if admin1_name:
query = query.where(FoodSecurityView.admin1_name.icontains(admin1_name))
if admin1_code:
query = case_insensitive_filter(query, FoodSecurityView.admin1_code, admin1_code)
if admin1_is_unspecified is not None:
query = query.where(FoodSecurityView.admin1_is_unspecified == admin1_is_unspecified)
if location_ref:
query = query.where(FoodSecurityView.location_ref == location_ref)
if admin2_code:
query = case_insensitive_filter(query, FoodSecurityView.admin2_code, admin2_code)
if admin2_name:
query = query.where(FoodSecurityView.admin2_name.icontains(admin2_name))
if admin2_is_unspecified is not None:
query = query.where(FoodSecurityView.admin2_is_unspecified == admin2_is_unspecified)
if admin1_ref:
query = query.where(FoodSecurityView.admin1_ref == admin1_ref)

query = apply_location_admin_filter(
query,
FoodSecurityView,
location_ref,
location_code,
location_name,
admin1_ref,
admin1_code,
admin1_name,
admin1_is_unspecified,
admin2_ref,
admin2_code,
admin2_name,
admin2_is_unspecified,
)

query = apply_pagination(query, pagination_parameters)

Expand Down
Loading
Loading