feat(platform): summarize missing vehicle sources
This commit is contained in:
@@ -384,6 +384,24 @@ func TestHandlerVehicleServiceSummaryEndpoint(t *testing.T) {
|
||||
if !noDataStatusFound {
|
||||
t.Fatalf("summary service status distribution should include no_data, got %+v", body.Data.ServiceStatuses)
|
||||
}
|
||||
var rawBody struct {
|
||||
Data struct {
|
||||
MissingSources []struct {
|
||||
Protocol string `json:"protocol"`
|
||||
Count int `json:"count"`
|
||||
} `json:"missingSources"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &rawBody); err != nil {
|
||||
t.Fatalf("response JSON should decode raw summary: %v body=%s", err, rec.Body.String())
|
||||
}
|
||||
missing := map[string]int{}
|
||||
for _, source := range rawBody.Data.MissingSources {
|
||||
missing[source.Protocol] = source.Count
|
||||
}
|
||||
if missing["GB32960"] != 2 || missing["JT808"] != 2 || missing["YUTONG_MQTT"] != 3 {
|
||||
t.Fatalf("summary should expose canonical missing source counts, got %+v body=%s", missing, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerVehicleServiceOverviewsEndpoint(t *testing.T) {
|
||||
|
||||
@@ -179,6 +179,7 @@ func (m *MockStore) VehicleServiceSummary(ctx context.Context) (VehicleServiceSu
|
||||
TotalVehicles: coverage.Total,
|
||||
ServiceStatuses: []ServiceStatusStat{},
|
||||
Protocols: []ProtocolStat{},
|
||||
MissingSources: []MissingSourceStat{},
|
||||
}
|
||||
statusCounts := map[string]*ServiceStatusStat{
|
||||
"healthy": {Status: "healthy", Title: "服务正常"},
|
||||
@@ -238,6 +239,15 @@ func (m *MockStore) VehicleServiceSummary(ctx context.Context) (VehicleServiceSu
|
||||
for _, protocol := range protocolCounts {
|
||||
summary.Protocols = append(summary.Protocols, *protocol)
|
||||
}
|
||||
for _, protocol := range canonicalVehicleProtocols {
|
||||
missing := 0
|
||||
for _, row := range coverage.Items {
|
||||
if !containsString(row.Protocols, protocol) {
|
||||
missing++
|
||||
}
|
||||
}
|
||||
summary.MissingSources = append(summary.MissingSources, MissingSourceStat{Protocol: protocol, Count: missing})
|
||||
}
|
||||
sort.Slice(summary.ServiceStatuses, func(i, j int) bool { return summary.ServiceStatuses[i].Status < summary.ServiceStatuses[j].Status })
|
||||
sort.Slice(summary.Protocols, func(i, j int) bool { return summary.Protocols[i].Protocol < summary.Protocols[j].Protocol })
|
||||
return summary, nil
|
||||
|
||||
@@ -147,6 +147,7 @@ type VehicleServiceSummary struct {
|
||||
IdentityRequiredVehicles int `json:"identityRequiredVehicles"`
|
||||
ServiceStatuses []ServiceStatusStat `json:"serviceStatuses"`
|
||||
Protocols []ProtocolStat `json:"protocols"`
|
||||
MissingSources []MissingSourceStat `json:"missingSources"`
|
||||
}
|
||||
|
||||
type VehicleServiceStatus struct {
|
||||
@@ -158,6 +159,11 @@ type VehicleServiceStatus struct {
|
||||
OnlineSourceCount int `json:"onlineSourceCount"`
|
||||
}
|
||||
|
||||
type MissingSourceStat struct {
|
||||
Protocol string `json:"protocol"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
type VehicleSourceStatus struct {
|
||||
Protocol string `json:"protocol"`
|
||||
Online bool `json:"online"`
|
||||
|
||||
@@ -158,9 +158,34 @@ GROUP BY v.vin
|
||||
return VehicleServiceSummary{}, err
|
||||
}
|
||||
summary.Protocols = protocols
|
||||
missingSources, err := s.missingSourceStats(ctx)
|
||||
if err != nil {
|
||||
return VehicleServiceSummary{}, err
|
||||
}
|
||||
summary.MissingSources = missingSources
|
||||
return summary, nil
|
||||
}
|
||||
|
||||
func (s *ProductionStore) missingSourceStats(ctx context.Context) ([]MissingSourceStat, error) {
|
||||
const vehicleSetSQL = `SELECT vin FROM vehicle_identity_binding WHERE vin IS NOT NULL AND vin <> ''
|
||||
UNION
|
||||
SELECT vin FROM vehicle_realtime_snapshot WHERE vin IS NOT NULL AND vin <> ''`
|
||||
out := make([]MissingSourceStat, 0, len(canonicalVehicleProtocols))
|
||||
for _, protocol := range canonicalVehicleProtocols {
|
||||
var count int
|
||||
err := s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM (`+vehicleSetSQL+`) v
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM vehicle_realtime_snapshot s
|
||||
WHERE s.vin = v.vin AND s.protocol = ?
|
||||
)`, protocol).Scan(&count)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, MissingSourceStat{Protocol: protocol, Count: count})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *ProductionStore) frameToday(ctx context.Context, now time.Time) (int, error) {
|
||||
if s.tdengine == nil {
|
||||
return 0, nil
|
||||
|
||||
Reference in New Issue
Block a user