feat(platform): return service status in vehicle overview

This commit is contained in:
lingniu
2026-07-04 03:00:50 +08:00
parent 8dfbdc83e5
commit 1a86e5d38c
7 changed files with 65 additions and 19 deletions

View File

@@ -224,6 +224,9 @@ func TestHandlerVehicleServiceOverviewEndpoint(t *testing.T) {
if body.Data.SourceCount != 2 || body.Data.OnlineSourceCount != 1 || body.Data.CoverageStatus != "partial" {
t.Fatalf("overview endpoint should summarize vehicle service coverage, got %+v", body.Data)
}
if body.Data.ServiceStatus == nil || body.Data.ServiceStatus.Status != "degraded" || body.Data.ServiceStatus.Title != "部分来源离线" {
t.Fatalf("overview endpoint should expose canonical service status, got %+v", body.Data.ServiceStatus)
}
if strings.Contains(rec.Body.String(), `"raw"`) || strings.Contains(rec.Body.String(), `"history"`) {
t.Fatalf("overview endpoint should not return heavy detail pages: %s", rec.Body.String())
}

View File

@@ -95,21 +95,22 @@ type VehicleDetail struct {
}
type VehicleServiceOverview struct {
VIN string `json:"vin"`
Plate string `json:"plate"`
Phone string `json:"phone"`
OEM string `json:"oem"`
Protocols []string `json:"protocols"`
PrimaryProtocol string `json:"primaryProtocol"`
CoverageStatus string `json:"coverageStatus"`
SourceCount int `json:"sourceCount"`
OnlineSourceCount int `json:"onlineSourceCount"`
LastSeen string `json:"lastSeen"`
RealtimeCount int `json:"realtimeCount"`
HistoryCount int `json:"historyCount"`
RawCount int `json:"rawCount"`
MileageCount int `json:"mileageCount"`
QualityIssueCount int `json:"qualityIssueCount"`
VIN string `json:"vin"`
Plate string `json:"plate"`
Phone string `json:"phone"`
OEM string `json:"oem"`
Protocols []string `json:"protocols"`
PrimaryProtocol string `json:"primaryProtocol"`
CoverageStatus string `json:"coverageStatus"`
SourceCount int `json:"sourceCount"`
OnlineSourceCount int `json:"onlineSourceCount"`
LastSeen string `json:"lastSeen"`
RealtimeCount int `json:"realtimeCount"`
HistoryCount int `json:"historyCount"`
RawCount int `json:"rawCount"`
MileageCount int `json:"mileageCount"`
QualityIssueCount int `json:"qualityIssueCount"`
ServiceStatus *VehicleServiceStatus `json:"serviceStatus,omitempty"`
}
type VehicleServiceStatus struct {

View File

@@ -296,9 +296,33 @@ func buildVehicleServiceOverview(vin string, lookupKey string, resolution *Vehic
overview.PrimaryProtocol = overview.Protocols[0]
}
overview.CoverageStatus = coverageStatus(overview.SourceCount, overview.OnlineSourceCount)
resolved := identity != nil && strings.TrimSpace(identity.VIN) != ""
if !resolved && resolution != nil {
resolved = resolution.Resolved
}
overview.ServiceStatus = buildVehicleServiceStatus(resolved, statusesForOverview(overview))
return overview
}
func statusesForOverview(overview *VehicleServiceOverview) []VehicleSourceStatus {
statuses := make([]VehicleSourceStatus, 0, overview.SourceCount)
for index, protocol := range overview.Protocols {
statuses = append(statuses, VehicleSourceStatus{
Protocol: protocol,
Online: index < overview.OnlineSourceCount,
LastSeen: overview.LastSeen,
})
}
for len(statuses) < overview.SourceCount {
statuses = append(statuses, VehicleSourceStatus{
Protocol: "UNKNOWN",
Online: len(statuses) < overview.OnlineSourceCount,
LastSeen: overview.LastSeen,
})
}
return statuses
}
func pageCount(total int, itemCount int) int {
if total > 0 {
return total