stygian-graph 0.9.2

High-performance graph-based web scraping engine with AI extraction, multi-modal support, and anti-bot capabilities
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
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
//! Health check reporting for Kubernetes liveness and readiness probes.
//!
//! Provides structured health-check types and a [`HealthReporter`](health::HealthReporter) for aggregating
//! component-level health into an overall [`HealthReport`](health::HealthReport).
//!
//! # Example
//!
//! ```
//! use stygian_graph::application::health::{HealthReporter, HealthStatus, ComponentHealth};
//!
//! let mut reporter = HealthReporter::new();
//! reporter.register("database", HealthStatus::Healthy);
//! reporter.register("cache", HealthStatus::Degraded("High latency".to_string()));
//!
//! let report = reporter.report();
//! assert!(report.is_ready());  // Degraded is still operational ⇒ ready
//! assert!(report.is_live());   // Still alive while degraded
//! ```

use std::collections::HashMap;
use std::time::SystemTime;

use parking_lot::RwLock;

use serde::{Deserialize, Serialize};

// ─── HealthStatus ─────────────────────────────────────────────────────────────

/// The health status of a single component.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "status", content = "reason", rename_all = "lowercase")]
pub enum HealthStatus {
    /// Component is operating normally.
    Healthy,
    /// Component is partially impaired but still serving requests.
    Degraded(String),
    /// Component is non-functional; requests will fail.
    Unhealthy(String),
}

impl HealthStatus {
    /// Returns `true` only when the component is fully healthy.
    ///
    /// # Example
    ///
    /// ```
    /// use stygian_graph::application::health::HealthStatus;
    /// assert!(HealthStatus::Healthy.is_healthy());
    /// assert!(!HealthStatus::Degraded("latency".into()).is_healthy());
    /// ```
    pub const fn is_healthy(&self) -> bool {
        matches!(self, Self::Healthy)
    }

    /// Returns `true` when the component can still serve requests (healthy or degraded).
    ///
    /// # Example
    ///
    /// ```
    /// use stygian_graph::application::health::HealthStatus;
    /// assert!(HealthStatus::Healthy.is_operational());
    /// assert!(HealthStatus::Degraded("high latency".into()).is_operational());
    /// assert!(!HealthStatus::Unhealthy("connection refused".into()).is_operational());
    /// ```
    pub const fn is_operational(&self) -> bool {
        !matches!(self, Self::Unhealthy(_))
    }
}

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

// ─── ComponentHealth ─────────────────────────────────────────────────────────

/// Health state for a single named component.
///
/// Returned as part of a [`HealthReport`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComponentHealth {
    /// Component identifier (e.g. `"database"`, `"cache"`, `"worker_pool"`)
    pub name: String,
    /// Component status
    pub status: HealthStatus,
    /// Optional free-form details (timings, error messages, etc.)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<serde_json::Value>,
}

impl ComponentHealth {
    /// Create a healthy component with no extra details.
    ///
    /// # Example
    ///
    /// ```
    /// use stygian_graph::application::health::{ComponentHealth, HealthStatus};
    ///
    /// let c = ComponentHealth::healthy("cache");
    /// assert_eq!(c.status, HealthStatus::Healthy);
    /// ```
    pub fn healthy(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            status: HealthStatus::Healthy,
            details: None,
        }
    }

    /// Create a degraded component.
    ///
    /// # Example
    ///
    /// ```
    /// use stygian_graph::application::health::ComponentHealth;
    ///
    /// let c = ComponentHealth::degraded("database", "replication lag 5s");
    /// assert!(!c.status.is_healthy());
    /// ```
    pub fn degraded(name: impl Into<String>, reason: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            status: HealthStatus::Degraded(reason.into()),
            details: None,
        }
    }

    /// Create an unhealthy component.
    ///
    /// # Example
    ///
    /// ```
    /// use stygian_graph::application::health::ComponentHealth;
    ///
    /// let c = ComponentHealth::unhealthy("valkey", "connection refused");
    /// assert!(!c.status.is_operational());
    /// ```
    pub fn unhealthy(name: impl Into<String>, reason: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            status: HealthStatus::Unhealthy(reason.into()),
            details: None,
        }
    }

    /// Attach arbitrary JSON details to this component.
    ///
    /// # Example
    ///
    /// ```
    /// use stygian_graph::application::health::ComponentHealth;
    ///
    /// let c = ComponentHealth::healthy("http_pool")
    ///     .with_details(serde_json::json!({ "idle_connections": 8, "max": 32 }));
    ///
    /// assert!(c.details.is_some());
    /// ```
    #[must_use]
    pub fn with_details(mut self, details: serde_json::Value) -> Self {
        self.details = Some(details);
        self
    }
}

