feat(platform): filter vehicles by service status

This commit is contained in:
lingniu
2026-07-04 01:54:30 +08:00
parent b7de38605d
commit 14408d135d
7 changed files with 120 additions and 2 deletions

View File

@@ -49,6 +49,22 @@ func TestHandlerVehicleCoverage(t *testing.T) {
}
}
func TestHandlerVehicleCoverageFiltersServiceStatus(t *testing.T) {
handler := NewHandler(NewService(NewMockStore()))
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/vehicles/coverage?limit=10&serviceStatus=degraded", 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("degraded coverage should include partially online multi-source vehicle: %s", rec.Body.String())
}
if strings.Contains(rec.Body.String(), "LMRKH9AC2R1004087") {
t.Fatalf("degraded coverage should exclude healthy single-source vehicle: %s", rec.Body.String())
}
}
func TestHandlerVehicleDetail(t *testing.T) {
handler := NewHandler(NewService(NewMockStore()))
rec := httptest.NewRecorder()

View File

@@ -427,9 +427,23 @@ func keepCoverageRow(row VehicleCoverageRow, query url.Values) bool {
}
switch strings.TrimSpace(query.Get("bindingStatus")) {
case "bound":
return row.BindingStatus == "bound"
if row.BindingStatus != "bound" {
return false
}
case "unbound":
return row.BindingStatus == "unbound"
if row.BindingStatus != "unbound" {
return false
}
}
switch strings.TrimSpace(query.Get("serviceStatus")) {
case "identity_required":
return row.BindingStatus != "bound"
case "offline":
return row.BindingStatus == "bound" && row.SourceCount > 0 && row.OnlineSourceCount == 0
case "degraded":
return row.BindingStatus == "bound" && row.SourceCount > 0 && row.OnlineSourceCount > 0 && row.OnlineSourceCount < row.SourceCount
case "healthy":
return row.BindingStatus == "bound" && row.SourceCount > 0 && row.OnlineSourceCount == row.SourceCount
default:
return true
}

View File

@@ -79,6 +79,23 @@ func buildVehicleCoverageSQL(query url.Values) SQLQuery {
case "unbound":
having = append(having, "MAX(CASE WHEN b.vin IS NOT NULL AND b.vin <> '' THEN 1 ELSE 0 END) = 0")
}
switch strings.TrimSpace(query.Get("serviceStatus")) {
case "identity_required":
having = append(having, "MAX(CASE WHEN b.vin IS NOT NULL AND b.vin <> '' THEN 1 ELSE 0 END) = 0")
case "offline":
having = append(having, "MAX(CASE WHEN b.vin IS NOT NULL AND b.vin <> '' THEN 1 ELSE 0 END) = 1")
having = append(having, "COUNT(DISTINCT s.protocol) > 0")
having = append(having, "COUNT(DISTINCT CASE WHEN s.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN s.protocol END) = 0")
case "degraded":
having = append(having, "MAX(CASE WHEN b.vin IS NOT NULL AND b.vin <> '' THEN 1 ELSE 0 END) = 1")
having = append(having, "COUNT(DISTINCT s.protocol) > 0")
having = append(having, "COUNT(DISTINCT CASE WHEN s.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN s.protocol END) > 0")
having = append(having, "COUNT(DISTINCT CASE WHEN s.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN s.protocol END) < COUNT(DISTINCT s.protocol)")
case "healthy":
having = append(having, "MAX(CASE WHEN b.vin IS NOT NULL AND b.vin <> '' THEN 1 ELSE 0 END) = 1")
having = append(having, "COUNT(DISTINCT s.protocol) > 0")
having = append(having, "COUNT(DISTINCT CASE WHEN s.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN s.protocol END) = COUNT(DISTINCT s.protocol)")
}
countArgs := append([]any(nil), args...)
args = append(args, limit, offset)
havingSQL := ""

View File

@@ -48,6 +48,24 @@ func TestBuildVehicleCoverageSQL(t *testing.T) {
}
}
func TestBuildVehicleCoverageSQLFiltersServiceStatus(t *testing.T) {
query := url.Values{"serviceStatus": {"degraded"}, "limit": {"8"}}
built := buildVehicleCoverageSQL(query)
for _, want := range []string{
"HAVING",
"COUNT(DISTINCT s.protocol) > 0",
"COUNT(DISTINCT CASE WHEN s.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN s.protocol END) > 0",
"COUNT(DISTINCT CASE WHEN s.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN s.protocol END) < COUNT(DISTINCT s.protocol)",
} {
if !strings.Contains(built.Text, want) {
t.Fatalf("SQL missing degraded service status predicate %q: %s", want, built.Text)
}
}
if !strings.Contains(built.CountText, "vehicle_coverage_count") || !strings.Contains(built.CountText, "HAVING") {
t.Fatalf("count SQL should include service status HAVING: %s", built.CountText)
}
}
func TestBuildRealtimeLocationSQL(t *testing.T) {
query := url.Values{"vin": {"粤A"}, "protocol": {"GB32960"}, "limit": {"10"}, "offset": {"20"}}
built := buildRealtimeLocationSQL(query)

View File

@@ -36,6 +36,7 @@ export function Vehicles({ onOpenVehicle }: { onOpenVehicle: (vin: string) => vo
if (values?.coverage) params.set('coverage', values.coverage);
if (values?.online) params.set('online', values.online);
if (values?.bindingStatus) params.set('bindingStatus', values.bindingStatus);
if (values?.serviceStatus) params.set('serviceStatus', values.serviceStatus);
api.vehicleCoverage(params)
.then((nextPage) => {
setRows(nextPage.items);
@@ -98,6 +99,12 @@ export function Vehicles({ onOpenVehicle }: { onOpenVehicle: (vin: string) => vo
<Select.Option value="single"></Select.Option>
<Select.Option value="multi"></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>
<Select.Option value="offline">线</Select.Option>
<Select.Option value="identity_required"></Select.Option>
</Form.Select>
<Form.Select field="online" label="在线" placeholder="全部" style={{ width: 130 }}>
<Select.Option value="online">线</Select.Option>
<Select.Option value="offline">线</Select.Option>

View File

@@ -294,6 +294,44 @@ test('shows vehicle service status in vehicle list', async () => {
expect(screen.getByText('部分来源离线')).toBeInTheDocument();
});
test('filters vehicle list by service status', async () => {
window.history.replaceState(null, '', '/#/vehicles');
const fetchMock = vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => {
const path = String(input);
if (path.includes('/api/vehicles/coverage')) {
return {
ok: true,
json: async () => ({
data: { items: [], total: 0, limit: 20, offset: 0 },
traceId: 'trace-test',
timestamp: 1783094400000
})
} as Response;
}
return {
ok: true,
json: async () => ({
data: path.includes('/api/ops/health')
? { linkHealth: [], kafkaLag: 0, redisOnlineKeys: 0, tdengineWritable: true, mysqlWritable: true, runtime: { requestTimeoutMs: 5000 } }
: { items: [], total: 0, limit: 10, offset: 0 },
traceId: 'trace-test',
timestamp: 1783094400000
})
} as Response;
});
render(<App />);
await screen.findByRole('heading', { name: '车辆服务' });
fireEvent.click(screen.getByTestId('service-status-filter'));
fireEvent.click(await screen.findByText('部分来源离线'));
fireEvent.click(screen.getByRole('button', { name: '查询' }));
await waitFor(() => {
expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('serviceStatus=degraded'), undefined);
});
});
test('switches vehicle detail to a source from the coverage cards', async () => {
window.history.replaceState(null, '', '/#/detail?keyword=VIN001');
const fetchMock = vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => {

View File

@@ -57,6 +57,14 @@ GET /api/realtime/vehicles?keyword=粤AG18312&protocol=JT808&online=online&limit
Returns VIN-level realtime rows. Protocol is a source filter, not a product boundary.
### Vehicle Coverage
```http
GET /api/vehicles/coverage?keyword=AG18312&serviceStatus=degraded&limit=20&offset=0
```
Returns VIN-level source coverage rows for the vehicle service list. `serviceStatus` accepts `healthy`, `degraded`, `offline`, and `identity_required`.
### History Locations
```http