fix(go): merge gb32960 realtime snapshot payloads
This commit is contained in:
@@ -129,7 +129,10 @@ func (w *SnapshotWriter) Update(ctx context.Context, env envelope.FrameEnvelope)
|
||||
eventTime := nullableTime(env.EventTimeMS)
|
||||
receivedAt := nullableTime(env.ReceivedAtMS)
|
||||
platformName := platformNameFromEnvelope(env)
|
||||
parsedJSON := parsedJSONFromEnvelope(env)
|
||||
parsedJSON, err := w.parsedJSONForEnvelope(ctx, env, vin)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = w.exec.ExecContext(ctx, upsertRealtimeSnapshotSQL,
|
||||
string(env.Protocol),
|
||||
vin,
|
||||
@@ -167,6 +170,23 @@ func (w *SnapshotWriter) Update(ctx context.Context, env envelope.FrameEnvelope)
|
||||
return err
|
||||
}
|
||||
|
||||
func (w *SnapshotWriter) parsedJSONForEnvelope(ctx context.Context, env envelope.FrameEnvelope, vin string) (any, error) {
|
||||
if len(env.Parsed) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
parsed := cloneMap(env.Parsed)
|
||||
if queryer, ok := w.exec.(Queryer); ok {
|
||||
existing, err := realtimeSnapshotParsedJSON(ctx, queryer, env.Protocol, vin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(existing) > 0 {
|
||||
parsed = mergeParsedForProtocol(env.Protocol, existing, env.Parsed)
|
||||
}
|
||||
}
|
||||
return marshalParsedJSON(parsed), nil
|
||||
}
|
||||
|
||||
func platformNameFromEnvelope(env envelope.FrameEnvelope) string {
|
||||
if env.Fields != nil {
|
||||
if value := strings.TrimSpace(stringValue(env.Fields["platform_account"])); value != "" {
|
||||
@@ -193,11 +213,30 @@ func stringValue(value any) string {
|
||||
}
|
||||
}
|
||||
|
||||
func parsedJSONFromEnvelope(env envelope.FrameEnvelope) any {
|
||||
if len(env.Parsed) == 0 {
|
||||
func realtimeSnapshotParsedJSON(ctx context.Context, queryer Queryer, protocol envelope.Protocol, vin string) (map[string]any, error) {
|
||||
var raw sql.NullString
|
||||
err := queryer.QueryRowContext(ctx, selectRealtimeSnapshotParsedJSONSQL, string(protocol), vin).Scan(&raw)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if !raw.Valid || strings.TrimSpace(raw.String) == "" {
|
||||
return nil, nil
|
||||
}
|
||||
var parsed map[string]any
|
||||
if err := json.Unmarshal([]byte(raw.String), &parsed); err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func marshalParsedJSON(parsed map[string]any) any {
|
||||
if len(parsed) == 0 {
|
||||
return nil
|
||||
}
|
||||
data, err := json.Marshal(env.Parsed)
|
||||
data, err := json.Marshal(parsed)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -436,6 +475,8 @@ ON DUPLICATE KEY UPDATE
|
||||
updated_at = IF(VALUES(event_time) IS NOT NULL AND (vehicle_realtime_snapshot.event_time IS NULL OR VALUES(event_time) >= vehicle_realtime_snapshot.event_time), CURRENT_TIMESTAMP, updated_at)
|
||||
`
|
||||
|
||||
const selectRealtimeSnapshotParsedJSONSQL = `SELECT parsed_json FROM vehicle_realtime_snapshot WHERE protocol = ? AND vin = ?`
|
||||
|
||||
const realtimeLocationTableSQL = `CREATE TABLE IF NOT EXISTS vehicle_realtime_location (
|
||||
protocol VARCHAR(32) NOT NULL,
|
||||
vin VARCHAR(32) NOT NULL DEFAULT '',
|
||||
|
||||
Reference in New Issue
Block a user