// ─── HealthReport ────────────────────────────────────────────────────────────

/// Aggregated health report for all registered components.
///
/// Returned by [`HealthReporter::report`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthReport {
    /// Overall system status (worst of all components)
    pub overall: HealthStatus,
    /// Per-component breakdown
    pub components: Vec<ComponentHealth>,
    /// When this report was generated (Unix seconds)
    #[serde(with = "system_time_serde")]
    pub checked_at: SystemTime,
}

impl HealthReport {
    /// Returns `true` when the system is ready to serve traffic.
    ///
    /// The system is ready only when **all** components are healthy or degraded
    /// (Kubernetes readiness probe).
    ///
    /// # Example
    ///
    /// ```
    /// use stygian_graph::application::health::{HealthReporter, HealthStatus};
    ///
    /// let mut r = HealthReporter::new();
    /// r.register("db", HealthStatus::Healthy);
    /// assert!(r.report().is_ready());
    /// ```
    pub fn is_ready(&self) -> bool {
        self.components.iter().all(|c| c.status.is_operational())
    }

    /// Returns `true` while the process should continue running.
    ///
    /// The process is considered alive unless every component is unhealthy
    /// (Kubernetes liveness probe).
    ///
    /// # Example
    ///
    /// ```
    /// use stygian_graph::application::health::{HealthReporter, HealthStatus};
    ///
    /// let r = HealthReporter::new();
    /// r.register("db", HealthStatus::Unhealthy("disk full".into()));
    /// r.register("cache", HealthStatus::Healthy);
    /// // One unhealthy component doesn't kill the process while others are healthy
    /// assert!(r.report().is_live());
    /// ```
    pub fn is_live(&self) -> bool {
        // Dead when ALL components are unhealthy (or no components registered)
        if self.components.is_empty() {
            return true;
        }
        self.components.iter().any(|c| c.status.is_operational())
    }

    /// HTTP status code suitable for a health-check endpoint.
    ///
    /// Returns `200` when ready, `503` when not.
    ///
    /// # Example
    ///
    /// ```
    /// use stygian_graph::application::health::{HealthReporter, HealthStatus};
    ///
    /// let mut r = HealthReporter::new();
    /// r.register("db", HealthStatus::Healthy);
    /// assert_eq!(r.report().http_status_code(), 200u16);
    /// ```
    pub fn http_status_code(&self) -> u16 {
        if self.is_ready() { 200 } else { 503 }
    }
}

// ─── System-time serde helper ─────────────────────────────────────────────────

mod system_time_serde {
    use serde::{Deserialize, Deserializer, Serializer};
    use std::time::{SystemTime, UNIX_EPOCH};

    pub fn serialize<S: Serializer>(t: &SystemTime, s: S) -> Result<S::Ok, S::Error> {
        let secs = t.duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
        s.serialize_u64(secs)
    }

    pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<SystemTime, D::Error> {
        let secs = u64::deserialize(d)?;
        Ok(UNIX_EPOCH + std::time::Duration::from_secs(secs))
    }
}

// ─── HealthReporter ──────────────────────────────────────────────────────────

