perf: 批量更新资产消息 (#10280)

Co-authored-by: feng <1304903146@qq.com>
pull/10283/head^2
fit2bot 2023-04-20 16:22:02 +08:00 committed by GitHub
parent 177d634d85
commit 10b033ee97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 6 deletions

View File

@ -111,7 +111,6 @@ class AssetViewSet(SuggestionMixin, NodeFilterMixin, OrgBulkModelViewSet):
("gathered_info", "assets.view_asset"), ("gathered_info", "assets.view_asset"),
) )
extra_filter_backends = [LabelFilterBackend, IpInFilterBackend, NodeFilterBackend] extra_filter_backends = [LabelFilterBackend, IpInFilterBackend, NodeFilterBackend]
skip_assets = []
def get_serializer_class(self): def get_serializer_class(self):
cls = super().get_serializer_class() cls = super().get_serializer_class()
@ -144,6 +143,7 @@ class AssetViewSet(SuggestionMixin, NodeFilterMixin, OrgBulkModelViewSet):
def filter_bulk_update_data(self): def filter_bulk_update_data(self):
bulk_data = [] bulk_data = []
skip_assets = []
for data in self.request.data: for data in self.request.data:
pk = data.get('id') pk = data.get('id')
platform = data.get('platform') platform = data.get('platform')
@ -155,16 +155,16 @@ class AssetViewSet(SuggestionMixin, NodeFilterMixin, OrgBulkModelViewSet):
if platform.type == asset.type: if platform.type == asset.type:
bulk_data.append(data) bulk_data.append(data)
continue continue
self.skip_assets.append(asset) skip_assets.append(asset)
return bulk_data return bulk_data, skip_assets
def bulk_update(self, request, *args, **kwargs): def bulk_update(self, request, *args, **kwargs):
bulk_data = self.filter_bulk_update_data() bulk_data, skip_assets = self.filter_bulk_update_data()
request._full_data = bulk_data request._full_data = bulk_data
response = super().bulk_update(request, *args, **kwargs) response = super().bulk_update(request, *args, **kwargs)
if response.status_code == HTTP_200_OK and self.skip_assets: if response.status_code == HTTP_200_OK and skip_assets:
user = request.user user = request.user
BulkUpdatePlatformSkipAssetUserMsg(user, self.skip_assets).publish() BulkUpdatePlatformSkipAssetUserMsg(user, skip_assets).publish()
return response return response