feat(setup): enhance ingest summary to track and display chunk sizes per section

- Introduced `SectionInfo` interface to store chunk count and individual chunk sizes.
- Replaced simple section count map with a map of `SectionInfo` for richer data.
- Updated logic to aggregate chunk sizes and counts per section.
- Modified summary output to show total characters, top 5 sections with detailed chunk count, total chars, and a list of chunk sizes.
- Refactored comments and variable names for clarity.
This commit is contained in:
2026-04-04 10:31:14 +05:30
parent f54a14f408
commit 822597e0e2
+27 -12
View File
@@ -348,24 +348,35 @@ function printSummary(documents: Document[]): void {
const minSize = Math.min(...sizes); const minSize = Math.min(...sizes);
const maxSize = Math.max(...sizes); const maxSize = Math.max(...sizes);
// Count unique sections // Count unique sections and track chunk sizes per section
const sectionIds = new Set<string>(); const sectionIds = new Set<string>();
const sectionsWithMultipleChunks = new Map<string, number>(); interface SectionInfo {
count: number;
chunkSizes: number[];
}
const sectionInfo = new Map<string, SectionInfo>();
for (const doc of documents) { for (const doc of documents) {
const sectionId = doc.metadata.sectionid as string; const sectionId = doc.metadata.sectionid as string;
if (sectionId) { if (sectionId) {
sectionIds.add(sectionId); sectionIds.add(sectionId);
sectionsWithMultipleChunks.set( const existing = sectionInfo.get(sectionId);
sectionId, if (existing) {
(sectionsWithMultipleChunks.get(sectionId) || 0) + 1, existing.count++;
); existing.chunkSizes.push(doc.pageContent.length);
} else {
sectionInfo.set(sectionId, {
count: 1,
chunkSizes: [doc.pageContent.length],
});
}
} }
} }
const multiChunkSections = Array.from(sectionsWithMultipleChunks.entries()) // Get sections with multiple chunks, sorted by chunk count
.filter(([, count]) => count > 1) const multiChunkSections = Array.from(sectionInfo.entries())
.sort((a, b) => b[1] - a[1]); .filter(([, info]) => info.count > 1)
.sort((a, b) => b[1].count - a[1].count);
console.log("\n📊 Ingest Summary:"); console.log("\n📊 Ingest Summary:");
console.log(` Total documents: ${documents.length}`); console.log(` Total documents: ${documents.length}`);
@@ -378,9 +389,13 @@ function printSummary(documents: Document[]): void {
console.log(` Total: ${totalSize} chars`); console.log(` Total: ${totalSize} chars`);
if (multiChunkSections.length > 0) { if (multiChunkSections.length > 0) {
console.log("\n Top sections by chunk count:"); console.log("\n Top 5 sections by chunk count:");
for (const [sectionId, count] of multiChunkSections.slice(0, 5)) { for (const [sectionId, info] of multiChunkSections.slice(0, 5)) {
console.log(` ${sectionId}: ${count} chunks`); const sectionTotal = info.chunkSizes.reduce((sum, size) => sum + size, 0);
const chunkSizesStr = info.chunkSizes.join(", ");
console.log(` ${sectionId}:`);
console.log(` Chunks: ${info.count}, Total: ${sectionTotal} chars`);
console.log(` Chunk sizes: [${chunkSizesStr}]`);
} }
if (multiChunkSections.length > 5) { if (multiChunkSections.length > 5) {
console.log(` ... and ${multiChunkSections.length - 5} more`); console.log(` ... and ${multiChunkSections.length - 5} more`);