()
| 107 | } |
| 108 | |
| 109 | func getOrCreateHeartbeatID() string { |
| 110 | db, err := sql.Open("sqlite3", "./heartbeat.db") |
| 111 | if err != nil { |
| 112 | logrus.Errorf("Failed to open SQLite DB: %v", err) |
| 113 | return uuid.New().String() // fallback to temp UUID |
| 114 | } |
| 115 | defer func() { _ = db.Close() }() |
| 116 | |
| 117 | _, err = db.Exec(`CREATE TABLE IF NOT EXISTS config (key TEXT PRIMARY KEY, value TEXT)`) |
| 118 | if err != nil { |
| 119 | logrus.Errorf("Failed to create config table: %v", err) |
| 120 | return uuid.New().String() |
| 121 | } |
| 122 | |
| 123 | var heartbeatID string |
| 124 | err = db.QueryRow(`SELECT value FROM config WHERE key = 'heartbeat_id'`).Scan(&heartbeatID) |
| 125 | if err == sql.ErrNoRows { |
| 126 | heartbeatID = uuid.New().String() |
| 127 | _, err = db.Exec(`INSERT INTO config (key, value) VALUES (?, ?)`, "heartbeat_id", heartbeatID) |
| 128 | if err != nil { |
| 129 | logrus.Errorf("Failed to insert heartbeat_id: %v", err) |
| 130 | } |
| 131 | } else if err != nil { |
| 132 | logrus.Errorf("Failed to read heartbeat_id: %v", err) |
| 133 | return uuid.New().String() |
| 134 | } |
| 135 | |
| 136 | return heartbeatID |
| 137 | } |
| 138 | |
| 139 | // sendHeartbeat initializes and maintains a periodic heartbeat to PostHog |
| 140 | func sendHeartbeat(client posthog.Client, heartbeatID string) { |
no test coverage detected