feat(platform): expose realtime service status
This commit is contained in:
@@ -276,6 +276,33 @@ func TestHandlerVehicleRealtime(t *testing.T) {
|
||||
t.Fatalf("response missing %q: %s", want, 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) == 0 || body.Data.Items[0].ServiceStatus == nil {
|
||||
t.Fatalf("realtime vehicle row should include canonical serviceStatus: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerVehicleRealtimeFiltersServiceStatus(t *testing.T) {
|
||||
handler := NewHandler(NewService(NewMockStore()))
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/realtime/vehicles?serviceStatus=degraded&limit=10", 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 realtime should include partially online vehicle: %s", rec.Body.String())
|
||||
}
|
||||
if strings.Contains(rec.Body.String(), "LNXNEGRR7SR318212") {
|
||||
t.Fatalf("degraded realtime should exclude healthy single-source vehicle: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerVehicleRealtimeAcceptsKeyword(t *testing.T) {
|
||||
|
||||
@@ -120,11 +120,15 @@ func (m *MockStore) VehicleRealtime(_ context.Context, query url.Values) (Page[V
|
||||
if current == nil {
|
||||
vehicle := m.vehicleByVIN(location.VIN)
|
||||
current = &VehicleRealtimeRow{
|
||||
VIN: location.VIN,
|
||||
Plate: firstNonEmpty(location.Plate, vehicle.Plate),
|
||||
Phone: vehicle.Phone,
|
||||
OEM: vehicle.OEM,
|
||||
LastSeen: location.LastSeen,
|
||||
VIN: location.VIN,
|
||||
Plate: firstNonEmpty(location.Plate, vehicle.Plate),
|
||||
Phone: vehicle.Phone,
|
||||
OEM: vehicle.OEM,
|
||||
BindingStatus: "unbound",
|
||||
LastSeen: location.LastSeen,
|
||||
}
|
||||
if vehicle.VIN != "" {
|
||||
current.BindingStatus = "bound"
|
||||
}
|
||||
byVIN[location.VIN] = current
|
||||
}
|
||||
@@ -149,6 +153,7 @@ func (m *MockStore) VehicleRealtime(_ context.Context, query url.Values) (Page[V
|
||||
items := make([]VehicleRealtimeRow, 0, len(byVIN))
|
||||
for _, row := range byVIN {
|
||||
sort.Strings(row.Protocols)
|
||||
row.ServiceStatus = buildRealtimeServiceStatus(*row)
|
||||
switch strings.TrimSpace(query.Get("online")) {
|
||||
case "online":
|
||||
if !row.Online {
|
||||
@@ -159,6 +164,9 @@ func (m *MockStore) VehicleRealtime(_ context.Context, query url.Values) (Page[V
|
||||
continue
|
||||
}
|
||||
}
|
||||
if !keepServiceStatus(row.ServiceStatus, query.Get("serviceStatus")) {
|
||||
continue
|
||||
}
|
||||
items = append(items, *row)
|
||||
}
|
||||
sort.Slice(items, func(i, j int) bool {
|
||||
@@ -442,15 +450,19 @@ func keepCoverageRow(row VehicleCoverageRow, query url.Values) bool {
|
||||
return false
|
||||
}
|
||||
}
|
||||
switch strings.TrimSpace(query.Get("serviceStatus")) {
|
||||
return keepServiceStatus(row.ServiceStatus, query.Get("serviceStatus"))
|
||||
}
|
||||
|
||||
func keepServiceStatus(status *VehicleServiceStatus, raw string) bool {
|
||||
switch strings.TrimSpace(raw) {
|
||||
case "identity_required":
|
||||
return row.BindingStatus != "bound"
|
||||
return status != nil && status.Status == "identity_required"
|
||||
case "offline":
|
||||
return row.BindingStatus == "bound" && row.SourceCount > 0 && row.OnlineSourceCount == 0
|
||||
return status != nil && status.Status == "offline"
|
||||
case "degraded":
|
||||
return row.BindingStatus == "bound" && row.SourceCount > 0 && row.OnlineSourceCount > 0 && row.OnlineSourceCount < row.SourceCount
|
||||
return status != nil && status.Status == "degraded"
|
||||
case "healthy":
|
||||
return row.BindingStatus == "bound" && row.SourceCount > 0 && row.OnlineSourceCount == row.SourceCount
|
||||
return status != nil && status.Status == "healthy"
|
||||
default:
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -123,21 +123,23 @@ type RealtimeLocationRow struct {
|
||||
}
|
||||
|
||||
type VehicleRealtimeRow struct {
|
||||
VIN string `json:"vin"`
|
||||
Plate string `json:"plate"`
|
||||
Phone string `json:"phone"`
|
||||
OEM string `json:"oem"`
|
||||
Protocols []string `json:"protocols"`
|
||||
SourceCount int `json:"sourceCount"`
|
||||
OnlineSourceCount int `json:"onlineSourceCount"`
|
||||
Online bool `json:"online"`
|
||||
PrimaryProtocol string `json:"primaryProtocol"`
|
||||
Longitude float64 `json:"longitude"`
|
||||
Latitude float64 `json:"latitude"`
|
||||
SpeedKmh float64 `json:"speedKmh"`
|
||||
SOCPercent float64 `json:"socPercent"`
|
||||
TotalMileageKm float64 `json:"totalMileageKm"`
|
||||
LastSeen string `json:"lastSeen"`
|
||||
VIN string `json:"vin"`
|
||||
Plate string `json:"plate"`
|
||||
Phone string `json:"phone"`
|
||||
OEM string `json:"oem"`
|
||||
Protocols []string `json:"protocols"`
|
||||
SourceCount int `json:"sourceCount"`
|
||||
OnlineSourceCount int `json:"onlineSourceCount"`
|
||||
Online bool `json:"online"`
|
||||
BindingStatus string `json:"bindingStatus"`
|
||||
ServiceStatus *VehicleServiceStatus `json:"serviceStatus,omitempty"`
|
||||
PrimaryProtocol string `json:"primaryProtocol"`
|
||||
Longitude float64 `json:"longitude"`
|
||||
Latitude float64 `json:"latitude"`
|
||||
SpeedKmh float64 `json:"speedKmh"`
|
||||
SOCPercent float64 `json:"socPercent"`
|
||||
TotalMileageKm float64 `json:"totalMileageKm"`
|
||||
LastSeen string `json:"lastSeen"`
|
||||
}
|
||||
|
||||
type HistoryLocationRow struct {
|
||||
|
||||
@@ -174,6 +174,29 @@ func buildVehicleRealtimeSQL(query url.Values) SQLQuery {
|
||||
case "offline":
|
||||
having = append(having, "COUNT(DISTINCT CASE WHEN l.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN l.protocol END) = 0")
|
||||
}
|
||||
switch strings.TrimSpace(query.Get("serviceStatus")) {
|
||||
case "healthy":
|
||||
having = append(having,
|
||||
"MAX(CASE WHEN b.vin IS NOT NULL THEN 1 ELSE 0 END) = 1",
|
||||
"COUNT(DISTINCT l.protocol) > 0",
|
||||
"COUNT(DISTINCT CASE WHEN l.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN l.protocol END) = COUNT(DISTINCT l.protocol)",
|
||||
)
|
||||
case "degraded":
|
||||
having = append(having,
|
||||
"MAX(CASE WHEN b.vin IS NOT NULL THEN 1 ELSE 0 END) = 1",
|
||||
"COUNT(DISTINCT l.protocol) > 0",
|
||||
"COUNT(DISTINCT CASE WHEN l.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN l.protocol END) > 0",
|
||||
"COUNT(DISTINCT CASE WHEN l.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN l.protocol END) < COUNT(DISTINCT l.protocol)",
|
||||
)
|
||||
case "offline":
|
||||
having = append(having,
|
||||
"MAX(CASE WHEN b.vin IS NOT NULL THEN 1 ELSE 0 END) = 1",
|
||||
"COUNT(DISTINCT l.protocol) > 0",
|
||||
"COUNT(DISTINCT CASE WHEN l.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN l.protocol END) = 0",
|
||||
)
|
||||
case "identity_required":
|
||||
having = append(having, "MAX(CASE WHEN b.vin IS NOT NULL THEN 1 ELSE 0 END) = 0")
|
||||
}
|
||||
countArgs := append([]any(nil), args...)
|
||||
args = append(args, limit, offset)
|
||||
havingSQL := ""
|
||||
@@ -194,6 +217,7 @@ func buildVehicleRealtimeSQL(query url.Values) SQLQuery {
|
||||
`COUNT(DISTINCT l.protocol) AS source_count, ` +
|
||||
`COUNT(DISTINCT CASE WHEN l.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN l.protocol END) AS online_source_count, ` +
|
||||
`CASE WHEN COUNT(DISTINCT CASE WHEN l.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN l.protocol END) > 0 THEN 1 ELSE 0 END AS online, ` +
|
||||
`CASE WHEN MAX(CASE WHEN b.vin IS NOT NULL THEN 1 ELSE 0 END) = 1 THEN 'bound' ELSE 'unbound' END AS binding_status, ` +
|
||||
`COALESCE(SUBSTRING_INDEX(GROUP_CONCAT(l.protocol ORDER BY ` + orderExpr + `), ',', 1), '') AS primary_protocol, ` +
|
||||
`COALESCE(SUBSTRING_INDEX(GROUP_CONCAT(CAST(l.longitude AS CHAR) ORDER BY ` + orderExpr + `), ',', 1), '') AS longitude, ` +
|
||||
`COALESCE(SUBSTRING_INDEX(GROUP_CONCAT(CAST(l.latitude AS CHAR) ORDER BY ` + orderExpr + `), ',', 1), '') AS latitude, ` +
|
||||
|
||||
@@ -193,11 +193,12 @@ func (s *ProductionStore) VehicleRealtime(ctx context.Context, query url.Values)
|
||||
var protocols string
|
||||
var online int
|
||||
var longitude, latitude, speed, soc, mileage string
|
||||
if err := rows.Scan(&row.VIN, &row.Plate, &row.Phone, &row.OEM, &protocols, &row.SourceCount, &row.OnlineSourceCount, &online, &row.PrimaryProtocol, &longitude, &latitude, &speed, &soc, &mileage, &row.LastSeen); err != nil {
|
||||
if err := rows.Scan(&row.VIN, &row.Plate, &row.Phone, &row.OEM, &protocols, &row.SourceCount, &row.OnlineSourceCount, &online, &row.BindingStatus, &row.PrimaryProtocol, &longitude, &latitude, &speed, &soc, &mileage, &row.LastSeen); err != nil {
|
||||
return Page[VehicleRealtimeRow]{}, err
|
||||
}
|
||||
row.Protocols = splitCSV(protocols)
|
||||
row.Online = online == 1
|
||||
row.ServiceStatus = buildRealtimeServiceStatus(row)
|
||||
row.Longitude = parseFloatString(longitude)
|
||||
row.Latitude = parseFloatString(latitude)
|
||||
row.SpeedKmh = parseFloatString(speed)
|
||||
|
||||
@@ -104,6 +104,24 @@ func TestBuildVehicleRealtimeSQL(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildVehicleRealtimeSQLFiltersServiceStatus(t *testing.T) {
|
||||
query := url.Values{"serviceStatus": {"degraded"}, "limit": {"8"}}
|
||||
built := buildVehicleRealtimeSQL(query)
|
||||
for _, want := range []string{
|
||||
"HAVING",
|
||||
"COUNT(DISTINCT l.protocol) > 0",
|
||||
"COUNT(DISTINCT CASE WHEN l.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN l.protocol END) > 0",
|
||||
"COUNT(DISTINCT CASE WHEN l.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN l.protocol END) < COUNT(DISTINCT l.protocol)",
|
||||
} {
|
||||
if !strings.Contains(built.Text, want) {
|
||||
t.Fatalf("SQL missing realtime service status predicate %q: %s", want, built.Text)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(built.CountText, "vehicle_realtime_count") || !strings.Contains(built.CountText, "HAVING") {
|
||||
t.Fatalf("count SQL should include realtime service status HAVING: %s", built.CountText)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildDailyMileageSQL(t *testing.T) {
|
||||
query := url.Values{"vin": {"VIN001"}, "protocol": {"JT808"}, "dateFrom": {"2026-07-01"}, "dateTo": {"2026-07-03"}}
|
||||
built := buildDailyMileageSQL(query)
|
||||
|
||||
@@ -529,6 +529,21 @@ func buildVehicleCoverageServiceStatus(row VehicleCoverageRow) *VehicleServiceSt
|
||||
}
|
||||
}
|
||||
|
||||
func buildRealtimeServiceStatus(row VehicleRealtimeRow) *VehicleServiceStatus {
|
||||
return buildVehicleCoverageServiceStatus(VehicleCoverageRow{
|
||||
VIN: row.VIN,
|
||||
Plate: row.Plate,
|
||||
Phone: row.Phone,
|
||||
OEM: row.OEM,
|
||||
Protocols: row.Protocols,
|
||||
SourceCount: row.SourceCount,
|
||||
OnlineSourceCount: row.OnlineSourceCount,
|
||||
Online: row.Online,
|
||||
LastSeen: row.LastSeen,
|
||||
BindingStatus: row.BindingStatus,
|
||||
})
|
||||
}
|
||||
|
||||
func sourceCoverageDetail(sourceCount int, onlineSourceCount int, suffix string) string {
|
||||
return strconv.Itoa(onlineSourceCount) + "/" + strconv.Itoa(sourceCount) + " 个来源在线," + suffix
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user