Skip to content

Commit

Permalink
API/tasks to remove duplicate versions of concepts
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Nov 28, 2023
1 parent f552f14 commit e234021
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
22 changes: 22 additions & 0 deletions core/common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.core.exceptions import ValidationError
from django.core.mail import EmailMessage
from django.core.management import call_command
from django.db.models import F
from django.template.loader import render_to_string
from django.utils import timezone
from django_elasticsearch_dsl.registries import registry
Expand Down Expand Up @@ -709,3 +710,24 @@ def calculate_checksums(resource_type, resource_id):
instance.get_latest_version().set_checksums()
if not instance.is_versioned_object:
instance.versioned_object.set_checksums()


@app.task(ignore_result=True, retry_kwargs={'max_retries': 0})
def delete_duplicate_concept_versions(source_mnemonic, source_filters=None, concept_filters=None): # pragma: no cover
source_filters = source_filters or {}
concept_filters = concept_filters or {}
from core.sources.models import Source
source = Source.objects.filter(version='HEAD', mnemonic=source_mnemonic).filter(**source_filters).first()
deleted = 0
for concept in source.concepts_set.exclude(id=F('versioned_object_id')).exclude(
is_latest_version=True).filter(**concept_filters).iterator(chunk_size=100):
source_versions = concept.sources
count = source_versions.count()
if not concept.comment and count == 0 or (count == 1 and source_versions.first().version == 'HEAD'):
if not concept.references.exists() and not concept.expansion_set.exists():
if not concept.mappings_from.exists() and not concept.mappings_to.exists():
print(f"***Deleting: {concept.uri}***")
concept.delete()
deleted += 1

print("***DELETED***", deleted)
25 changes: 22 additions & 3 deletions core/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pydash import get, compact, flatten
from rest_framework import response, generics, status
from rest_framework.generics import ListAPIView, RetrieveUpdateDestroyAPIView
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.permissions import AllowAny, IsAuthenticated, IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

Expand Down Expand Up @@ -1089,8 +1089,8 @@ def task_response(self, task, queue='default'):
status=status.HTTP_202_ACCEPTED
)

def perform_task(self, task_func, task_args, queue='default'):
is_async = self.is_async_requested()
def perform_task(self, task_func, task_args, queue='default', is_default_async=False):
is_async = is_default_async or self.is_async_requested()
if self.is_inline_requested() or (get(settings, 'TEST_MODE', False) and not is_async):
result = task_func(*task_args)
else:
Expand All @@ -1108,3 +1108,22 @@ def perform_task(self, task_func, task_args, queue='default'):
return self.task_response(task, queue)

return result


class ConceptDuplicateDeleteView(BaseAPIView, TaskMixin): # pragma: no-cover
permission_classes = (IsAdminUser, )

def post(self, _):
source_mnemonic = self.request.data.get('source_mnemonic', None)
source_filters = self.request.data.get('source_filters', None) or {}
concept_filters = self.request.data.get('concept_filters', None) or {}
if not source_mnemonic:
raise Http400(detail='source_mnemonic is required.')

from core.common.tasks import delete_duplicate_concept_versions
result = self.perform_task(
task_func=delete_duplicate_concept_versions,
task_args=(source_mnemonic, source_filters, concept_filters),
is_default_async=True
)
return result
5 changes: 4 additions & 1 deletion core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from core.common.constants import NAMESPACE_PATTERN
from core.common.utils import get_api_base_url
from core.common.views import RootView, FeedbackView, APIVersionView, ChangeLogView, StandardChecksumView, \
SmartChecksumView
SmartChecksumView, ConceptDuplicateDeleteView
from core.concepts.views import ConceptsHierarchyAmendAdminView
from core.importers.views import BulkImportView

Expand Down Expand Up @@ -91,6 +91,9 @@
),
path('manage/bulkimport/', BulkImportView.as_view(), name='bulk_import_urls'),
path('toggles/', include('core.toggles.urls'), name='toggles'),
path(
'concepts-duplicate-versions-delete/',
ConceptDuplicateDeleteView.as_view(), name='concepts-duplicate-versions-delete'),
]

handler500 = 'rest_framework.exceptions.server_error'
Expand Down

0 comments on commit e234021

Please sign in to comment.