fix(go): normalize tdengine phone tags
This commit is contained in:
@@ -149,11 +149,16 @@ func (w *Writer) ensureChild(ctx context.Context, table string, stable string, e
|
|||||||
quote(string(env.Protocol)),
|
quote(string(env.Protocol)),
|
||||||
quote(env.VehicleKey()),
|
quote(env.VehicleKey()),
|
||||||
quote(env.VIN),
|
quote(env.VIN),
|
||||||
quote(env.Phone),
|
quote(normalizePhoneTag(env.Phone)),
|
||||||
quote(env.DeviceID))
|
quote(env.DeviceID))
|
||||||
if _, err := w.exec.ExecContext(ctx, statement); err != nil {
|
if _, err := w.exec.ExecContext(ctx, statement); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if phone := normalizePhoneTag(env.Phone); phone != "" {
|
||||||
|
if _, err := w.exec.ExecContext(ctx, fmt.Sprintf("ALTER TABLE %s SET TAG phone = '%s'", table, quote(phone))); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
w.cache.mu.Lock()
|
w.cache.mu.Lock()
|
||||||
w.cache.seen[key] = struct{}{}
|
w.cache.seen[key] = struct{}{}
|
||||||
w.cache.mu.Unlock()
|
w.cache.mu.Unlock()
|
||||||
@@ -276,6 +281,14 @@ func tableName(prefix string, env envelope.FrameEnvelope) string {
|
|||||||
return prefix + "_" + strings.ToLower(string(env.Protocol)) + "_" + hash16(env.VehicleKey())
|
return prefix + "_" + strings.ToLower(string(env.Protocol)) + "_" + hash16(env.VehicleKey())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func normalizePhoneTag(phone string) string {
|
||||||
|
trimmed := strings.TrimLeft(strings.TrimSpace(phone), "0")
|
||||||
|
if trimmed == "" {
|
||||||
|
return strings.TrimSpace(phone)
|
||||||
|
}
|
||||||
|
return trimmed
|
||||||
|
}
|
||||||
|
|
||||||
func hash16(value string) string {
|
func hash16(value string) string {
|
||||||
sum := sha1.Sum([]byte(value))
|
sum := sha1.Sum([]byte(value))
|
||||||
return hex.EncodeToString(sum[:8])
|
return hex.EncodeToString(sum[:8])
|
||||||
|
|||||||
@@ -54,6 +54,28 @@ func TestWriterAppendsRawLocationAndMileage(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriterNormalizesPhoneTagAndRefreshesExistingChildTags(t *testing.T) {
|
||||||
|
exec := &recordingExec{}
|
||||||
|
writer := NewWriter(exec)
|
||||||
|
env := sampleEnvelope()
|
||||||
|
|
||||||
|
if err := writer.AppendRawFrame(context.Background(), env); err != nil {
|
||||||
|
t.Fatalf("AppendRawFrame() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
createChild := findSQL(exec.calls, "USING raw_frames")
|
||||||
|
if !strings.Contains(createChild, "'13307795425'") {
|
||||||
|
t.Fatalf("child table phone tag should be normalized: %s", createChild)
|
||||||
|
}
|
||||||
|
if strings.Contains(createChild, "'013307795425'") {
|
||||||
|
t.Fatalf("child table phone tag should not keep leading zero: %s", createChild)
|
||||||
|
}
|
||||||
|
refreshTag := findSQL(exec.calls, "SET TAG phone")
|
||||||
|
if !strings.Contains(refreshTag, "'13307795425'") {
|
||||||
|
t.Fatalf("phone tag refresh should use normalized phone: %s", refreshTag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestWriterChunksOversizedParsedJSON(t *testing.T) {
|
func TestWriterChunksOversizedParsedJSON(t *testing.T) {
|
||||||
exec := &recordingExec{}
|
exec := &recordingExec{}
|
||||||
writer := NewWriter(exec)
|
writer := NewWriter(exec)
|
||||||
|
|||||||
Reference in New Issue
Block a user