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

Add facets to each page #53

Merged
merged 4 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
11 changes: 6 additions & 5 deletions src/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
from sssom_schema import Mapping, MappingSet, MappingRegistry, MappingSetReference

T = TypeVar("T")
# class ResponseMapping(BaseModel):
# subject_id: Mapping.subject_id
# predicate_id: Mapping.predicate_id
# object_id: Mapping.object_id
# mapping_justification: Mapping.mapping_justification

class PaginationParams(BaseModel):
request: Request
Expand All @@ -30,9 +25,15 @@ class PaginationInfo(BaseModel):
total_pages: int


class FacetInfo(BaseModel):
mapping_justification: dict
predicate: dict
anitacaron marked this conversation as resolved.
Show resolved Hide resolved


class Page(GenericModel, Generic[T]):
data: List[T]
pagination: PaginationInfo
facets: FacetInfo


class SearchEntity(BaseModel):
Expand Down
14 changes: 12 additions & 2 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import itertools
import functools
import math
from collections import Counter

from fastapi import Request

from .models import Page, PaginationInfo
from .models import Page, PaginationInfo, FacetInfo


def _replace_page_param(request: Request, new_page: Union[int, None]) -> Union[str, None]:
Expand All @@ -24,7 +25,7 @@ def paginate(iterable: Iterable[T], page: int, limit: int, request: Request) ->
prev_page = None
next_page = None
data = []
iter_data, iter_total = itertools.tee(iterable)
iter_data, iter_total, iter_facets = itertools.tee(iterable, 3)
total_items = functools.reduce(lambda prev, curr: prev + 1, iter_total, 0)
total_pages = math.ceil(total_items / limit)
for idx, item in enumerate(iter_data):
Expand All @@ -44,6 +45,15 @@ def paginate(iterable: Iterable[T], page: int, limit: int, request: Request) ->
total_items=total_items,
total_pages=total_pages
),
facets=_create_facets(iter_facets)
)

def _create_facets(data: Iterable[T]) -> FacetInfo:
iter_mj, iter_pred = itertools.tee(data)

return FacetInfo(
mapping_justification=dict(Counter(list(map(lambda d: d["mapping_justification"], iter_mj)))),
predicate=dict(Counter(list(map(lambda d: d["predicate_id"], iter_pred)))),
)

def parser_filter(datamodel: T, filter: Union[List[str], None] = None) -> Union[List[dict], None]:
Expand Down