refactor(go): avoid duplicate realtime protocol payloads
This commit is contained in:
@@ -7,19 +7,18 @@ import (
|
||||
)
|
||||
|
||||
type Snapshot struct {
|
||||
VehicleKey string `json:"vehicle_key"`
|
||||
VIN string `json:"vin"`
|
||||
Protocol envelope.Protocol `json:"protocol,omitempty"`
|
||||
Protocols []envelope.Protocol `json:"protocols,omitempty"`
|
||||
EventID string `json:"event_id,omitempty"`
|
||||
EventTimeMS int64 `json:"event_time_ms"`
|
||||
ReceivedAtMS int64 `json:"received_at_ms"`
|
||||
SourceEndpoint string `json:"source_endpoint,omitempty"`
|
||||
Fields map[string]any `json:"fields,omitempty"`
|
||||
FieldTimesMS map[string]int64 `json:"field_times_ms,omitempty"`
|
||||
Parsed map[string]any `json:"parsed,omitempty"`
|
||||
ProtocolData map[envelope.Protocol]map[string]any `json:"protocol_data,omitempty"`
|
||||
UpdatedAtMS int64 `json:"updated_at_ms"`
|
||||
VehicleKey string `json:"vehicle_key"`
|
||||
VIN string `json:"vin"`
|
||||
Protocol envelope.Protocol `json:"protocol,omitempty"`
|
||||
Protocols []envelope.Protocol `json:"protocols,omitempty"`
|
||||
EventID string `json:"event_id,omitempty"`
|
||||
EventTimeMS int64 `json:"event_time_ms"`
|
||||
ReceivedAtMS int64 `json:"received_at_ms"`
|
||||
SourceEndpoint string `json:"source_endpoint,omitempty"`
|
||||
Fields map[string]any `json:"fields,omitempty"`
|
||||
FieldTimesMS map[string]int64 `json:"field_times_ms,omitempty"`
|
||||
Parsed map[string]any `json:"parsed,omitempty"`
|
||||
UpdatedAtMS int64 `json:"updated_at_ms"`
|
||||
}
|
||||
|
||||
type OnlineStatus struct {
|
||||
|
||||
@@ -92,7 +92,6 @@ func (r *Repository) Update(ctx context.Context, env envelope.FrameEnvelope) err
|
||||
VIN: vin,
|
||||
Fields: map[string]any{},
|
||||
FieldTimesMS: map[string]int64{},
|
||||
ProtocolData: map[envelope.Protocol]map[string]any{},
|
||||
}
|
||||
} else if merged.VIN == "" && vin != "" {
|
||||
merged.VIN = vin
|
||||
@@ -105,10 +104,6 @@ func (r *Repository) Update(ctx context.Context, env envelope.FrameEnvelope) err
|
||||
merged.SourceEndpoint = env.SourceEndpoint
|
||||
}
|
||||
merged.Protocols = protocols
|
||||
if merged.ProtocolData == nil {
|
||||
merged.ProtocolData = map[envelope.Protocol]map[string]any{}
|
||||
}
|
||||
merged.ProtocolData[env.Protocol] = cloneMap(protocolSnapshot.Parsed)
|
||||
merged.UpdatedAtMS = nowMS
|
||||
if err := r.setJSON(ctx, mergedKey(vehicleKey), merged, r.cfg.ttl()); err != nil {
|
||||
return err
|
||||
|
||||
@@ -2,6 +2,7 @@ package realtime
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
@@ -120,12 +121,12 @@ func TestRepositoryStoresFullParsedProtocolSnapshotAndMergesGB32960Units(t *test
|
||||
if err != nil {
|
||||
t.Fatalf("GetMerged() error = %v", err)
|
||||
}
|
||||
gb, ok := merged.ProtocolData[envelope.ProtocolGB32960]
|
||||
if !ok {
|
||||
t.Fatalf("merged protocol data missing: %#v", merged.ProtocolData)
|
||||
mergedJSON, err := json.Marshal(merged)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(gb["data_units"].([]any)) != 2 {
|
||||
t.Fatalf("merged protocol data did not keep full parsed fields: %#v", gb)
|
||||
if strings.Contains(string(mergedJSON), "protocol_data") {
|
||||
t.Fatalf("merged snapshot should not duplicate full protocol parsed data: %s", string(mergedJSON))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user