feat(platform): expose missing source diagnosis
This commit is contained in:
@@ -253,8 +253,8 @@ func TestHandlerVehicleServiceIncludesVehicleLevelStatus(t *testing.T) {
|
||||
if body.Data.ServiceStatus == nil {
|
||||
t.Fatalf("vehicle service should include serviceStatus: %s", rec.Body.String())
|
||||
}
|
||||
if body.Data.ServiceStatus.Status != "degraded" || body.Data.ServiceStatus.Title != "部分来源离线" {
|
||||
t.Fatalf("vehicle service should summarize partial source health, got %+v body=%s", body.Data.ServiceStatus, rec.Body.String())
|
||||
if body.Data.ServiceStatus.Status != "degraded" || body.Data.ServiceStatus.Title != "来源不完整" {
|
||||
t.Fatalf("vehicle service should prioritize missing source evidence, got %+v body=%s", body.Data.ServiceStatus, rec.Body.String())
|
||||
}
|
||||
if body.Data.ServiceStatus.OnlineSourceCount != 1 || body.Data.ServiceStatus.SourceCount != 3 {
|
||||
t.Fatalf("vehicle service should expose source counts, got %+v body=%s", body.Data.ServiceStatus, rec.Body.String())
|
||||
@@ -320,8 +320,8 @@ func TestHandlerVehicleServiceIncludesSourceConsistency(t *testing.T) {
|
||||
if consistency.Status == "" || consistency.Severity == "" || consistency.Title == "" || consistency.Detail == "" {
|
||||
t.Fatalf("sourceConsistency should expose actionable diagnosis, got %+v", consistency)
|
||||
}
|
||||
if consistency.Status != "degraded" || consistency.Title != "部分来源离线" {
|
||||
t.Fatalf("sourceConsistency should diagnose partial source health, got %+v", consistency)
|
||||
if consistency.Status != "degraded" || consistency.Title != "来源不完整" {
|
||||
t.Fatalf("sourceConsistency should diagnose missing source evidence before offline evidence, got %+v", consistency)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -354,6 +354,29 @@ func TestHandlerVehicleServiceExposesMissingSourceSlots(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerVehicleServiceConsistencyExposesMissingProtocols(t *testing.T) {
|
||||
handler := NewHandler(NewService(NewMockStore()))
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/vehicle-service?keyword=粤AG18312", nil)
|
||||
handler.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var body struct {
|
||||
Data struct {
|
||||
SourceConsistency struct {
|
||||
MissingProtocols []string `json:"missingProtocols"`
|
||||
} `json:"sourceConsistency"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("response JSON should decode: %v body=%s", err, rec.Body.String())
|
||||
}
|
||||
if !containsString(body.Data.SourceConsistency.MissingProtocols, "YUTONG_MQTT") {
|
||||
t.Fatalf("sourceConsistency should expose missing source evidence, got %+v body=%s", body.Data.SourceConsistency.MissingProtocols, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerVehicleServiceOverviewEndpoint(t *testing.T) {
|
||||
handler := NewHandler(NewService(NewMockStore()))
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
@@ -106,16 +106,17 @@ type VehicleDetail struct {
|
||||
}
|
||||
|
||||
type VehicleSourceConsistency struct {
|
||||
SourceCount int `json:"sourceCount"`
|
||||
OnlineSourceCount int `json:"onlineSourceCount"`
|
||||
LocatedSourceCount int `json:"locatedSourceCount"`
|
||||
MileageDeltaKm float64 `json:"mileageDeltaKm"`
|
||||
SourceTimeDeltaSeconds float64 `json:"sourceTimeDeltaSeconds"`
|
||||
Scope string `json:"scope"`
|
||||
Status string `json:"status"`
|
||||
Severity string `json:"severity"`
|
||||
Title string `json:"title"`
|
||||
Detail string `json:"detail"`
|
||||
SourceCount int `json:"sourceCount"`
|
||||
OnlineSourceCount int `json:"onlineSourceCount"`
|
||||
LocatedSourceCount int `json:"locatedSourceCount"`
|
||||
MissingProtocols []string `json:"missingProtocols"`
|
||||
MileageDeltaKm float64 `json:"mileageDeltaKm"`
|
||||
SourceTimeDeltaSeconds float64 `json:"sourceTimeDeltaSeconds"`
|
||||
Scope string `json:"scope"`
|
||||
Status string `json:"status"`
|
||||
Severity string `json:"severity"`
|
||||
Title string `json:"title"`
|
||||
Detail string `json:"detail"`
|
||||
}
|
||||
|
||||
type VehicleServiceOverview struct {
|
||||
|
||||
@@ -57,6 +57,9 @@ func TestVehicleServiceOverviewDerivedFieldsIncludeSourceConsistency(t *testing.
|
||||
if overview.SourceConsistency.SourceCount != 2 || overview.SourceConsistency.OnlineSourceCount != 1 {
|
||||
t.Fatalf("source consistency should mirror overview source coverage, got %+v", overview.SourceConsistency)
|
||||
}
|
||||
if !containsString(overview.SourceConsistency.MissingProtocols, "YUTONG_MQTT") {
|
||||
t.Fatalf("source consistency should expose missing canonical source evidence, got %+v", overview.SourceConsistency)
|
||||
}
|
||||
if overview.ServiceStatus == nil || overview.ServiceStatus.Status != "degraded" {
|
||||
t.Fatalf("overview should keep canonical service status, got %+v", overview.ServiceStatus)
|
||||
}
|
||||
|
||||
@@ -333,6 +333,9 @@ func buildVehicleSourceConsistency(statuses []VehicleSourceStatus, realtime []Re
|
||||
if status.Online {
|
||||
consistency.OnlineSourceCount++
|
||||
}
|
||||
if vehicleSourceEvidenceMissing(status) {
|
||||
consistency.MissingProtocols = append(consistency.MissingProtocols, status.Protocol)
|
||||
}
|
||||
}
|
||||
var minMileage, maxMileage float64
|
||||
hasMileage := false
|
||||
@@ -393,6 +396,16 @@ func buildVehicleSourceConsistency(statuses []VehicleSourceStatus, realtime []Re
|
||||
return consistency
|
||||
}
|
||||
|
||||
func vehicleSourceEvidenceMissing(status VehicleSourceStatus) bool {
|
||||
return strings.TrimSpace(status.Protocol) != "" &&
|
||||
!status.Online &&
|
||||
strings.TrimSpace(status.LastSeen) == "" &&
|
||||
!status.HasRealtime &&
|
||||
!status.HasHistory &&
|
||||
!status.HasRaw &&
|
||||
!status.HasMileage
|
||||
}
|
||||
|
||||
func applyVehicleSourceConsistencyDiagnosis(consistency *VehicleSourceConsistency) {
|
||||
if consistency == nil {
|
||||
return
|
||||
@@ -413,6 +426,11 @@ func applyVehicleSourceConsistencyDiagnosis(consistency *VehicleSourceConsistenc
|
||||
consistency.Severity = "error"
|
||||
consistency.Title = "全部来源离线"
|
||||
consistency.Detail = sourceCoverageDetail(consistency.SourceCount, consistency.OnlineSourceCount, "无法判断实时一致性。")
|
||||
case len(consistency.MissingProtocols) > 0:
|
||||
consistency.Status = "degraded"
|
||||
consistency.Severity = "warning"
|
||||
consistency.Title = "来源不完整"
|
||||
consistency.Detail = sourceCoverageDetail(consistency.SourceCount, consistency.OnlineSourceCount, "车辆服务可用但缺少 "+strings.Join(consistency.MissingProtocols, "、")+" 来源。")
|
||||
case consistency.OnlineSourceCount < consistency.SourceCount:
|
||||
consistency.Status = "degraded"
|
||||
consistency.Severity = "warning"
|
||||
@@ -520,6 +538,8 @@ func buildVehicleServiceOverview(vin string, lookupKey string, resolution *Vehic
|
||||
resolved = resolution.Resolved
|
||||
}
|
||||
applyVehicleServiceOverviewDerivedFields(overview, resolved)
|
||||
overview.SourceConsistency.MissingProtocols = missingProtocolsFromStatuses(statuses)
|
||||
applyVehicleSourceConsistencyDiagnosis(overview.SourceConsistency)
|
||||
return overview
|
||||
}
|
||||
|
||||
@@ -532,6 +552,7 @@ func applyVehicleServiceOverviewDerivedFields(overview *VehicleServiceOverview,
|
||||
overview.SourceConsistency = &VehicleSourceConsistency{
|
||||
SourceCount: overview.SourceCount,
|
||||
OnlineSourceCount: overview.OnlineSourceCount,
|
||||
MissingProtocols: missingCanonicalProtocols(overview.Protocols),
|
||||
Scope: "all_sources",
|
||||
}
|
||||
applyVehicleSourceConsistencyDiagnosis(overview.SourceConsistency)
|
||||
@@ -899,7 +920,8 @@ func buildVehicleServiceStatus(resolved bool, statuses []VehicleSourceStatus) *V
|
||||
onlineSourceCount++
|
||||
}
|
||||
}
|
||||
if sourceCount == 0 {
|
||||
missingProtocols := missingProtocolsFromStatuses(statuses)
|
||||
if sourceCount == 0 || sourceCount == len(missingProtocols) {
|
||||
return &VehicleServiceStatus{
|
||||
Status: "no_data",
|
||||
Severity: "warning",
|
||||
@@ -919,6 +941,16 @@ func buildVehicleServiceStatus(resolved bool, statuses []VehicleSourceStatus) *V
|
||||
OnlineSourceCount: onlineSourceCount,
|
||||
}
|
||||
}
|
||||
if len(missingProtocols) > 0 {
|
||||
return &VehicleServiceStatus{
|
||||
Status: "degraded",
|
||||
Severity: "warning",
|
||||
Title: "来源不完整",
|
||||
Detail: sourceCoverageDetail(sourceCount, onlineSourceCount, "车辆服务可用但缺少 "+strings.Join(missingProtocols, "、")+" 来源。"),
|
||||
SourceCount: sourceCount,
|
||||
OnlineSourceCount: onlineSourceCount,
|
||||
}
|
||||
}
|
||||
if onlineSourceCount < sourceCount {
|
||||
return &VehicleServiceStatus{
|
||||
Status: "degraded",
|
||||
@@ -939,6 +971,16 @@ func buildVehicleServiceStatus(resolved bool, statuses []VehicleSourceStatus) *V
|
||||
}
|
||||
}
|
||||
|
||||
func missingProtocolsFromStatuses(statuses []VehicleSourceStatus) []string {
|
||||
missing := make([]string, 0, len(statuses))
|
||||
for _, status := range statuses {
|
||||
if vehicleSourceEvidenceMissing(status) {
|
||||
missing = append(missing, status.Protocol)
|
||||
}
|
||||
}
|
||||
return missing
|
||||
}
|
||||
|
||||
func buildVehicleCoverageServiceStatus(row VehicleCoverageRow) *VehicleServiceStatus {
|
||||
if row.BindingStatus != "bound" {
|
||||
return &VehicleServiceStatus{
|
||||
|
||||
Reference in New Issue
Block a user