From 1500dc6211a22e51ccd6cc3cbe5207d4ba4d98de Mon Sep 17 00:00:00 2001 From: IrfanUddinAhmad Date: Wed, 1 Nov 2023 20:46:18 +0500 Subject: [PATCH] feat: ENT-7554 Added academies in Algolia index --- enterprise_catalog/apps/api/tasks.py | 23 +++++++++++++++++++ .../apps/catalog/algolia_utils.py | 4 ++++ 2 files changed, 27 insertions(+) diff --git a/enterprise_catalog/apps/api/tasks.py b/enterprise_catalog/apps/api/tasks.py index 16ca4c6a4..398a5e5a2 100644 --- a/enterprise_catalog/apps/api/tasks.py +++ b/enterprise_catalog/apps/api/tasks.py @@ -632,6 +632,7 @@ def add_metadata_to_algolia_objects( catalog_uuids, customer_uuids, catalog_queries, + academy_uuids, ): """ Convert ContentMetadata objects into Algolia products and accumulate results into `algolia_products_by_object_id`. @@ -676,6 +677,16 @@ def add_metadata_to_algolia_objects( ) _add_in_algolia_products_by_object_id(algolia_products_by_object_id, batched_metadata) + # academy uuids + academy_uuids = sorted(list(academy_uuids)) + batched_metadata = _batched_metadata( + json_metadata, + academy_uuids, + 'academy_uuids', + '{}-academy-uuids-{}', + ) + _add_in_algolia_products_by_object_id(algolia_products_by_object_id, batched_metadata) + # enterprise catalog queries (tuples of (query uuid, query title)), note: account for None being present # within the list queries = sorted(list(catalog_queries)) @@ -737,9 +748,11 @@ def _get_algolia_products_for_batch( catalog_uuids_by_key = defaultdict(set) customer_uuids_by_key = defaultdict(set) catalog_queries_by_key = defaultdict(set) + academy_uuids_by_key = defaultdict(set) catalog_query_uuid_by_catalog_uuid = defaultdict(set) customer_uuid_by_catalog_uuid = defaultdict(set) + academy_uuids_by_catalog_uuid = defaultdict(set) # Create a shared convenience queryset to prefetch catalogs for all metadata lookups below. all_catalog_queries = CatalogQuery.objects.prefetch_related('enterprise_catalogs') @@ -806,6 +819,10 @@ def _get_algolia_products_for_batch( # Cache UUIDs related to each catalog. catalog_query_uuid_by_catalog_uuid[str(catalog.uuid)] = (str(catalog_query.uuid), catalog_query.title) customer_uuid_by_catalog_uuid[str(catalog.uuid)] = str(catalog.enterprise_uuid) + associated_academies = catalog.academies.all() + for academy in associated_academies: + academy_uuids_by_key[content_key].add(str(academy.uuid)) + academy_uuids_by_catalog_uuid[str(catalog.uuid)].add(str(academy.uuid)) # Second pass. This time the goal is to capture indirect relationships on programs: # * For each program: @@ -833,6 +850,10 @@ def _get_algolia_products_for_batch( customer_uuids_by_key[program_content_key].update( customer_uuid_by_catalog_uuid[catalog_uuid] for catalog_uuid in common_catalogs ) + for catalog_uuid in common_catalogs: + academy_uuids_by_key[program_content_key].update( + academy_uuids_by_catalog_uuid[catalog_uuid] + ) # Third pass. This time the goal is to capture indirect relationships on pathways: # * For each pathway: @@ -847,6 +868,7 @@ def _get_algolia_products_for_batch( catalog_queries_by_key[pathway_content_key].update(catalog_queries_by_key[metadata.content_key]) catalog_uuids_by_key[pathway_content_key].update(catalog_uuids_by_key[metadata.content_key]) customer_uuids_by_key[pathway_content_key].update(customer_uuids_by_key[metadata.content_key]) + academy_uuids_by_key[pathway_content_key].update(academy_uuids_by_key[metadata.content_key]) # Extra disabled logic to additionally absorb UUIDs from courses linked to this pathway indirectly via a # program (chain of association is course -> program -> pathway). This doesn't work because @@ -881,6 +903,7 @@ def _get_algolia_products_for_batch( catalog_uuids_by_key[metadata.content_key], customer_uuids_by_key[metadata.content_key], catalog_queries_by_key[metadata.content_key], + academy_uuids_by_key[metadata.content_key], ) num_content_metadata_indexed += 1 diff --git a/enterprise_catalog/apps/catalog/algolia_utils.py b/enterprise_catalog/apps/catalog/algolia_utils.py index a548a5def..a0fd0afc4 100644 --- a/enterprise_catalog/apps/catalog/algolia_utils.py +++ b/enterprise_catalog/apps/catalog/algolia_utils.py @@ -37,6 +37,7 @@ 'enterprise_catalog_uuids', 'enterprise_catalog_query_uuids', 'enterprise_customer_uuids', + 'academy_uuids', 'full_description', 'key', # for links to Course about pages from the Learner Portal search page 'uuid', @@ -103,6 +104,7 @@ 'enterprise_catalog_uuids', 'enterprise_catalog_query_uuids', 'enterprise_customer_uuids', + 'academy_uuids', 'language', 'level_type', 'program_type', @@ -126,6 +128,7 @@ 'unretrievableAttributes': [ 'enterprise_catalog_uuids', 'enterprise_customer_uuids', + 'academy_uuids', ], 'customRanking': [ 'asc(visible_via_association)', @@ -156,6 +159,7 @@ def _should_index_course(course_metadata): Returns: bool: Whether or not the course should be indexed by algolia. """ + return True course_json_metadata = course_metadata.json_metadata advertised_course_run_uuid = course_json_metadata.get('advertised_course_run_uuid') advertised_course_run = _get_course_run_by_uuid(