MCPcopy Create free account
hub / github.com/QodeXcli/QodeX / ensureOpen

Method ensureOpen

src/utils/telemetry.ts:93–134  ·  view source on GitHub ↗

Lazy init — only opens the DB the first time something tries to record.

()

Source from the content-addressed store, hash-verified

91
92 /** Lazy init — only opens the DB the first time something tries to record. */
93 private ensureOpen(): Database.Database | null {
94 if (!this.enabled) return null;
95 if (this.db) return this.db;
96 try {
97 this.db = new Database(QODEX_TELEMETRY_DB);
98 this.db.pragma('journal_mode = WAL');
99 this.db.exec(`
100 CREATE TABLE IF NOT EXISTS tool_events (
101 id INTEGER PRIMARY KEY AUTOINCREMENT,
102 ts INTEGER NOT NULL,
103 cwd TEXT NOT NULL,
104 tool TEXT NOT NULL,
105 duration_ms INTEGER NOT NULL,
106 success INTEGER NOT NULL,
107 error_class TEXT
108 );
109 CREATE INDEX IF NOT EXISTS idx_tool_events_tool ON tool_events(tool);
110 CREATE INDEX IF NOT EXISTS idx_tool_events_cwd ON tool_events(cwd);
111
112 CREATE TABLE IF NOT EXISTS llm_events (
113 id INTEGER PRIMARY KEY AUTOINCREMENT,
114 ts INTEGER NOT NULL,
115 cwd TEXT NOT NULL,
116 provider TEXT NOT NULL,
117 model TEXT NOT NULL,
118 role TEXT NOT NULL,
119 input_tokens INTEGER NOT NULL,
120 output_tokens INTEGER NOT NULL,
121 duration_ms INTEGER NOT NULL,
122 cost_usd REAL NOT NULL,
123 success INTEGER NOT NULL
124 );
125 CREATE INDEX IF NOT EXISTS idx_llm_events_model ON llm_events(model);
126 CREATE INDEX IF NOT EXISTS idx_llm_events_role ON llm_events(role);
127 `);
128 return this.db;
129 } catch (e: any) {
130 logger.warn('Telemetry DB open failed; disabling for this session', { err: e?.message });
131 this.enabled = false;
132 return null;
133 }
134 }
135
136 setEnabled(enabled: boolean, anonymize = false): void {
137 this.enabled = enabled;

Callers 5

recordToolMethod · 0.95
recordLlmMethod · 0.95
getToolStatsMethod · 0.95
getModelStatsMethod · 0.95
clearMethod · 0.95

Calls 1

warnMethod · 0.80

Tested by

no test coverage detected