ipfrs 0.2.0

Next-generation distributed file system with content-addressing, semantic search, and logic programming
Documentation
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Diagnostic utilities for IPFRS nodes
//!
//! This module provides comprehensive diagnostic information about node health,
//! resource usage, and performance metrics.

use serde::{Deserialize, Serialize};
use std::time::{Duration, SystemTime};

/// Comprehensive node diagnostics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeDiagnostics {
    /// Timestamp when diagnostics were collected
    pub timestamp: SystemTime,
    /// Node uptime
    pub uptime: Duration,
    /// Storage diagnostics
    pub storage: StorageDiagnostics,
    /// Semantic router diagnostics (if enabled)
    pub semantic: Option<SemanticDiagnostics>,
    /// TensorLogic diagnostics (if enabled)
    pub tensorlogic: Option<TensorLogicDiagnostics>,
    /// Network diagnostics (if enabled)
    pub network: Option<NetworkDiagnostics>,
    /// System resource usage
    pub resources: ResourceUsage,
}

/// Storage-related diagnostics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageDiagnostics {
    /// Total number of blocks stored
    pub total_blocks: u64,
    /// Total size in bytes
    pub total_bytes: u64,
    /// Average block size
    pub avg_block_size: u64,
    /// Storage path
    pub storage_path: String,
    /// Database health status
    pub health: HealthStatus,
}

/// Semantic router diagnostics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SemanticDiagnostics {
    /// Number of indexed vectors
    pub num_vectors: usize,
    /// Index dimensions
    pub dimensions: usize,
    /// Index health status
    pub health: HealthStatus,
    /// Cache hit rate (if available)
    pub cache_hit_rate: Option<f64>,
}

/// TensorLogic diagnostics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TensorLogicDiagnostics {
    /// Number of facts in knowledge base
    pub num_facts: usize,
    /// Number of rules
    pub num_rules: usize,
    /// Knowledge base health status
    pub health: HealthStatus,
    /// Average inference time (if available)
    pub avg_inference_ms: Option<f64>,
}

/// Network diagnostics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkDiagnostics {
    /// Number of connected peers
    pub connected_peers: usize,
    /// Network health status
    pub health: HealthStatus,
    /// Bytes sent
    pub bytes_sent: u64,
    /// Bytes received
    pub bytes_received: u64,
}

/// System resource usage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceUsage {
    /// Memory usage in bytes (approximate)
    pub memory_bytes: u64,
    /// CPU usage percentage (if available)
    pub cpu_percent: Option<f64>,
}

/// Health status indicator
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum HealthStatus {
    /// Component is healthy
    Healthy,
    /// Component is degraded but functional
    Degraded,
    /// Component is unhealthy
    Unhealthy,
    /// Component status is unknown
    Unknown,
}

impl HealthStatus {
    /// Check if the status is healthy
    pub fn is_healthy(&self) -> bool {
        matches!(self, HealthStatus::Healthy)
    }

    /// Check if the status indicates problems
    pub fn has_issues(&self) -> bool {
        matches!(self, HealthStatus::Degraded | HealthStatus::Unhealthy)
    }
}

/// Diagnostic recommendations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiagnosticRecommendation {
    /// Recommendation severity
    pub severity: RecommendationSeverity,
    /// Component this recommendation applies to
    pub component: String,
    /// Recommendation message
    pub message: String,
    /// Optional action to take
    pub action: Option<String>,
}

/// Recommendation severity levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RecommendationSeverity {
    /// Informational
    Info,
    /// Warning
    Warning,
    /// Critical issue
    Critical,
}

/// Diagnostic analyzer that generates recommendations
pub struct DiagnosticAnalyzer;

