diff --git a/src/bk-user/bkuser/apis/web/data_source/serializers.py b/src/bk-user/bkuser/apis/web/data_source/serializers.py index cc96d4cc5..f5429f195 100644 --- a/src/bk-user/bkuser/apis/web/data_source/serializers.py +++ b/src/bk-user/bkuser/apis/web/data_source/serializers.py @@ -322,6 +322,11 @@ class DataSourceImportOrSyncOutputSLZ(serializers.Serializer): summary = serializers.CharField(help_text="任务执行结果概述") +class DataSourceSyncRecordSearchInputSLZ(serializers.Serializer): + data_source_id = serializers.IntegerField(help_text="数据源 ID", required=False) + status = serializers.ChoiceField(help_text="数据源同步状态", choices=SyncTaskStatus.get_choices(), required=False) + + class DataSourceSyncRecordListOutputSLZ(serializers.Serializer): id = serializers.IntegerField(help_text="同步记录 ID") data_source_id = serializers.IntegerField(help_text="数据源 ID") diff --git a/src/bk-user/bkuser/apis/web/data_source/views.py b/src/bk-user/bkuser/apis/web/data_source/views.py index ce08464b2..d6a6bf29f 100644 --- a/src/bk-user/bkuser/apis/web/data_source/views.py +++ b/src/bk-user/bkuser/apis/web/data_source/views.py @@ -33,6 +33,7 @@ DataSourceSwitchStatusOutputSLZ, DataSourceSyncRecordListOutputSLZ, DataSourceSyncRecordRetrieveOutputSLZ, + DataSourceSyncRecordSearchInputSLZ, DataSourceTestConnectionInputSLZ, DataSourceTestConnectionOutputSLZ, DataSourceUpdateInputSLZ, @@ -409,7 +410,18 @@ class DataSourceSyncRecordListApi(CurrentUserTenantMixin, generics.ListAPIView): serializer_class = DataSourceSyncRecordListOutputSLZ def get_queryset(self): - return DataSourceSyncTask.objects.filter(data_source__owner_tenant_id=self.get_current_tenant_id()) + slz = DataSourceSyncRecordSearchInputSLZ(data=self.request.query_params) + slz.is_valid(raise_exception=True) + data = slz.validated_data + + queryset = DataSourceSyncTask.objects.filter(data_source__owner_tenant_id=self.get_current_tenant_id()) + if data_source_id := data.get("data_source_id"): + queryset = queryset.filter(data_source_id=data_source_id) + + if status := data.get("status"): + queryset = queryset.filter(status=status) + + return queryset def get_serializer_context(self): context = super().get_serializer_context() @@ -421,6 +433,7 @@ def get_serializer_context(self): @swagger_auto_schema( tags=["data_source"], operation_description="数据源更新记录", + query_serializer=DataSourceSyncRecordSearchInputSLZ(), responses={status.HTTP_200_OK: DataSourceSyncRecordListOutputSLZ(many=True)}, ) def get(self, request, *args, **kwargs): diff --git a/src/bk-user/tests/apis/web/data_source/test_data_source.py b/src/bk-user/tests/apis/web/data_source/test_data_source.py index 6067e3849..664f00d0c 100644 --- a/src/bk-user/tests/apis/web/data_source/test_data_source.py +++ b/src/bk-user/tests/apis/web/data_source/test_data_source.py @@ -437,6 +437,10 @@ def test_list(self, api_client, data_source_sync_tasks): "extras", } + def test_list_with_filter(self, api_client, data_source_sync_tasks): + resp = api_client.get(reverse("data_source.sync_record.list"), data={"status": "success"}) + assert len(resp.data["results"]) == 1 # noqa: PLR2004 + def test_retrieve(self, api_client, data_source_sync_tasks): success_task = data_source_sync_tasks[0] resp = api_client.get(reverse("data_source.sync_record.retrieve", kwargs={"id": success_task.id}))