feat(go): batch redis fast writer projections
This commit is contained in:
@@ -42,6 +42,36 @@ func (r *Repository) FastUpdate(ctx context.Context, env envelope.FrameEnvelope)
|
||||
return r.setFastProjection(ctx, vehicleKey, vin, env)
|
||||
}
|
||||
|
||||
func (r *Repository) FastUpdateBatch(ctx context.Context, envs []envelope.FrameEnvelope) error {
|
||||
if len(envs) == 0 {
|
||||
return nil
|
||||
}
|
||||
pipe := r.client.Pipeline()
|
||||
queued := 0
|
||||
for _, env := range envs {
|
||||
if !envelope.IsRealtimeTelemetryFrame(env) {
|
||||
continue
|
||||
}
|
||||
vin := strings.TrimSpace(env.VIN)
|
||||
if vin == "" {
|
||||
continue
|
||||
}
|
||||
vehicleKey := strings.TrimSpace(env.VehicleKey())
|
||||
if vehicleKey == "" || strings.HasSuffix(vehicleKey, ":unknown") {
|
||||
continue
|
||||
}
|
||||
if err := r.queueFastProjection(ctx, pipe, vehicleKey, vin, env); err != nil {
|
||||
return err
|
||||
}
|
||||
queued++
|
||||
}
|
||||
if queued == 0 {
|
||||
return nil
|
||||
}
|
||||
_, err := pipe.Exec(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repository) Update(ctx context.Context, env envelope.FrameEnvelope) error {
|
||||
if !envelope.IsRealtimeTelemetryFrame(env) {
|
||||
return nil
|
||||
@@ -373,6 +403,15 @@ func (r *Repository) setKV(ctx context.Context, vin string, env envelope.FrameEn
|
||||
}
|
||||
|
||||
func (r *Repository) setFastProjection(ctx context.Context, vehicleKey string, vin string, env envelope.FrameEnvelope) error {
|
||||
pipe := r.client.Pipeline()
|
||||
if err := r.queueFastProjection(ctx, pipe, vehicleKey, vin, env); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := pipe.Exec(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repository) queueFastProjection(ctx context.Context, pipe redis.Pipeliner, vehicleKey string, vin string, env envelope.FrameEnvelope) error {
|
||||
values, types := realtimeKVMapsForEnvelope(env)
|
||||
eventTimeMS := eventTimeOrReceivedMS(env)
|
||||
offlineAfterMS := env.ReceivedAtMS + r.cfg.ttl().Milliseconds()
|
||||
@@ -410,7 +449,6 @@ func (r *Repository) setFastProjection(ctx context.Context, vehicleKey string, v
|
||||
"ttl_seconds": strconv.FormatInt(int64(r.cfg.ttl().Seconds()), 10),
|
||||
"source_endpoint": env.SourceEndpoint,
|
||||
}
|
||||
pipe := r.client.Pipeline()
|
||||
if len(values) > 0 {
|
||||
pipe.HSet(ctx, realtimeKVValuesKey(env.Protocol, vin), values)
|
||||
pipe.HSet(ctx, realtimeKVTypesKey(env.Protocol, vin), types)
|
||||
@@ -420,8 +458,7 @@ func (r *Repository) setFastProjection(ctx context.Context, vehicleKey string, v
|
||||
pipe.HSet(ctx, onlineStateKey(env.Protocol, vin), state)
|
||||
pipe.ZAdd(ctx, "vehicle:last_seen", redis.Z{Score: float64(env.ReceivedAtMS), Member: onlineMember(env.Protocol, vin)})
|
||||
pipe.SAdd(ctx, realtimeIndexKey(env.Protocol), vin)
|
||||
_, err = pipe.Exec(ctx)
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
func realtimeKVMapsForEnvelope(env envelope.FrameEnvelope) (map[string]any, map[string]any) {
|
||||
|
||||
@@ -430,6 +430,57 @@ func TestRepositoryFastUpdateOnlyWritesPermanentKVAndMinuteOnline(t *testing.T)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepositoryFastUpdateBatchWritesMultipleRealtimeProjections(t *testing.T) {
|
||||
repo, closeFn := newTestRepository(t)
|
||||
defer closeFn()
|
||||
ctx := context.Background()
|
||||
|
||||
err := repo.FastUpdateBatch(ctx, []envelope.FrameEnvelope{
|
||||
{
|
||||
Protocol: envelope.ProtocolGB32960,
|
||||
VIN: "VIN001",
|
||||
EventTimeMS: 1000,
|
||||
ReceivedAtMS: 1100,
|
||||
Parsed: map[string]any{
|
||||
"data_units": []any{map[string]any{"type": "0x01", "name": "vehicle", "value": map[string]any{"soc_percent": 88.0}}},
|
||||
},
|
||||
},
|
||||
{
|
||||
Protocol: envelope.ProtocolJT808,
|
||||
VIN: "VIN002",
|
||||
EventTimeMS: 2000,
|
||||
ReceivedAtMS: 2200,
|
||||
MessageID: "0x0200",
|
||||
Parsed: map[string]any{"location": map[string]any{"longitude": 121.1, "latitude": 30.2}},
|
||||
Fields: map[string]any{envelope.FieldLongitude: 121.1, envelope.FieldLatitude: 30.2},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("FastUpdateBatch() error = %v", err)
|
||||
}
|
||||
|
||||
gbValues, err := repo.client.HGetAll(ctx, "vehicle:rt-kv:GB32960:VIN001:values").Result()
|
||||
if err != nil {
|
||||
t.Fatalf("gb values HGetAll error = %v", err)
|
||||
}
|
||||
if gbValues["gb32960.vehicle.soc_percent"] != "88" {
|
||||
t.Fatalf("gb values = %#v", gbValues)
|
||||
}
|
||||
jtValues, err := repo.client.HGetAll(ctx, "vehicle:rt-kv:JT808:VIN002:values").Result()
|
||||
if err != nil {
|
||||
t.Fatalf("jt values HGetAll error = %v", err)
|
||||
}
|
||||
if jtValues["jt808.location.longitude"] != "121.1" || jtValues["jt808.location.latitude"] != "30.2" {
|
||||
t.Fatalf("jt values = %#v", jtValues)
|
||||
}
|
||||
if repo.client.ZScore(ctx, "vehicle:last_seen", "GB32960:VIN001").Err() != nil {
|
||||
t.Fatal("last_seen should contain GB32960 VIN001")
|
||||
}
|
||||
if repo.client.ZScore(ctx, "vehicle:last_seen", "JT808:VIN002").Err() != nil {
|
||||
t.Fatal("last_seen should contain JT808 VIN002")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepositoryListsOnlineStatusesAndPipelineSummary(t *testing.T) {
|
||||
repo, closeFn := newTestRepository(t)
|
||||
defer closeFn()
|
||||
|
||||
Reference in New Issue
Block a user