Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: list sync records api support filter #1367

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/bk-user/bkuser/apis/web/data_source/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
15 changes: 14 additions & 1 deletion src/bk-user/bkuser/apis/web/data_source/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
DataSourceSwitchStatusOutputSLZ,
DataSourceSyncRecordListOutputSLZ,
DataSourceSyncRecordRetrieveOutputSLZ,
DataSourceSyncRecordSearchInputSLZ,
DataSourceTestConnectionInputSLZ,
DataSourceTestConnectionOutputSLZ,
DataSourceUpdateInputSLZ,
Expand Down Expand Up @@ -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()
Expand All @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions src/bk-user/tests/apis/web/data_source/test_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}))
Expand Down
Loading