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)

View File

@@ -74,6 +74,7 @@ export function Vehicles({
if (values?.keyword) params.set('keyword', values.keyword);
if (values?.protocol) params.set('protocol', values.protocol);
if (values?.coverage) params.set('coverage', values.coverage);
if (values?.missingProtocol) params.set('missingProtocol', values.missingProtocol);
if (values?.online) params.set('online', values.online);
if (values?.bindingStatus) params.set('bindingStatus', values.bindingStatus);
if (values?.serviceStatus) params.set('serviceStatus', values.serviceStatus);
@@ -151,6 +152,11 @@ export function Vehicles({
<Select.Option value="single"></Select.Option>
<Select.Option value="multi"></Select.Option>
</Form.Select>
<Form.Select field="missingProtocol" label="缺失来源" placeholder="全部" style={{ width: 170 }} data-testid="missing-protocol-filter">
<Select.Option value="GB32960"> GB32960</Select.Option>
<Select.Option value="JT808"> JT808</Select.Option>
<Select.Option value="YUTONG_MQTT"> YUTONG_MQTT</Select.Option>
</Form.Select>
<Form.Select field="serviceStatus" label="服务状态" placeholder="全部" style={{ width: 150 }} data-testid="service-status-filter">
<Select.Option value="healthy"></Select.Option>
<Select.Option value="degraded">线</Select.Option>

View File

@@ -337,6 +337,85 @@ test('filters vehicle list from result summary actions', async () => {
expect(window.location.hash).toBe('#/vehicles?online=online');
});
test('filters vehicle list by missing source evidence', async () => {
window.history.replaceState(null, '', '/#/vehicles');
const fetchMock = vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => {
const path = String(input);
if (path.includes('/api/ops/health')) {
return {
ok: true,
json: async () => ({
data: { linkHealth: [], kafkaLag: 0, redisOnlineKeys: 0, tdengineWritable: true, mysqlWritable: true, runtime: { requestTimeoutMs: 5000 } },
traceId: 'trace-test',
timestamp: 1783094400000
})
} as Response;
}
if (path.includes('/api/vehicles/coverage/summary')) {
return {
ok: true,
json: async () => ({
data: {
totalVehicles: 1,
onlineVehicles: 0,
singleSourceVehicles: 0,
multiSourceVehicles: 1,
noDataVehicles: 0,
unboundVehicles: 0
},
traceId: 'trace-test',
timestamp: 1783094400000
})
} as Response;
}
if (path.includes('/api/vehicles/coverage')) {
return {
ok: true,
json: async () => ({
data: {
items: [{
vin: 'VIN001',
plate: '粤AG18312',
phone: '13307795425',
oem: 'G7s',
protocols: ['GB32960', 'JT808'],
sourceCount: 2,
onlineSourceCount: 0,
online: false,
lastSeen: '2026-07-03 20:12:10',
bindingStatus: 'bound'
}],
total: 1,
limit: 20,
offset: 0
},
traceId: 'trace-test',
timestamp: 1783094400000
})
} as Response;
}
return {
ok: true,
json: async () => ({
data: { items: [], total: 0, limit: 20, offset: 0 },
traceId: 'trace-test',
timestamp: 1783094400000
})
} as Response;
});
render(<App />);
expect(await screen.findByText('缺失来源')).toBeInTheDocument();
fireEvent.click(screen.getByTestId('missing-protocol-filter'));
fireEvent.click(await screen.findByText('缺 YUTONG_MQTT'));
fireEvent.click(screen.getByRole('button', { name: '查询' }));
await waitFor(() => {
expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('missingProtocol=YUTONG_MQTT'), undefined);
});
});
test('copies shareable vehicle service filter link', async () => {
window.history.replaceState(null, '', '/#/vehicles?keyword=%E7%B2%A4A&coverage=multi&serviceStatus=degraded');
const writeText = vi.fn(() => Promise.resolve());