feat(platform): filter vehicles by missing source

This commit is contained in:
lingniu
2026-07-04 05:28:29 +08:00
parent ef35137236
commit 999d58da1d
6 changed files with 131 additions and 0 deletions

View File

@@ -110,6 +110,22 @@ func TestHandlerVehicleCoverageFiltersServiceStatus(t *testing.T) {
}
}
func TestHandlerVehicleCoverageFiltersMissingProtocol(t *testing.T) {
handler := NewHandler(NewService(NewMockStore()))
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/vehicles/coverage?limit=10&missingProtocol=YUTONG_MQTT", nil)
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
}
if !strings.Contains(rec.Body.String(), "LB9A32A24R0LS1426") {
t.Fatalf("missing MQTT coverage should include vehicle without MQTT source: %s", rec.Body.String())
}
if strings.Contains(rec.Body.String(), "LMRKH9AC2R1004087") {
t.Fatalf("missing MQTT coverage should exclude vehicle with MQTT source: %s", rec.Body.String())
}
}
func TestHandlerVehicleCoverageSummary(t *testing.T) {
handler := NewHandler(NewService(NewMockStore()))
rec := httptest.NewRecorder()

View File

@@ -679,6 +679,11 @@ func keepCoverageRow(row VehicleCoverageRow, query url.Values) bool {
return false
}
}
if missingProtocol := strings.TrimSpace(query.Get("missingProtocol")); missingProtocol != "" {
if containsString(row.Protocols, missingProtocol) {
return false
}
}
return keepServiceStatus(row.ServiceStatus, query.Get("serviceStatus"))
}

View File

@@ -84,6 +84,10 @@ func buildVehicleCoverageSQL(query url.Values) SQLQuery {
case "multi":
having = append(having, "COUNT(DISTINCT s.protocol) > 1")
}
if missingProtocol := strings.TrimSpace(query.Get("missingProtocol")); missingProtocol != "" {
having = append(having, "COUNT(DISTINCT CASE WHEN s.protocol = ? THEN s.protocol END) = 0")
args = append(args, missingProtocol)
}
switch strings.TrimSpace(query.Get("online")) {
case "online":
having = append(having, "COUNT(DISTINCT CASE WHEN s.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN s.protocol END) > 0")
@@ -167,6 +171,10 @@ func buildVehicleCoverageSummarySQL(query url.Values) SQLQuery {
case "multi":
having = append(having, "COUNT(DISTINCT s.protocol) > 1")
}
if missingProtocol := strings.TrimSpace(query.Get("missingProtocol")); missingProtocol != "" {
having = append(having, "COUNT(DISTINCT CASE WHEN s.protocol = ? THEN s.protocol END) = 0")
args = append(args, missingProtocol)
}
switch strings.TrimSpace(query.Get("online")) {
case "online":
having = append(having, "COUNT(DISTINCT CASE WHEN s.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN s.protocol END) > 0")

View File

@@ -100,6 +100,23 @@ func TestBuildVehicleCoverageSQLIncludesNoDataVehicles(t *testing.T) {
}
}
func TestBuildVehicleCoverageSQLFiltersMissingProtocol(t *testing.T) {
query := url.Values{"missingProtocol": {"YUTONG_MQTT"}, "limit": {"8"}}
built := buildVehicleCoverageSQL(query)
for _, want := range []string{
"HAVING",
"COUNT(DISTINCT CASE WHEN s.protocol = ? THEN s.protocol END) = 0",
"vehicle_coverage_count",
} {
if !strings.Contains(built.Text+built.CountText, want) {
t.Fatalf("missing protocol SQL missing %q: %s / %s", want, built.Text, built.CountText)
}
}
if len(built.Args) < 3 || built.Args[0] != "YUTONG_MQTT" || built.CountArgs[0] != "YUTONG_MQTT" {
t.Fatalf("missing protocol args should be shared by data and count queries, args=%#v count=%#v", built.Args, built.CountArgs)
}
}
func TestBuildVehicleCoverageSummarySQL(t *testing.T) {
query := url.Values{"keyword": {"粤A"}, "coverage": {"multi"}, "online": {"online"}, "bindingStatus": {"bound"}, "serviceStatus": {"healthy"}}
built := buildVehicleCoverageSummarySQL(query)