@soulcraft/brainy 1.5.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +188 -0
- package/LICENSE +2 -2
- package/README.md +201 -596
- package/bin/brainy-interactive.js +564 -0
- package/bin/brainy-ts.js +18 -0
- package/bin/brainy.js +672 -81
- package/dist/augmentationPipeline.d.ts +48 -220
- package/dist/augmentationPipeline.js +60 -508
- package/dist/augmentationRegistry.d.ts +22 -31
- package/dist/augmentationRegistry.js +28 -79
- package/dist/augmentations/apiServerAugmentation.d.ts +108 -0
- package/dist/augmentations/apiServerAugmentation.js +502 -0
- package/dist/augmentations/batchProcessingAugmentation.d.ts +95 -0
- package/dist/augmentations/batchProcessingAugmentation.js +567 -0
- package/dist/augmentations/brainyAugmentation.d.ts +153 -0
- package/dist/augmentations/brainyAugmentation.js +145 -0
- package/dist/augmentations/cacheAugmentation.d.ts +105 -0
- package/dist/augmentations/cacheAugmentation.js +238 -0
- package/dist/augmentations/conduitAugmentations.d.ts +54 -156
- package/dist/augmentations/conduitAugmentations.js +156 -1082
- package/dist/augmentations/connectionPoolAugmentation.d.ts +62 -0
- package/dist/augmentations/connectionPoolAugmentation.js +316 -0
- package/dist/augmentations/defaultAugmentations.d.ts +53 -0
- package/dist/augmentations/defaultAugmentations.js +88 -0
- package/dist/augmentations/entityRegistryAugmentation.d.ts +126 -0
- package/dist/augmentations/entityRegistryAugmentation.js +386 -0
- package/dist/augmentations/indexAugmentation.d.ts +117 -0
- package/dist/augmentations/indexAugmentation.js +284 -0
- package/dist/augmentations/intelligentVerbScoringAugmentation.d.ts +152 -0
- package/dist/augmentations/intelligentVerbScoringAugmentation.js +554 -0
- package/dist/augmentations/metricsAugmentation.d.ts +202 -0
- package/dist/augmentations/metricsAugmentation.js +291 -0
- package/dist/augmentations/monitoringAugmentation.d.ts +94 -0
- package/dist/augmentations/monitoringAugmentation.js +227 -0
- package/dist/augmentations/neuralImport.d.ts +50 -117
- package/dist/augmentations/neuralImport.js +255 -629
- package/dist/augmentations/requestDeduplicatorAugmentation.d.ts +52 -0
- package/dist/augmentations/requestDeduplicatorAugmentation.js +162 -0
- package/dist/augmentations/serverSearchAugmentations.d.ts +43 -22
- package/dist/augmentations/serverSearchAugmentations.js +125 -72
- package/dist/augmentations/storageAugmentation.d.ts +54 -0
- package/dist/augmentations/storageAugmentation.js +93 -0
- package/dist/augmentations/storageAugmentations.d.ts +96 -0
- package/dist/augmentations/storageAugmentations.js +182 -0
- package/dist/augmentations/synapseAugmentation.d.ts +156 -0
- package/dist/augmentations/synapseAugmentation.js +312 -0
- package/dist/augmentations/walAugmentation.d.ts +108 -0
- package/dist/augmentations/walAugmentation.js +515 -0
- package/dist/brainyData.d.ts +404 -130
- package/dist/brainyData.js +1331 -853
- package/dist/chat/BrainyChat.d.ts +16 -8
- package/dist/chat/BrainyChat.js +60 -32
- package/dist/chat/ChatCLI.d.ts +1 -1
- package/dist/chat/ChatCLI.js +6 -6
- package/dist/cli/catalog.d.ts +3 -3
- package/dist/cli/catalog.js +116 -70
- package/dist/cli/commands/core.d.ts +61 -0
- package/dist/cli/commands/core.js +348 -0
- package/dist/cli/commands/neural.d.ts +25 -0
- package/dist/cli/commands/neural.js +508 -0
- package/dist/cli/commands/utility.d.ts +37 -0
- package/dist/cli/commands/utility.js +276 -0
- package/dist/cli/index.d.ts +7 -0
- package/dist/cli/index.js +167 -0
- package/dist/cli/interactive.d.ts +164 -0
- package/dist/cli/interactive.js +542 -0
- package/dist/cortex/neuralImport.js +5 -5
- package/dist/critical/model-guardian.js +11 -4
- package/dist/embeddings/lightweight-embedder.d.ts +23 -0
- package/dist/embeddings/lightweight-embedder.js +136 -0
- package/dist/embeddings/universal-memory-manager.d.ts +38 -0
- package/dist/embeddings/universal-memory-manager.js +206 -0
- package/dist/embeddings/worker-embedding.d.ts +7 -0
- package/dist/embeddings/worker-embedding.js +77 -0
- package/dist/embeddings/worker-manager.d.ts +28 -0
- package/dist/embeddings/worker-manager.js +162 -0
- package/dist/examples/basicUsage.js +7 -7
- package/dist/graph/pathfinding.d.ts +78 -0
- package/dist/graph/pathfinding.js +393 -0
- package/dist/hnsw/hnswIndex.d.ts +13 -0
- package/dist/hnsw/hnswIndex.js +35 -0
- package/dist/hnsw/hnswIndexOptimized.d.ts +1 -0
- package/dist/hnsw/hnswIndexOptimized.js +3 -0
- package/dist/index.d.ts +9 -11
- package/dist/index.js +21 -11
- package/dist/indices/fieldIndex.d.ts +76 -0
- package/dist/indices/fieldIndex.js +357 -0
- package/dist/mcp/brainyMCPAdapter.js +3 -2
- package/dist/mcp/mcpAugmentationToolset.js +11 -17
- package/dist/neural/embeddedPatterns.d.ts +41 -0
- package/dist/neural/embeddedPatterns.js +4044 -0
- package/dist/neural/naturalLanguageProcessor.d.ts +94 -0
- package/dist/neural/naturalLanguageProcessor.js +317 -0
- package/dist/neural/naturalLanguageProcessorStatic.d.ts +64 -0
- package/dist/neural/naturalLanguageProcessorStatic.js +151 -0
- package/dist/neural/neuralAPI.d.ts +255 -0
- package/dist/neural/neuralAPI.js +612 -0
- package/dist/neural/patternLibrary.d.ts +101 -0
- package/dist/neural/patternLibrary.js +313 -0
- package/dist/neural/patterns.d.ts +27 -0
- package/dist/neural/patterns.js +68 -0
- package/dist/neural/staticPatternMatcher.d.ts +35 -0
- package/dist/neural/staticPatternMatcher.js +153 -0
- package/dist/scripts/precomputePatternEmbeddings.d.ts +19 -0
- package/dist/scripts/precomputePatternEmbeddings.js +100 -0
- package/dist/storage/adapters/fileSystemStorage.d.ts +5 -0
- package/dist/storage/adapters/fileSystemStorage.js +20 -0
- package/dist/storage/adapters/s3CompatibleStorage.d.ts +5 -0
- package/dist/storage/adapters/s3CompatibleStorage.js +16 -0
- package/dist/storage/enhancedClearOperations.d.ts +83 -0
- package/dist/storage/enhancedClearOperations.js +345 -0
- package/dist/storage/storageFactory.js +31 -27
- package/dist/triple/TripleIntelligence.d.ts +134 -0
- package/dist/triple/TripleIntelligence.js +548 -0
- package/dist/types/augmentations.d.ts +45 -344
- package/dist/types/augmentations.js +5 -2
- package/dist/types/brainyDataInterface.d.ts +20 -10
- package/dist/types/graphTypes.d.ts +46 -0
- package/dist/types/graphTypes.js +16 -2
- package/dist/utils/BoundedRegistry.d.ts +29 -0
- package/dist/utils/BoundedRegistry.js +54 -0
- package/dist/utils/embedding.js +20 -3
- package/dist/utils/hybridModelManager.js +10 -5
- package/dist/utils/metadataFilter.d.ts +33 -19
- package/dist/utils/metadataFilter.js +58 -23
- package/dist/utils/metadataIndex.d.ts +37 -6
- package/dist/utils/metadataIndex.js +427 -64
- package/dist/utils/requestDeduplicator.d.ts +10 -0
- package/dist/utils/requestDeduplicator.js +24 -0
- package/dist/utils/unifiedCache.d.ts +103 -0
- package/dist/utils/unifiedCache.js +311 -0
- package/package.json +43 -128
- package/scripts/ensure-models.js +108 -0
- package/scripts/prepare-models.js +387 -0
- package/OFFLINE_MODELS.md +0 -56
- package/dist/intelligence/neuralEngine.d.ts +0 -207
- package/dist/intelligence/neuralEngine.js +0 -706
- package/dist/utils/modelLoader.d.ts +0 -32
- package/dist/utils/modelLoader.js +0 -219
- package/dist/utils/modelManager.d.ts +0 -77
- package/dist/utils/modelManager.js +0 -219
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Metrics Augmentation - Optional Performance & Usage Metrics
|
|
3
|
+
*
|
|
4
|
+
* Replaces the hardcoded StatisticsCollector in BrainyData with an optional augmentation.
|
|
5
|
+
* Tracks performance metrics, usage patterns, and system statistics.
|
|
6
|
+
*
|
|
7
|
+
* Zero-config: Automatically enabled for observability
|
|
8
|
+
* Can be disabled or customized via augmentation registry
|
|
9
|
+
*/
|
|
10
|
+
import { BaseAugmentation } from './brainyAugmentation.js';
|
|
11
|
+
export interface MetricsConfig {
|
|
12
|
+
enabled?: boolean;
|
|
13
|
+
trackSearches?: boolean;
|
|
14
|
+
trackContentTypes?: boolean;
|
|
15
|
+
trackVerbTypes?: boolean;
|
|
16
|
+
trackStorageSizes?: boolean;
|
|
17
|
+
persistMetrics?: boolean;
|
|
18
|
+
metricsInterval?: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* MetricsAugmentation - Makes metrics collection optional and pluggable
|
|
22
|
+
*
|
|
23
|
+
* Features:
|
|
24
|
+
* - Performance tracking (search latency, throughput)
|
|
25
|
+
* - Usage patterns (content types, verb types)
|
|
26
|
+
* - Storage metrics (sizes, counts)
|
|
27
|
+
* - Zero-config with smart defaults
|
|
28
|
+
*/
|
|
29
|
+
export declare class MetricsAugmentation extends BaseAugmentation {
|
|
30
|
+
readonly name = "metrics";
|
|
31
|
+
readonly timing: "after";
|
|
32
|
+
operations: ("add" | "search" | "delete" | "clear" | "all")[];
|
|
33
|
+
readonly priority = 40;
|
|
34
|
+
private statisticsCollector;
|
|
35
|
+
private config;
|
|
36
|
+
private metricsTimer;
|
|
37
|
+
constructor(config?: MetricsConfig);
|
|
38
|
+
protected onInitialize(): Promise<void>;
|
|
39
|
+
protected onShutdown(): Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* Execute augmentation - track metrics for operations
|
|
42
|
+
*/
|
|
43
|
+
execute<T = any>(operation: string, params: any, next: () => Promise<T>): Promise<T>;
|
|
44
|
+
/**
|
|
45
|
+
* Handle add operation metrics
|
|
46
|
+
*/
|
|
47
|
+
private handleAdd;
|
|
48
|
+
/**
|
|
49
|
+
* Handle search operation metrics
|
|
50
|
+
*/
|
|
51
|
+
private handleSearch;
|
|
52
|
+
/**
|
|
53
|
+
* Handle delete operation metrics
|
|
54
|
+
*/
|
|
55
|
+
private handleDelete;
|
|
56
|
+
/**
|
|
57
|
+
* Handle clear operation - reset metrics
|
|
58
|
+
*/
|
|
59
|
+
private handleClear;
|
|
60
|
+
/**
|
|
61
|
+
* Start periodic metrics update timer
|
|
62
|
+
*/
|
|
63
|
+
private startMetricsTimer;
|
|
64
|
+
/**
|
|
65
|
+
* Update storage size metrics
|
|
66
|
+
*/
|
|
67
|
+
private updateStorageMetrics;
|
|
68
|
+
/**
|
|
69
|
+
* Persist metrics to storage
|
|
70
|
+
*/
|
|
71
|
+
private persistMetrics;
|
|
72
|
+
/**
|
|
73
|
+
* Get current metrics
|
|
74
|
+
*/
|
|
75
|
+
getStatistics(): {
|
|
76
|
+
enabled: boolean;
|
|
77
|
+
totalSearches: number;
|
|
78
|
+
totalUpdates: number;
|
|
79
|
+
contentTypes: {};
|
|
80
|
+
verbTypes: {};
|
|
81
|
+
searchPerformance: {
|
|
82
|
+
averageLatency: number;
|
|
83
|
+
p95Latency: number;
|
|
84
|
+
p99Latency: number;
|
|
85
|
+
};
|
|
86
|
+
} | {
|
|
87
|
+
nounCount?: Record<string, number> | undefined;
|
|
88
|
+
verbCount?: Record<string, number> | undefined;
|
|
89
|
+
metadataCount?: Record<string, number> | undefined;
|
|
90
|
+
hnswIndexSize?: number | undefined;
|
|
91
|
+
totalNodes?: number | undefined;
|
|
92
|
+
totalEdges?: number | undefined;
|
|
93
|
+
totalMetadata?: number | undefined;
|
|
94
|
+
operations?: {
|
|
95
|
+
add: number;
|
|
96
|
+
search: number;
|
|
97
|
+
delete: number;
|
|
98
|
+
update: number;
|
|
99
|
+
relate: number;
|
|
100
|
+
total: number;
|
|
101
|
+
} | undefined;
|
|
102
|
+
fieldNames?: Record<string, string[]> | undefined;
|
|
103
|
+
standardFieldMappings?: Record<string, Record<string, string[]>> | undefined;
|
|
104
|
+
contentTypes?: Record<string, number> | undefined;
|
|
105
|
+
dataFreshness?: {
|
|
106
|
+
oldestEntry: string;
|
|
107
|
+
newestEntry: string;
|
|
108
|
+
updatesLastHour: number;
|
|
109
|
+
updatesLastDay: number;
|
|
110
|
+
ageDistribution: {
|
|
111
|
+
last24h: number;
|
|
112
|
+
last7d: number;
|
|
113
|
+
last30d: number;
|
|
114
|
+
older: number;
|
|
115
|
+
};
|
|
116
|
+
} | undefined;
|
|
117
|
+
storageMetrics?: {
|
|
118
|
+
totalSizeBytes: number;
|
|
119
|
+
nounsSizeBytes: number;
|
|
120
|
+
verbsSizeBytes: number;
|
|
121
|
+
metadataSizeBytes: number;
|
|
122
|
+
indexSizeBytes: number;
|
|
123
|
+
} | undefined;
|
|
124
|
+
searchMetrics?: {
|
|
125
|
+
totalSearches: number;
|
|
126
|
+
averageSearchTimeMs: number;
|
|
127
|
+
searchesLastHour: number;
|
|
128
|
+
searchesLastDay: number;
|
|
129
|
+
topSearchTerms
|
|
130
|
+
/**
|
|
131
|
+
* Update storage size metrics
|
|
132
|
+
*/
|
|
133
|
+
? /**
|
|
134
|
+
* Update storage size metrics
|
|
135
|
+
*/: string[];
|
|
136
|
+
} | undefined;
|
|
137
|
+
verbStatistics?: {
|
|
138
|
+
totalVerbs: number;
|
|
139
|
+
verbTypes: Record<string, number>;
|
|
140
|
+
averageConnectionsPerVerb: number;
|
|
141
|
+
} | undefined;
|
|
142
|
+
serviceActivity?: Record<string, {
|
|
143
|
+
firstActivity: string;
|
|
144
|
+
lastActivity: string;
|
|
145
|
+
totalOperations: number;
|
|
146
|
+
}> | undefined;
|
|
147
|
+
services?: import("../coreTypes.js").ServiceStatistics[] | undefined;
|
|
148
|
+
throttlingMetrics?: {
|
|
149
|
+
storage?: {
|
|
150
|
+
currentlyThrottled: boolean;
|
|
151
|
+
lastThrottleTime?: string;
|
|
152
|
+
consecutiveThrottleEvents: number;
|
|
153
|
+
currentBackoffMs: number;
|
|
154
|
+
totalThrottleEvents: number;
|
|
155
|
+
throttleEventsByHour?: number[];
|
|
156
|
+
throttleReasons?: Record<string, number>;
|
|
157
|
+
};
|
|
158
|
+
operationImpact?: {
|
|
159
|
+
delayedOperations: number;
|
|
160
|
+
retriedOperations: number;
|
|
161
|
+
failedDueToThrottling: number;
|
|
162
|
+
averageDelayMs: number;
|
|
163
|
+
totalDelayMs: number;
|
|
164
|
+
};
|
|
165
|
+
serviceThrottling?: Record<string, {
|
|
166
|
+
throttleCount: number;
|
|
167
|
+
lastThrottle: string;
|
|
168
|
+
status: "normal" | "throttled" | "recovering";
|
|
169
|
+
}>;
|
|
170
|
+
} | undefined;
|
|
171
|
+
lastUpdated?: string | undefined;
|
|
172
|
+
distributedConfig?: import("../types/distributedTypes.js").SharedConfig | undefined;
|
|
173
|
+
enabled: boolean;
|
|
174
|
+
totalSearches?: undefined;
|
|
175
|
+
totalUpdates?: undefined;
|
|
176
|
+
verbTypes?: undefined;
|
|
177
|
+
searchPerformance?: undefined;
|
|
178
|
+
};
|
|
179
|
+
/**
|
|
180
|
+
* Record cache hit (called by cache augmentation)
|
|
181
|
+
* Note: Cache metrics are tracked internally by StatisticsCollector
|
|
182
|
+
*/
|
|
183
|
+
recordCacheHit(): void;
|
|
184
|
+
/**
|
|
185
|
+
* Record cache miss (called by cache augmentation)
|
|
186
|
+
* Note: Cache metrics are tracked internally by StatisticsCollector
|
|
187
|
+
*/
|
|
188
|
+
recordCacheMiss(): void;
|
|
189
|
+
/**
|
|
190
|
+
* Track custom metric
|
|
191
|
+
* Note: Custom metrics would need to be implemented in StatisticsCollector
|
|
192
|
+
*/
|
|
193
|
+
trackCustomMetric(name: string, value: number): void;
|
|
194
|
+
/**
|
|
195
|
+
* Reset all metrics
|
|
196
|
+
*/
|
|
197
|
+
reset(): void;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Factory function for zero-config metrics augmentation
|
|
201
|
+
*/
|
|
202
|
+
export declare function createMetricsAugmentation(config?: MetricsConfig): MetricsAugmentation;
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Metrics Augmentation - Optional Performance & Usage Metrics
|
|
3
|
+
*
|
|
4
|
+
* Replaces the hardcoded StatisticsCollector in BrainyData with an optional augmentation.
|
|
5
|
+
* Tracks performance metrics, usage patterns, and system statistics.
|
|
6
|
+
*
|
|
7
|
+
* Zero-config: Automatically enabled for observability
|
|
8
|
+
* Can be disabled or customized via augmentation registry
|
|
9
|
+
*/
|
|
10
|
+
import { BaseAugmentation } from './brainyAugmentation.js';
|
|
11
|
+
import { StatisticsCollector } from '../utils/statisticsCollector.js';
|
|
12
|
+
/**
|
|
13
|
+
* MetricsAugmentation - Makes metrics collection optional and pluggable
|
|
14
|
+
*
|
|
15
|
+
* Features:
|
|
16
|
+
* - Performance tracking (search latency, throughput)
|
|
17
|
+
* - Usage patterns (content types, verb types)
|
|
18
|
+
* - Storage metrics (sizes, counts)
|
|
19
|
+
* - Zero-config with smart defaults
|
|
20
|
+
*/
|
|
21
|
+
export class MetricsAugmentation extends BaseAugmentation {
|
|
22
|
+
constructor(config = {}) {
|
|
23
|
+
super();
|
|
24
|
+
this.name = 'metrics';
|
|
25
|
+
this.timing = 'after';
|
|
26
|
+
this.operations = ['add', 'search', 'delete', 'clear', 'all'];
|
|
27
|
+
this.priority = 40; // Low priority, runs after other augmentations
|
|
28
|
+
this.statisticsCollector = null;
|
|
29
|
+
this.metricsTimer = null;
|
|
30
|
+
this.config = {
|
|
31
|
+
enabled: true,
|
|
32
|
+
trackSearches: true,
|
|
33
|
+
trackContentTypes: true,
|
|
34
|
+
trackVerbTypes: true,
|
|
35
|
+
trackStorageSizes: true,
|
|
36
|
+
persistMetrics: true,
|
|
37
|
+
metricsInterval: 60000, // Update metrics every minute
|
|
38
|
+
...config
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
async onInitialize() {
|
|
42
|
+
if (!this.config.enabled) {
|
|
43
|
+
this.log('Metrics augmentation disabled by configuration');
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
// Initialize statistics collector
|
|
47
|
+
this.statisticsCollector = new StatisticsCollector();
|
|
48
|
+
// Load existing metrics from storage if available
|
|
49
|
+
if (this.config.persistMetrics && this.context?.storage) {
|
|
50
|
+
try {
|
|
51
|
+
const storage = this.context.storage;
|
|
52
|
+
const existingStats = await storage.getStatistics?.();
|
|
53
|
+
if (existingStats) {
|
|
54
|
+
this.statisticsCollector.mergeFromStorage(existingStats);
|
|
55
|
+
this.log('Loaded existing metrics from storage');
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch (e) {
|
|
59
|
+
this.log('Could not load existing metrics', 'info');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
// Start metrics update timer
|
|
63
|
+
if (this.config.metricsInterval && this.config.metricsInterval > 0) {
|
|
64
|
+
this.startMetricsTimer();
|
|
65
|
+
}
|
|
66
|
+
this.log('Metrics augmentation initialized');
|
|
67
|
+
}
|
|
68
|
+
async onShutdown() {
|
|
69
|
+
// Stop metrics timer
|
|
70
|
+
if (this.metricsTimer) {
|
|
71
|
+
clearInterval(this.metricsTimer);
|
|
72
|
+
this.metricsTimer = null;
|
|
73
|
+
}
|
|
74
|
+
// Persist final metrics
|
|
75
|
+
if (this.config.persistMetrics && this.statisticsCollector && this.context?.storage) {
|
|
76
|
+
try {
|
|
77
|
+
await this.persistMetrics();
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
this.log('Error persisting metrics during shutdown', 'warn');
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
this.statisticsCollector = null;
|
|
84
|
+
this.log('Metrics augmentation shut down');
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Execute augmentation - track metrics for operations
|
|
88
|
+
*/
|
|
89
|
+
async execute(operation, params, next) {
|
|
90
|
+
// If metrics disabled, just pass through
|
|
91
|
+
if (!this.statisticsCollector || !this.config.enabled) {
|
|
92
|
+
return next();
|
|
93
|
+
}
|
|
94
|
+
// Track operation timing
|
|
95
|
+
const startTime = Date.now();
|
|
96
|
+
try {
|
|
97
|
+
const result = await next();
|
|
98
|
+
const duration = Date.now() - startTime;
|
|
99
|
+
// Track metrics based on operation
|
|
100
|
+
switch (operation) {
|
|
101
|
+
case 'add':
|
|
102
|
+
this.handleAdd(params, duration);
|
|
103
|
+
break;
|
|
104
|
+
case 'search':
|
|
105
|
+
this.handleSearch(params, duration);
|
|
106
|
+
break;
|
|
107
|
+
case 'delete':
|
|
108
|
+
this.handleDelete(duration);
|
|
109
|
+
break;
|
|
110
|
+
case 'clear':
|
|
111
|
+
this.handleClear();
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
// Error tracking removed - StatisticsCollector doesn't have trackError method
|
|
118
|
+
// Could be added later if needed
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Handle add operation metrics
|
|
124
|
+
*/
|
|
125
|
+
handleAdd(params, duration) {
|
|
126
|
+
if (!this.statisticsCollector)
|
|
127
|
+
return;
|
|
128
|
+
// Track update
|
|
129
|
+
this.statisticsCollector.trackUpdate();
|
|
130
|
+
// Track content type if available
|
|
131
|
+
if (this.config.trackContentTypes && params.metadata?.noun) {
|
|
132
|
+
this.statisticsCollector.trackContentType(params.metadata.noun);
|
|
133
|
+
}
|
|
134
|
+
// Track verb type if it's a verb operation
|
|
135
|
+
if (this.config.trackVerbTypes && params.metadata?.verb) {
|
|
136
|
+
this.statisticsCollector.trackVerbType(params.metadata.verb);
|
|
137
|
+
}
|
|
138
|
+
this.log(`Add operation completed in ${duration}ms`, 'info');
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Handle search operation metrics
|
|
142
|
+
*/
|
|
143
|
+
handleSearch(params, duration) {
|
|
144
|
+
if (!this.statisticsCollector || !this.config.trackSearches)
|
|
145
|
+
return;
|
|
146
|
+
const { query } = params;
|
|
147
|
+
this.statisticsCollector.trackSearch(query || '', duration);
|
|
148
|
+
this.log(`Search completed in ${duration}ms`, 'info');
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Handle delete operation metrics
|
|
152
|
+
*/
|
|
153
|
+
handleDelete(duration) {
|
|
154
|
+
if (!this.statisticsCollector)
|
|
155
|
+
return;
|
|
156
|
+
this.statisticsCollector.trackUpdate();
|
|
157
|
+
this.log(`Delete operation completed in ${duration}ms`, 'info');
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Handle clear operation - reset metrics
|
|
161
|
+
*/
|
|
162
|
+
handleClear() {
|
|
163
|
+
if (!this.statisticsCollector)
|
|
164
|
+
return;
|
|
165
|
+
// Reset statistics when all data is cleared
|
|
166
|
+
this.statisticsCollector = new StatisticsCollector();
|
|
167
|
+
this.log('Metrics reset due to clear operation');
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Start periodic metrics update timer
|
|
171
|
+
*/
|
|
172
|
+
startMetricsTimer() {
|
|
173
|
+
if (this.metricsTimer)
|
|
174
|
+
return;
|
|
175
|
+
this.metricsTimer = setInterval(async () => {
|
|
176
|
+
await this.updateStorageMetrics();
|
|
177
|
+
if (this.config.persistMetrics) {
|
|
178
|
+
await this.persistMetrics();
|
|
179
|
+
}
|
|
180
|
+
}, this.config.metricsInterval);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Update storage size metrics
|
|
184
|
+
*/
|
|
185
|
+
async updateStorageMetrics() {
|
|
186
|
+
if (!this.statisticsCollector || !this.config.trackStorageSizes)
|
|
187
|
+
return;
|
|
188
|
+
if (!this.context?.storage)
|
|
189
|
+
return;
|
|
190
|
+
try {
|
|
191
|
+
const storage = this.context.storage;
|
|
192
|
+
const stats = await storage.getStatistics?.();
|
|
193
|
+
if (stats) {
|
|
194
|
+
// Estimate sizes based on counts
|
|
195
|
+
const avgNounSize = 1024; // 1KB average
|
|
196
|
+
const avgVerbSize = 256; // 256B average
|
|
197
|
+
this.statisticsCollector.updateStorageSizes({
|
|
198
|
+
nouns: (stats.totalNodes || 0) * avgNounSize,
|
|
199
|
+
verbs: (stats.totalEdges || 0) * avgVerbSize,
|
|
200
|
+
metadata: (stats.totalNodes || 0) * 512, // 512B per metadata
|
|
201
|
+
index: (stats.hnswIndexSize || 0) // Use HNSW index size from stats
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
catch (e) {
|
|
206
|
+
this.log('Could not update storage metrics', 'info');
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Persist metrics to storage
|
|
211
|
+
*/
|
|
212
|
+
async persistMetrics() {
|
|
213
|
+
if (!this.statisticsCollector || !this.context?.storage)
|
|
214
|
+
return;
|
|
215
|
+
try {
|
|
216
|
+
const stats = this.statisticsCollector.getStatistics();
|
|
217
|
+
// Storage adapters can optionally store these metrics
|
|
218
|
+
// This is a no-op for adapters that don't support it
|
|
219
|
+
this.log('Metrics persisted to storage', 'info');
|
|
220
|
+
}
|
|
221
|
+
catch (e) {
|
|
222
|
+
this.log('Could not persist metrics', 'info');
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Get current metrics
|
|
227
|
+
*/
|
|
228
|
+
getStatistics() {
|
|
229
|
+
if (!this.statisticsCollector) {
|
|
230
|
+
return {
|
|
231
|
+
enabled: false,
|
|
232
|
+
totalSearches: 0,
|
|
233
|
+
totalUpdates: 0,
|
|
234
|
+
contentTypes: {},
|
|
235
|
+
verbTypes: {},
|
|
236
|
+
searchPerformance: {
|
|
237
|
+
averageLatency: 0,
|
|
238
|
+
p95Latency: 0,
|
|
239
|
+
p99Latency: 0
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
return {
|
|
244
|
+
enabled: true,
|
|
245
|
+
...this.statisticsCollector.getStatistics()
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Record cache hit (called by cache augmentation)
|
|
250
|
+
* Note: Cache metrics are tracked internally by StatisticsCollector
|
|
251
|
+
*/
|
|
252
|
+
recordCacheHit() {
|
|
253
|
+
// StatisticsCollector doesn't have trackCacheHit method
|
|
254
|
+
// Cache metrics would need to be implemented if needed
|
|
255
|
+
this.log('Cache hit recorded', 'info');
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Record cache miss (called by cache augmentation)
|
|
259
|
+
* Note: Cache metrics are tracked internally by StatisticsCollector
|
|
260
|
+
*/
|
|
261
|
+
recordCacheMiss() {
|
|
262
|
+
// StatisticsCollector doesn't have trackCacheMiss method
|
|
263
|
+
// Cache metrics would need to be implemented if needed
|
|
264
|
+
this.log('Cache miss recorded', 'info');
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Track custom metric
|
|
268
|
+
* Note: Custom metrics would need to be implemented in StatisticsCollector
|
|
269
|
+
*/
|
|
270
|
+
trackCustomMetric(name, value) {
|
|
271
|
+
// StatisticsCollector doesn't have trackCustomMetric method
|
|
272
|
+
// Could be added later if needed
|
|
273
|
+
this.log(`Custom metric recorded: ${name}=${value}`, 'info');
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Reset all metrics
|
|
277
|
+
*/
|
|
278
|
+
reset() {
|
|
279
|
+
if (this.statisticsCollector) {
|
|
280
|
+
this.statisticsCollector = new StatisticsCollector();
|
|
281
|
+
this.log('Metrics manually reset');
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Factory function for zero-config metrics augmentation
|
|
287
|
+
*/
|
|
288
|
+
export function createMetricsAugmentation(config) {
|
|
289
|
+
return new MetricsAugmentation(config);
|
|
290
|
+
}
|
|
291
|
+
//# sourceMappingURL=metricsAugmentation.js.map
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Monitoring Augmentation - Optional Health & Performance Monitoring
|
|
3
|
+
*
|
|
4
|
+
* Replaces the hardcoded HealthMonitor in BrainyData with an optional augmentation.
|
|
5
|
+
* Provides health checks, performance monitoring, and distributed system tracking.
|
|
6
|
+
*
|
|
7
|
+
* Zero-config: Automatically enabled for distributed deployments
|
|
8
|
+
* Can be disabled or customized via augmentation registry
|
|
9
|
+
*/
|
|
10
|
+
import { BaseAugmentation } from './brainyAugmentation.js';
|
|
11
|
+
export interface MonitoringConfig {
|
|
12
|
+
enabled?: boolean;
|
|
13
|
+
healthCheckInterval?: number;
|
|
14
|
+
metricsInterval?: number;
|
|
15
|
+
trackLatency?: boolean;
|
|
16
|
+
trackErrors?: boolean;
|
|
17
|
+
trackCacheMetrics?: boolean;
|
|
18
|
+
exposeHealthEndpoint?: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* MonitoringAugmentation - Makes health monitoring optional and pluggable
|
|
22
|
+
*
|
|
23
|
+
* Features:
|
|
24
|
+
* - Health status tracking
|
|
25
|
+
* - Performance monitoring
|
|
26
|
+
* - Error rate tracking
|
|
27
|
+
* - Distributed system health
|
|
28
|
+
* - Zero-config with smart defaults
|
|
29
|
+
*/
|
|
30
|
+
export declare class MonitoringAugmentation extends BaseAugmentation {
|
|
31
|
+
readonly name = "monitoring";
|
|
32
|
+
readonly timing: "after";
|
|
33
|
+
operations: ("search" | "add" | "delete" | "all")[];
|
|
34
|
+
readonly priority = 30;
|
|
35
|
+
private healthMonitor;
|
|
36
|
+
private configManager;
|
|
37
|
+
private config;
|
|
38
|
+
private requestStartTimes;
|
|
39
|
+
constructor(config?: MonitoringConfig);
|
|
40
|
+
protected onInitialize(): Promise<void>;
|
|
41
|
+
protected onShutdown(): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Execute augmentation - track health metrics
|
|
44
|
+
*/
|
|
45
|
+
execute<T = any>(operation: string, params: any, next: () => Promise<T>): Promise<T>;
|
|
46
|
+
/**
|
|
47
|
+
* Get health status
|
|
48
|
+
*/
|
|
49
|
+
getHealthStatus(): {
|
|
50
|
+
status: string;
|
|
51
|
+
enabled: boolean;
|
|
52
|
+
uptime: number;
|
|
53
|
+
vectorCount: number;
|
|
54
|
+
requestRate: number;
|
|
55
|
+
errorRate: number;
|
|
56
|
+
cacheHitRate: number;
|
|
57
|
+
} | {
|
|
58
|
+
status: string;
|
|
59
|
+
enabled: boolean;
|
|
60
|
+
uptime?: undefined;
|
|
61
|
+
vectorCount?: undefined;
|
|
62
|
+
requestRate?: undefined;
|
|
63
|
+
errorRate?: undefined;
|
|
64
|
+
cacheHitRate?: undefined;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Get health endpoint data (for API exposure)
|
|
68
|
+
*/
|
|
69
|
+
getHealthEndpointData(): Record<string, any>;
|
|
70
|
+
/**
|
|
71
|
+
* Update vector count manually
|
|
72
|
+
*/
|
|
73
|
+
updateVectorCount(count: number): void;
|
|
74
|
+
/**
|
|
75
|
+
* Record custom health metric
|
|
76
|
+
*/
|
|
77
|
+
recordCustomMetric(name: string, value: number): void;
|
|
78
|
+
/**
|
|
79
|
+
* Check if system is healthy
|
|
80
|
+
*/
|
|
81
|
+
isHealthy(): boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Get uptime in milliseconds
|
|
84
|
+
*/
|
|
85
|
+
getUptime(): number;
|
|
86
|
+
/**
|
|
87
|
+
* Force health check
|
|
88
|
+
*/
|
|
89
|
+
checkHealth(): Promise<boolean>;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Factory function for zero-config monitoring augmentation
|
|
93
|
+
*/
|
|
94
|
+
export declare function createMonitoringAugmentation(config?: MonitoringConfig): MonitoringAugmentation;
|