Skip to main content

ralph_workflow/json_parser/boundary/
health_monitor.rs

1use std::cell::Cell;
2
3use crate::json_parser::health::ParserHealth;
4use crate::logger::Colors;
5
6pub struct HealthMonitor {
7    health: Cell<ParserHealth>,
8    parser_name: &'static str,
9    threshold_warned: Cell<bool>,
10}
11
12impl HealthMonitor {
13    #[must_use]
14    pub fn new(parser_name: &'static str) -> Self {
15        Self {
16            health: Cell::new(ParserHealth::new()),
17            parser_name,
18            threshold_warned: Cell::new(false),
19        }
20    }
21
22    pub fn record_parsed(&self) {
23        self.health.update(|mut h| {
24            h.record_parsed();
25            h
26        });
27    }
28
29    pub fn record_ignored(&self) {
30        self.health.update(|mut h| {
31            h.record_ignored();
32            h
33        });
34    }
35
36    pub fn record_unknown_event(&self) {
37        self.health.update(|mut h| {
38            h.record_unknown_event();
39            h
40        });
41    }
42
43    pub fn record_parse_error(&self) {
44        self.health.update(|mut h| {
45            h.record_parse_error();
46            h
47        });
48    }
49
50    pub fn record_control_event(&self) {
51        self.health.update(|mut h| {
52            h.record_control_event();
53            h
54        });
55    }
56
57    pub fn record_partial_event(&self) {
58        self.health.update(|mut h| {
59            h.record_partial_event();
60            h
61        });
62    }
63
64    pub fn check_and_warn(&self, colors: Colors) -> Option<String> {
65        if self.threshold_warned.get() {
66            return None;
67        }
68
69        let health = self.health.get();
70        let warning = health.warning(self.parser_name, colors);
71        if warning.is_some() {
72            self.threshold_warned.set(true);
73        }
74        warning
75    }
76}