dmsc 0.1.9

Ri - A high-performance Rust middleware framework with modular architecture
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
//!
//! This file is part of Ri.
//! The Ri project belongs to the Dunimd Team.
//!
//! Licensed under the Apache License, Version 2.0 (the "License");
//! You may not use this file except in compliance with the License.
//! You may obtain a copy of the License at
//!
//!     http://www.apache.org/licenses/LICENSE-2.0
//!
//! Unless required by applicable law or agreed to in writing, software
//! distributed under the License is distributed on an "AS IS" BASIS,
//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//! See the License for the specific language governing permissions and
//! limitations under the License.

#![allow(non_snake_case)]

//! # Health Check System
//!
//! This module provides comprehensive health checking functionality for Ri modules and services.
//! It supports both active health checks (proactive monitoring) and passive health indicators
//! (reactive status reporting).
//!
//! ## Key Components
//!
//! - **HealthStatus**: Enum representing the health state of a component
//! - **HealthCheck**: Trait for implementing custom health checks
//! - **HealthChecker**: Service for managing and executing health checks
//! - **HealthReport**: Comprehensive health status report
//!
//! ## Design Principles
//!
//! 1. **Non-Intrusive**: Health checks can be added without modifying existing code
//! 2. **Configurable**: Check intervals, timeouts, and thresholds are configurable
//! 3. **Comprehensive**: Supports multiple health indicators and aggregation
//! 4. **Performance-Aware**: Minimal impact on system performance
//! 5. **Extensible**: Easy to add new health check types

use crate::core::RiResult;
use serde::{Deserialize, Serialize};
use std::collections::HashMap as FxHashMap;
use std::time::{Duration, SystemTime};

/// Health status enumeration representing the state of a component or service.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub enum RiHealthStatus {
    /// Component is functioning normally
    Healthy,
    /// Component is experiencing issues but still operational
    Degraded,
    /// Component is not functioning and requires attention
    Unhealthy,
    /// Health status is unknown (check failed or not performed)
    Unknown,
}

impl RiHealthStatus {
    /// Returns true if the status is considered healthy (Healthy or Degraded).
    pub fn is_healthy(&self) -> bool {
        matches!(self, RiHealthStatus::Healthy | RiHealthStatus::Degraded)
    }

    /// Returns true if the status requires immediate attention.
    pub fn requires_attention(&self) -> bool {
        matches!(self, RiHealthStatus::Unhealthy)
    }

    /// Merges multiple health statuses into a single status.
    /// The most severe status takes precedence: Unhealthy > Degraded > Unknown > Healthy
    pub fn merge(statuses: &[RiHealthStatus]) -> RiHealthStatus {
        if statuses.is_empty() {
            return RiHealthStatus::Unknown;
        }

        let mut has_unhealthy = false;
        let mut has_degraded = false;
        let mut has_unknown = false;

        for status in statuses {
            match status {
                RiHealthStatus::Unhealthy => has_unhealthy = true,
                RiHealthStatus::Degraded => has_degraded = true,
                RiHealthStatus::Unknown => has_unknown = true,
                RiHealthStatus::Healthy => {}
            }
        }

        if has_unhealthy {
            RiHealthStatus::Unhealthy
        } else if has_degraded {
            RiHealthStatus::Degraded
        } else if has_unknown {
            RiHealthStatus::Unknown
        } else {
            RiHealthStatus::Healthy
        }
    }
}

impl std::fmt::Display for RiHealthStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RiHealthStatus::Healthy => write!(f, "healthy"),
            RiHealthStatus::Degraded => write!(f, "degraded"),
            RiHealthStatus::Unhealthy => write!(f, "unhealthy"),
            RiHealthStatus::Unknown => write!(f, "unknown"),
        }
    }
}

#[cfg(feature = "pyo3")]
#[pyo3::prelude::pymethods]
impl RiHealthStatus {
    fn __str__(&self) -> String {
        self.to_string()
    }

    fn __repr__(&self) -> String {
        format!("RiHealthStatus::{}", self)
    }

    #[staticmethod]
    fn merge_statuses(statuses: Vec<RiHealthStatus>) -> Self {
        RiHealthStatus::merge(&statuses)
    }
}

/// Result of a health check execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct RiHealthCheckResult {
    /// Name of the health check
    pub name: String,
    /// Health status
    pub status: RiHealthStatus,
    /// Optional message providing additional context
    pub message: Option<String>,
    /// Timestamp when the check was performed
    pub timestamp: SystemTime,
    /// Duration of the health check execution
    pub duration: Duration,
}

impl RiHealthCheckResult {
    /// Creates a new successful health check result.
    pub fn healthy(name: String, message: Option<String>) -> Self {
        Self {
            name,
            status: RiHealthStatus::Healthy,
            message,
            timestamp: SystemTime::now(),
            duration: Duration::ZERO,
        }
    }

    /// Creates a new degraded health check result.
    pub fn degraded(name: String, message: Option<String>) -> Self {
        Self {
            name,
            status: RiHealthStatus::Degraded,
            message,
            timestamp: SystemTime::now(),
            duration: Duration::ZERO,
        }
    }

