Skip to content

Commit 5773d6d

Browse files
authored
Merge pull request #73 from JaD1ng/develop
refactor(service_manager): 根据会议内容调整健康状态模型并更新相关代码
2 parents 23a8f7c + 2807068 commit 5773d6d

File tree

6 files changed

+25
-67
lines changed

6 files changed

+25
-67
lines changed

docs/service_manager/model/schema.sql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ CREATE TABLE IF NOT EXISTS service_instances (
3434
CREATE TABLE IF NOT EXISTS service_states (
3535
service VARCHAR(255),
3636
version VARCHAR(255),
37-
level VARCHAR(50),
38-
detail TEXT,
3937
report_at TIMESTAMP,
4038
resolved_at TIMESTAMP,
41-
health_status VARCHAR(50),
39+
health_state VARCHAR(50),
4240
correlation_id VARCHAR(255),
4341
PRIMARY KEY (service, version),
4442
FOREIGN KEY (service) REFERENCES services(name) ON DELETE CASCADE

internal/service_manager/database/info_repo.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,13 @@ func (d *Database) CreateServiceInstance(ctx context.Context, instance *model.Se
155155

156156
// GetServiceState 获取服务状态
157157
func (d *Database) GetServiceState(ctx context.Context, serviceName string) (*model.ServiceState, error) {
158-
query := `SELECT service, version, level, detail, report_at, resolved_at, health_status, correlation_id
158+
query := `SELECT service, version, report_at, resolved_at, health_state, correlation_id
159159
FROM service_states WHERE service = $1 ORDER BY report_at DESC LIMIT 1`
160160
row := d.QueryRowContext(ctx, query, serviceName)
161161

162162
var state model.ServiceState
163-
if err := row.Scan(&state.Service, &state.Version, &state.Level, &state.Detail, &state.ReportAt,
164-
&state.ResolvedAt, &state.HealthStatus, &state.CorrelationID); err != nil {
163+
if err := row.Scan(&state.Service, &state.Version, &state.ReportAt,
164+
&state.ResolvedAt, &state.HealthState, &state.CorrelationID); err != nil {
165165
if err == sql.ErrNoRows {
166166
return nil, nil
167167
}

internal/service_manager/model/api.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import "time"
66

77
// ServiceItem API响应用的服务信息(对应/v1/services接口items格式)
88
type ServiceItem struct {
9-
Name string `json:"name"` // 服务名称
10-
DeployState DeployStatus `json:"deployState"` // 发布状态:InDeploying|AllDeployFinish
11-
Health HealthLevel `json:"health"` // 健康状态:Normal/Warning/Error
12-
Deps []string `json:"deps"` // 依赖关系(直接使用Service.Deps)
9+
Name string `json:"name"` // 服务名称
10+
DeployState DeployState `json:"deployState"` // 发布状态
11+
Health HealthState `json:"health"` // 健康状态:Normal/Warning/Error
12+
Deps []string `json:"deps"` // 依赖关系(直接使用Service.Deps)
1313
}
1414

1515
// ServicesResponse 服务列表API响应(对应/v1/services接口)
@@ -25,7 +25,7 @@ type ActiveVersionItem struct {
2525
StartTime time.Time `json:"startTime"` // 开始时间
2626
EstimatedCompletionTime time.Time `json:"estimatedCompletionTime"` // 预估完成时间
2727
Instances int `json:"instances"` // 实例个数
28-
Health HealthLevel `json:"health"` // 健康状态:Normal/Warning/Error
28+
Health HealthState `json:"health"` // 健康状态:Normal/Warning/Error
2929
}
3030

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

internal/service_manager/model/constants.go

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,12 @@
11
package model
22

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

66
const (
7-
StatusNew Status = "new"
8-
StatusAnalyzing Status = "analyzing"
9-
StatusProcessing Status = "processing"
10-
StatusResolved Status = "resolved"
11-
)
12-
13-
// Level 健康状态枚举
14-
type Level string
15-
16-
const (
17-
LevelWarning Level = "Warning"
18-
LevelError Level = "Error"
19-
)
20-
21-
// HealthLevel API响应用的健康状态枚举(包含正常状态)
22-
type HealthLevel string
23-
24-
const (
25-
HealthLevelNormal HealthLevel = "Normal"
26-
HealthLevelWarning HealthLevel = "Warning"
27-
HealthLevelError HealthLevel = "Error"
28-
)
29-
30-
// DeployStatus 部署状态枚举
31-
type DeployStatus string
32-
33-
const (
34-
DeployStatusInDeploying DeployStatus = "InDeploying"
35-
DeployStatusAllDeployFinish DeployStatus = "AllDeployFinish"
7+
HealthStateNormal HealthState = "Normal"
8+
HealthStateWarning HealthState = "Warning"
9+
HealthStateError HealthState = "Error"
3610
)
3711

3812
// DeployState 发布状态枚举

internal/service_manager/model/service_state.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ 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 Status `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+
ReportAt time.Time `json:"reportAt" db:"report_at"` // time - 报告时间
10+
ResolvedAt *time.Time `json:"resolvedAt" db:"resolved_at"` // time - 解决时间
11+
HealthState HealthState `json:"healthState" db:"health_state"` // 健康状态
12+
CorrelationID string `json:"correlationId" db:"correlation_id"` // varchar - 关联ID
1513
}

internal/service_manager/service/info_service.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,13 @@ func (s *Service) GetServicesResponse(ctx context.Context) (*model.ServicesRespo
2727
}
2828

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

4135
// 默认设置为已完成部署状态
42-
deployState := model.DeployStatusAllDeployFinish
36+
deployState := model.StatusCompleted
4337

4438
items[i] = model.ServiceItem{
4539
Name: service.Name,
@@ -82,16 +76,10 @@ func (s *Service) GetServiceActiveVersions(ctx context.Context, serviceName stri
8276
}
8377

8478
// 默认为正常状态,因为正常状态的服务不会存储在service_state表中
85-
health := model.HealthLevelNormal
79+
health := model.HealthStateNormal
8680
reportAt := &model.ServiceState{}
8781
if state != nil {
88-
// 将数据库中的异常状态映射为API响应状态
89-
switch state.Level {
90-
case model.LevelWarning:
91-
health = model.HealthLevelWarning
92-
case model.LevelError:
93-
health = model.HealthLevelError
94-
}
82+
health = state.HealthState
9583
reportAt = state
9684
}
9785

0 commit comments

Comments
 (0)