Skip to content

Commit

Permalink
refactor(release/publish): refactor (#954)
Browse files Browse the repository at this point in the history
  • Loading branch information
wklken authored Sep 26, 2024
1 parent 11c1926 commit 7f4fa1e
Show file tree
Hide file tree
Showing 32 changed files with 269 additions and 418 deletions.
4 changes: 0 additions & 4 deletions src/dashboard/apigateway/.importlinter
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,5 @@ ignore_imports =
# 需要重构的依赖
apigateway.biz.esb.permissions -> apigateway.apps.esb.helpers
apigateway.biz.esb.permissions -> apigateway.apps.esb.utils
apigateway.controller.tasks.syncing -> apigateway.biz.release
apigateway.controller.tasks.release -> apigateway.biz.**
apigateway.controller.crds.release_data.release_data -> apigateway.biz.gateway_jwt
apigateway.common.audit.shortcuts -> apigateway.apps.audit.signals
apigateway.common.release.publish -> apigateway.controller.tasks
apigateway.common.release.publish -> apigateway.controller.constants
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from apigateway.biz.gateway_related_app import GatewayRelatedAppHandler
from apigateway.common.contexts import GatewayAuthContext
from apigateway.common.error_codes import error_codes
from apigateway.common.release.publish import trigger_gateway_publish
from apigateway.controller.publisher.publish import trigger_gateway_publish
from apigateway.core.constants import GatewayStatusEnum, PublishSourceEnum
from apigateway.core.models import Gateway
from apigateway.utils.django import get_model_dict
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
from apigateway.apps.plugin.models import PluginBinding, PluginConfig, PluginForm, PluginType
from apigateway.biz.audit import Auditor
from apigateway.common.error_codes import error_codes
from apigateway.common.release.publish import trigger_gateway_publish
from apigateway.common.renderers import BkStandardApiJSONRenderer
from apigateway.controller.publisher.publish import trigger_gateway_publish
from apigateway.core.constants import PublishSourceEnum
from apigateway.core.models import Resource, Stage
from apigateway.utils.django import get_model_dict
Expand Down
31 changes: 15 additions & 16 deletions src/dashboard/apigateway/apigateway/apis/web/release/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# TencentBlueKing is pleased to support the open source community by making
# 蓝鲸智云 - API 网关(BlueKing - APIGateway) available.
# 蓝鲸智云 - API 网关 (BlueKing - APIGateway) available.
# Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
# Licensed under the MIT License (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
Expand All @@ -21,7 +21,6 @@
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers

from apigateway.biz.release import ReleaseHandler
from apigateway.common.fields import CurrentGatewayDefault, TimestampField
from apigateway.common.i18n.field import SerializerTranslatedField
from apigateway.core.constants import (
Expand All @@ -35,8 +34,8 @@

class ReleaseInputSLZ(serializers.Serializer):
gateway = serializers.HiddenField(default=CurrentGatewayDefault())
stage_id = serializers.IntegerField(required=True, help_text="环境id")
resource_version_id = serializers.IntegerField(required=True, help_text="资源版本id")
stage_id = serializers.IntegerField(required=True, help_text="环境 id")
resource_version_id = serializers.IntegerField(required=True, help_text="资源版本 id")
comment = serializers.CharField(allow_blank=True, required=False, help_text="发布日志")

def validate_stage_id(self, value):
Expand Down Expand Up @@ -73,7 +72,7 @@ def get_labels(self, obj):


class ReleaseResourceSchemaOutputSLZ(serializers.Serializer):
resource_id = serializers.IntegerField(allow_null=False, required=True, help_text="资源id")
resource_id = serializers.IntegerField(allow_null=False, required=True, help_text="资源 id")
body_schema = serializers.JSONField(required=False, help_text="request_body schema")
body_example = serializers.JSONField(required=False, help_text="request_body example")
parameter_schema = serializers.JSONField(required=False, help_text="parameters schema")
Expand All @@ -82,25 +81,25 @@ class ReleaseResourceSchemaOutputSLZ(serializers.Serializer):

class ReleaseHistoryQueryInputSLZ(serializers.Serializer):
keyword = serializers.CharField(allow_blank=True, required=False, help_text="查询参数关键字")
stage_id = serializers.IntegerField(allow_null=True, required=False, help_text="环境id")
stage_id = serializers.IntegerField(allow_null=True, required=False, help_text="环境 id")
created_by = serializers.CharField(allow_blank=True, required=False, help_text="创建者")
time_start = TimestampField(allow_null=True, required=False, help_text="开始时间")
time_end = TimestampField(allow_null=True, required=False, help_text="结束时间")


class ReleaseStageSLZ(serializers.Serializer):
id = serializers.IntegerField(read_only=True, help_text="环境id")
name = serializers.CharField(allow_blank=True, required=False, help_text="环境name")
id = serializers.IntegerField(read_only=True, help_text="环境 id")
name = serializers.CharField(allow_blank=True, required=False, help_text="环境 name")


class ReleaseHistoryOutputSLZ(serializers.Serializer):
id = serializers.IntegerField(read_only=True, help_text="发布历史id")
id = serializers.IntegerField(read_only=True, help_text="发布历史 id")
stage = ReleaseStageSLZ()
resource_version_display = serializers.SerializerMethodField(read_only=True, help_text="发布资源版本")
created_time = serializers.DateTimeField(read_only=True, help_text="发布创建事件")
created_by = serializers.CharField(read_only=True, help_text="发布人")
source = serializers.CharField(read_only=True, help_text="发布来源")
duration = serializers.SerializerMethodField(read_only=True, help_text="发布耗时(s)")
duration = serializers.SerializerMethodField(read_only=True, help_text="发布耗时 (s)")
status = serializers.SerializerMethodField(read_only=True, help_text="发布状态")

def get_resource_version_display(self, obj: ReleaseHistory) -> str:
Expand All @@ -112,29 +111,29 @@ def get_status(self, obj: ReleaseHistory) -> str:
# 兼容历史数据
return obj.status

# 通过最新的event获取release_history状态
return ReleaseHandler.get_status(event)
# 通过最新的 event 获取 release_history 状态
return event.get_release_history_status()

def get_duration(self, obj: ReleaseHistory) -> int:
# 获取最新事件
event = self.context["release_history_events_map"].get(obj.id, None)
if not event:
return 0

# 如果失败,返回event的创建时间和release_history创建时间之差
# 如果失败,返回 event 的创建时间和 release_history 创建时间之差
if event.status == PublishEventStatusEnum.FAILURE.value or (
event.status != PublishEventStatusEnum.DOING.value
and event.name == PublishEventNameTypeEnum.LOAD_CONFIGURATION.value
):
return int((event.created_time - obj.created_time).total_seconds())

# 0代表还没到达终态
# 0 代表还没到达终态
return 0


class ReleaseHistoryEventInfoSLZ(serializers.Serializer):
id = serializers.IntegerField(read_only=True, help_text="发布事件id")
release_history_id = serializers.IntegerField(source="publish_id", allow_null=False, help_text="发布历史id")
id = serializers.IntegerField(read_only=True, help_text="发布事件 id")
release_history_id = serializers.IntegerField(source="publish_id", allow_null=False, help_text="发布历史 id")
name = serializers.SerializerMethodField(read_only=True, help_text="发布事件节点名称")
step = serializers.IntegerField(read_only=True, help_text="发布事件节点所属步骤")
status = serializers.ChoiceField(
Expand Down
10 changes: 5 additions & 5 deletions src/dashboard/apigateway/apigateway/apis/web/release/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from apigateway.biz.resource_version import ResourceVersionHandler
from apigateway.common.error_codes import error_codes
from apigateway.common.user_credentials import get_user_credentials_from_request
from apigateway.core.models import Release, ReleaseHistory
from apigateway.core.models import PublishEvent, Release, ReleaseHistory
from apigateway.utils import openapi
from apigateway.utils.exception import LockTimeout
from apigateway.utils.redis_utils import Lock
Expand Down Expand Up @@ -186,7 +186,7 @@ def create(self, request, *args, **kwargs):
slz = ReleaseHistoryOutputSLZ(
history,
context={
"release_history_events_map": ReleaseHandler.get_release_history_id_to_latest_publish_event_map(
"release_history_events_map": PublishEvent.objects.get_release_history_id_to_latest_publish_event_map(
[history.id]
),
},
Expand Down Expand Up @@ -231,7 +231,7 @@ def list(self, request, *args, **kwargs):
page,
many=True,
context={
"release_history_events_map": ReleaseHandler.get_release_history_id_to_latest_publish_event_map(
"release_history_events_map": PublishEvent.objects.get_release_history_id_to_latest_publish_event_map(
[release_history.id for release_history in page]
),
},
Expand Down Expand Up @@ -264,7 +264,7 @@ def retrieve(self, request, *args, **kwargs):
slz = slz_class(
instance,
context={
"release_history_events_map": ReleaseHandler.get_release_history_id_to_latest_publish_event_map(
"release_history_events_map": PublishEvent.objects.get_release_history_id_to_latest_publish_event_map(
[instance.id]
),
},
Expand Down Expand Up @@ -293,7 +293,7 @@ def retrieve(self, request, *args, **kwargs):
release_history,
context={
"release_history_events": ReleaseHandler.list_publish_events_by_release_history_id(release_history.id),
"release_history_events_map": ReleaseHandler.get_release_history_id_to_latest_publish_event_map(
"release_history_events_map": PublishEvent.objects.get_release_history_id_to_latest_publish_event_map(
[release_history.id]
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from apigateway.biz.resource_version import ResourceVersionHandler
from apigateway.biz.stage import StageHandler
from apigateway.common.error_codes import error_codes
from apigateway.common.release.publish import trigger_gateway_publish
from apigateway.controller.publisher.publish import trigger_gateway_publish
from apigateway.core.constants import PublishSourceEnum
from apigateway.core.models import BackendConfig, Stage
from apigateway.utils.django import get_model_dict
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard/apigateway/apigateway/biz/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from django.db import transaction

from apigateway.common.release.publish import trigger_gateway_publish
from apigateway.controller.publisher.publish import trigger_gateway_publish
from apigateway.core.constants import DEFAULT_BACKEND_NAME, PublishSourceEnum
from apigateway.core.models import Backend, BackendConfig, Proxy
from apigateway.utils.time import now_datetime
Expand Down
3 changes: 0 additions & 3 deletions src/dashboard/apigateway/apigateway/biz/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,3 @@ class OpenAPIFormatEnum(StructuredEnum):

# stage var
STAGE_VAR_FOR_PATH_PATTERN = re.compile(r"^[\w/.-]*$")

# release gateway interval
RELEASE_GATEWAY_INTERVAL_SECOND = 15
Loading

0 comments on commit 7f4fa1e

Please sign in to comment.