Skip to content

Commit 7ca07a2

Browse files
committed
feat: improve analytics tracking with model/provider info and faster shutdown
1 parent 6ed5178 commit 7ca07a2

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

extension/secureflow/packages/secureflow-cli/bin/secureflow

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,14 @@ async function main() {
6666
.option('--defectdojo-test-title <title>', 'Title for the DefectDojo test (defaults to "SecureFlow Scan")')
6767
.action(async (projectPath, options) => {
6868
try {
69-
// Track scan command usage
69+
// Load config to get actual model being used
70+
const config = loadConfig();
71+
const actualModel = options.model || config.model;
72+
73+
// Track scan command usage with actual model
7074
await analytics.trackEvent('CLI Command: Scan', {
71-
ai_model: options.model || 'default',
75+
ai_model: actualModel,
76+
ai_provider: config.provider,
7277
output_format: options.format || 'text',
7378
has_defectdojo: !!options.defectdojo
7479
});
@@ -111,9 +116,14 @@ async function main() {
111116
.option('--output <file>', 'Save results to file')
112117
.action(async (projectPath, options) => {
113118
try {
114-
// Track profile command usage
119+
// Load config to get actual model being used
120+
const config = loadConfig();
121+
const actualModel = options.model || config.model;
122+
123+
// Track profile command usage with actual model
115124
await analytics.trackEvent('CLI Command: Profile', {
116-
ai_model: options.model || 'default',
125+
ai_model: actualModel,
126+
ai_provider: config.provider,
117127
output_format: options.format || 'text'
118128
});
119129

@@ -198,8 +208,8 @@ async function main() {
198208
// Parse CLI arguments
199209
await program.parseAsync(process.argv);
200210

201-
// Shutdown analytics after command completes
202-
await analytics.shutdown();
211+
// Quick shutdown for fast exit (fire and forget)
212+
analytics.shutdown(true).catch(() => {});
203213
}
204214

205215
// Run the main function

extension/secureflow/packages/secureflow-cli/lib/services/analytics.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export declare class AnalyticsService {
6666

6767
/**
6868
* Shutdown analytics service
69+
* @param quick - If true, don't wait for flush (faster exit)
6970
*/
70-
shutdown(): Promise<void>;
71+
shutdown(quick?: boolean): Promise<void>;
7172
}

extension/secureflow/packages/secureflow-cli/lib/services/analytics.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const os = require('os');
1717
const POSTHOG_API_KEY = 'phc_iOS0SOw2gDax8kq44Z9FBEVAs8m6QG7yANvBF8ItV6g';
1818
const POSTHOG_HOST = 'https://us.i.posthog.com';
1919
const DISTINCT_ID_KEY = 'secureflow.analytics.distinctId';
20-
const SHUTDOWN_TIMEOUT_MS = 2000;
20+
const SHUTDOWN_TIMEOUT_MS = 500; // Reduced from 2000ms to 500ms
2121

2222
/**
2323
* Storage adapter interface for distinct ID persistence
@@ -248,7 +248,8 @@ class AnalyticsService {
248248
event: eventName,
249249
properties: eventProperties
250250
});
251-
await this.posthog.flush();
251+
// Fire and forget - don't wait for flush to complete
252+
this.posthog.flush().catch(() => {});
252253
} catch (error) {
253254
// Silently fail - analytics should not disrupt application
254255
}
@@ -270,13 +271,23 @@ class AnalyticsService {
270271

271272
/**
272273
* Shutdown analytics service
274+
* @param {boolean} quick - If true, don't wait for flush (faster exit)
273275
*/
274-
async shutdown() {
276+
async shutdown(quick = false) {
275277
if (!this.posthog) {
276278
return;
277279
}
278280

279281
try {
282+
if (quick) {
283+
// For quick commands, just shutdown without waiting
284+
this.posthog.shutdown().catch(() => {}); // Fire and forget
285+
this.posthog = null;
286+
this.initialized = false;
287+
return;
288+
}
289+
290+
// Normal shutdown with reduced timeout
280291
const shutdownPromise = this.posthog.shutdown();
281292
const timeoutPromise = new Promise((_, reject) =>
282293
setTimeout(() => reject(new Error('Shutdown timeout')), SHUTDOWN_TIMEOUT_MS)

0 commit comments

Comments
 (0)