feat(platform): add vehicle coverage view

This commit is contained in:
lingniu
2026-07-03 21:54:09 +08:00
parent 6352289c25
commit 110041a9db
13 changed files with 223 additions and 5 deletions

View File

@@ -27,6 +27,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (h *Handler) routes() {
h.mux.HandleFunc("GET /api/dashboard/summary", h.handleDashboardSummary)
h.mux.HandleFunc("GET /api/vehicles", h.handleVehicles)
h.mux.HandleFunc("GET /api/vehicles/coverage", h.handleVehicleCoverage)
h.mux.HandleFunc("GET /api/vehicles/detail", h.handleVehicleDetail)
h.mux.HandleFunc("GET /api/realtime/locations", h.handleRealtimeLocations)
h.mux.HandleFunc("GET /api/history/locations", h.handleHistoryLocations)
@@ -47,6 +48,11 @@ func (h *Handler) handleVehicles(w http.ResponseWriter, r *http.Request) {
h.write(w, r, data, err)
}
func (h *Handler) handleVehicleCoverage(w http.ResponseWriter, r *http.Request) {
data, err := h.service.VehicleCoverage(r.Context(), r.URL.Query())
h.write(w, r, data, err)
}
func (h *Handler) handleVehicleDetail(w http.ResponseWriter, r *http.Request) {
vin := strings.TrimSpace(r.URL.Query().Get("vin"))
if vin == "" {

View File

@@ -33,6 +33,21 @@ func TestHandlerVehicles(t *testing.T) {
}
}
func TestHandlerVehicleCoverage(t *testing.T) {
handler := NewHandler(NewService(NewMockStore()))
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/vehicles/coverage?limit=10", nil)
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
}
for _, want := range []string{"sourceCount", "onlineSourceCount", "protocols", "LB9A32A24R0LS1426"} {
if !strings.Contains(rec.Body.String(), want) {
t.Fatalf("response missing %q: %s", want, rec.Body.String())
}
}
}
func TestHandlerVehicleDetail(t *testing.T) {
handler := NewHandler(NewService(NewMockStore()))
rec := httptest.NewRecorder()

View File

@@ -3,6 +3,7 @@ package platform
import (
"context"
"net/url"
"sort"
"strconv"
"strings"
)
@@ -15,6 +16,7 @@ type MockStore struct {
func NewMockStore() *MockStore {
vehicles := []VehicleRow{
{VIN: "LB9A32A24R0LS1426", Plate: "粤AG18312", Phone: "13307795425", OEM: "G7s", Protocol: "JT808", Online: true, LastSeen: "2026-07-03 20:12:10", LocationText: "广东省广州市", BindingScore: 96},
{VIN: "LB9A32A24R0LS1426", Plate: "粤AG18312", Phone: "13307795425", OEM: "G7s", Protocol: "GB32960", Online: false, LastSeen: "2026-07-03 20:10:10", LocationText: "广东省广州市", BindingScore: 96},
{VIN: "LNXNEGRR7SR318212", Plate: "川AHTWO1", Phone: "", OEM: "Hyundai", Protocol: "GB32960", Online: true, LastSeen: "2026-07-03 20:12:06", LocationText: "四川省成都市", BindingScore: 100},
{VIN: "LMRKH9AC2R1004087", Plate: "豫A88888", Phone: "", OEM: "宇通", Protocol: "YUTONG_MQTT", Online: true, LastSeen: "2026-07-03 20:11:59", LocationText: "上海市临港", BindingScore: 92},
{VIN: "LB9A32A24P0LS1230", Plate: "粤AFF7936", Phone: "13307795426", OEM: "广安车联", Protocol: "JT808", Online: false, LastSeen: "2026-07-03 19:58:00", LocationText: "广东省佛山市", BindingScore: 88},
@@ -55,6 +57,42 @@ func (m *MockStore) Vehicles(_ context.Context, query url.Values) (Page[VehicleR
return page(items, query), nil
}
func (m *MockStore) VehicleCoverage(_ context.Context, query url.Values) (Page[VehicleCoverageRow], error) {
vehicles := filterVehicles(m.vehicles, query)
byVIN := map[string]*VehicleCoverageRow{}
for _, vehicle := range vehicles {
current := byVIN[vehicle.VIN]
if current == nil {
current = &VehicleCoverageRow{
VIN: vehicle.VIN,
Plate: vehicle.Plate,
Phone: vehicle.Phone,
OEM: vehicle.OEM,
LastSeen: vehicle.LastSeen,
BindingStatus: "bound",
}
byVIN[vehicle.VIN] = current
}
if vehicle.LastSeen > current.LastSeen {
current.LastSeen = vehicle.LastSeen
}
if vehicle.Online {
current.Online = true
current.OnlineSourceCount++
}
if !containsString(current.Protocols, vehicle.Protocol) {
current.Protocols = append(current.Protocols, vehicle.Protocol)
current.SourceCount = len(current.Protocols)
}
}
items := make([]VehicleCoverageRow, 0, len(byVIN))
for _, row := range byVIN {
items = append(items, *row)
}
sortVehicleCoverage(items)
return page(items, query), nil
}
func (m *MockStore) RealtimeLocations(_ context.Context, query url.Values) (Page[RealtimeLocationRow], error) {
items := m.locations
if vin := strings.TrimSpace(query.Get("vin")); vin != "" {
@@ -181,6 +219,24 @@ func keep[T any](rows []T, fn func(T) bool) []T {
return out
}
func containsString(values []string, target string) bool {
for _, value := range values {
if value == target {
return true
}
}
return false
}
func sortVehicleCoverage(rows []VehicleCoverageRow) {
sort.Slice(rows, func(i, j int) bool {
if rows[i].LastSeen == rows[j].LastSeen {
return rows[i].VIN < rows[j].VIN
}
return rows[i].LastSeen > rows[j].LastSeen
})
}
func parsePositive(raw string, fallback int) int {
value, err := strconv.Atoi(raw)
if err != nil || value < 0 {

View File

@@ -41,6 +41,19 @@ type VehicleRow struct {
BindingScore int `json:"bindingScore"`
}
type VehicleCoverageRow 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"`
LastSeen string `json:"lastSeen"`
BindingStatus string `json:"bindingStatus"`
}
type VehicleDetail struct {
VIN string `json:"vin"`
Identity *VehicleRow `json:"identity,omitempty"`

View File

@@ -40,6 +40,40 @@ func buildVehicleListSQL(query url.Values) SQLQuery {
}
}
func buildVehicleCoverageSQL(query url.Values) SQLQuery {
limit := parsePositive(query.Get("limit"), 20)
offset := parsePositive(query.Get("offset"), 0)
args := []any{}
where := []string{"s.vin IS NOT NULL", "s.vin <> ''"}
if keyword := strings.TrimSpace(query.Get("keyword")); keyword != "" {
where = append(where, "(s.vin LIKE ? OR s.plate LIKE ? OR b.vin LIKE ? OR b.plate LIKE ? OR b.phone LIKE ? OR b.oem LIKE ?)")
like := "%" + keyword + "%"
args = append(args, like, like, like, like, like, like)
}
if protocol := strings.TrimSpace(query.Get("protocol")); protocol != "" {
where = append(where, "s.protocol = ?")
args = append(args, protocol)
}
args = append(args, limit, offset)
return SQLQuery{
Text: `SELECT s.vin, ` +
`COALESCE(NULLIF(MAX(NULLIF(s.plate, '')), ''), b.plate, '') AS plate, ` +
`COALESCE(b.phone, '') AS phone, COALESCE(b.oem, '') AS oem, ` +
`COALESCE(GROUP_CONCAT(DISTINCT s.protocol ORDER BY s.protocol SEPARATOR ','), '') AS protocols, ` +
`COUNT(DISTINCT s.protocol) AS source_count, ` +
`SUM(CASE WHEN s.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN 1 ELSE 0 END) AS online_source_count, ` +
`CASE WHEN SUM(CASE WHEN s.updated_at >= DATE_SUB(NOW(), INTERVAL 1 MINUTE) THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END AS online, ` +
`COALESCE(DATE_FORMAT(MAX(s.updated_at), '%Y-%m-%d %H:%i:%s'), '') AS last_seen, ` +
`CASE WHEN b.vin IS NOT NULL AND b.vin <> '' THEN 'bound' ELSE 'unbound' END AS binding_status ` +
`FROM vehicle_realtime_snapshot s ` +
`LEFT JOIN vehicle_identity_binding b ON b.vin = s.vin ` +
`WHERE ` + strings.Join(where, " AND ") + ` ` +
`GROUP BY s.vin, b.plate, b.phone, b.oem, b.vin ` +
`ORDER BY MAX(s.updated_at) DESC LIMIT ? OFFSET ?`,
Args: args,
}
}
func buildDailyMileageSQL(query url.Values) SQLQuery {
limit := parsePositive(query.Get("limit"), 20)
offset := parsePositive(query.Get("offset"), 0)

View File

@@ -89,6 +89,32 @@ func (s *ProductionStore) Vehicles(ctx context.Context, query url.Values) (Page[
return Page[VehicleRow]{Items: items, Total: len(items), Limit: limit, Offset: offset}, nil
}
func (s *ProductionStore) VehicleCoverage(ctx context.Context, query url.Values) (Page[VehicleCoverageRow], error) {
built := buildVehicleCoverageSQL(query)
rows, err := s.db.QueryContext(ctx, built.Text, built.Args...)
if err != nil {
return Page[VehicleCoverageRow]{}, err
}
defer rows.Close()
items := make([]VehicleCoverageRow, 0)
for rows.Next() {
var row VehicleCoverageRow
var protocols string
var online int
if err := rows.Scan(&row.VIN, &row.Plate, &row.Phone, &row.OEM, &protocols, &row.SourceCount, &row.OnlineSourceCount, &online, &row.LastSeen, &row.BindingStatus); err != nil {
return Page[VehicleCoverageRow]{}, err
}
row.Protocols = splitCSV(protocols)
row.Online = online == 1
items = append(items, row)
}
if err := rows.Err(); err != nil {
return Page[VehicleCoverageRow]{}, err
}
limit, offset := buildLimitOffset(query)
return Page[VehicleCoverageRow]{Items: items, Total: len(items), Limit: limit, Offset: offset}, nil
}
func (s *ProductionStore) RealtimeLocations(ctx context.Context, query url.Values) (Page[RealtimeLocationRow], error) {
limit, offset := buildLimitOffset(query)
where := []string{"1 = 1"}

View File

@@ -19,6 +19,19 @@ func TestBuildVehicleListSQL(t *testing.T) {
}
}
func TestBuildVehicleCoverageSQL(t *testing.T) {
query := url.Values{"keyword": {"粤A"}, "protocol": {"GB32960"}, "limit": {"8"}, "offset": {"16"}}
built := buildVehicleCoverageSQL(query)
for _, want := range []string{"GROUP BY s.vin", "GROUP_CONCAT(DISTINCT s.protocol", "source_count", "online_source_count"} {
if !strings.Contains(built.Text, want) {
t.Fatalf("SQL missing %q: %s", want, built.Text)
}
}
if len(built.Args) != 9 || built.Args[0] != "%粤A%" || built.Args[6] != "GB32960" || built.Args[7] != 8 || built.Args[8] != 16 {
t.Fatalf("args = %#v", built.Args)
}
}
func TestBuildDailyMileageSQL(t *testing.T) {
query := url.Values{"vin": {"VIN001"}, "dateFrom": {"2026-07-01"}, "dateTo": {"2026-07-03"}}
built := buildDailyMileageSQL(query)

View File

@@ -10,6 +10,7 @@ import (
type Store interface {
DashboardSummary(context.Context) (DashboardSummary, error)
Vehicles(context.Context, url.Values) (Page[VehicleRow], error)
VehicleCoverage(context.Context, url.Values) (Page[VehicleCoverageRow], error)
RealtimeLocations(context.Context, url.Values) (Page[RealtimeLocationRow], error)
HistoryLocations(context.Context, url.Values) (Page[HistoryLocationRow], error)
HistoryLocationsFromTDengine(context.Context, url.Values) (Page[HistoryLocationRow], error)
@@ -46,6 +47,10 @@ func (s *Service) Vehicles(ctx context.Context, query url.Values) (Page[VehicleR
return s.store.Vehicles(ctx, query)
}
func (s *Service) VehicleCoverage(ctx context.Context, query url.Values) (Page[VehicleCoverageRow], error) {
return s.store.VehicleCoverage(ctx, query)
}
func (s *Service) VehicleDetail(ctx context.Context, vin string, protocol string) (VehicleDetail, error) {
keyword := strings.TrimSpace(vin)
protocol = strings.TrimSpace(protocol)