    /// Creates a new unhealthy health check result.
    pub fn unhealthy(name: String, message: Option<String>) -> Self {
        Self {
            name,
            status: RiHealthStatus::Unhealthy,
            message,
            timestamp: SystemTime::now(),
            duration: Duration::ZERO,
        }
    }

    /// Creates a new unknown health check result.
    pub fn unknown(name: String, message: Option<String>) -> Self {
        Self {
            name,
            status: RiHealthStatus::Unknown,
            message,
            timestamp: SystemTime::now(),
            duration: Duration::ZERO,
        }
    }
}

#[cfg(feature = "pyo3")]
#[pyo3::prelude::pymethods]
impl RiHealthCheckResult {
    #[new]
    fn new_py(name: String, status: RiHealthStatus, message: Option<String>) -> Self {
        Self {
            name,
            status,
            message,
            timestamp: SystemTime::now(),
            duration: Duration::ZERO,
        }
    }

    #[staticmethod]
    fn create_healthy(name: String, message: Option<String>) -> Self {
        Self::healthy(name, message)
    }

    #[staticmethod]
    fn create_degraded(name: String, message: Option<String>) -> Self {
        Self::degraded(name, message)
    }

    #[staticmethod]
    fn create_unhealthy(name: String, message: Option<String>) -> Self {
        Self::unhealthy(name, message)
    }

    #[staticmethod]
    fn create_unknown(name: String, message: Option<String>) -> Self {
        Self::unknown(name, message)
    }

    #[getter]
    fn name(&self) -> String {
        self.name.clone()
    }

    #[getter]
    fn status(&self) -> RiHealthStatus {
        self.status
    }

    #[getter]
    fn message(&self) -> Option<String> {
        self.message.clone()
    }

    fn __str__(&self) -> String {
        format!("{}: {}", self.name, self.status)
    }

    fn __repr__(&self) -> String {
        format!("RiHealthCheckResult {{ name: {:?}, status: {:?} }}", self.name, self.status)
    }
}

/// Configuration for health checks.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct RiHealthCheckConfig {
    /// Interval between health checks
    pub check_interval: Duration,
    /// Timeout for individual health checks
    pub timeout: Duration,
    /// Number of consecutive failures before marking as unhealthy
    pub failure_threshold: u32,
    /// Number of consecutive successes before marking as healthy
    pub success_threshold: u32,
    /// Whether the health check is enabled
    pub enabled: bool,
}

impl Default for RiHealthCheckConfig {
    fn default() -> Self {
        Self {
            check_interval: Duration::from_secs(30),
            timeout: Duration::from_secs(5),
            failure_threshold: 3,
            success_threshold: 2,
            enabled: true,
        }
    }
}

#[cfg(feature = "pyo3")]
#[pyo3::prelude::pymethods]
impl RiHealthCheckConfig {
    #[new]
    fn new_py(check_interval: u64, timeout: u64, failure_threshold: u32, success_threshold: u32, enabled: bool) -> Self {
        Self {
            check_interval: Duration::from_secs(check_interval),
            timeout: Duration::from_secs(timeout),
            failure_threshold,
            success_threshold,
            enabled,
        }
    }

    #[staticmethod]
    fn default_config() -> Self {
        Self::default()
    }

    #[getter]
    fn check_interval(&self) -> u64 {
        self.check_interval.as_secs()
    }

    #[setter]
    fn set_check_interval(&mut self, value: u64) {
        self.check_interval = Duration::from_secs(value);
    }

    #[getter]
    fn timeout(&self) -> u64 {
        self.timeout.as_secs()
    }

    #[setter]
    fn set_timeout(&mut self, value: u64) {
        self.timeout = Duration::from_secs(value);
    }

    #[getter]
    fn failure_threshold(&self) -> u32 {
        self.failure_threshold
    }

    #[getter]
    fn success_threshold(&self) -> u32 {
        self.success_threshold
    }

    #[getter]
    fn enabled(&self) -> bool {
        self.enabled
    }

    fn __repr__(&self) -> String {
        format!("RiHealthCheckConfig {{ check_interval: {}, timeout: {}, failure_threshold: {}, success_threshold: {}, enabled: {} }}",
            self.check_interval.as_secs(), self.timeout.as_secs(), self.failure_threshold, self.success_threshold, self.enabled)
    }
}

/// Trait for implementing custom health checks.
#[async_trait::async_trait]
pub trait HealthCheck: Send + Sync {
    /// Performs the health check and returns the result.
    async fn check(&self) -> RiResult<RiHealthCheckResult>;

    /// Returns the name of this health check.
    fn name(&self) -> &str;

    /// Returns the configuration for this health check.
    fn config(&self) -> &RiHealthCheckConfig;
}

