Skip to content

Commit

Permalink
scope filtering terms
Browse files Browse the repository at this point in the history
  • Loading branch information
gsfk committed Dec 13, 2024
1 parent 6d36444 commit 177d0c6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
30 changes: 19 additions & 11 deletions bento_beacon/endpoints/info_permissions_required.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Blueprint, current_app
from flask import Blueprint, current_app, g
from ..authz.middleware import authz_middleware
from .info import beacon_format_service_details
from ..utils.beacon_response import beacon_info_response, add_info_to_response, summary_stats
Expand All @@ -8,7 +8,7 @@
)
from ..utils.gohan_utils import gohan_counts_for_overview
from ..utils.scope import scoped_route_decorator_for_blueprint

from ..utils.exceptions import InvalidQuery

info_permissions_required = Blueprint("info_permissions_required", __name__)
route_with_optional_project_id = scoped_route_decorator_for_blueprint(info_permissions_required)
Expand All @@ -17,12 +17,12 @@
@route_with_optional_project_id("/info")
async def beacon_info_with_overview(project_id=None):
"""
returns same beacon-format service info served from root endpoint, but with an overview added
Returns same beacon-format service info served from root endpoint, but with an overview added.
overview is unscoped, no overview info is returned for scoped requests
description field is scoped (this is pulled from datasets data)
"""
service_info = await beacon_format_service_details(project_id)
return beacon_info_response({**service_info, "overview": await overview()})
return beacon_info_response({**service_info, "overview": await overview(project_id)})


@route_with_optional_project_id("/overview")
Expand All @@ -35,18 +35,26 @@ async def beacon_overview(project_id=None):
return beacon_info_response({**service_info, "overview": await overview(project_id)})


# could be scoped by dataset only by adding query params for dataset (endpoint is GET only)
# this endpoint normally doesn't take queries at all, the only params it recognizes are for pagination
# could also annotating filtering terms with a dataset id in cases where it's limited to a particular dataset
@route_with_optional_project_id("/filtering_terms")
@authz_middleware.deco_public_endpoint
async def filtering_terms(project_id=None):
"""
response scoped by project.
could be scoped by dataset by adding query params
but this endpoint isn't meant to accept queries, the only params it recognizes are for pagination
Filtering terms for single node, single project or single dataset
Could be generalized to arbitrary datatsets, but this reflects the current restrictions in katsu and the UI
"""
filtering_terms = await get_filtering_terms(project_id)
dataset_ids = g.beacon_query["dataset_ids"]

# if scoping by dataset, katsu can only handle a single dataset, and needs a corresponding project
if dataset_ids:
if len(dataset_ids) != 1:
# or, we can throw this together with multiple calls?
raise InvalidQuery("for filtering terms by dataset, provide a single dataset only")
if project_id is None:
# or, we could just look this up ourselves with an extra katsu call
raise InvalidQuery("dataset ids require a corresponding project id")
dataset_id = dataset_ids[0]

filtering_terms = await get_filtering_terms(project_id, dataset_id)
return beacon_info_response({"resources": [], "filteringTerms": filtering_terms})


Expand Down
12 changes: 6 additions & 6 deletions bento_beacon/utils/katsu_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ async def search_from_config(config_filters):
return response.get("matches", [])


async def get_katsu_config_search_fields(project_id, requires_auth: RequiresAuthOptions):
async def get_katsu_config_search_fields(project_id, dataset_id, requires_auth: RequiresAuthOptions):
fields = await katsu_get(
current_app.config["KATSU_PUBLIC_CONFIG_ENDPOINT"], project_id=project_id, requires_auth="forwarded"
current_app.config["KATSU_PUBLIC_CONFIG_ENDPOINT"], project_id=project_id, dataset_id=dataset_id, requires_auth="forwarded"
)
current_app.config["KATSU_CONFIG_SEARCH_FIELDS"] = fields
return fields
Expand Down Expand Up @@ -232,9 +232,9 @@ def katsu_json_payload(filters, datatype, get_biosample_ids):
# -------------------------------------------------------


async def katsu_config_filtering_terms(project_id):
async def katsu_config_filtering_terms(project_id, dataset_id):
filtering_terms = []
sections = (await get_katsu_config_search_fields(project_id, requires_auth="forwarded")).get("sections", [])
sections = (await get_katsu_config_search_fields(project_id, dataset_id, requires_auth="forwarded")).get("sections", [])
for section in sections:
for field in section["fields"]:
filtering_term = {
Expand Down Expand Up @@ -264,10 +264,10 @@ async def katsu_config_filtering_terms(project_id):
return filtering_terms


async def get_filtering_terms(project_id):
async def get_filtering_terms(project_id, dataset_id):
# add ontology filters here when we start supporting ontologies
# could also add filters for phenopacket and experiment queries if user has correct permissions
return await katsu_config_filtering_terms(project_id)
return await katsu_config_filtering_terms(project_id, dataset_id)


# -------------------------------------------------------
Expand Down

0 comments on commit 177d0c6

Please sign in to comment.