Skip to content

Commit

Permalink
pagination in draft entries
Browse files Browse the repository at this point in the history
  • Loading branch information
sudan45 committed Dec 20, 2023
1 parent 9932b9e commit 2a92844
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 49 deletions.
2 changes: 1 addition & 1 deletion apps/assisted_tagging/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
for field, enum in (
(DraftEntry.prediction_status, DraftEntryPredictionStatusEnum),
(AssistedTaggingPrediction.data_type, AssistedTaggingPredictionDataTypeEnum),
(DraftEntry.draft_entry_type, DraftEntryTypeEnum),
(DraftEntry.type, DraftEntryTypeEnum),
(Lead.auto_entry_extraction_status, AutoEntryExtractionTypeEnum),
)
}
Expand Down
4 changes: 2 additions & 2 deletions apps/assisted_tagging/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@


class DraftEntryFilterSet(django_filters.FilterSet):
leads = IDFilter(field_name='lead')
draft_entry_types = MultipleInputFilter(DraftEntryTypeEnum, field_name='draft_entry_type')
lead = IDFilter(field_name='lead')
draft_entry_types = MultipleInputFilter(DraftEntryTypeEnum, field_name='type')
is_discarded = django_filters.BooleanFilter()

class Meta:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.17 on 2023-12-20 11:34

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('assisted_tagging', '0012_draftentry_is_discarded'),
]

operations = [
migrations.RenameField(
model_name='draftentry',
old_name='draft_entry_type',
new_name='type',
),
]
2 changes: 1 addition & 1 deletion apps/assisted_tagging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Type(models.IntegerChoices):
prediction_received_at = models.DateTimeField(null=True, blank=True)
# Additional attribues
related_geoareas = models.ManyToManyField(GeoArea, blank=True)
draft_entry_type = models.SmallIntegerField(choices=Type.choices, default=Type.MANUAL)
type = models.SmallIntegerField(choices=Type.choices, default=Type.MANUAL)
is_discarded = models.BooleanField(default=False)

def __str__(self):
Expand Down
3 changes: 2 additions & 1 deletion apps/assisted_tagging/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
)
from deep.permissions import ProjectPermissions as PP

from lead.schema import LeadType
from .models import (
DraftEntry,
MissingPredictionReview,
Expand Down Expand Up @@ -116,7 +117,7 @@ class Arguments:
data = TriggerAutoDraftEntryInputType(required=True)
model = DraftEntry
serializer_class = TriggerDraftEntryGqlSerializer
result = graphene.Field(DraftEntryType)
result = graphene.Field(LeadType)
permissions = [PP.Permission.CREATE_ENTRY]


Expand Down
72 changes: 46 additions & 26 deletions apps/assisted_tagging/schema.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import graphene
from graphene_django import DjangoObjectType, DjangoListField
from graphene_django import DjangoObjectType
from graphene_django_extras import DjangoObjectField
from django.db.models import Prefetch
from assisted_tagging.filters import DraftEntryFilterDataInputType, DraftEntryFilterSet
from assisted_tagging.filters import DraftEntryFilterSet

from utils.graphene.enums import EnumDescription
from user_resource.schema import UserResourceMixin
Expand All @@ -12,6 +12,9 @@
ProjectGeoAreaType,
get_geo_area_queryset_for_project_geo_area_type,
)
from utils.graphene.fields import DjangoPaginatedListObjectField
from utils.graphene.pagination import NoOrderingPageGraphqlPagination
from utils.graphene.types import CustomDjangoListObjectType
from .models import (
DraftEntry,
AssistedTaggingModel,
Expand Down Expand Up @@ -99,10 +102,24 @@ def resolve_prediction_tags(root, info, **kwargs):


# -- Project Level
def get_draft_entry_qs(info):
def get_draft_entry_qs(info): # TODO use dataloder
qs = DraftEntry.objects.filter(project=info.context.active_project)
if PP.check_permission(info, PP.Permission.VIEW_ENTRY):
return qs
return qs.prefetch_related(
Prefetch(
'predictions',
queryset=AssistedTaggingPrediction.objects.filter(is_selected=True).order_by('id'),
),
Prefetch(
'related_geoareas',
queryset=get_geo_area_queryset_for_project_geo_area_type().order_by('id'),
),
'predictions__model_version',
'predictions__model_version__model',
'predictions__wrong_prediction_reviews',
'missing_prediction_reviews',
'related_geoareas',
)
return qs.none()


Expand Down Expand Up @@ -188,22 +205,8 @@ class Meta:
)

