perf(platform): reuse batch path for vehicle overview
This commit is contained in:
@@ -99,6 +99,20 @@ func (s *Service) VehicleRealtime(ctx context.Context, query url.Values) (Page[V
|
|||||||
func (s *Service) VehicleServiceOverview(ctx context.Context, keyword string, protocol string) (VehicleServiceOverview, error) {
|
func (s *Service) VehicleServiceOverview(ctx context.Context, keyword string, protocol string) (VehicleServiceOverview, error) {
|
||||||
keyword = strings.TrimSpace(keyword)
|
keyword = strings.TrimSpace(keyword)
|
||||||
protocol = strings.TrimSpace(protocol)
|
protocol = strings.TrimSpace(protocol)
|
||||||
|
if batchStore, ok := s.store.(VehicleOverviewBatchStore); ok {
|
||||||
|
page, err := batchStore.VehicleServiceOverviews(ctx, VehicleOverviewBatchQuery{
|
||||||
|
Keywords: []string{keyword},
|
||||||
|
Protocol: protocol,
|
||||||
|
Limit: 1,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return VehicleServiceOverview{}, err
|
||||||
|
}
|
||||||
|
if len(page.Items) > 0 {
|
||||||
|
return page.Items[0], nil
|
||||||
|
}
|
||||||
|
return *buildVehicleServiceOverview("", keyword, &VehicleIdentityResolution{LookupKey: keyword, Protocols: []string{}}, nil, nil, nil, Page[HistoryLocationRow]{}, Page[RawFrameRow]{}, Page[DailyMileageRow]{}, Page[QualityIssueRow]{}), nil
|
||||||
|
}
|
||||||
vehicleQuery := url.Values{"keyword": {keyword}, "limit": {"20"}}
|
vehicleQuery := url.Values{"keyword": {keyword}, "limit": {"20"}}
|
||||||
if protocol != "" {
|
if protocol != "" {
|
||||||
vehicleQuery.Set("protocol", protocol)
|
vehicleQuery.Set("protocol", protocol)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ type countingStore struct {
|
|||||||
*MockStore
|
*MockStore
|
||||||
vehiclesCalls int
|
vehiclesCalls int
|
||||||
vehicleRealtimeCalls int
|
vehicleRealtimeCalls int
|
||||||
|
overviewBatchCalls int
|
||||||
}
|
}
|
||||||
|
|
||||||
func newCountingStore() *countingStore {
|
func newCountingStore() *countingStore {
|
||||||
@@ -26,6 +27,26 @@ func (s *countingStore) VehicleRealtime(ctx context.Context, query url.Values) (
|
|||||||
return s.MockStore.VehicleRealtime(ctx, query)
|
return s.MockStore.VehicleRealtime(ctx, query)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *countingStore) VehicleServiceOverviews(ctx context.Context, query VehicleOverviewBatchQuery) (Page[VehicleServiceOverview], error) {
|
||||||
|
s.overviewBatchCalls++
|
||||||
|
return s.MockStore.VehicleServiceOverviews(ctx, query)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestVehicleServiceOverviewUsesBatchDataPath(t *testing.T) {
|
||||||
|
store := newCountingStore()
|
||||||
|
service := NewService(store)
|
||||||
|
overview, err := service.VehicleServiceOverview(context.Background(), "粤AG18312", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("VehicleServiceOverview returned error: %v", err)
|
||||||
|
}
|
||||||
|
if overview.VIN != "LB9A32A24R0LS1426" {
|
||||||
|
t.Fatalf("expected canonical VIN from overview, got %+v", overview)
|
||||||
|
}
|
||||||
|
if store.overviewBatchCalls != 1 || store.vehiclesCalls != 0 || store.vehicleRealtimeCalls != 0 {
|
||||||
|
t.Fatalf("single overview should use one batch store call, batch=%d vehicles=%d realtime=%d", store.overviewBatchCalls, store.vehiclesCalls, store.vehicleRealtimeCalls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestVehicleServiceOverviewsUsesBatchDataPath(t *testing.T) {
|
func TestVehicleServiceOverviewsUsesBatchDataPath(t *testing.T) {
|
||||||
store := newCountingStore()
|
store := newCountingStore()
|
||||||
service := NewService(store)
|
service := NewService(store)
|
||||||
|
|||||||
Reference in New Issue
Block a user