fix(go): normalize tdengine phone tags

This commit is contained in:
lingniu
2026-07-02 13:43:15 +08:00
parent 75e7c3fbe5
commit b27f909109
2 changed files with 36 additions and 1 deletions

View File

@@ -149,11 +149,16 @@ func (w *Writer) ensureChild(ctx context.Context, table string, stable string, e
quote(string(env.Protocol)),
quote(env.VehicleKey()),
quote(env.VIN),
quote(env.Phone),
quote(normalizePhoneTag(env.Phone)),
quote(env.DeviceID))
if _, err := w.exec.ExecContext(ctx, statement); err != nil {
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.seen[key] = struct{}{}
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())
}
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 {
sum := sha1.Sum([]byte(value))
return hex.EncodeToString(sum[:8])

View File

@@ -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) {
exec := &recordingExec{}
writer := NewWriter(exec)