@staticmethod
def get_custom_queryset(root, info, _filter, **kwargs):
return get_draft_entry_with_filter_qs(info, _filter).prefetch_related(
Prefetch(
'predictions',
queryset=AssistedTaggingPrediction.objects.order_by('id'),
),
Prefetch(
'related_geoareas',
queryset=get_geo_area_queryset_for_project_geo_area_type().order_by('id'),
),
'predictions__model_version',
'predictions__model_version__model',
'predictions__wrong_prediction_reviews',
'missing_prediction_reviews',
'related_geoareas',
)
def get_custom_queryset(root, info, **kwargs):
return get_draft_entry_qs(info)

@staticmethod
def resolve_predictions(root, info, **kwargs):
Expand All @@ -218,7 +221,13 @@ def resolve_related_geoareas(root, info, **kwargs):
return root.related_geoareas.all() # NOTE: Prefetched by DraftEntry


class AutoextractionStatusType(graphene.ObjectType):
class DraftEntryListType(CustomDjangoListObjectType):
class Meta:
model = DraftEntry
filterset_class = DraftEntryFilterSet


class AutoExtractionStatusType(graphene.ObjectType):
auto_entry_extraction_status = graphene.Field(AutoEntryExtractionTypeEnum, required=True)

@staticmethod
Expand All @@ -229,11 +238,22 @@ def custom_queryset(root, info, lead_id):

class AssistedTaggingQueryType(graphene.ObjectType):
draft_entry = DjangoObjectField(DraftEntryType)
draft_entry_by_leads = DjangoListField(DraftEntryType, filter=DraftEntryFilterDataInputType())
extraction_status_by_lead = graphene.Field(AutoextractionStatusType, lead_id=graphene.ID(required=True))
draft_entries = DjangoPaginatedListObjectField(
DraftEntryListType,
pagination=NoOrderingPageGraphqlPagination(
page_size_query_param='pageSize',
),
)
extraction_status_by_lead = graphene.Field(AutoExtractionStatusType, lead_id=graphene.ID(required=True))

def resolve_draft_entry_by_leads(root, info, filter):
return DraftEntryType.get_custom_queryset(root, info, filter)
@staticmethod
def resolve_draft_entry(root, info, **kwargs):
return DraftEntryType.get_custom_queryset(root, info, **kwargs)

@staticmethod
def resolve_draft_entries(root, info, **_):
return get_draft_entry_qs(info)

@staticmethod
def resolve_extraction_status_by_lead(root, info, lead_id):
return AutoextractionStatusType.custom_queryset(root, info, lead_id)
return AutoExtractionStatusType.custom_queryset(root, info, lead_id)
6 changes: 3 additions & 3 deletions apps/assisted_tagging/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def validate_lead(self, lead):
raise serializers.ValidationError('Assisted tagging is disabled for the Framework used by this project.')
if self.project.is_private:
raise serializers.ValidationError('Assisted tagging is not available for private projects.')
if lead.confidentiality == Lead.Confidentiality.CONFIDENTIAL:
raise serializers.ValidationError('Assisted tagging is not available for confidential lead')
if lead.confidentiality == (Lead.Confidentiality.CONFIDENTIAL or Lead.Confidentiality.RESTRICTED):
raise serializers.ValidationError('Assisted tagging is not available for confidential lead or restricated')
return lead


Expand Down Expand Up @@ -161,7 +161,7 @@ def create(self, data):
lead.auto_entry_extraction_status = Lead.AutoExtractionStatus.PENDING
lead.save(update_fields=['auto_entry_extraction_status'])
transaction.on_commit(
lambda: trigger_request_for_auto_draft_entry_task.delay(self.data['lead'])
lambda: trigger_request_for_auto_draft_entry_task.delay(lead.id)
)
return True

