feat(platform): batch vehicle service overview lookup

This commit is contained in:
lingniu
2026-07-04 03:12:21 +08:00
parent f996fa0df9
commit e35565f993
4 changed files with 259 additions and 16 deletions

View File

@@ -24,6 +24,10 @@ type Store interface {
OpsHealth(context.Context) (OpsHealth, error)
}
type VehicleOverviewBatchStore interface {
VehicleServiceOverviews(context.Context, VehicleOverviewBatchQuery) (Page[VehicleServiceOverview], error)
}
type RawFrameQuery struct {
Protocol string `json:"protocol"`
VIN string `json:"vin"`
@@ -133,30 +137,32 @@ func (s *Service) VehicleServiceOverview(ctx context.Context, keyword string, pr
}
func (s *Service) VehicleServiceOverviews(ctx context.Context, query VehicleOverviewBatchQuery) (Page[VehicleServiceOverview], error) {
keywords := normalizedKeywords(query.Keywords)
total := len(keywords)
if query.Offset < 0 {
query.Offset = 0
keywords, total, limit, offset := overviewBatchPage(query)
if offset >= total {
return Page[VehicleServiceOverview]{Items: []VehicleServiceOverview{}, Total: total, Limit: limit, Offset: offset}, nil
}
if query.Limit <= 0 || query.Limit > 200 {
query.Limit = 200
query.Keywords = keywords
query.Limit = limit
query.Offset = offset
if batchStore, ok := s.store.(VehicleOverviewBatchStore); ok {
page, err := batchStore.VehicleServiceOverviews(ctx, query)
if err != nil {
return Page[VehicleServiceOverview]{}, err
}
page.Total = total
page.Limit = limit
page.Offset = offset
return page, nil
}
if query.Offset >= total {
return Page[VehicleServiceOverview]{Items: []VehicleServiceOverview{}, Total: total, Limit: query.Limit, Offset: query.Offset}, nil
}
end := query.Offset + query.Limit
if end > total {
end = total
}
items := make([]VehicleServiceOverview, 0, end-query.Offset)
for _, keyword := range keywords[query.Offset:end] {
items := make([]VehicleServiceOverview, 0, len(keywords))
for _, keyword := range keywords {
overview, err := s.VehicleServiceOverview(ctx, keyword, query.Protocol)
if err != nil {
return Page[VehicleServiceOverview]{}, err
}
items = append(items, overview)
}
return Page[VehicleServiceOverview]{Items: items, Total: total, Limit: query.Limit, Offset: query.Offset}, nil
return Page[VehicleServiceOverview]{Items: items, Total: total, Limit: limit, Offset: offset}, nil
}
func (s *Service) VehicleDetail(ctx context.Context, vin string, protocol string) (VehicleDetail, error) {
@@ -566,6 +572,27 @@ func normalizedKeywords(values []string) []string {
return out
}
func overviewBatchPage(query VehicleOverviewBatchQuery) ([]string, int, int, int) {
keywords := normalizedKeywords(query.Keywords)
total := len(keywords)
limit := query.Limit
offset := query.Offset
if offset < 0 {
offset = 0
}
if limit <= 0 || limit > 200 {
limit = 200
}
if offset >= total {
return []string{}, total, limit, offset
}
end := offset + limit
if end > total {
end = total
}
return keywords[offset:end], total, limit, offset
}
func vehicleSourceStatus(vehicles []VehicleRow, realtime []RealtimeLocationRow, history []HistoryLocationRow, raw []RawFrameRow, mileage []DailyMileageRow) []VehicleSourceStatus {
statusByProtocol := map[string]*VehicleSourceStatus{}
ensure := func(protocol string) *VehicleSourceStatus {