/// Comprehensive health report containing status of all components.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct RiHealthReport {
    /// Overall system health status
    pub overall_status: RiHealthStatus,
    /// Individual component health results
    pub components: FxHashMap<String, RiHealthCheckResult>,
    /// Timestamp when the report was generated
    pub timestamp: SystemTime,
    /// Total number of components checked
    pub total_components: usize,
    /// Number of healthy components
    pub healthy_count: usize,
    /// Number of degraded components
    pub degraded_count: usize,
    /// Number of unhealthy components
    pub unhealthy_count: usize,
    /// Number of unknown components
    pub unknown_count: usize,
}

impl RiHealthReport {
    /// Creates a new empty health report.
    pub fn new() -> Self {
        Self {
            overall_status: RiHealthStatus::Unknown,
            components: FxHashMap::default(),
            timestamp: SystemTime::now(),
            total_components: 0,
            healthy_count: 0,
            degraded_count: 0,
            unhealthy_count: 0,
            unknown_count: 0,
        }
    }

    /// Adds a health check result to the report.
    pub fn add_result(&mut self, result: RiHealthCheckResult) {
        match result.status {
            RiHealthStatus::Healthy => self.healthy_count += 1,
            RiHealthStatus::Degraded => self.degraded_count += 1,
            RiHealthStatus::Unhealthy => self.unhealthy_count += 1,
            RiHealthStatus::Unknown => self.unknown_count += 1,
        }
        self.total_components += 1;
        self.components.insert(result.name.clone(), result);
        self.update_overall_status();
    }

    /// Updates the overall health status based on component statuses.
    fn update_overall_status(&mut self) {
        let statuses: Vec<RiHealthStatus> = self.components.values().map(|r| r.status).collect();
        self.overall_status = RiHealthStatus::merge(&statuses);
    }
}

impl Default for RiHealthReport {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "pyo3")]
#[pyo3::prelude::pymethods]
impl RiHealthReport {
    #[new]
    fn new_py() -> Self {
        Self::new()
    }

    #[staticmethod]
    fn create() -> Self {
        Self::new()
    }

    #[staticmethod]
    fn from_results(results: Vec<RiHealthCheckResult>) -> Self {
        let mut report = Self::new();
        for result in results {
            report.add_result(result);
        }
        report
    }

    #[getter]
    fn overall_status(&self) -> RiHealthStatus {
        self.overall_status
    }

    #[getter]
    fn total_components(&self) -> usize {
        self.total_components
    }

    #[getter]
    fn healthy_count(&self) -> usize {
        self.healthy_count
    }

    #[getter]
    fn degraded_count(&self) -> usize {
        self.degraded_count
    }

    #[getter]
    fn unhealthy_count(&self) -> usize {
        self.unhealthy_count
    }

    #[getter]
    fn unknown_count(&self) -> usize {
        self.unknown_count
    }

    fn __str__(&self) -> String {
        format!("RiHealthReport: {} ({}/{} healthy, {} degraded, {} unhealthy, {} unknown)",
            self.overall_status, self.healthy_count, self.total_components,
            self.degraded_count, self.unhealthy_count, self.unknown_count)
    }

    fn __repr__(&self) -> String {
        format!("RiHealthReport {{ overall_status: {:?}, total_components: {} }}", self.overall_status, self.total_components)
    }
}

/// Health checker service that manages and executes health checks.
pub struct RiHealthChecker {
    /// Registered health checks
    checks: Vec<Box<dyn HealthCheck>>,
    /// Global configuration
    _config: RiHealthCheckConfig,
}

impl RiHealthChecker {
    /// Creates a new health checker with default configuration.
    pub fn new() -> Self {
        Self {
            checks: Vec::new(),
            _config: RiHealthCheckConfig::default(),
        }
    }

    /// Creates a new health checker with custom configuration.
    pub fn with_config(config: RiHealthCheckConfig) -> Self {
        Self {
            checks: Vec::new(),
            _config: config,
        }
    }

    /// Registers a health check.
    pub fn register_check(&mut self, check: Box<dyn HealthCheck>) {
        self.checks.push(check);
    }

    /// Performs all health checks and returns a comprehensive report.
    pub async fn check_all(&self) -> RiHealthReport {
        let mut report = RiHealthReport::new();

        for check in &self.checks {
            if !check.config().enabled {
                continue;
            }

            let start_time = SystemTime::now();
            let result = match tokio::time::timeout(check.config().timeout, check.check()).await {
                Ok(Ok(result)) => result,
                Ok(Err(err)) => RiHealthCheckResult::unknown(
                    check.name().to_string(),
                    Some(format!("Check failed: {err}")),
                ),
                Err(_) => RiHealthCheckResult::unknown(
                    check.name().to_string(),
                    Some("Check timed out".to_string()),
                ),
            };

            let duration = SystemTime::now()
                .duration_since(start_time)
                .unwrap_or(Duration::ZERO);

            let mut result_with_duration = result;
            result_with_duration.duration = duration;
            report.add_result(result_with_duration);
        }

        report
    }

    /// Gets the number of registered health checks.
    pub fn check_count(&self) -> usize {
        self.checks.len()
    }
}

impl Default for RiHealthChecker {
    fn default() -> Self {
        Self::new()
    }
}