Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ENT-7554 Added academies in Algolia index
Browse files Browse the repository at this point in the history
IrfanUddinAhmad committed Nov 20, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent def14ed commit 9ac9b4d
Showing 5 changed files with 182 additions and 12 deletions.
Empty file.
50 changes: 50 additions & 0 deletions enterprise_catalog/apps/academy/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from uuid import uuid4

import factory
from factory.fuzzy import FuzzyText

from enterprise_catalog.apps.academy.models import Academy, Tag
from enterprise_catalog.apps.catalog.tests.factories import (
EnterpriseCatalogFactory,
)


class TagFactory(factory.django.DjangoModelFactory):
"""
Test factory for the `Tag` model
"""
class Meta:
model = Tag


class AcademyFactory(factory.django.DjangoModelFactory):
"""
Test factory for the `Academy` model
"""
class Meta:
model = Academy

uuid = factory.LazyFunction(uuid4)
title = FuzzyText(length=32)
short_description = FuzzyText(length=32)
long_description = FuzzyText(length=255)

@factory.post_generation
def enterprise_catalogs(self, create, extracted, **kwargs): # pylint: disable=unused-argument
if extracted:
for enterprise_catalog in extracted:
self.enterprise_catalogs.add(enterprise_catalog) # pylint: disable=no-member

Check warning on line 36 in enterprise_catalog/apps/academy/tests/factories.py

Codecov / codecov/patch

enterprise_catalog/apps/academy/tests/factories.py#L36

Added line #L36 was not covered by tests
else:
enterprise_catalog1 = EnterpriseCatalogFactory()
enterprise_catalog2 = EnterpriseCatalogFactory()
self.enterprise_catalogs.set([enterprise_catalog1, enterprise_catalog2]) # pylint: disable=no-member

@factory.post_generation
def tags(self, create, extracted, **kwargs): # pylint: disable=unused-argument
if extracted:
for tag in extracted:
self.tags.add(tag) # pylint: disable=no-member

Check warning on line 46 in enterprise_catalog/apps/academy/tests/factories.py

Codecov / codecov/patch

enterprise_catalog/apps/academy/tests/factories.py#L46

Added line #L46 was not covered by tests
else:
tag1 = TagFactory()
tag2 = TagFactory()
self.tags.set([tag1, tag2]) # pylint: disable=no-member
27 changes: 26 additions & 1 deletion enterprise_catalog/apps/api/tasks.py
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
from datetime import timedelta
from operator import itemgetter

import newrelic.agent
from celery import shared_task, states
from celery.exceptions import Ignore
from celery_utils.logged_task import LoggedTask
@@ -632,6 +633,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 +678,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))
@@ -684,6 +696,7 @@ def add_metadata_to_algolia_objects(


# pylint: disable=too-many-statements
@newrelic.agent.profile_trace()
def _get_algolia_products_for_batch(
batch_num,
content_keys_batch,
@@ -737,9 +750,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')
@@ -799,13 +814,17 @@ def _get_algolia_products_for_batch(
for catalog_query in associated_catalog_queries:
catalog_queries_by_key[content_key].add((str(catalog_query.uuid), catalog_query.title))
# This line is possible thanks to `all_catalog_queries` with the prefectch_related() above.
associated_catalogs = catalog_query.enterprise_catalogs.all()
associated_catalogs = catalog_query.enterprise_catalogs.prefetch_related('academies').all()
for catalog in associated_catalogs:
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)
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 +852,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 +870,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 +905,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
Loading

0 comments on commit 9ac9b4d

Please sign in to comment.