From b7df4a4ad928b3176756c92742e2575e36107d01 Mon Sep 17 00:00:00 2001 From: IrfanUddinAhmad Date: Sat, 25 Nov 2023 07:50:11 +0500 Subject: [PATCH] feat: ENT-7918 Added academy tags to Algolia index --- enterprise_catalog/apps/api/tasks.py | 37 +++++++++++++++---- .../apps/catalog/algolia_utils.py | 2 + 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/enterprise_catalog/apps/api/tasks.py b/enterprise_catalog/apps/api/tasks.py index ee209c113..680f7a47a 100644 --- a/enterprise_catalog/apps/api/tasks.py +++ b/enterprise_catalog/apps/api/tasks.py @@ -633,6 +633,7 @@ def add_metadata_to_algolia_objects( customer_uuids, catalog_queries, academy_uuids, + academy_tags, ): """ Convert ContentMetadata objects into Algolia products and accumulate results into `algolia_products_by_object_id`. @@ -650,6 +651,7 @@ def add_metadata_to_algolia_objects( catalog_uuids (list of str): Associated catalog UUIDs. customer_uuids (list of str): Associated customer UUIDs. academy_uuids (list of str): Associated academy UUIDs. + academy_tags (list of str): Associated academy tags. catalog_queries (list of tuple(str, str)): Associated catalog queries, as a list of (UUID, title) tuples. """ # add enterprise-related uuids to json_metadata @@ -688,6 +690,16 @@ def add_metadata_to_algolia_objects( ) _add_in_algolia_products_by_object_id(algolia_products_by_object_id, batched_metadata) + # academy tags + academy_tags = sorted(list(academy_tags)) + batched_metadata = _batched_metadata( + json_metadata, + academy_tags, + 'academy_tags', + '{}-academy-tags-{}', + ) + _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)) @@ -750,10 +762,12 @@ def _get_algolia_products_for_batch( customer_uuids_by_key = defaultdict(set) catalog_queries_by_key = defaultdict(set) academy_uuids_by_key = defaultdict(set) + academy_tags_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) + academy_tags_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') @@ -822,8 +836,13 @@ def _get_algolia_products_for_batch( customer_uuid_by_catalog_uuid[str(catalog.uuid)] = str(catalog.enterprise_uuid) associated_academies = catalog.academies.all() for academy in associated_academies: + associated_academy_tags = academy.tags.all() academy_uuids_by_key[content_key].add(str(academy.uuid)) academy_uuids_by_catalog_uuid[str(catalog.uuid)].add(str(academy.uuid)) + for tag in associated_academy_tags: + academy_tags_by_key[content_key].add(str(tag.title)) + academy_tags_by_catalog_uuid[str(catalog.uuid)].add(str(tag.title)) + # Second pass. This time the goal is to capture indirect relationships on programs: # * For each program: @@ -843,18 +862,20 @@ def _get_algolia_products_for_batch( common_catalogs = set() if catalog_uuids_for_all_courses_of_program: common_catalogs = set.intersection(*catalog_uuids_for_all_courses_of_program) - for course_metadata in program_to_courses_mapping[program_content_key]: - catalog_queries_by_key[program_content_key].update( - catalog_query_uuid_by_catalog_uuid[catalog_uuid] for catalog_uuid in common_catalogs - ) catalog_uuids_by_key[program_content_key].update(common_catalogs) - 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: + catalog_queries_by_key[program_content_key].update( + catalog_query_uuid_by_catalog_uuid[catalog_uuid] + ) + customer_uuids_by_key[program_content_key].update( + customer_uuid_by_catalog_uuid[catalog_uuid] + ) academy_uuids_by_key[program_content_key].update( academy_uuids_by_catalog_uuid[catalog_uuid] ) + academy_tags_by_key[program_content_key].update( + academy_tags_by_catalog_uuid[catalog_uuid] + ) # Third pass. This time the goal is to capture indirect relationships on pathways: # * For each pathway: @@ -870,6 +891,7 @@ def _get_algolia_products_for_batch( 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]) + academy_tags_by_key[pathway_content_key].update(academy_tags_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 @@ -905,6 +927,7 @@ def _get_algolia_products_for_batch( customer_uuids_by_key[metadata.content_key], catalog_queries_by_key[metadata.content_key], academy_uuids_by_key[metadata.content_key], + academy_tags_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 206aeb455..b63552198 100644 --- a/enterprise_catalog/apps/catalog/algolia_utils.py +++ b/enterprise_catalog/apps/catalog/algolia_utils.py @@ -38,6 +38,7 @@ 'enterprise_catalog_query_uuids', 'enterprise_customer_uuids', 'academy_uuids', + 'academy_tags', 'full_description', 'key', # for links to Course about pages from the Learner Portal search page 'uuid', @@ -105,6 +106,7 @@ 'enterprise_catalog_query_uuids', 'enterprise_customer_uuids', 'academy_uuids', + 'academy_tags', 'language', 'level_type', 'program_type',