impl DiagnosticAnalyzer {
    /// Analyze diagnostics and generate recommendations
    pub fn analyze(diagnostics: &NodeDiagnostics) -> Vec<DiagnosticRecommendation> {
        let mut recommendations = Vec::new();

        // Check storage health
        if diagnostics.storage.health.has_issues() {
            recommendations.push(DiagnosticRecommendation {
                severity: RecommendationSeverity::Critical,
                component: "Storage".to_string(),
                message: "Storage health is degraded".to_string(),
                action: Some("Check disk space and database integrity".to_string()),
            });
        }

        // Check storage utilization
        if diagnostics.storage.total_blocks > 1_000_000 {
            recommendations.push(DiagnosticRecommendation {
                severity: RecommendationSeverity::Info,
                component: "Storage".to_string(),
                message: format!(
                    "Large number of blocks: {}",
                    diagnostics.storage.total_blocks
                ),
                action: Some("Consider running garbage collection".to_string()),
            });
        }

        // Check semantic router
        if let Some(ref semantic) = diagnostics.semantic {
            if semantic.health.has_issues() {
                recommendations.push(DiagnosticRecommendation {
                    severity: RecommendationSeverity::Warning,
                    component: "Semantic".to_string(),
                    message: "Semantic router health is degraded".to_string(),
                    action: Some("Check index integrity and rebuild if necessary".to_string()),
                });
            }

            if semantic.num_vectors > 100_000 {
                recommendations.push(DiagnosticRecommendation {
                    severity: RecommendationSeverity::Info,
                    component: "Semantic".to_string(),
                    message: format!("Large vector index: {} vectors", semantic.num_vectors),
                    action: Some("Consider index optimization or sharding".to_string()),
                });
            }
        }

        // Check TensorLogic
        if let Some(ref logic) = diagnostics.tensorlogic {
            if logic.health.has_issues() {
                recommendations.push(DiagnosticRecommendation {
                    severity: RecommendationSeverity::Warning,
                    component: "TensorLogic".to_string(),
                    message: "Knowledge base health is degraded".to_string(),
                    action: Some("Verify KB integrity and reload if necessary".to_string()),
                });
            }

            if logic.num_facts > 10_000 {
                recommendations.push(DiagnosticRecommendation {
                    severity: RecommendationSeverity::Info,
                    component: "TensorLogic".to_string(),
                    message: format!("Large knowledge base: {} facts", logic.num_facts),
                    action: Some("Consider query optimization or KB pruning".to_string()),
                });
            }
        }

        // Check network
        if let Some(ref network) = diagnostics.network {
            if network.health.has_issues() {
                recommendations.push(DiagnosticRecommendation {
                    severity: RecommendationSeverity::Warning,
                    component: "Network".to_string(),
                    message: "Network health is degraded".to_string(),
                    action: Some("Check network connectivity and peer connections".to_string()),
                });
            }

            if network.connected_peers == 0 {
                recommendations.push(DiagnosticRecommendation {
                    severity: RecommendationSeverity::Warning,
                    component: "Network".to_string(),
                    message: "No connected peers".to_string(),
                    action: Some("Check bootstrap nodes and network configuration".to_string()),
                });
            }
        }

        // Check memory usage
        if diagnostics.resources.memory_bytes > 1_000_000_000 {
            // > 1GB
            recommendations.push(DiagnosticRecommendation {
                severity: RecommendationSeverity::Info,
                component: "Resources".to_string(),
                message: format!(
                    "High memory usage: {} MB",
                    diagnostics.resources.memory_bytes / 1_000_000
                ),
                action: Some("Consider reducing cache sizes or restarting node".to_string()),
            });
        }

        recommendations
    }

