perf(go): decouple realtime mysql projection
This commit is contained in:
@@ -185,26 +185,34 @@ func (r *Repository) setJSON(ctx context.Context, key string, value any, ttl tim
|
||||
|
||||
func (r *Repository) setKV(ctx context.Context, vin string, env envelope.FrameEnvelope, parsed map[string]any) error {
|
||||
rows := realtimeKVFields(env, parsed)
|
||||
if len(rows) == 0 {
|
||||
return nil
|
||||
}
|
||||
grouped := make(map[string]map[string]any)
|
||||
for _, row := range rows {
|
||||
key := realtimeKVKey(row.Protocol, vin, row.Domain)
|
||||
values := map[string]any{
|
||||
row.Field: row.Value,
|
||||
"_event_time_ms": strconv.FormatInt(row.EventTimeMS, 10),
|
||||
"_received_at_ms": strconv.FormatInt(row.ReceivedAtMS, 10),
|
||||
"_event_id": row.EventID,
|
||||
"_protocol": string(row.Protocol),
|
||||
"_vin": vin,
|
||||
"_domain": row.Domain,
|
||||
"_value_type:" + row.Field: row.ValueType,
|
||||
}
|
||||
if err := r.client.HSet(ctx, key, values).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.client.Expire(ctx, key, r.cfg.ttl()).Err(); err != nil {
|
||||
return err
|
||||
values := grouped[key]
|
||||
if values == nil {
|
||||
values = map[string]any{
|
||||
"_event_time_ms": strconv.FormatInt(row.EventTimeMS, 10),
|
||||
"_received_at_ms": strconv.FormatInt(row.ReceivedAtMS, 10),
|
||||
"_event_id": row.EventID,
|
||||
"_protocol": string(row.Protocol),
|
||||
"_vin": vin,
|
||||
"_domain": row.Domain,
|
||||
}
|
||||
grouped[key] = values
|
||||
}
|
||||
values[row.Field] = row.Value
|
||||
values["_value_type:"+row.Field] = row.ValueType
|
||||
}
|
||||
return nil
|
||||
pipe := r.client.Pipeline()
|
||||
for key, values := range grouped {
|
||||
pipe.HSet(ctx, key, values)
|
||||
pipe.Expire(ctx, key, r.cfg.ttl())
|
||||
}
|
||||
_, err := pipe.Exec(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repository) addProtocol(ctx context.Context, vehicleKey string, protocol envelope.Protocol) ([]envelope.Protocol, error) {
|
||||
|
||||
Reference in New Issue
Block a user