Skip to content

Commit 625dc3a

Browse files
committed
refactor(model): 重构健康状态枚举和API响应映射
将ExceptionStatus重命名为Status以简化命名 新增HealthLevel枚举用于API响应,包含Normal状态 修改service_state和API模型以使用新的枚举类型 更新info_service中的状态映射逻辑
1 parent 52f5cd1 commit 625dc3a

File tree

4 files changed

+48
-26
lines changed

4 files changed

+48
-26
lines changed

internal/service_manager/model/api.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import "time"
88
type ServiceItem struct {
99
Name string `json:"name"` // 服务名称
1010
DeployState DeployStatus `json:"deployState"` // 发布状态:InDeploying|AllDeployFinish
11-
Health Level `json:"health"` // 健康状态:Normal/Warning/Error
11+
Health HealthLevel `json:"health"` // 健康状态:Normal/Warning/Error
1212
Deps []string `json:"deps"` // 依赖关系(直接使用Service.Deps)
1313
}
1414

@@ -20,12 +20,12 @@ type ServicesResponse struct {
2020

2121
// ActiveVersionItem 活跃版本项目
2222
type ActiveVersionItem struct {
23-
Version string `json:"version"` // v1.0.1
24-
DeployID string `json:"deployID"` // 1001
25-
StartTime time.Time `json:"startTime"` // 开始时间
26-
EstimatedCompletionTime time.Time `json:"estimatedCompletionTime"` // 预估完成时间
27-
Instances int `json:"instances"` // 实例个数
28-
Health Level `json:"health"` // 健康状态:Normal/Warning/Error
23+
Version string `json:"version"` // v1.0.1
24+
DeployID string `json:"deployID"` // 1001
25+
StartTime time.Time `json:"startTime"` // 开始时间
26+
EstimatedCompletionTime time.Time `json:"estimatedCompletionTime"` // 预估完成时间
27+
Instances int `json:"instances"` // 实例个数
28+
Health HealthLevel `json:"health"` // 健康状态:Normal/Warning/Error
2929
}
3030

3131
// PrometheusQueryRangeResponse Prometheus query_range接口响应格式

internal/service_manager/model/constants.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
package model
22

3-
// ExceptionStatus 异常处理状态枚举
4-
type ExceptionStatus string
3+
// Status 异常处理状态枚举
4+
type Status string
55

66
const (
7-
ExceptionStatusNew ExceptionStatus = "new"
8-
ExceptionStatusAnalyzing ExceptionStatus = "analyzing"
9-
ExceptionStatusProcessing ExceptionStatus = "processing"
10-
ExceptionStatusResolved ExceptionStatus = "resolved"
7+
StatusNew Status = "new"
8+
StatusAnalyzing Status = "analyzing"
9+
StatusProcessing Status = "processing"
10+
StatusResolved Status = "resolved"
1111
)
1212

1313
// Level 健康状态枚举
1414
type Level string
1515

1616
const (
17-
LevelNormal Level = "Normal"
1817
LevelWarning Level = "Warning"
1918
LevelError Level = "Error"
2019
)
2120

21+
// HealthLevel API响应用的健康状态枚举(包含正常状态)
22+
type HealthLevel string
23+
24+
const (
25+
HealthLevelNormal HealthLevel = "Normal"
26+
HealthLevelWarning HealthLevel = "Warning"
27+
HealthLevelError HealthLevel = "Error"
28+
)
29+
2230
// DeployStatus 部署状态枚举
2331
type DeployStatus string
2432

internal/service_manager/model/service_state.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import "time"
44

55
// ServiceState 服务状态信息
66
type ServiceState struct {
7-
Service string `json:"service" db:"service"` // varchar(255) - 联合PK
8-
Version string `json:"version" db:"version"` // varchar(255) - 联合PK
9-
Level Level `json:"level" db:"level"` // 异常级别
10-
Detail string `json:"detail" db:"detail"` // text - 详细信息
11-
ReportAt time.Time `json:"reportAt" db:"report_at"` // time - 报告时间
12-
ResolvedAt *time.Time `json:"resolvedAt" db:"resolved_at"` // time - 解决时间
13-
HealthStatus ExceptionStatus `json:"healthStatus" db:"health_status"` // 健康状态
14-
CorrelationID string `json:"correlationId" db:"correlation_id"` // varchar - 关联ID
7+
Service string `json:"service" db:"service"` // varchar(255) - 联合PK
8+
Version string `json:"version" db:"version"` // varchar(255) - 联合PK
9+
Level Level `json:"level" db:"level"` // 异常级别
10+
Detail string `json:"detail" db:"detail"` // text - 详细信息
11+
ReportAt time.Time `json:"reportAt" db:"report_at"` // time - 报告时间
12+
ResolvedAt *time.Time `json:"resolvedAt" db:"resolved_at"` // time - 解决时间
13+
HealthStatus Status `json:"healthStatus" db:"health_status"` // 健康状态
14+
CorrelationID string `json:"correlationId" db:"correlation_id"` // varchar - 关联ID
1515
}

internal/service_manager/service/info_service.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,16 @@ func (s *Service) GetServicesResponse(ctx context.Context) (*model.ServicesRespo
2626
log.Error().Err(err).Str("service", service.Name).Msg("failed to get service state")
2727
}
2828

29-
health := model.LevelNormal
29+
// 默认为正常状态,因为正常状态的服务不会存储在service_state表中
30+
health := model.HealthLevelNormal
3031
if state != nil {
31-
health = state.Level
32+
// 将数据库中的异常状态映射为API响应状态
33+
switch state.Level {
34+
case model.LevelWarning:
35+
health = model.HealthLevelWarning
36+
case model.LevelError:
37+
health = model.HealthLevelError
38+
}
3239
}
3340

3441
// 默认设置为已完成部署状态
@@ -74,10 +81,17 @@ func (s *Service) GetServiceActiveVersions(ctx context.Context, serviceName stri
7481
log.Error().Err(err).Str("service", serviceName).Msg("failed to get service state")
7582
}
7683

77-
health := model.LevelNormal
84+
// 默认为正常状态,因为正常状态的服务不会存储在service_state表中
85+
health := model.HealthLevelNormal
7886
reportAt := &model.ServiceState{}
7987
if state != nil {
80-
health = state.Level
88+
// 将数据库中的异常状态映射为API响应状态
89+
switch state.Level {
90+
case model.LevelWarning:
91+
health = model.HealthLevelWarning
92+
case model.LevelError:
93+
health = model.HealthLevelError
94+
}
8195
reportAt = state
8296
}
8397

0 commit comments

Comments
 (0)