1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//! Data types for advanced TDB diagnostics.
//!
//! This module defines the report and analysis structures produced by the
//! [`AdvancedDiagnosticEngine`], along with the engine's internal tracking
//! structures:
//! - Public report types ([`AdvancedDiagnosticReport`] and its component analyses)
//! - Trend/forecast types ([`PredictiveHealthIndicators`], [`CapacityForecast`], etc.)
//! - The [`AdvancedDiagnosticEngine`] struct and its internal trackers
//!
//! [`AdvancedDiagnosticEngine`]: crate::advanced_diagnostics_engine::AdvancedDiagnosticEngine
use crate::diagnostics::Severity;
use crate::storage::BufferPoolStats;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::time::{Duration, SystemTime};
/// Advanced diagnostic report with trend analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdvancedDiagnosticReport {
/// Timestamp
pub timestamp: SystemTime,
/// Query performance analysis
pub query_analysis: QueryPerformanceAnalysis,
/// Transaction pattern analysis
pub transaction_analysis: TransactionPatternAnalysis,
/// Fragmentation analysis
pub fragmentation_analysis: FragmentationAnalysis,
/// Index usage statistics
pub index_usage: IndexUsageStatistics,
/// Predictive health indicators
pub predictive_health: PredictiveHealthIndicators,
/// Auto-tuning recommendations
pub tuning_recommendations: Vec<TuningRecommendation>,
/// Anomaly detections
pub anomalies: Vec<AnomalyDetection>,
/// Capacity planning forecast
pub capacity_forecast: CapacityForecast,
}
/// Query performance analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryPerformanceAnalysis {
/// Total queries analyzed
pub total_queries: u64,
/// Average query execution time
pub avg_execution_time: Duration,
/// Median query execution time
pub median_execution_time: Duration,
/// P95 query execution time
pub p95_execution_time: Duration,
/// P99 query execution time
pub p99_execution_time: Duration,
/// Slow query count (above threshold)
pub slow_query_count: u64,
/// Query patterns detected
pub query_patterns: Vec<QueryPattern>,
/// Optimization opportunities
pub optimization_opportunities: Vec<String>,
/// Query cache hit rate
pub cache_hit_rate: f64,
}
/// Query pattern detected in workload
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryPattern {
/// Pattern signature (simplified query structure)
pub signature: String,
/// Frequency of this pattern
pub frequency: u64,
/// Average execution time for this pattern
pub avg_time: Duration,
/// Index usage for this pattern
pub indexes_used: Vec<String>,
/// Optimization potential (0.0 = none, 1.0 = high)
pub optimization_potential: f64,
}
/// Transaction pattern analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransactionPatternAnalysis {
/// Total transactions analyzed
pub total_transactions: u64,
/// Average transaction duration
pub avg_duration: Duration,
/// Median transaction duration
pub median_duration: Duration,
/// Transaction commit rate
pub commit_rate: f64,
/// Transaction abort rate
pub abort_rate: f64,
/// Conflict rate (conflicts per transaction)
pub conflict_rate: f64,
/// Deadlock rate (deadlocks per 1000 transactions)
pub deadlock_rate: f64,
/// Lock contention points
pub contention_points: Vec<ContentionPoint>,
/// Transaction size distribution
pub size_distribution: TransactionSizeDistribution,
}
/// Lock contention point
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContentionPoint {
/// Resource identifier (e.g., "SPO_INDEX", "DICTIONARY")
pub resource: String,
/// Number of contentions detected
pub contention_count: u64,
/// Average wait time
pub avg_wait_time: Duration,
/// Severity (0.0 = low, 1.0 = high)
pub severity: f64,
}
/// Transaction size distribution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransactionSizeDistribution {
/// Small transactions (< 10 operations)
pub small_pct: f64,
/// Medium transactions (10-100 operations)
pub medium_pct: f64,
/// Large transactions (> 100 operations)
pub large_pct: f64,
}
/// Storage fragmentation analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FragmentationAnalysis {
/// Overall fragmentation percentage
pub overall_fragmentation_pct: f64,
/// Dictionary fragmentation
pub dictionary_fragmentation_pct: f64,
/// Index fragmentation by type
pub index_fragmentation: HashMap<String, f64>,
/// Free space distribution
pub free_space_distribution: Vec<FreeSpaceRegion>,
/// Compaction benefit estimate (space recoverable)
pub compaction_benefit_bytes: u64,
/// Recommended compaction priority (0.0 = low, 1.0 = critical)
pub compaction_priority: f64,
}
/// Free space region in storage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FreeSpaceRegion {
/// Starting offset
pub offset: u64,
/// Size in bytes
pub size: u64,
}
/// Index usage statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexUsageStatistics {
/// Total index scans performed
pub total_scans: u64,
/// Usage by index type
pub usage_by_index: HashMap<String, IndexUsageStats>,
/// Unused indexes
pub unused_indexes: Vec<String>,
/// Overused indexes (potential bottleneck)
pub overused_indexes: Vec<String>,
/// Missing index opportunities
pub missing_index_opportunities: Vec<MissingIndexOpportunity>,
}
/// Statistics for a specific index
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexUsageStats {
/// Number of scans
pub scan_count: u64,
/// Average selectivity (rows returned / rows scanned)
pub avg_selectivity: f64,
/// Average scan time
pub avg_scan_time: Duration,
/// Index efficiency score (0.0 = inefficient, 1.0 = highly efficient)
pub efficiency_score: f64,
}
/// Opportunity to create a missing index
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MissingIndexOpportunity {
/// Suggested index name
pub suggested_index: String,
/// Query patterns that would benefit
pub benefiting_patterns: Vec<String>,
/// Estimated performance improvement (percentage)
pub estimated_improvement_pct: f64,
}
/// Predictive health indicators
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PredictiveHealthIndicators {
/// Time series of historical metrics
pub historical_metrics: HistoricalMetrics,
/// Predicted issues in next 24 hours
pub predicted_issues_24h: Vec<PredictedIssue>,
/// Predicted issues in next 7 days
pub predicted_issues_7d: Vec<PredictedIssue>,
/// Health trend (improving, stable, degrading)
pub health_trend: HealthTrend,
/// Resource exhaustion predictions
pub resource_predictions: ResourcePredictions,
}
/// Historical metrics for trend analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoricalMetrics {
/// Number of data points
pub data_points: usize,
/// Time range covered
pub time_range: Duration,
/// Query latency trend (per hour)
pub query_latency_trend: Vec<f64>,
/// Transaction throughput trend (per hour)
pub txn_throughput_trend: Vec<f64>,
/// Storage growth trend (bytes per hour)
pub storage_growth_trend: Vec<f64>,
/// Error rate trend (errors per hour)
pub error_rate_trend: Vec<f64>,
}
/// Predicted issue
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PredictedIssue {
/// Issue category
pub category: String,
/// Issue description
pub description: String,
/// Confidence level (0.0 = low, 1.0 = certain)
pub confidence: f64,
/// Estimated time until issue occurs
pub eta: Duration,
/// Severity if issue occurs
pub severity: Severity,
/// Preventive action
pub preventive_action: String,
}
/// Health trend direction
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum HealthTrend {
/// System health is improving
Improving,
/// System health is stable
Stable,
/// System health is degrading slowly
DegradingSlowly,
/// System health is degrading rapidly
DegradingRapidly,
}
/// Resource exhaustion predictions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourcePredictions {
/// Predicted storage full date
pub storage_full_eta: Option<SystemTime>,
/// Predicted memory exhaustion
pub memory_exhaustion_eta: Option<SystemTime>,
/// Predicted connection pool saturation
pub connection_saturation_eta: Option<SystemTime>,
}
/// Auto-tuning recommendation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TuningRecommendation {
/// Configuration parameter
pub parameter: String,
/// Current value
pub current_value: String,
/// Recommended value
pub recommended_value: String,
/// Rationale
pub rationale: String,
/// Expected impact
pub expected_impact: String,
/// Priority (0.0 = low, 1.0 = critical)
pub priority: f64,
}
/// Anomaly detection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnomalyDetection {
/// Metric name
pub metric: String,
/// Anomaly description
pub description: String,
/// Detected value
pub detected_value: f64,
/// Expected value range
pub expected_range: (f64, f64),
/// Deviation from normal (standard deviations)
pub deviation: f64,
/// Detection timestamp
pub detected_at: SystemTime,
/// Severity
pub severity: Severity,
}
/// Capacity planning forecast
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CapacityForecast {
/// Current storage usage (bytes)
pub current_storage_bytes: u64,
/// Predicted storage in 30 days
pub predicted_30d_bytes: u64,
/// Predicted storage in 90 days
pub predicted_90d_bytes: u64,
/// Growth rate (bytes per day)
pub growth_rate_per_day: f64,
/// Days until 80% capacity
pub days_until_80pct: Option<u32>,
/// Days until 90% capacity
pub days_until_90pct: Option<u32>,
/// Recommended actions
pub recommended_actions: Vec<String>,
}
/// Advanced diagnostic engine
pub struct AdvancedDiagnosticEngine {
/// Historical metrics buffer (circular buffer)
pub(crate) historical_buffer: VecDeque<MetricSnapshot>,
/// Maximum history size (24 hours of hourly snapshots)
pub(crate) max_history_size: usize,
/// Query performance tracker
pub(crate) query_tracker: QueryPerformanceTracker,
/// Transaction pattern tracker
pub(crate) transaction_tracker: TransactionPatternTracker,
/// Anomaly detector
pub(crate) anomaly_detector: AnomalyDetector,
}
/// Snapshot of metrics at a point in time
#[derive(Debug, Clone)]
pub(crate) struct MetricSnapshot {
pub(crate) timestamp: SystemTime,
pub(crate) query_latency: f64,
pub(crate) txn_throughput: f64,
pub(crate) storage_bytes: u64,
pub(crate) error_count: u64,
#[allow(dead_code)]
pub(crate) buffer_pool_stats: BufferPoolStats,
}
/// Query performance tracker
pub(crate) struct QueryPerformanceTracker {
/// Query execution times (most recent 1000)
pub(crate) recent_queries: VecDeque<Duration>,
/// Query patterns
pub(crate) patterns: HashMap<String, QueryPatternStats>,
/// Cache hits vs misses
pub(crate) cache_hits: u64,
pub(crate) cache_misses: u64,
}
#[derive(Debug, Clone)]
pub(crate) struct QueryPatternStats {
pub(crate) frequency: u64,
pub(crate) total_time: Duration,
pub(crate) indexes_used: Vec<String>,
}
/// Transaction pattern tracker
pub(crate) struct TransactionPatternTracker {
/// Recent transaction durations
pub(crate) recent_durations: VecDeque<Duration>,
/// Commit/abort counts
pub(crate) commits: u64,
pub(crate) aborts: u64,
/// Conflict tracking
pub(crate) conflicts: u64,
pub(crate) deadlocks: u64,
/// Contention points
pub(crate) contention_map: HashMap<String, ContentionStats>,
}
#[derive(Debug, Clone)]
pub(crate) struct ContentionStats {
pub(crate) count: u64,
pub(crate) total_wait_time: Duration,
}
/// Anomaly detector using statistical methods
pub(crate) struct AnomalyDetector {
/// Detection threshold (standard deviations)
pub(crate) threshold: f64,
}