From ce35f63ba197636e38a877fbbc83922b7a02ee45 Mon Sep 17 00:00:00 2001 From: Alberto Islas Date: Wed, 20 Nov 2024 18:03:05 -0600 Subject: [PATCH 1/2] fix(api): Refactored the use of flags in the V3 API to support percentage brown-out --- cl/search/api_utils.py | 26 +++++++++++------------- cl/search/api_views.py | 45 +++++++++++++++++++++++++++++------------- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/cl/search/api_utils.py b/cl/search/api_utils.py index 122b06f944..dde358207a 100644 --- a/cl/search/api_utils.py +++ b/cl/search/api_utils.py @@ -40,7 +40,7 @@ logger = logging.getLogger(__name__) -def get_object_list(request, cd, paginator): +def get_object_list(request, cd, paginator, es_flag_status): """Perform the Solr work""" # Set the offset value try: @@ -56,21 +56,19 @@ def get_object_list(request, cd, paginator): if cd["type"] == SEARCH_TYPES.DOCKETS: group = True - is_oral_argument_active = cd[ - "type" - ] == SEARCH_TYPES.ORAL_ARGUMENT and waffle.flag_is_active( - request, "oa-es-activate" + is_oral_argument_active = ( + cd["type"] == SEARCH_TYPES.ORAL_ARGUMENT and es_flag_status ) - is_people_active = cd[ - "type" - ] == SEARCH_TYPES.PEOPLE and waffle.flag_is_active(request, "p-es-active") - is_opinion_active = cd["type"] == SEARCH_TYPES.OPINION and ( - waffle.flag_is_active(request, "o-es-search-api-active") + is_people_active = cd["type"] == SEARCH_TYPES.PEOPLE and es_flag_status + is_opinion_active = cd["type"] == SEARCH_TYPES.OPINION and es_flag_status + is_recap_active = ( + cd["type"] + in [ + SEARCH_TYPES.RECAP, + SEARCH_TYPES.DOCKETS, + ] + and es_flag_status ) - is_recap_active = cd["type"] in [ - SEARCH_TYPES.RECAP, - SEARCH_TYPES.DOCKETS, - ] and (waffle.flag_is_active(request, "r-es-search-api-active")) if is_oral_argument_active: search_query = AudioDocument.search() diff --git a/cl/search/api_views.py b/cl/search/api_views.py index 21011af363..6fd8269554 100644 --- a/cl/search/api_views.py +++ b/cl/search/api_views.py @@ -269,36 +269,53 @@ class SearchViewSet(LoggingMixin, viewsets.ViewSet): def list(self, request, *args, **kwargs): - is_opinion_active = waffle.flag_is_active( - request, "o-es-search-api-active" - ) - search_form = SearchForm(request.GET, is_es_form=is_opinion_active) + match request.GET["type"]: + case SEARCH_TYPES.ORAL_ARGUMENT if waffle.flag_is_active( + request, "oa-es-active" + ): + es_flag_is_active = True + case SEARCH_TYPES.PEOPLE if waffle.flag_is_active( + request, "p-es-active" + ): + es_flag_is_active = True + case SEARCH_TYPES.OPINION if waffle.flag_is_active( + request, "o-es-search-api-active" + ): + es_flag_is_active = True + case ( + SEARCH_TYPES.RECAP | SEARCH_TYPES.DOCKETS + ) if waffle.flag_is_active(request, "r-es-search-api-active"): + es_flag_is_active = True + case _: + es_flag_is_active = False + + search_form = SearchForm(request.GET, is_es_form=es_flag_is_active) if search_form.is_valid(): cd = search_form.cleaned_data - search_type = cd["type"] paginator = pagination.PageNumberPagination() - sl = api_utils.get_object_list(request, cd=cd, paginator=paginator) + sl = api_utils.get_object_list( + request, + cd=cd, + paginator=paginator, + es_flag_status=es_flag_is_active, + ) result_page = paginator.paginate_queryset(sl, request) match search_type: - case SEARCH_TYPES.ORAL_ARGUMENT if waffle.flag_is_active( - request, "oa-es-active" - ): + case SEARCH_TYPES.ORAL_ARGUMENT if es_flag_is_active: serializer = V3OAESResultSerializer(result_page, many=True) - case SEARCH_TYPES.PEOPLE if waffle.flag_is_active( - request, "p-es-active" - ): + case SEARCH_TYPES.PEOPLE if es_flag_is_active: serializer = ExtendedPersonESSerializer( result_page, many=True ) - case SEARCH_TYPES.OPINION if is_opinion_active: + case SEARCH_TYPES.OPINION if es_flag_is_active: serializer = V3OpinionESResultSerializer( result_page, many=True ) case ( SEARCH_TYPES.RECAP | SEARCH_TYPES.DOCKETS - ) if waffle.flag_is_active(request, "r-es-search-api-active"): + ) if es_flag_is_active: serializer = V3RECAPDocumentESResultSerializer( result_page, many=True ) From 729f1607c53dfa5a276ae0f8c960ec18484e1417 Mon Sep 17 00:00:00 2001 From: Alberto Islas Date: Wed, 20 Nov 2024 18:14:10 -0600 Subject: [PATCH 2/2] fix(api): Set OPINION as type default value --- cl/search/api_views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cl/search/api_views.py b/cl/search/api_views.py index 6fd8269554..2a2ca2eeeb 100644 --- a/cl/search/api_views.py +++ b/cl/search/api_views.py @@ -269,7 +269,7 @@ class SearchViewSet(LoggingMixin, viewsets.ViewSet): def list(self, request, *args, **kwargs): - match request.GET["type"]: + match request.GET.get("type", SEARCH_TYPES.OPINION): case SEARCH_TYPES.ORAL_ARGUMENT if waffle.flag_is_active( request, "oa-es-active" ):