**feat(core, storage, tests): add service-level statistics tracking and storage adapter enhancements**

- **Core**: Enhanced `getStatistics` function to support `service` and `service[]` filters, enabling statistics breakdown by service. Modified return structure to include `serviceBreakdown` for detailed insights.
- **Storage**: Implemented a new `BaseStorageAdapter` abstract class to centralize statistics-related functionality, such as incrementing/decrementing counters and updating HNSW index size. Refactored all storage adapters (`FileSystemStorage`, `S3CompatibleStorage`, `MemoryStorage`, `OPFSStorage`) to extend `BaseStorageAdapter`, ensuring consistent statistics tracking.
- **Tests**: Added new test cases in `statistics.test.ts` to validate service-level statistics tracking, breakdown accuracy, and multi-service filtering.

**Purpose**: Improve insight into data trends by tracking service-specific usage in statistics. Enhance maintainability and consistency through storage adapter centralization and robust testing.
This commit is contained in:
David Snelling 2025-07-24 11:35:52 -07:00
parent 3c95ee011d
commit 85e5a6bfb9
8 changed files with 535 additions and 12 deletions

View file

@ -9,21 +9,34 @@ import { BrainyData } from '../brainyData.js'
* This function provides access to statistics at the root level of the library
*
* @param instance A BrainyData instance to get statistics from
* @param options Additional options for retrieving statistics
* @returns Object containing counts of nouns, verbs, metadata entries, and HNSW index size
* @throws Error if the instance is not provided or if statistics retrieval fails
*/
export async function getStatistics(instance: BrainyData): Promise<{
export async function getStatistics(
instance: BrainyData,
options: {
service?: string | string[] // Filter statistics by service(s)
} = {}
): Promise<{
nounCount: number
verbCount: number
metadataCount: number
hnswIndexSize: number
serviceBreakdown?: {
[service: string]: {
nounCount: number
verbCount: number
metadataCount: number
}
}
}> {
if (!instance) {
throw new Error('BrainyData instance must be provided to getStatistics')
}
try {
return await instance.getStatistics()
return await instance.getStatistics(options)
} catch (error) {
console.error('Failed to get statistics:', error)
throw new Error(`Failed to get statistics: ${error}`)