From b348fcc4384ebccbc75300c4cf188ceb7f7f041f Mon Sep 17 00:00:00 2001 From: David Snelling Date: Wed, 23 Jul 2025 12:39:17 -0700 Subject: [PATCH] **feat(core): add getStatistics method to BrainyData for detailed DB insights** - **New Method**: - Introduced a new `getStatistics` method in `brainyData.ts` to retrieve key database metrics: - Counts for nouns, verbs, metadata entries, and HNSW index size. - **Error Handling**: - Added try-catch blocks to ensure robust error management when fetching metadata or logging failures. - **Purpose**: - Enhances observability of the database state, providing valuable insights for diagnostics and monitoring. --- src/brainyData.ts | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/brainyData.ts b/src/brainyData.ts index adfbb3aa..acd8e930 100644 --- a/src/brainyData.ts +++ b/src/brainyData.ts @@ -1518,6 +1518,56 @@ export class BrainyData implements BrainyDataInterface { return this.index.size() } + /** + * Get statistics about the current state of the database + * @returns Object containing counts of nouns, verbs, metadata entries, and HNSW index size + */ + public async getStatistics(): Promise<{ + nounCount: number + verbCount: number + metadataCount: number + hnswIndexSize: number + }> { + await this.ensureInitialized() + + try { + // Get noun count from the index + const nounCount = this.index.getNouns().size + + // Get verb count from storage + const allVerbs = await this.storage!.getAllVerbs() + const verbCount = allVerbs.length + + // Count metadata entries by checking each noun for metadata + let metadataCount = 0 + const nouns = this.index.getNouns() + for (const [id] of nouns.entries()) { + try { + const metadata = await this.storage!.getMetadata(id) + if (metadata !== null && metadata !== undefined) { + metadataCount++ + } + } catch (error) { + // Ignore errors when checking individual metadata entries + // This could happen if metadata is corrupted or missing + } + } + + // Get HNSW index size + const hnswIndexSize = this.index.size() + + return { + nounCount, + verbCount, + metadataCount, + hnswIndexSize + } + } catch (error) { + console.error('Failed to get statistics:', error) + throw new Error(`Failed to get statistics: ${error}`) + } + } + /** * Check if the database is in read-only mode * @returns True if the database is in read-only mode, false otherwise