-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/pdct 1661 world map should only show counts of families from …
…allowed corpora (#423) * Make documents router dependent on app token * Driveby: Add CORS tests for MCF * Update slug lookup query to respect allowed corpora * Include actual CCLW corpus ID in test token * Bump to 1.19.11 * Refactor _get_query_template * Refactor doc and fam lookup tests * Add integration tests for doc/fam lookup when corpora mismatch * Add alternative corpora token * Refactor download code * Geo * Merge in main * Bump to 1.19.16 * Fix docstring param name * Fix capitalisation * Fix capitalisation for MCF category * Fix world map query counts * Update world map stats repo function * Move get_world_map_stats to service layer * Rename function and add world map service * Import from world map service * Update counts * Fix counts * Rename router world map * PR comment fixes * Make representative of entity
- Loading branch information
1 parent
9089898
commit 1e2917a
Showing
13 changed files
with
374 additions
and
217 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import logging | ||
from typing import Annotated | ||
|
||
from fastapi import APIRouter, Depends, Header, HTTPException, Request, status | ||
|
||
from app.clients.db.session import get_db | ||
from app.errors import RepositoryError, ValidationError | ||
from app.models.geography import GeographyStatsDTO | ||
from app.service.custom_app import AppTokenFactory | ||
from app.service.world_map import get_world_map_stats | ||
|
||
_LOGGER = logging.getLogger(__file__) | ||
|
||
world_map_router = APIRouter() | ||
|
||
|
||
@world_map_router.get("/geographies", response_model=list[GeographyStatsDTO]) | ||
async def world_map_stats( | ||
request: Request, app_token: Annotated[str, Header()], db=Depends(get_db) | ||
): | ||
"""Get a summary of family counts for all geographies for world map.""" | ||
_LOGGER.info( | ||
"Getting world map counts for all geographies", | ||
extra={ | ||
"props": {"app_token": str(app_token)}, | ||
}, | ||
) | ||
|
||
# Decode the app token and validate it. | ||
token = AppTokenFactory() | ||
token.decode_and_validate(db, request, app_token) | ||
|
||
try: | ||
world_map_stats = get_world_map_stats(db, token.allowed_corpora_ids) | ||
|
||
if world_map_stats == []: | ||
_LOGGER.error("No stats for world map found") | ||
raise HTTPException( | ||
status_code=status.HTTP_404_NOT_FOUND, | ||
detail="No stats for world map found", | ||
) | ||
|
||
return world_map_stats | ||
except RepositoryError as e: | ||
_LOGGER.error(e) | ||
raise HTTPException( | ||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail=e.message | ||
) | ||
except ValidationError as e: | ||
_LOGGER.error(e) | ||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=e.message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
WITH counts AS ( | ||
SELECT | ||
family.family_category, | ||
family_geography.geography_id, | ||
COUNT(*) AS records_count | ||
FROM | ||
family | ||
INNER JOIN | ||
family_corpus | ||
ON family.import_id = family_corpus.family_import_id | ||
INNER JOIN corpus ON family_corpus.corpus_import_id = corpus.import_id | ||
INNER JOIN | ||
family_geography | ||
ON family.import_id = family_geography.family_import_id | ||
WHERE | ||
family_corpus.corpus_import_id = ANY(:allowed_corpora_ids) | ||
AND CASE | ||
WHEN ( | ||
NOT ( | ||
EXISTS ( | ||
SELECT | ||
1 | ||
FROM | ||
family_document | ||
WHERE | ||
family.import_id = family_document.family_import_id | ||
) | ||
) | ||
) THEN 'Created' | ||
WHEN ( | ||
( | ||
SELECT | ||
COUNT(family_document.document_status) AS count_1 | ||
FROM | ||
family_document | ||
WHERE | ||
family_document.family_import_id = family.import_id | ||
AND family_document.document_status = 'PUBLISHED' | ||
) > 0 | ||
) THEN 'Published' | ||
WHEN ( | ||
( | ||
SELECT | ||
COUNT(family_document.document_status) AS count_2 | ||
FROM | ||
family_document | ||
WHERE | ||
family_document.family_import_id = family.import_id | ||
AND family_document.document_status = 'CREATED' | ||
) > 0 | ||
) THEN 'Created' | ||
ELSE 'Deleted' | ||
END = 'Published' | ||
GROUP BY | ||
family.family_category, | ||
family_geography.geography_id | ||
) | ||
|
||
SELECT | ||
geo_family_combinations.display_value, | ||
geo_family_combinations.slug, | ||
geo_family_combinations.value, | ||
JSONB_OBJECT_AGG( | ||
geo_family_combinations.family_category, | ||
COALESCE(counts.records_count, 0) | ||
) AS counts | ||
FROM | ||
( | ||
SELECT | ||
geography.id AS geography_id, | ||
geography.display_value, | ||
geography.slug, | ||
geography.value, | ||
anon_1.family_category | ||
FROM | ||
geography, | ||
( | ||
SELECT DISTINCT | ||
family.family_category | ||
FROM | ||
family | ||
) AS anon_1 | ||
) AS geo_family_combinations | ||
LEFT OUTER JOIN | ||
counts | ||
ON geo_family_combinations.geography_id = counts.geography_id | ||
AND geo_family_combinations.family_category = counts.family_category | ||
GROUP BY | ||
geo_family_combinations.display_value, | ||
geo_family_combinations.slug, | ||
geo_family_combinations.value | ||
ORDER BY | ||
geo_family_combinations.display_value; |
Oops, something went wrong.