perf: 优化标签绑定,仅绑定到资产上

This commit is contained in:
ibuler
2024-01-03 16:14:27 +08:00
committed by 老广
parent eb5a53b91b
commit 15ac81a422
7 changed files with 44 additions and 24 deletions

View File

@@ -73,7 +73,7 @@ class LabelContentTypeResourceViewSet(JMSModelViewSet):
queryset = model.objects.all()
if bound == '1':
queryset = queryset.filter(id__in=list(res_ids))
elif bound == '0':
else:
queryset = queryset.exclude(id__in=list(res_ids))
keyword = self.request.query_params.get('search')
if keyword:
@@ -90,9 +90,10 @@ class LabelContentTypeResourceViewSet(JMSModelViewSet):
LabeledResource.objects \
.filter(res_type=content_type, label=label) \
.exclude(res_id__in=res_ids).delete()
resources = []
for res_id in res_ids:
resources.append(LabeledResource(res_type=content_type, res_id=res_id, label=label, org_id=current_org.id))
resources = [
LabeledResource(res_type=content_type, res_id=res_id, label=label, org_id=current_org.id)
for res_id in res_ids
]
LabeledResource.objects.bulk_create(resources, ignore_conflicts=True)
return Response({"total": len(res_ids)})
@@ -129,15 +130,22 @@ class LabeledResourceViewSet(OrgBulkModelViewSet):
}
ordering_fields = ('res_type', 'date_created')
# Todo: 这里需要优化,查询 sql 太多
def filter_search(self, queryset):
keyword = self.request.query_params.get('search')
if not keyword:
return queryset
keyword = keyword.strip().lower()
matched = []
for instance in queryset:
if keyword.lower() in str(instance.resource).lower():
matched.append(instance.id)
offset = 0
limit = 10000
while True:
page = queryset[offset:offset + limit]
if not page:
break
offset += limit
for instance in page:
if keyword in str(instance.resource).lower():
matched.append(instance.id)
return queryset.filter(id__in=matched)
def get_queryset(self):