feat(platform): add vehicle service overview
This commit is contained in:
@@ -115,18 +115,19 @@ func (s *Service) VehicleDetail(ctx context.Context, vin string, protocol string
|
||||
return VehicleDetail{}, err
|
||||
}
|
||||
return VehicleDetail{
|
||||
VIN: "",
|
||||
LookupKey: keyword,
|
||||
LookupResolved: false,
|
||||
Resolution: &resolution,
|
||||
ServiceStatus: buildVehicleServiceStatus(false, nil),
|
||||
Sources: []string{},
|
||||
SourceStatus: []VehicleSourceStatus{},
|
||||
Realtime: []RealtimeLocationRow{},
|
||||
History: Page[HistoryLocationRow]{Items: []HistoryLocationRow{}, Limit: 20, Offset: 0},
|
||||
Raw: Page[RawFrameRow]{Items: []RawFrameRow{}, Limit: 10, Offset: 0},
|
||||
Mileage: Page[DailyMileageRow]{Items: []DailyMileageRow{}, Limit: 20, Offset: 0},
|
||||
Quality: quality,
|
||||
VIN: "",
|
||||
LookupKey: keyword,
|
||||
LookupResolved: false,
|
||||
Resolution: &resolution,
|
||||
ServiceStatus: buildVehicleServiceStatus(false, nil),
|
||||
ServiceOverview: buildVehicleServiceOverview("", keyword, &resolution, nil, nil, nil, Page[HistoryLocationRow]{}, Page[RawFrameRow]{}, Page[DailyMileageRow]{}, quality),
|
||||
Sources: []string{},
|
||||
SourceStatus: []VehicleSourceStatus{},
|
||||
Realtime: []RealtimeLocationRow{},
|
||||
History: Page[HistoryLocationRow]{Items: []HistoryLocationRow{}, Limit: 20, Offset: 0},
|
||||
Raw: Page[RawFrameRow]{Items: []RawFrameRow{}, Limit: 10, Offset: 0},
|
||||
Mileage: Page[DailyMileageRow]{Items: []DailyMileageRow{}, Limit: 20, Offset: 0},
|
||||
Quality: quality,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -179,6 +180,7 @@ func (s *Service) VehicleDetail(ctx context.Context, vin string, protocol string
|
||||
Identity: identity,
|
||||
RealtimeSummary: summary,
|
||||
ServiceStatus: buildVehicleServiceStatus(true, sourceStatus),
|
||||
ServiceOverview: buildVehicleServiceOverview(resolvedVIN, keyword, &resolution, identity, summary, sourceStatus, history, raw, mileage, quality),
|
||||
Sources: sourceNames(sourceStatus),
|
||||
SourceStatus: sourceStatus,
|
||||
Realtime: realtime.Items,
|
||||
@@ -189,6 +191,94 @@ func (s *Service) VehicleDetail(ctx context.Context, vin string, protocol string
|
||||
}, nil
|
||||
}
|
||||
|
||||
func buildVehicleServiceOverview(vin string, lookupKey string, resolution *VehicleIdentityResolution, identity *VehicleRow, summary *VehicleRealtimeRow, statuses []VehicleSourceStatus, history Page[HistoryLocationRow], raw Page[RawFrameRow], mileage Page[DailyMileageRow], quality Page[QualityIssueRow]) *VehicleServiceOverview {
|
||||
overview := &VehicleServiceOverview{
|
||||
VIN: strings.TrimSpace(vin),
|
||||
Plate: "",
|
||||
Phone: "",
|
||||
OEM: "",
|
||||
Protocols: sourceNames(statuses),
|
||||
CoverageStatus: "no_data",
|
||||
SourceCount: len(statuses),
|
||||
OnlineSourceCount: 0,
|
||||
HistoryCount: pageCount(history.Total, len(history.Items)),
|
||||
RawCount: pageCount(raw.Total, len(raw.Items)),
|
||||
MileageCount: pageCount(mileage.Total, len(mileage.Items)),
|
||||
QualityIssueCount: pageCount(quality.Total, len(quality.Items)),
|
||||
}
|
||||
if overview.VIN == "" && resolution != nil && resolution.Resolved {
|
||||
overview.VIN = strings.TrimSpace(resolution.VIN)
|
||||
}
|
||||
if overview.VIN == "" {
|
||||
overview.VIN = strings.TrimSpace(lookupKey)
|
||||
}
|
||||
if resolution != nil {
|
||||
overview.Plate = resolution.Plate
|
||||
overview.Phone = resolution.Phone
|
||||
overview.OEM = resolution.OEM
|
||||
if len(overview.Protocols) == 0 {
|
||||
overview.Protocols = append([]string(nil), resolution.Protocols...)
|
||||
overview.SourceCount = len(overview.Protocols)
|
||||
}
|
||||
overview.LastSeen = latestString(overview.LastSeen, resolution.LastSeen)
|
||||
}
|
||||
if identity != nil {
|
||||
overview.Plate = firstNonEmpty(overview.Plate, identity.Plate)
|
||||
overview.Phone = firstNonEmpty(overview.Phone, identity.Phone)
|
||||
overview.OEM = firstNonEmpty(overview.OEM, identity.OEM)
|
||||
overview.LastSeen = latestString(overview.LastSeen, identity.LastSeen)
|
||||
}
|
||||
if summary != nil {
|
||||
overview.Plate = firstNonEmpty(overview.Plate, summary.Plate)
|
||||
overview.Phone = firstNonEmpty(overview.Phone, summary.Phone)
|
||||
overview.OEM = firstNonEmpty(overview.OEM, summary.OEM)
|
||||
overview.PrimaryProtocol = summary.PrimaryProtocol
|
||||
overview.LastSeen = latestString(overview.LastSeen, summary.LastSeen)
|
||||
overview.RealtimeCount = pageCount(summary.SourceCount, len(summary.Protocols))
|
||||
if len(statuses) == 0 {
|
||||
overview.SourceCount = summary.SourceCount
|
||||
overview.OnlineSourceCount = summary.OnlineSourceCount
|
||||
}
|
||||
if len(overview.Protocols) == 0 {
|
||||
overview.Protocols = append([]string(nil), summary.Protocols...)
|
||||
}
|
||||
}
|
||||
for _, status := range statuses {
|
||||
if status.Online {
|
||||
overview.OnlineSourceCount++
|
||||
}
|
||||
if overview.PrimaryProtocol == "" && status.Online {
|
||||
overview.PrimaryProtocol = status.Protocol
|
||||
}
|
||||
overview.LastSeen = latestString(overview.LastSeen, status.LastSeen)
|
||||
}
|
||||
if overview.PrimaryProtocol == "" && len(overview.Protocols) > 0 {
|
||||
overview.PrimaryProtocol = overview.Protocols[0]
|
||||
}
|
||||
overview.CoverageStatus = coverageStatus(overview.SourceCount, overview.OnlineSourceCount)
|
||||
return overview
|
||||
}
|
||||
|
||||
func pageCount(total int, itemCount int) int {
|
||||
if total > 0 {
|
||||
return total
|
||||
}
|
||||
return itemCount
|
||||
}
|
||||
|
||||
func coverageStatus(sourceCount int, onlineSourceCount int) string {
|
||||
if sourceCount <= 0 {
|
||||
return "no_data"
|
||||
}
|
||||
if onlineSourceCount <= 0 {
|
||||
return "offline"
|
||||
}
|
||||
if onlineSourceCount < sourceCount {
|
||||
return "partial"
|
||||
}
|
||||
return "online"
|
||||
}
|
||||
|
||||
func (s *Service) enrichVehicleSourceStatus(ctx context.Context, vin string, statuses []VehicleSourceStatus) []VehicleSourceStatus {
|
||||
for index := range statuses {
|
||||
protocol := statuses[index].Protocol
|
||||
|
||||
Reference in New Issue
Block a user