    /// Generate a health report string
    pub fn health_report(diagnostics: &NodeDiagnostics) -> String {
        let mut report = String::new();
        report.push_str("=== IPFRS Node Health Report ===\n\n");

        report.push_str(&format!("Uptime: {:?}\n", diagnostics.uptime));
        report.push_str(&format!(
            "Storage: {:?} ({} blocks, {} bytes)\n",
            diagnostics.storage.health,
            diagnostics.storage.total_blocks,
            diagnostics.storage.total_bytes
        ));

        if let Some(ref semantic) = diagnostics.semantic {
            report.push_str(&format!(
                "Semantic: {:?} ({} vectors, {} dims)\n",
                semantic.health, semantic.num_vectors, semantic.dimensions
            ));
        }

        if let Some(ref logic) = diagnostics.tensorlogic {
            report.push_str(&format!(
                "TensorLogic: {:?} ({} facts, {} rules)\n",
                logic.health, logic.num_facts, logic.num_rules
            ));
        }

        if let Some(ref network) = diagnostics.network {
            report.push_str(&format!(
                "Network: {:?} ({} peers)\n",
                network.health, network.connected_peers
            ));
        }

        report.push_str(&format!(
            "Memory: {} MB\n",
            diagnostics.resources.memory_bytes / 1_000_000
        ));

        report.push_str("\n--- Recommendations ---\n");
        let recommendations = Self::analyze(diagnostics);
        if recommendations.is_empty() {
            report.push_str("No issues detected\n");
        } else {
            for rec in recommendations {
                report.push_str(&format!(
                    "[{:?}] {}: {}\n",
                    rec.severity, rec.component, rec.message
                ));
                if let Some(ref action) = rec.action {
                    report.push_str(&format!("  Action: {}\n", action));
                }
            }
        }

        report
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_health_status() {
        assert!(HealthStatus::Healthy.is_healthy());
        assert!(!HealthStatus::Degraded.is_healthy());
        assert!(HealthStatus::Degraded.has_issues());
        assert!(HealthStatus::Unhealthy.has_issues());
    }

    #[test]
    fn test_diagnostic_analyzer_no_issues() {
        let diagnostics = NodeDiagnostics {
            timestamp: SystemTime::now(),
            uptime: Duration::from_secs(60),
            storage: StorageDiagnostics {
                total_blocks: 100,
                total_bytes: 10000,
                avg_block_size: 100,
                storage_path: "/tmp/ipfrs".to_string(),
                health: HealthStatus::Healthy,
            },
            semantic: Some(SemanticDiagnostics {
                num_vectors: 50,
                dimensions: 768,
                health: HealthStatus::Healthy,
                cache_hit_rate: Some(0.8),
            }),
            tensorlogic: Some(TensorLogicDiagnostics {
                num_facts: 20,
                num_rules: 5,
                health: HealthStatus::Healthy,
                avg_inference_ms: Some(1.5),
            }),
            network: Some(NetworkDiagnostics {
                connected_peers: 5,
                health: HealthStatus::Healthy,
                bytes_sent: 1000,
                bytes_received: 2000,
            }),
            resources: ResourceUsage {
                memory_bytes: 100_000_000, // 100MB
                cpu_percent: Some(10.0),
            },
        };

        let recommendations = DiagnosticAnalyzer::analyze(&diagnostics);
        assert_eq!(recommendations.len(), 0);
    }

    #[test]
    fn test_diagnostic_analyzer_storage_issues() {
        let diagnostics = NodeDiagnostics {
            timestamp: SystemTime::now(),
            uptime: Duration::from_secs(60),
            storage: StorageDiagnostics {
                total_blocks: 2_000_000,
                total_bytes: 10_000_000_000,
                avg_block_size: 5000,
                storage_path: "/tmp/ipfrs".to_string(),
                health: HealthStatus::Degraded,
            },
            semantic: None,
            tensorlogic: None,
            network: None,
            resources: ResourceUsage {
                memory_bytes: 100_000_000,
                cpu_percent: None,
            },
        };

        let recommendations = DiagnosticAnalyzer::analyze(&diagnostics);
        assert!(recommendations.len() >= 2);
        assert!(recommendations
            .iter()
            .any(|r| r.severity == RecommendationSeverity::Critical));
    }

    #[test]
    fn test_health_report_generation() {
        let diagnostics = NodeDiagnostics {
            timestamp: SystemTime::now(),
            uptime: Duration::from_secs(3600),
            storage: StorageDiagnostics {
                total_blocks: 100,
                total_bytes: 10000,
                avg_block_size: 100,
                storage_path: "/tmp/ipfrs".to_string(),
                health: HealthStatus::Healthy,
            },
            semantic: None,
            tensorlogic: None,
            network: None,
            resources: ResourceUsage {
                memory_bytes: 100_000_000,
                cpu_percent: Some(5.0),
            },
        };

        let report = DiagnosticAnalyzer::health_report(&diagnostics);
        assert!(report.contains("Health Report"));
        assert!(report.contains("Storage"));
        assert!(report.contains("Healthy"));
    }
}