Skip to main content

rustfs_notify/
status_view.rs

1// Copyright 2024 RustFS Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::integration::{NotificationMetricSnapshot, NotificationMetrics};
16use hashbrown::HashMap;
17use std::sync::Arc;
18
19#[derive(Clone)]
20pub struct NotifyStatusView {
21    metrics: Arc<NotificationMetrics>,
22}
23
24impl NotifyStatusView {
25    pub fn new(metrics: Arc<NotificationMetrics>) -> Self {
26        Self { metrics }
27    }
28
29    pub fn get_status(&self) -> HashMap<String, String> {
30        let mut status = HashMap::new();
31
32        status.insert("uptime_seconds".to_string(), self.metrics.uptime().as_secs().to_string());
33        status.insert("processing_events".to_string(), self.metrics.processing_count().to_string());
34        status.insert("processed_events".to_string(), self.metrics.processed_count().to_string());
35        status.insert("failed_events".to_string(), self.metrics.failed_count().to_string());
36        status.insert("skipped_events".to_string(), self.metrics.skipped_count().to_string());
37
38        status
39    }
40
41    pub fn snapshot_metrics(&self) -> NotificationMetricSnapshot {
42        self.metrics.snapshot()
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::NotifyStatusView;
49    use crate::integration::NotificationMetrics;
50    use std::sync::Arc;
51
52    #[test]
53    fn status_view_reports_empty_metrics_snapshot() {
54        let status_view = NotifyStatusView::new(Arc::new(NotificationMetrics::new()));
55
56        let snapshot = status_view.snapshot_metrics();
57        assert_eq!(snapshot.current_send_in_progress, 0);
58        assert_eq!(snapshot.events_errors_total, 0);
59        assert_eq!(snapshot.events_sent_total, 0);
60        assert_eq!(snapshot.events_skipped_total, 0);
61    }
62
63    #[test]
64    fn status_view_exposes_status_map_keys() {
65        let status_view = NotifyStatusView::new(Arc::new(NotificationMetrics::new()));
66        let status = status_view.get_status();
67
68        assert!(status.contains_key("uptime_seconds"));
69        assert!(status.contains_key("processing_events"));
70        assert!(status.contains_key("processed_events"));
71        assert!(status.contains_key("failed_events"));
72        assert!(status.contains_key("skipped_events"));
73    }
74}