feat: add telemetry collection functionality
This commit is contained in:
1 parent
5141bcdb0f
commit
5a285ab1a6
10 files changed
+412
-5
No files matched your search
@@ -86,6 +86,21 @@ class CredentialsManager:
|
||||
logger.info("Added disconnect_timeout column to connections table")
|
||||
except sqlite3.OperationalError:
|
||||
pass
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS telemetry_state (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
installation_uuid TEXT,
|
||||
register_request_id TEXT,
|
||||
credential_id TEXT,
|
||||
credential_secret_encrypted BLOB,
|
||||
expires_at TEXT,
|
||||
last_heartbeat_at REAL,
|
||||
last_heartbeat_request_id TEXT,
|
||||
last_reported_version TEXT,
|
||||
update_request_id TEXT,
|
||||
renewal_request_id TEXT
|
||||
)
|
||||
''')
|
||||
conn.commit()
|
||||
conn.close()
|
||||
logger.info(f"Credentials database initialized at {self.db_path}")
|
||||
@@ -450,6 +465,56 @@ class CredentialsManager:
|
||||
logger.error(f"Failed to get old recordings: {e}")
|
||||
return []
|
||||
|
||||
def get_telemetry_state(self) -> Optional[Dict[str, Any]]:
|
||||
"""Return the encrypted telemetry state, if it exists."""
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
row = conn.execute('SELECT * FROM telemetry_state WHERE id = 1').fetchone()
|
||||
conn.close()
|
||||
if not row:
|
||||
return None
|
||||
return {
|
||||
'installation_uuid': row[1],
|
||||
'register_request_id': row[2],
|
||||
'credential_id': row[3],
|
||||
'credential_secret': self._decrypt(row[4]) if row[4] else None,
|
||||
'expires_at': row[5],
|
||||
'last_heartbeat_at': row[6],
|
||||
'last_heartbeat_request_id': row[7],
|
||||
'last_reported_version': row[8],
|
||||
'update_request_id': row[9],
|
||||
'renewal_request_id': row[10],
|
||||
}
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to read telemetry state: {e}")
|
||||
return None
|
||||
|
||||
def save_telemetry_state(self, state: Dict[str, Any]) -> bool:
|
||||
"""Persist telemetry state with the credential secret encrypted."""
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
conn.execute('''
|
||||
INSERT OR REPLACE INTO telemetry_state
|
||||
(id, installation_uuid, register_request_id, credential_id,
|
||||
credential_secret_encrypted, expires_at, last_heartbeat_at,
|
||||
last_heartbeat_request_id, last_reported_version, update_request_id,
|
||||
renewal_request_id)
|
||||
VALUES (1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
''', (
|
||||
state.get('installation_uuid'), state.get('register_request_id'),
|
||||
state.get('credential_id'),
|
||||
self._encrypt(state['credential_secret']) if state.get('credential_secret') else None,
|
||||
state.get('expires_at'), state.get('last_heartbeat_at'),
|
||||
state.get('last_heartbeat_request_id'), state.get('last_reported_version'),
|
||||
state.get('update_request_id'), state.get('renewal_request_id'),
|
||||
))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to save telemetry state: {e}")
|
||||
return False
|
||||
|
||||
|
||||
# Initialize credentials manager (singleton)
|
||||
try:
|
||||
|
||||
Reference in new issue
Block a user