From d3dd148ac3fa03346510283f7f51db386c60b908 Mon Sep 17 00:00:00 2001 From: knguyen2 Date: Thu, 6 Jul 2023 20:02:49 +0000 Subject: [PATCH 1/4] feat: remove transmission audits without catalog uuids --- ...remove_null_catalog_transmission_audits.py | 30 ++++++++++++++++++ .../integrated_channel/tasks.py | 25 +++++++++++++++ tests/test_management.py | 31 +++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 integrated_channels/integrated_channel/management/commands/remove_null_catalog_transmission_audits.py diff --git a/integrated_channels/integrated_channel/management/commands/remove_null_catalog_transmission_audits.py b/integrated_channels/integrated_channel/management/commands/remove_null_catalog_transmission_audits.py new file mode 100644 index 0000000000..3614bc9c18 --- /dev/null +++ b/integrated_channels/integrated_channel/management/commands/remove_null_catalog_transmission_audits.py @@ -0,0 +1,30 @@ +""" +Remove content transmission audit records that do not contain a catalog UUID. +""" +import logging + +from django.core.management.base import BaseCommand + +from integrated_channels.integrated_channel.management.commands import IntegratedChannelCommandMixin +from integrated_channels.integrated_channel.tasks import remove_null_catalog_transmission_audits + +LOGGER = logging.getLogger(__name__) + + +class Command(IntegratedChannelCommandMixin, BaseCommand): + """ + Remove content transmission audit records that do not contain a catalog UUID. + ./manage.py lms remove_null_catalog_transmission_audits + """ + + def handle(self, *args, **options): + """ + Filter content transmission audit records that do not contain a catalog UUID and remove them. + """ + try: + remove_null_catalog_transmission_audits.delay() + except Exception as exc: # pylint: disable=broad-except + LOGGER.exception( + f'''Failed to remove content transmission audits that do not + contain a catalog UUID. Task failed with exception: {exc}''' + ) diff --git a/integrated_channels/integrated_channel/tasks.py b/integrated_channels/integrated_channel/tasks.py index ac45530356..55f68472f9 100644 --- a/integrated_channels/integrated_channel/tasks.py +++ b/integrated_channels/integrated_channel/tasks.py @@ -91,6 +91,31 @@ def _log_batch_task_finish(task_name, channel_code, job_user_id, )) +@shared_task +@set_code_owner_attribute +def remove_null_catalog_transmission_audits(): + """ + Task to remove content transmission audit records that do not contain a catalog UUID. + """ + start = time.time() + + _log_batch_task_start('remove_null_catalog_transmission_audits', None, None, None) + + deleted_null_catalog_uuids = ContentMetadataItemTransmission.objects.filter( + enterprise_customer_catalog_uuid=None + ).delete() + + duration_seconds = time.time() - start + _log_batch_task_finish( + 'remove_null_catalog_transmission_audits', + channel_code=None, + job_user_id=None, + integrated_channel_full_config=None, + duration_seconds=duration_seconds, + extra_message=f"{deleted_null_catalog_uuids[0]} transmission audits with no catalog UUIDs removed" + ) + + @shared_task @set_code_owner_attribute def remove_duplicate_transmission_audits(): diff --git a/tests/test_management.py b/tests/test_management.py index ec6ad6b365..39f32dc2c0 100644 --- a/tests/test_management.py +++ b/tests/test_management.py @@ -2003,3 +2003,34 @@ def test_invalid_audits(self): assert moodle_config.last_sync_errored_at == old_timestamp assert moodle_config.last_content_sync_errored_at == old_timestamp assert moodle_config.last_learner_sync_errored_at is None + + +@mark.django_db +@ddt.ddt +class TestRemoveNullCatalogTransmissionAuditsManagementCommand(unittest.TestCase, EnterpriseMockMixin): + """ + Test the ``remove_null_catalog_transmission_audits`` management command. + """ + def setUp(self): + self.enterprise_customer_1 = factories.EnterpriseCustomerFactory( + name='Wonka Factory', + ) + self.enterprise_customer_2 = factories.EnterpriseCustomerFactory( + name='Hershey LLC', + ) + factories.ContentMetadataItemTransmissionFactory( + enterprise_customer=self.enterprise_customer_1, + enterprise_customer_catalog_uuid=None + ) + factories.ContentMetadataItemTransmissionFactory( + enterprise_customer=self.enterprise_customer_2, + enterprise_customer_catalog_uuid="d9efab41-5e09-4094-977a-96313b3dca08" + ) + super().setUp() + + def test_normal_run(self): + assert ContentMetadataItemTransmission.objects.all().count() == 2 + call_command('remove_null_catalog_transmission_audits') + assert ContentMetadataItemTransmission.objects.filter( + enterprise_customer_catalog_uuid=None + ).count() == 0 From 4162d91f52c366c8629fa412cdca58334554815e Mon Sep 17 00:00:00 2001 From: knguyen2 Date: Thu, 6 Jul 2023 20:05:33 +0000 Subject: [PATCH 2/4] fix: updated changelog --- CHANGELOG.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 1de30ba9a4..c1af4f644f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,6 +15,10 @@ Change Log Unreleased ---------- +[3.68.2] +-------- +feat: remove content transmission audits without a catalog uuid + [3.68.1] -------- fix: pick first object from CourseDetails From 777f088acf5fe7bf93cb1fa389409c85a48658bc Mon Sep 17 00:00:00 2001 From: knguyen2 Date: Thu, 6 Jul 2023 20:06:48 +0000 Subject: [PATCH 3/4] fix: bumped version --- enterprise/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/enterprise/__init__.py b/enterprise/__init__.py index ad74471075..093da50190 100644 --- a/enterprise/__init__.py +++ b/enterprise/__init__.py @@ -2,6 +2,6 @@ Your project description goes here. """ -__version__ = "3.68.1" +__version__ = "3.68.2" default_app_config = "enterprise.apps.EnterpriseConfig" From 60488e836516cbce581aee4adb254957f6088292 Mon Sep 17 00:00:00 2001 From: knguyen2 Date: Thu, 6 Jul 2023 20:10:56 +0000 Subject: [PATCH 4/4] fix: updated test --- tests/test_management.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_management.py b/tests/test_management.py index 39f32dc2c0..76c4c65095 100644 --- a/tests/test_management.py +++ b/tests/test_management.py @@ -2031,6 +2031,7 @@ def setUp(self): def test_normal_run(self): assert ContentMetadataItemTransmission.objects.all().count() == 2 call_command('remove_null_catalog_transmission_audits') + assert ContentMetadataItemTransmission.objects.all().count() == 1 assert ContentMetadataItemTransmission.objects.filter( enterprise_customer_catalog_uuid=None ).count() == 0