perf(go): trim frame id from location history

This commit is contained in:
lingniu
2026-07-03 08:56:04 +08:00
parent c916120ea2
commit c2e4c2b955
5 changed files with 16 additions and 13 deletions

View File

@@ -66,7 +66,6 @@ type LocationQuery struct {
type LocationRow struct { type LocationRow struct {
TS string `json:"ts"` TS string `json:"ts"`
EventID string `json:"event_id"` EventID string `json:"event_id"`
FrameID string `json:"frame_id"`
ReceivedAt string `json:"received_at"` ReceivedAt string `json:"received_at"`
Longitude float64 `json:"longitude"` Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"` Latitude float64 `json:"latitude"`
@@ -193,7 +192,6 @@ func (r *LocationRepository) Query(ctx context.Context, query LocationQuery) ([]
if err := rows.Scan( if err := rows.Scan(
&ts, &ts,
&row.EventID, &row.EventID,
&row.FrameID,
&receivedAt, &receivedAt,
&row.Longitude, &row.Longitude,
&row.Latitude, &row.Latitude,
@@ -430,7 +428,7 @@ func quotedList(values []string) string {
func buildLocationSQL(table string, query LocationQuery) (string, []any) { func buildLocationSQL(table string, query LocationQuery) (string, []any) {
where := locationWhere(query) where := locationWhere(query)
sqlText := `SELECT ts, event_id, frame_id, received_at, longitude, latitude, altitude_m, speed_kmh, direction_deg, alarm_flag, status_flag, total_mileage_km, protocol, vin FROM ` + table sqlText := `SELECT ts, event_id, received_at, longitude, latitude, altitude_m, speed_kmh, direction_deg, alarm_flag, status_flag, total_mileage_km, protocol, vin FROM ` + table
if len(where) > 0 { if len(where) > 0 {
sqlText += " WHERE " + strings.Join(where, " AND ") sqlText += " WHERE " + strings.Join(where, " AND ")
} }

View File

@@ -306,10 +306,10 @@ func TestLocationHandlerReturnsLocationsByVIN(t *testing.T) {
WillReturnRows(sqlmock.NewRows([]string{"total"}).AddRow(17)) WillReturnRows(sqlmock.NewRows([]string{"total"}).AddRow(17))
mock.ExpectQuery("vin = 'LKLG7C4E3NA774736'"). mock.ExpectQuery("vin = 'LKLG7C4E3NA774736'").
WillReturnRows(sqlmock.NewRows([]string{ WillReturnRows(sqlmock.NewRows([]string{
"ts", "event_id", "frame_id", "received_at", "longitude", "latitude", "altitude_m", "speed_kmh", "ts", "event_id", "received_at", "longitude", "latitude", "altitude_m", "speed_kmh",
"direction_deg", "alarm_flag", "status_flag", "total_mileage_km", "protocol", "vin", "direction_deg", "alarm_flag", "status_flag", "total_mileage_km", "protocol", "vin",
}).AddRow( }).AddRow(
"2026-07-02 00:18:22", "event-3", "go_frame", "2026-07-02 00:22:43", "2026-07-02 00:18:22", "event-3", "2026-07-02 00:22:43",
121.07764, 30.585928, 11.0, 8.0, 171, 0, 4718595, 8792.8, 121.07764, 30.585928, 11.0, 8.0, 171, 0, 4718595, 8792.8,
"JT808", "LKLG7C4E3NA774736", "JT808", "LKLG7C4E3NA774736",
)) ))
@@ -329,7 +329,7 @@ func TestLocationHandlerReturnsLocationsByVIN(t *testing.T) {
t.Fatalf("response missing %s: %s", want, body) t.Fatalf("response missing %s: %s", want, body)
} }
} }
for _, legacy := range []string{"vehicle_key", "phone", "device_id"} { for _, legacy := range []string{"frame_id", "vehicle_key", "phone", "device_id"} {
if strings.Contains(body, legacy) { if strings.Contains(body, legacy) {
t.Fatalf("location response should not expose %s: %s", legacy, body) t.Fatalf("location response should not expose %s: %s", legacy, body)
} }
@@ -347,10 +347,10 @@ func TestLocationHandlerSkipsTotalCountByDefault(t *testing.T) {
defer db.Close() defer db.Close()
mock.ExpectQuery("vin = 'LKLG7C4E3NA774736'"). mock.ExpectQuery("vin = 'LKLG7C4E3NA774736'").
WillReturnRows(sqlmock.NewRows([]string{ WillReturnRows(sqlmock.NewRows([]string{
"ts", "event_id", "frame_id", "received_at", "longitude", "latitude", "altitude_m", "speed_kmh", "ts", "event_id", "received_at", "longitude", "latitude", "altitude_m", "speed_kmh",
"direction_deg", "alarm_flag", "status_flag", "total_mileage_km", "protocol", "vin", "direction_deg", "alarm_flag", "status_flag", "total_mileage_km", "protocol", "vin",
}).AddRow( }).AddRow(
"2026-07-02 00:18:22", "event-3", "go_frame", "2026-07-02 00:22:43", "2026-07-02 00:18:22", "event-3", "2026-07-02 00:22:43",
121.07764, 30.585928, 11.0, 8.0, 171, 0, 4718595, 8792.8, 121.07764, 30.585928, 11.0, 8.0, 171, 0, 4718595, 8792.8,
"JT808", "LKLG7C4E3NA774736", "JT808", "LKLG7C4E3NA774736",
)) ))
@@ -448,6 +448,9 @@ func TestBuildLocationSQLUsesLiteralsForTDengine(t *testing.T) {
if strings.Contains(sqlText, "vehicle_key") || strings.Contains(sqlText, "phone") || strings.Contains(sqlText, "device_id") { if strings.Contains(sqlText, "vehicle_key") || strings.Contains(sqlText, "phone") || strings.Contains(sqlText, "device_id") {
t.Fatalf("location sql should use vin-only identity filters: %s", sqlText) t.Fatalf("location sql should use vin-only identity filters: %s", sqlText)
} }
if strings.Contains(sqlText, "frame_id") {
t.Fatalf("location sql should not select duplicate raw frame id: %s", sqlText)
}
} }
func TestBuildRawFrameSQLUsesLiteralsForTDengine(t *testing.T) { func TestBuildRawFrameSQLUsesLiteralsForTDengine(t *testing.T) {

View File

@@ -49,7 +49,6 @@ func SchemaStatements(database string) []string {
`CREATE STABLE IF NOT EXISTS vehicle_locations ( `CREATE STABLE IF NOT EXISTS vehicle_locations (
ts TIMESTAMP, ts TIMESTAMP,
event_id NCHAR(64), event_id NCHAR(64),
frame_id NCHAR(64),
received_at TIMESTAMP, received_at TIMESTAMP,
longitude DOUBLE, longitude DOUBLE,
latitude DOUBLE, latitude DOUBLE,

View File

@@ -113,7 +113,7 @@ func (w *Writer) AppendLocation(ctx context.Context, env envelope.FrameEnvelope)
return err return err
} }
_, err := w.exec.ExecContext(ctx, fmt.Sprintf(`INSERT INTO %s _, err := w.exec.ExecContext(ctx, fmt.Sprintf(`INSERT INTO %s
(ts, event_id, frame_id, received_at, longitude, latitude, altitude_m, speed_kmh, (ts, event_id, received_at, longitude, latitude, altitude_m, speed_kmh,
direction_deg, alarm_flag, status_flag, total_mileage_km) direction_deg, alarm_flag, status_flag, total_mileage_km)
VALUES (%s)`, table, joinLiterals(locationValues(env, longitude, latitude)))) VALUES (%s)`, table, joinLiterals(locationValues(env, longitude, latitude))))
return err return err
@@ -249,7 +249,6 @@ func locationValues(env envelope.FrameEnvelope, longitude float64, latitude floa
return []any{ return []any{
eventTimeOrReceived(env), eventTimeOrReceived(env),
env.StableEventID(), env.StableEventID(),
frameID(env),
received, received,
longitude, longitude,
latitude, latitude,

View File

@@ -23,9 +23,9 @@ func TestSchemaStatementsCreateCoreStables(t *testing.T) {
t.Fatalf("raw schema should not create duplicate fields_json column:\n%s", statements) t.Fatalf("raw schema should not create duplicate fields_json column:\n%s", statements)
} }
locationStable := between(statements, "CREATE STABLE IF NOT EXISTS vehicle_locations", "}") locationStable := between(statements, "CREATE STABLE IF NOT EXISTS vehicle_locations", "}")
for _, field := range []string{"vehicle_key", "phone", "device_id"} { for _, field := range []string{"frame_id", "vehicle_key", "phone", "device_id"} {
if strings.Contains(locationStable, field) { if strings.Contains(locationStable, field) {
t.Fatalf("location stable should only keep protocol/vin identity tags, found %s:\n%s", field, locationStable) t.Fatalf("location stable should only keep core location columns and protocol/vin tags, found %s:\n%s", field, locationStable)
} }
} }
} }
@@ -78,6 +78,10 @@ func TestWriterAppendsRawAndLocationOnly(t *testing.T) {
t.Fatalf("location child should not carry raw identity fallback tag %s: %s", field, locationChild) t.Fatalf("location child should not carry raw identity fallback tag %s: %s", field, locationChild)
} }
} }
locationInsert := findSQL(exec.calls, "INSERT INTO loc_")
if strings.Contains(locationInsert, "frame_id") || strings.Contains(locationInsert, "go_") {
t.Fatalf("location insert should not duplicate raw frame id: %s", locationInsert)
}
} }
func TestWriterNormalizesPhoneTagAndRefreshesExistingChildTags(t *testing.T) { func TestWriterNormalizesPhoneTagAndRefreshesExistingChildTags(t *testing.T) {