/// Collects component-level health checks and produces a [`HealthReport`].
///
/// Thread-safe; cheaply cloneable via `Arc` patterns.
///
/// # Example
///
/// ```
/// use stygian_graph::application::health::{HealthReporter, HealthStatus, ComponentHealth};
///
/// let mut reporter = HealthReporter::new();
/// reporter.register("database", HealthStatus::Healthy);
/// reporter.register_component(
///     ComponentHealth::degraded("cache", "latency p99 > 100ms")
///         .with_details(serde_json::json!({ "p99_ms": 142 }))
/// );
///
/// let report = reporter.report();
/// assert_eq!(report.http_status_code(), 200u16); // degraded is operational
/// ```
pub struct HealthReporter {
    components: RwLock<HashMap<String, ComponentHealth>>,
}

impl HealthReporter {
    /// Create an empty reporter.
    ///
    /// # Example
    ///
    /// ```
    /// use stygian_graph::application::health::HealthReporter;
    ///
    /// let r = HealthReporter::new();
    /// assert!(r.report().components.is_empty());
    /// ```
    pub fn new() -> Self {
        Self {
            components: RwLock::new(HashMap::new()),
        }
    }

    /// Register or update a component's status by name.
    ///
    /// # Example
    ///
    /// ```
    /// use stygian_graph::application::health::{HealthReporter, HealthStatus};
    ///
    /// let mut r = HealthReporter::new();
    /// r.register("db", HealthStatus::Healthy);
    /// assert_eq!(r.report().components.len(), 1);
    /// ```
    pub fn register(&self, name: impl Into<String>, status: HealthStatus) {
        let name = name.into();
        let component = ComponentHealth {
            name: name.clone(),
            status,
            details: None,
        };
        self.components.write().insert(name, component);
    }

    /// Register or update a component with full [`ComponentHealth`].
    ///
    /// # Example
    ///
    /// ```
    /// use stygian_graph::application::health::{HealthReporter, ComponentHealth};
    ///
    /// let mut r = HealthReporter::new();
    /// r.register_component(ComponentHealth::healthy("cache"));
    /// assert_eq!(r.report().components.len(), 1);
    /// ```
    pub fn register_component(&self, component: ComponentHealth) {
        self.components
            .write()
            .insert(component.name.clone(), component);
    }

    /// Remove a component from reporting.
    ///
    /// # Example
    ///
    /// ```
    /// use stygian_graph::application::health::{HealthReporter, HealthStatus};
    ///
    /// let mut r = HealthReporter::new();
    /// r.register("db", HealthStatus::Healthy);
    /// r.deregister("db");
    /// assert!(r.report().components.is_empty());
    /// ```
    pub fn deregister(&self, name: &str) {
        self.components.write().remove(name);
    }

    /// Generate a [`HealthReport`] from current component states.
    ///
    /// # Example
    ///
    /// ```
    /// use stygian_graph::application::health::{HealthReporter, HealthStatus};
    ///
    /// let r = HealthReporter::new();
    /// let report = r.report();
    /// assert_eq!(report.overall, HealthStatus::Healthy);
    /// assert!(report.is_live());
    /// ```
    pub fn report(&self) -> HealthReport {
        let components: Vec<ComponentHealth> = self.components.read().values().cloned().collect();

        let overall = aggregate_status(&components);
        HealthReport {
            overall,
            components,
            checked_at: SystemTime::now(),
        }
    }
}

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

fn aggregate_status(components: &[ComponentHealth]) -> HealthStatus {
    let mut worst = HealthStatus::Healthy;
    for c in components {
        match &c.status {
            HealthStatus::Unhealthy(r) => {
                return HealthStatus::Unhealthy(r.clone());
            }
            HealthStatus::Degraded(r) => {
                if worst == HealthStatus::Healthy {
                    worst = HealthStatus::Degraded(r.clone());
                }
            }
            HealthStatus::Healthy => {}
        }
    }
    worst
}

