Skip to content

Commit

Permalink
feature: 订阅详情接口支持过滤现存范围内执行快照 (closed #1222)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuoZhuoCrayon committed Dec 22, 2022
1 parent dc678b8 commit ba5cf6d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
22 changes: 19 additions & 3 deletions apps/backend/subscription/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def task_result(
statuses: List = None,
instance_id_list: List = None,
need_detail: bool = False,
need_aggregate_all_tasks: bool = False,
need_out_of_scope_snapshots: bool = True,
page: int = None,
pagesize: int = -1,
return_all: bool = False,
Expand All @@ -69,6 +71,8 @@ def task_result(
:param instance_id_list: 需过滤的实例ID列表
:param statuses: 过滤的状态列表
:param need_detail: 是否需要详情
:param need_aggregate_all_tasks: 是否需要聚合全部任务查询最后一次视图
:param need_out_of_scope_snapshots: 是否需要已不在范围内的快照信息
:param page: 页数
:param pagesize: 页码
:param return_all: 是否返回全量(用于兼容老接口)
Expand Down Expand Up @@ -97,8 +101,6 @@ def task_result(
)
task_id_list = [subscription_tasks[0]["id"]]

id__task_map = {task["id"]: task for task in subscription_tasks}

# 检查任务是否已准备就绪
is_ready = self.check_task_ready(task_id_list)
if not is_ready:
Expand All @@ -114,6 +116,9 @@ def task_result(
begin, end = None, None

base_kwargs = {"subscription_id": self.subscription_id, "task_id__in": task_id_list}
if need_aggregate_all_tasks:
# 如果需要聚合全部业务,这里需要取消过滤任务ID列表
base_kwargs.pop("task_id__in")
filter_kwargs = deepcopy(base_kwargs)
is_query_change = False

Expand All @@ -124,6 +129,17 @@ def task_result(
is_query_change = True
filter_kwargs["status__in"] = statuses

if not need_out_of_scope_snapshots:
# 如果不需要已不在订阅范围内的执行快照,查询订阅范围过滤掉移除的实例 ID
subscription = models.Subscription.objects.get(id=self.subscription_id)
scope_instance_id_list: Set[str] = set(
tools.get_instances_by_scope(subscription.scope, get_cache=True).keys()
)
if filter_kwargs.get("instance_id__in"):
filter_kwargs["instance_id__in"] = set(filter_kwargs["instance_id__in"]) & scope_instance_id_list
else:
filter_kwargs["instance_id__in"] = scope_instance_id_list

all_instance_record_ids = SubscriptionTools.fetch_latest_record_ids_in_same_inst_id(
models.SubscriptionInstanceRecord.objects.filter(**base_kwargs)
)
Expand All @@ -150,7 +166,7 @@ def task_result(
instance_status_list = []
else:
# 用 subscription_task 是否有pipeline_id字段作为新老记录的区别,要么是新的要么是旧的,不存在混合的情况
is_new_task = id__task_map[instance_records[0].task_id]["pipeline_id"]
is_new_task = subscription_tasks[0]["pipeline_id"]
if is_new_task:
instance_status_list = task_tools.TaskResultTools.list_subscription_task_instance_status(
instance_records, need_detail=need_detail
Expand Down
2 changes: 2 additions & 0 deletions apps/backend/subscription/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ class TaskResultSerializer(GatewaySerializer):
subscription_id = serializers.IntegerField(label="订阅任务ID")
task_id_list = serializers.ListField(child=serializers.IntegerField(), required=False, label="任务ID列表")
need_detail = serializers.BooleanField(default=False, label="是否需要详情")
need_aggregate_all_tasks = serializers.BooleanField(default=False, label="是否需要聚合全部任务查询最后一次视图")
need_out_of_scope_snapshots = serializers.BooleanField(default=True, label="是否需要已不在范围内的快照信息")


class TaskResultDetailSerializer(GatewaySerializer):
Expand Down
2 changes: 2 additions & 0 deletions apps/backend/subscription/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ def task_result(self, request):
statuses=params.get("statuses"),
instance_id_list=params.get("instance_id_list"),
need_detail=params["need_detail"],
need_aggregate_all_tasks=params["need_aggregate_all_tasks"],
need_out_of_scope_snapshots=params["need_out_of_scope_snapshots"],
page=params["page"],
pagesize=params["pagesize"],
return_all=params["return_all"],
Expand Down
3 changes: 3 additions & 0 deletions dev_log/2.2.32/crayon_202211082006.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
feature:
- "订阅详情接口支持过滤现存范围内执行快照 (closed #1222)"

0 comments on commit ba5cf6d

Please sign in to comment.