Skip to content

Commit

Permalink
Merge pull request #724 from openedx/iahmad/ENT-7918
Browse files Browse the repository at this point in the history
feat: ENT-7918 ENT-7554 Added academies in Algolia index
  • Loading branch information
irfanuddinahmad authored Dec 5, 2023
2 parents ae3e09e + dce873f commit 2e694ce
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 29 deletions.
Empty file.
2 changes: 2 additions & 0 deletions enterprise_catalog/apps/academy/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class TagFactory(factory.django.DjangoModelFactory):
class Meta:
model = Tag

title = FuzzyText(length=32)


class AcademyFactory(factory.django.DjangoModelFactory):
"""
Expand Down
72 changes: 62 additions & 10 deletions enterprise_catalog/apps/api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,8 @@ def add_metadata_to_algolia_objects(
catalog_uuids,
customer_uuids,
catalog_queries,
academy_uuids,
academy_tags,
):
"""
Convert ContentMetadata objects into Algolia products and accumulate results into `algolia_products_by_object_id`.
Expand All @@ -648,6 +650,8 @@ def add_metadata_to_algolia_objects(
actually index.
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
Expand Down Expand Up @@ -676,6 +680,26 @@ 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)

# 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))
Expand Down Expand Up @@ -737,12 +761,20 @@ 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)
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')
all_catalog_queries = CatalogQuery.objects.prefetch_related(
'enterprise_catalogs',
'enterprise_catalogs__academies',
'enterprise_catalogs__academies__tags',
)

# Retrieve ContentMetadata records for:
# * Course runs, courses, programs and learner pathways that are directly requested, and
Expand Down Expand Up @@ -804,8 +836,18 @@ def _get_algolia_products_for_batch(
catalog_uuids_by_key[content_key].add(str(catalog.uuid))
customer_uuids_by_key[content_key].add(str(catalog.enterprise_uuid))
# 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)
catalog_query_uuid_by_catalog_uuid[str(catalog.uuid)].add(
(str(catalog_query.uuid), catalog_query.title)
)
customer_uuid_by_catalog_uuid[str(catalog.uuid)].add(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:
Expand All @@ -825,14 +867,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:
Expand All @@ -847,6 +895,8 @@ 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])
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
Expand Down Expand Up @@ -881,6 +931,8 @@ 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],
academy_tags_by_key[metadata.content_key],
)

num_content_metadata_indexed += 1
Expand Down
Loading

0 comments on commit 2e694ce

Please sign in to comment.