Expand Down
8 changes: 2 additions & 6 deletions apps/deepl_integration/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ def send_trigger_request_to_extractor(cls, draft_entry):
draft_entry.prediction_status = DraftEntry.PredictionStatus.SEND_FAILED
draft_entry.save(update_fields=('prediction_status',))
_response = locals().get('response')
logger.error(payload)
logger.error(
'Assisted tagging send failed!!',
extra={
Expand Down Expand Up @@ -339,7 +338,6 @@ def save_data(cls, draft_entry, data):
with transaction.atomic():
draft_entry.clear_data() # Clear old data if exists
draft_entry.calculated_at = timezone.now()
# for prediction in model_preds:
model_version = models_version_map[(model_preds['model_info']['id'], model_preds['model_info']['version'])]
cls._process_model_preds(model_version, current_tags_map, draft_entry, model_preds)
draft_entry.prediction_status = DraftEntry.PredictionStatus.DONE
Expand Down Expand Up @@ -370,7 +368,6 @@ def auto_trigger_request_to_extractor(cls, lead):
headers=cls.REQUEST_HEADERS,
json=payload
)
logger.error(response.status_code)
if response.status_code == 202:
lead.auto_entry_extraction_status = Lead.AutoExtractionStatus.PENDING
lead.save()
Expand All @@ -379,7 +376,6 @@ def auto_trigger_request_to_extractor(cls, lead):
except Exception:
logger.error('Entry Extraction send failed, Exception occurred!!', exc_info=True)
_response = locals().get('response')
logger.error(payload)
logger.error(
'Entry Extraction send failed!!',
extra={
Expand Down Expand Up @@ -508,7 +504,7 @@ def _process_model_preds(cls, model_version, current_tags_map, draft_entry, mode
@classmethod
@transaction.atomic
def save_data(cls, lead, data):
draft_entry = DraftEntry.objects.filter(lead=lead, draft_entry_type=0)
draft_entry = DraftEntry.objects.filter(lead=lead, type=0)
if draft_entry.exists():
raise serializers.ValidationError('Draft entries already exit')
for model_preds in data['blocks']:
Expand All @@ -532,7 +528,7 @@ def save_data(cls, lead, data):
lead=lead,
excerpt=model_preds['text'],
prediction_status=DraftEntry.PredictionStatus.DONE,
draft_entry_type=0
type=0
)
draft.save()
model_version = models_version_map[
Expand Down
5 changes: 1 addition & 4 deletions apps/deepl_integration/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class LeadExtractCallbackSerializer(DeeplServerBaseCallbackSerializer):
)
text_path = serializers.CharField(required=False, allow_null=True)
total_words_count = serializers.IntegerField(required=False, default=0, allow_null=True)
total_pages = serializers.IntegerField(required=False, default=0)
total_pages = serializers.IntegerField(required=False, default=0, allow_null=True)
text_extraction_id = serializers.CharField(required=True)
nlp_handler = LeadExtractionHandler

Expand Down Expand Up @@ -162,9 +162,6 @@ def create(self, data):

# --- AssistedTagging
class AssistedTaggingModelPredictionCallbackSerializer(serializers.Serializer):
# class ModelInfoCallbackSerializer(serializers.Serializer):
# id = serializers.CharField()
# version = serializers.CharField()

class ModelPredictionCallbackSerializerTagValue(serializers.Serializer):
prediction = serializers.DecimalField(
Expand Down
2 changes: 1 addition & 1 deletion deep/deepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ class DeeplServiceEndpoint():
ANALYSIS_AUTOMATIC_SUMMARY = f'{DEEPL_SERVER_DOMAIN}/api/v1/summarization/'
ANALYSIS_AUTOMATIC_NGRAM = f'{DEEPL_SERVER_DOMAIN}/api/v1/ngrams/'
ANALYSIS_GEO = f'{DEEPL_SERVER_DOMAIN}/api/v1/geolocation/'
ASSISTED_TAGGING_ENTRY_PREDICT_ENDPOINT = f'{DEEPL_SERVICE_DOMAIN}/api/v1/entry-classification/'
ASSISTED_TAGGING_ENTRY_PREDICT_ENDPOINT = f'{DEEPL_SERVER_DOMAIN}/api/v1/entry-classification/'
ENTRY_EXTRACTION_CLASSIFICATION = f'{DEEPL_SERVER_DOMAIN}/api/v1/entry-extraction-classification/'
8 changes: 4 additions & 4 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@ type AssistedTaggingPredictionType {
type AssistedTaggingQueryType {
draftEntry(id: ID!): DraftEntryType
draftEntryByLeads(filter: DraftEntryFilterDataInputType): [DraftEntryType!]
extractionStatusByLead(leadId: ID!): AutoextractionStatusType
extractionStatusByLead(leadId: ID!): AutoExtractionStatusType
}

type AssistedTaggingRootQueryType {
Expand Down Expand Up @@ -1467,7 +1467,7 @@ enum AutoEntryExtractionTypeEnum {
FAILED
}

type AutoextractionStatusType {
type AutoExtractionStatusType {
autoEntryExtractionStatus: AutoEntryExtractionTypeEnum!
}

Expand Down Expand Up @@ -1967,7 +1967,7 @@ type DjangoDebugSQL {

type DraftEntryCountByLead {
undiscardedDraftEntry: Int
discardedDraftEnrty: Int
discardedDraftEntry: Int
}

input DraftEntryFilterDataInputType {
Expand Down Expand Up @@ -3993,7 +3993,7 @@ type TriggerAnalysisTopicModel {
type TriggerAutoDraftEntry {
errors: [GenericScalar!]
ok: Boolean
result: DraftEntryType
result: LeadType
}

input TriggerAutoDraftEntryInputType {
Expand Down

0 comments on commit 2a92844

Please sign in to comment.