-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
50 bug wrong response type for glom (#51)
* #50 bug wrong response type for glom WIP: Reproducing bug * #50 bug wrong response type for glom WIP: Reproducing bug * #50 bug wrong response type for glom Refactoring glom usage * #50 bug wrong response type for glom Refactoring glom usage * #50 bug wrong response type for glom Refactoring naming Added description * #50 bug wrong response type for glom Refactoring naming Forcing glom to return empty strings instead of none for missing description * #50 bug wrong response type for glom Refactoring enum unions * #50 bug wrong response type for glom Refactoring CollectionAttributes * #50 bug wrong response type for glom Refactoring CollectionAttributes * #50 bug wrong response type for glom Refactoring and cleanup, removing outdated logic and code Adding documentation * #50 bug wrong response type for glom Cleanup
- Loading branch information
1 parent
9b413a9
commit 2b33ccc
Showing
13 changed files
with
323 additions
and
100 deletions.
There are no files selected for viewing
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,87 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Optional | ||
from uuid import UUID | ||
|
||
from elasticsearch_dsl.query import Q | ||
from glom import Coalesce, Iter | ||
|
||
from app.api.collections.models import MissingMaterials | ||
from app.api.collections.utils import map_elastic_response_to_model | ||
from app.core.config import ELASTIC_TOTAL_SIZE | ||
from app.elastic.dsl import qbool, qmatch | ||
from app.elastic.elastic import ResourceType, type_filter | ||
from app.elastic.search import Search | ||
from app.models import CollectionAttribute, ElasticResourceAttribute | ||
|
||
missing_attribute_filter = [ | ||
CollectionAttribute.TITLE, | ||
ElasticResourceAttribute.NAME, | ||
ElasticResourceAttribute.KEYWORDS, | ||
CollectionAttribute.DESCRIPTION, | ||
] | ||
|
||
|
||
all_source_fields: list = [ | ||
ElasticResourceAttribute.NODEREF_ID, | ||
ElasticResourceAttribute.TYPE, | ||
ElasticResourceAttribute.NAME, | ||
CollectionAttribute.TITLE, | ||
ElasticResourceAttribute.KEYWORDS, | ||
CollectionAttribute.DESCRIPTION, | ||
CollectionAttribute.PATH, | ||
CollectionAttribute.PARENT_ID, | ||
] | ||
|
||
missing_attributes_spec = { | ||
"title": Coalesce(CollectionAttribute.TITLE.path, default=""), | ||
"keywords": ( | ||
Coalesce(ElasticResourceAttribute.KEYWORDS.path, default=[]), | ||
Iter().all(), | ||
), | ||
"description": Coalesce(CollectionAttribute.DESCRIPTION.path, default=""), | ||
"path": ( | ||
Coalesce(CollectionAttribute.PATH.path, default=[]), | ||
Iter().all(), | ||
), | ||
"parent_id": Coalesce(CollectionAttribute.PARENT_ID.path, default=""), | ||
"noderef_id": Coalesce(CollectionAttribute.NODE_ID.path, default=""), | ||
"name": Coalesce(ElasticResourceAttribute.NAME.path, default=""), | ||
"type": Coalesce(ElasticResourceAttribute.TYPE.path, default=""), | ||
"children": Coalesce("", default=[]), # workaround to map easier to pydantic model | ||
} | ||
|
||
|
||
def missing_attributes_search( | ||
noderef_id: UUID, missing_attribute: str, max_hits: int | ||
) -> Search: | ||
query = { | ||
"filter": [*type_filter[ResourceType.COLLECTION]], | ||
"minimum_should_match": 1, | ||
"should": [ | ||
qmatch(**{"path": noderef_id}), | ||
qmatch(**{"nodeRef.id": noderef_id}), | ||
], | ||
"must_not": Q("wildcard", **{missing_attribute: {"value": "*"}}), | ||
} | ||
|
||
return ( | ||
Search() | ||
.base_filters() | ||
.query(qbool(**query)) | ||
.source(includes=[source.path for source in all_source_fields])[:max_hits] | ||
) | ||
|
||
|
||
async def collections_with_missing_attributes( | ||
noderef_id: UUID, | ||
missing_attribute: str, | ||
max_hits: Optional[int] = ELASTIC_TOTAL_SIZE, | ||
) -> list[MissingMaterials]: | ||
search = missing_attributes_search(noderef_id, missing_attribute, max_hits) | ||
|
||
response = search.execute() | ||
if response.success(): | ||
return map_elastic_response_to_model( | ||
response, missing_attributes_spec, MissingMaterials | ||
) |
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,14 @@ | ||
from typing import Generic, TypeVar | ||
|
||
from elasticsearch_dsl.response import Response | ||
from glom import glom | ||
|
||
from app.api.collections.models import CollectionNode, MissingMaterials | ||
|
||
T = TypeVar("T", CollectionNode, MissingMaterials) | ||
|
||
|
||
def map_elastic_response_to_model( | ||
response: Response, specs: dict, model: Generic[T] | ||
) -> list[T]: | ||
return [model(**glom(hit.to_dict(), specs)) for hit in response] |
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
Oops, something went wrong.