|
5 | 5 | import pm.util.Constants; |
6 | 6 |
|
7 | 7 | import java.nio.file.Files; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
8 | 10 | import java.util.Map; |
9 | 11 | import java.util.UUID; |
10 | 12 |
|
@@ -48,21 +50,30 @@ public static void init() { |
48 | 50 | /** |
49 | 51 | * Tracks a command execution event (fire-and-forget). |
50 | 52 | * |
51 | | - * @param command The command name (e.g., "build", "test") |
| 53 | + * @param command The command name (e.g., "build", "test") |
| 54 | + * @param success Whether the command completed without exception |
| 55 | + * @param elapsedMs Execution time in milliseconds |
52 | 56 | */ |
53 | | - public static void trackCommand(String command) { |
| 57 | + public static void trackCommand(String command, boolean success, long elapsedMs) { |
54 | 58 | if (config == null || !config.isTelemetryEnabled()) { |
55 | 59 | return; |
56 | 60 | } |
57 | 61 | try { |
58 | | - TelemetryEvent event = new TelemetryEvent("command_executed", Map.of( |
59 | | - "command", command, |
60 | | - "version", Constants.VERSION, |
61 | | - "os", System.getProperty("os.name"), |
62 | | - "os_version", System.getProperty("os.version"), |
63 | | - "java_version", System.getProperty("java.version"), |
64 | | - "project_count", getProjectCount() |
65 | | - )); |
| 62 | + Map<String, Object> props = new HashMap<>(); |
| 63 | + props.put("command", command); |
| 64 | + props.put("version", Constants.VERSION); |
| 65 | + props.put("os", System.getProperty("os.name")); |
| 66 | + props.put("os_version", System.getProperty("os.version")); |
| 67 | + props.put("java_version", System.getProperty("java.version")); |
| 68 | + props.put("arch", System.getProperty("os.arch")); |
| 69 | + props.put("locale", java.util.Locale.getDefault().toString()); |
| 70 | + props.put("terminal", System.getenv("TERM") != null ? System.getenv("TERM") : "unknown"); |
| 71 | + props.put("project_count", getProjectCount()); |
| 72 | + props.put("project_types", getProjectTypes()); |
| 73 | + props.put("success", success); |
| 74 | + props.put("execution_ms", elapsedMs); |
| 75 | + |
| 76 | + TelemetryEvent event = new TelemetryEvent("command_executed", props); |
66 | 77 | TelemetryClient.send(event, Constants.POSTHOG_KEY, config.getDistinctId()); |
67 | 78 | } catch (Exception ignored) { |
68 | 79 | // Telemetry tracking failure is non-critical |
@@ -134,16 +145,41 @@ private static void promptConsent() { |
134 | 145 | * Reads projects.json directly and counts top-level keys. |
135 | 146 | */ |
136 | 147 | private static int getProjectCount() { |
| 148 | + try { |
| 149 | + return loadProjectsMap().size(); |
| 150 | + } catch (Exception e) { |
| 151 | + return 0; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Extracts unique project types (e.g., ["Maven", "Rust", "Flutter"]). |
| 157 | + * No project names or paths — only the type field. |
| 158 | + */ |
| 159 | + private static List<String> getProjectTypes() { |
| 160 | + try { |
| 161 | + Map<String, Map<String, Object>> projects = loadProjectsMap(); |
| 162 | + return projects.values().stream() |
| 163 | + .map(p -> p.getOrDefault("type", "UNKNOWN").toString()) |
| 164 | + .distinct() |
| 165 | + .sorted() |
| 166 | + .toList(); |
| 167 | + } catch (Exception e) { |
| 168 | + return List.of(); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + private static Map<String, Map<String, Object>> loadProjectsMap() { |
137 | 173 | try { |
138 | 174 | if (!Files.exists(Constants.PROJECTS_FILE)) { |
139 | | - return 0; |
| 175 | + return Map.of(); |
140 | 176 | } |
141 | 177 | String json = Files.readString(Constants.PROJECTS_FILE); |
142 | | - Map<String, Object> projects = new Gson().fromJson(json, |
143 | | - new TypeToken<Map<String, Object>>() {}.getType()); |
144 | | - return projects != null ? projects.size() : 0; |
| 178 | + Map<String, Map<String, Object>> projects = new Gson().fromJson(json, |
| 179 | + new TypeToken<Map<String, Map<String, Object>>>() {}.getType()); |
| 180 | + return projects != null ? projects : Map.of(); |
145 | 181 | } catch (Exception e) { |
146 | | - return 0; |
| 182 | + return Map.of(); |
147 | 183 | } |
148 | 184 | } |
149 | 185 |
|
|
0 commit comments