@dudousxd/nestjs-telescope
Version:
Laravel Telescope-style observability console for NestJS — core: watchers, recorder, correlation, SQLite store, headless API.
29 lines • 1.09 kB
JavaScript
// packages/core/src/query/n-plus-one.ts
import { EntryType } from '../entry/entry.js';
/** Group query entries by familyHash; report templates run >= threshold times. */
export function detectNPlusOne(entries, threshold) {
const groups = new Map();
for (const entry of entries) {
if (entry.type !== EntryType.Query || entry.familyHash === null)
continue;
const record = typeof entry.content === 'object' && entry.content !== null
? entry.content
: null;
const sql = record !== null && typeof record.sql === 'string' ? record.sql : '';
const existing = groups.get(entry.familyHash);
if (existing) {
existing.count += 1;
}
else {
groups.set(entry.familyHash, { count: 1, sql });
}
}
const insights = [];
for (const [familyHash, group] of groups) {
if (group.count >= threshold) {
insights.push({ familyHash, count: group.count, sql: group.sql });
}
}
return insights;
}
//# sourceMappingURL=n-plus-one.js.map