// ─── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    #[test]
    fn healthy_status_is_healthy() {
        assert!(HealthStatus::Healthy.is_healthy());
        assert!(HealthStatus::Healthy.is_operational());
    }

    #[test]
    fn degraded_status_is_not_healthy_but_operational() {
        let s = HealthStatus::Degraded("reason".into());
        assert!(!s.is_healthy());
        assert!(s.is_operational());
    }

    #[test]
    fn unhealthy_status_is_not_operational() {
        let s = HealthStatus::Unhealthy("crashed".into());
        assert!(!s.is_healthy());
        assert!(!s.is_operational());
    }

    #[test]
    fn empty_reporter_overall_is_healthy() {
        let reporter = HealthReporter::new();
        assert_eq!(reporter.report().overall, HealthStatus::Healthy);
    }

    #[test]
    fn all_healthy_report_is_ready_and_live() {
        let r = HealthReporter::new();
        r.register("db", HealthStatus::Healthy);
        r.register("cache", HealthStatus::Healthy);
        let report = r.report();
        assert!(report.is_ready());
        assert!(report.is_live());
        assert_eq!(report.http_status_code(), 200);
    }

    #[test]
    fn degraded_component_report_not_ready_but_still_live() {
        let r = HealthReporter::new();
        r.register("db", HealthStatus::Healthy);
        r.register("cache", HealthStatus::Degraded("high latency".into()));
        let report = r.report();
        // Degraded is operational so is_ready returns true
        assert!(report.is_ready());
        assert!(report.is_live());
    }

    #[test]
    fn unhealthy_component_makes_report_not_ready() {
        let r = HealthReporter::new();
        r.register("db", HealthStatus::Unhealthy("connection refused".into()));
        let report = r.report();
        assert!(!report.is_ready());
        assert_eq!(report.http_status_code(), 503);
    }

    #[test]
    fn all_unhealthy_not_live() {
        let r = HealthReporter::new();
        r.register("a", HealthStatus::Unhealthy("x".into()));
        r.register("b", HealthStatus::Unhealthy("y".into()));
        assert!(!r.report().is_live());
    }

    #[test]
    fn register_same_component_updates_status() {
        let r = HealthReporter::new();
        r.register("db", HealthStatus::Healthy);
        r.register("db", HealthStatus::Unhealthy("disk full".into()));
        let report = r.report();
        assert_eq!(report.components.len(), 1);
        assert!(!report.is_ready());
    }

    #[test]
    fn deregister_removes_component() {
        let r = HealthReporter::new();
        r.register("db", HealthStatus::Healthy);
        r.deregister("db");
        assert!(r.report().components.is_empty());
    }

    #[test]
    fn component_health_builders() {
        assert!(ComponentHealth::healthy("x").status.is_healthy());
        assert!(
            ComponentHealth::degraded("x", "slow")
                .status
                .is_operational()
        );
        assert!(
            !ComponentHealth::unhealthy("x", "down")
                .status
                .is_operational()
        );
    }

    #[test]
    fn component_with_details_serializes() {
        let c = ComponentHealth::healthy("pool").with_details(serde_json::json!({ "idle": 8 }));
        assert!(c.details.is_some());
        let json = serde_json::to_string(&c).unwrap();
        assert!(json.contains("idle"));
    }

    #[test]
    fn health_report_serializes_to_json() {
        let r = HealthReporter::new();
        r.register("db", HealthStatus::Healthy);
        let report = r.report();
        let json = serde_json::to_string(&report).expect("serialize");
        assert!(json.contains("healthy"));
    }

    #[test]
    fn aggregate_status_worst_wins() {
        let components = vec![
            ComponentHealth::healthy("a"),
            ComponentHealth::degraded("b", "slow"),
            ComponentHealth::unhealthy("c", "down"),
        ];
        let status = aggregate_status(&components);
        assert!(matches!(status, HealthStatus::Unhealthy(_)));
    }
}