feat(platform): use vehicle keyword for data queries

This commit is contained in:
lingniu
2026-07-04 00:42:40 +08:00
parent f62fe13647
commit 20e2781f35
6 changed files with 74 additions and 27 deletions

View File

@@ -86,7 +86,8 @@ func (h *Handler) handleRawFramesGet(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
query := RawFrameQuery{
Protocol: q.Get("protocol"),
VIN: q.Get("vin"),
VIN: firstNonEmpty(q.Get("vin"), q.Get("keyword")),
Keyword: q.Get("keyword"),
DateFrom: q.Get("dateFrom"),
DateTo: q.Get("dateTo"),
Fields: splitCSV(q.Get("fields")),

View File

@@ -145,6 +145,27 @@ func TestHandlerVehicleRealtime(t *testing.T) {
}
}
func TestHandlerVehicleRealtimeAcceptsKeyword(t *testing.T) {
handler := NewHandler(NewService(NewMockStore()))
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/realtime/vehicles?keyword=川AHTWO1&limit=10", nil)
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
}
var body struct {
Data struct {
Items []VehicleRealtimeRow `json:"items"`
} `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("response JSON should decode: %v body=%s", err, rec.Body.String())
}
if len(body.Data.Items) != 1 || body.Data.Items[0].VIN != "LNXNEGRR7SR318212" {
t.Fatalf("realtime vehicles should accept vehicle keyword, got %+v body=%s", body.Data.Items, rec.Body.String())
}
}
func TestHandlerHistoryMileageQualityOps(t *testing.T) {
cases := []struct {
path string
@@ -177,12 +198,16 @@ func TestHandlerHistoryMileageQualityOps(t *testing.T) {
func TestHandlerVehicleDataAPIsResolveVehicleKeyword(t *testing.T) {
handler := NewHandler(NewService(NewMockStore()))
cases := []struct {
name string
path string
name string
path string
wantVIN string
}{
{name: "history locations", path: "/api/history/locations?vin=粤AG18312&limit=10"},
{name: "raw frames", path: "/api/history/raw-frames?vin=粤AG18312&limit=1"},
{name: "daily mileage", path: "/api/mileage/daily?vin=粤AG18312&limit=10"},
{name: "history locations vin alias", path: "/api/history/locations?vin=粤AG18312&limit=10", wantVIN: "LB9A32A24R0LS1426"},
{name: "raw frames vin alias", path: "/api/history/raw-frames?vin=粤AG18312&limit=1", wantVIN: "LB9A32A24R0LS1426"},
{name: "daily mileage vin alias", path: "/api/mileage/daily?vin=粤AG18312&limit=10", wantVIN: "LB9A32A24R0LS1426"},
{name: "history locations keyword", path: "/api/history/locations?keyword=川AHTWO1&limit=10", wantVIN: "LNXNEGRR7SR318212"},
{name: "raw frames keyword", path: "/api/history/raw-frames?keyword=川AHTWO1&limit=1", wantVIN: "LNXNEGRR7SR318212"},
{name: "daily mileage keyword", path: "/api/mileage/daily?keyword=川AHTWO1&limit=10", wantVIN: "LNXNEGRR7SR318212"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
@@ -192,8 +217,21 @@ func TestHandlerVehicleDataAPIsResolveVehicleKeyword(t *testing.T) {
if rec.Code != http.StatusOK {
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
}
if !strings.Contains(rec.Body.String(), `"vin":"LB9A32A24R0LS1426"`) {
t.Fatalf("vehicle data API should resolve plate keyword to VIN: %s", rec.Body.String())
var body struct {
Data struct {
Items []struct {
VIN string `json:"vin"`
} `json:"items"`
} `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("response JSON should decode: %v body=%s", err, rec.Body.String())
}
if len(body.Data.Items) == 0 {
t.Fatalf("vehicle data API should return rows for resolved keyword: %s", rec.Body.String())
}
if body.Data.Items[0].VIN != tc.wantVIN {
t.Fatalf("vehicle data API should resolve keyword to VIN %s, got %s body=%s", tc.wantVIN, body.Data.Items[0].VIN, rec.Body.String())
}
})
}

View File

@@ -26,6 +26,7 @@ type Store interface {
type RawFrameQuery struct {
Protocol string `json:"protocol"`
VIN string `json:"vin"`
Keyword string `json:"keyword"`
DateFrom string `json:"dateFrom"`
DateTo string `json:"dateTo"`
Fields []string `json:"fields"`
@@ -55,7 +56,11 @@ func (s *Service) VehicleCoverage(ctx context.Context, query url.Values) (Page[V
}
func (s *Service) VehicleRealtime(ctx context.Context, query url.Values) (Page[VehicleRealtimeRow], error) {
return s.store.VehicleRealtime(ctx, query)
resolvedQuery, err := s.resolveVehicleQuery(ctx, query)
if err != nil {
return Page[VehicleRealtimeRow]{}, err
}
return s.store.VehicleRealtime(ctx, resolvedQuery)
}
func (s *Service) VehicleDetail(ctx context.Context, vin string, protocol string) (VehicleDetail, error) {
@@ -239,6 +244,9 @@ func (s *Service) RawFrames(ctx context.Context, query RawFrameQuery) (Page[RawF
if query.Limit <= 0 || query.Limit > 500 {
query.Limit = 100
}
if strings.TrimSpace(query.VIN) == "" {
query.VIN = query.Keyword
}
resolvedVIN, err := s.resolveVehicleVIN(ctx, query.VIN, query.Protocol)
if err != nil {
return Page[RawFrameRow]{}, err
@@ -279,7 +287,7 @@ func (s *Service) OpsHealth(ctx context.Context) (OpsHealth, error) {
func (s *Service) resolveVehicleQuery(ctx context.Context, query url.Values) (url.Values, error) {
resolved := cloneValues(query)
vin := strings.TrimSpace(resolved.Get("vin"))
vin := firstNonEmpty(resolved.Get("vin"), resolved.Get("keyword"))
if vin == "" {
return resolved, nil
}