( collectionId: string, fieldPath: Array<string>, executionTimeMs: number, )
| 94 | * Track a query for dev mode analysis |
| 95 | */ |
| 96 | export function trackQuery( |
| 97 | collectionId: string, |
| 98 | fieldPath: Array<string>, |
| 99 | executionTimeMs: number, |
| 100 | ): void { |
| 101 | if (!isDevModeEnabled()) return |
| 102 | |
| 103 | const key = `${collectionId}:${fieldPath.join(`.`)}` |
| 104 | const existing = queryPatterns.get(key) |
| 105 | |
| 106 | if (existing) { |
| 107 | existing.queryCount++ |
| 108 | existing.totalTimeMs += executionTimeMs |
| 109 | existing.avgTimeMs = existing.totalTimeMs / existing.queryCount |
| 110 | } else { |
| 111 | queryPatterns.set(key, { |
| 112 | fieldPath, |
| 113 | queryCount: 1, |
| 114 | totalTimeMs: executionTimeMs, |
| 115 | avgTimeMs: executionTimeMs, |
| 116 | }) |
| 117 | } |
| 118 | |
| 119 | // Check if we should suggest an index |
| 120 | const pattern = queryPatterns.get(key)! |
| 121 | if (pattern.avgTimeMs > devModeConfig.slowQueryThresholdMs) { |
| 122 | emitIndexSuggestion({ |
| 123 | type: `slow-query`, |
| 124 | collectionId, |
| 125 | fieldPath, |
| 126 | message: `Queries on "${fieldPath.join(`.`)}" are slow (avg ${pattern.avgTimeMs.toFixed(1)}ms). Consider adding an index.`, |
| 127 | queryTimeMs: pattern.avgTimeMs, |
| 128 | queryCount: pattern.queryCount, |
| 129 | }) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Check collection size and suggest index if needed (dev mode) |
nothing calls this directly
no test coverage detected