Skip to content

Commit

Permalink
using raw query for dormant locales count
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Aug 11, 2021
1 parent 666e297 commit c5b3e96
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion core/common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def delete_duplicate_locales(start_from=None): # pragma: no cover
@app.task
def delete_dormant_locales(): # pragma: no cover
from core.concepts.models import LocalizedText
queryset = LocalizedText.objects.filter(name_locales__isnull=True, description_locales__isnull=True)
queryset = LocalizedText.get_dormant_queryset()
total = queryset.count()
logger.info('%s Dormant locales found. Deleting in batches...' % total) # pylint: disable=logging-not-lazy

Expand Down
2 changes: 1 addition & 1 deletion core/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ class ConceptDormantLocalesView(APIView): # pragma: no cover
@staticmethod
def get(_, **kwargs): # pylint: disable=unused-argument
from core.concepts.models import LocalizedText
count = LocalizedText.objects.filter(name_locales__isnull=True, description_locales__isnull=True).count()
count = LocalizedText.dormants()
return Response(count, status=status.HTTP_200_OK)

@staticmethod
Expand Down
24 changes: 23 additions & 1 deletion core/concepts/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.db import models, IntegrityError, transaction
from django.db import models, IntegrityError, transaction, connection
from django.db.models import F
from pydash import get, compact

Expand Down Expand Up @@ -40,6 +40,28 @@ class Meta:
locale_preferred = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)

@classmethod
def get_dormant_queryset(cls):
return cls.objects.filter(name_locales__isnull=True, description_locales__isnull=True)

@classmethod
def dormants(cls, raw=True):
if raw:
with connection.cursor() as cursor:
cursor.execute(
"""
SELECT COUNT("localized_texts"."id") FROM "localized_texts"
WHERE NOT EXISTS (SELECT 1 FROM "concepts_names" WHERE
"concepts_names"."localizedtext_id" = "localized_texts"."id")
AND NOT EXISTS (SELECT 1 FROM "concepts_descriptions"
WHERE "concepts_descriptions"."localizedtext_id" = "localized_texts"."id")
"""
)
count, = cursor.fetchone()
return count

return cls.get_dormant_queryset().count()

def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
if not self.internal_reference_id and self.id:
self.internal_reference_id = str(self.id)
Expand Down
27 changes: 26 additions & 1 deletion core/concepts/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
SHORT, INDEX_TERM, OPENMRS_NAMES_EXCEPT_SHORT_MUST_BE_UNIQUE, OPENMRS_ONE_FULLY_SPECIFIED_NAME_PER_LOCALE,
OPENMRS_NO_MORE_THAN_ONE_SHORT_NAME_PER_LOCALE, CONCEPT_IS_ALREADY_RETIRED, CONCEPT_IS_ALREADY_NOT_RETIRED,
OPENMRS_CONCEPT_CLASS, OPENMRS_DATATYPE, OPENMRS_DESCRIPTION_TYPE, OPENMRS_NAME_LOCALE, OPENMRS_DESCRIPTION_LOCALE)
from core.concepts.models import Concept
from core.concepts.models import Concept, LocalizedText
from core.concepts.tests.factories import LocalizedTextFactory, ConceptFactory
from core.concepts.validators import ValidatorSpecifier
from core.mappings.tests.factories import MappingFactory
Expand All @@ -28,6 +28,31 @@ def test_clone(self):
self.assertNotEqual(saved_locale.id, cloned_locale.id)
self.assertIsNone(cloned_locale.internal_reference_id)

def test_dormants_raw_query(self):
self.assertEqual(LocalizedText.dormants(), 0)

locale = LocalizedTextFactory()
self.assertEqual(LocalizedText.dormants(), 1)

ConceptFactory(names=[locale])

self.assertEqual(LocalizedText.dormants(), 0)

LocalizedTextFactory()
self.assertEqual(LocalizedText.dormants(raw=False), 1)

def test_dormants(self):
self.assertEqual(LocalizedText.dormants(raw=False), 0)

locale = LocalizedTextFactory()
self.assertEqual(LocalizedText.dormants(raw=False), 1)

ConceptFactory(names=[locale])
self.assertEqual(LocalizedText.dormants(raw=False), 0)

LocalizedTextFactory()
self.assertEqual(LocalizedText.dormants(raw=False), 1)


class ConceptTest(OCLTestCase):
def test_is_versioned(self):
Expand Down

0 comments on commit c5b3e96

Please sign in to comment.