feat(platform): expose capacity check findings

This commit is contained in:
lingniu
2026-07-04 08:58:40 +08:00
parent e29f8768ca
commit f651e48434
6 changed files with 69 additions and 5 deletions

View File

@@ -285,6 +285,7 @@ type OpsHealth struct {
LinkHealth []LinkHealth `json:"linkHealth"`
KafkaLag *int `json:"kafkaLag"`
ActiveConnections *int `json:"activeConnections"`
CapacityFindings []string `json:"capacityFindings"`
RedisOnlineKeys *int `json:"redisOnlineKeys"`
TDengineWritable bool `json:"tdengineWritable"`
MySQLWritable bool `json:"mysqlWritable"`

View File

@@ -905,7 +905,7 @@ func (s *ProductionStore) OpsHealth(ctx context.Context) (OpsHealth, error) {
locationHealth := s.mysqlTableReadHealth(ctx, "vehicle_realtime_location", "读取实时位置表正常")
tdengineHealth := s.tdengineRawFrameHealth(ctx)
redisHealth, redisOnlineKeys := s.redisOnlineKeyHealth(ctx)
capacityHealth, kafkaLag, activeConnections := s.capacityCheckHealth(ctx)
capacityHealth, kafkaLag, activeConnections, capacityFindings := s.capacityCheckHealth(ctx)
mysqlWritable := mysqlStatus == "ok" && snapshotHealth.Status == "ok" && locationHealth.Status == "ok"
return OpsHealth{
LinkHealth: []LinkHealth{
@@ -918,22 +918,23 @@ func (s *ProductionStore) OpsHealth(ctx context.Context) (OpsHealth, error) {
},
KafkaLag: kafkaLag,
ActiveConnections: activeConnections,
CapacityFindings: capacityFindings,
RedisOnlineKeys: redisOnlineKeys,
TDengineWritable: tdengineHealth.Status == "ok",
MySQLWritable: mysqlWritable,
}, nil
}
func (s *ProductionStore) capacityCheckHealth(ctx context.Context) (LinkHealth, *int, *int) {
func (s *ProductionStore) capacityCheckHealth(ctx context.Context) (LinkHealth, *int, *int, []string) {
health := LinkHealth{Name: "Capacity check", Status: "warning", Detail: "平台暂未接入 capacity-check"}
if s.capacityCheck == nil {
return health, nil, nil
return health, nil, nil, []string{}
}
result, err := s.capacityCheck.CheckCapacity(ctx)
if err != nil {
health.Status = "error"
health.Detail = err.Error()
return health, nil, nil
return health, nil, nil, []string{}
}
kafkaLag := int(result.KafkaLag)
activeConnections := int(result.ActiveConnections)
@@ -947,7 +948,7 @@ func (s *ProductionStore) capacityCheckHealth(ctx context.Context) (LinkHealth,
health.Status = "warning"
}
health.Detail = detail
return health, &kafkaLag, &activeConnections
return health, &kafkaLag, &activeConnections, append([]string{}, result.Findings...)
}
func (s *ProductionStore) redisOnlineKeyHealth(ctx context.Context) (LinkHealth, *int) {

View File

@@ -230,6 +230,9 @@ func TestOpsHealthUsesCapacityCheckProbeForKafkaLag(t *testing.T) {
if health.ActiveConnections == nil || *health.ActiveConnections != 120000 {
t.Fatalf("OpsHealth should expose capacity-check active connections, got %+v", health.ActiveConnections)
}
if len(health.CapacityFindings) != 1 || health.CapacityFindings[0] != "kafka lag 42" {
t.Fatalf("OpsHealth should expose structured capacity findings, got %+v", health.CapacityFindings)
}
var capacityHealth *LinkHealth
for index := range health.LinkHealth {
if health.LinkHealth[index].Name == "Capacity check" {
@@ -242,6 +245,30 @@ func TestOpsHealthUsesCapacityCheckProbeForKafkaLag(t *testing.T) {
}
}
func TestOpsHealthReturnsEmptyCapacityFindingsArray(t *testing.T) {
db, err := sql.Open("ops_health_test", "")
if err != nil {
t.Fatalf("open ops health db: %v", err)
}
defer db.Close()
store := &ProductionStore{
db: db,
capacityCheck: fakeCapacityCheckProbe{result: CapacityCheckResult{
Status: "ok",
ActiveConnections: 254,
KafkaLag: 0,
}},
}
health, err := store.OpsHealth(context.Background())
if err != nil {
t.Fatalf("OpsHealth returned error: %v", err)
}
if health.CapacityFindings == nil || len(health.CapacityFindings) != 0 {
t.Fatalf("OpsHealth should expose an empty capacity findings array, got %#v", health.CapacityFindings)
}
}
func TestVehicleServiceOverviewMatchesPartialKeyword(t *testing.T) {
overview := VehicleServiceOverview{
VIN: "LB9A32A24R0LS1426",