Mountain/ApplicationState/State/FeatureState/Diagnostics/
DiagnosticsState.rs1use std::{
35 collections::HashMap,
36 sync::{Arc, Mutex as StandardMutex},
37};
38
39use log::debug;
40
41use crate::ApplicationState::DTO::MarkerDataDTO::MarkerDataDTO;
42
43#[derive(Clone)]
45pub struct DiagnosticsState {
46 pub DiagnosticsMap:Arc<StandardMutex<HashMap<String, HashMap<String, Vec<MarkerDataDTO>>>>>,
50}
51
52impl Default for DiagnosticsState {
53 fn default() -> Self {
54 debug!("[DiagnosticsState] Initializing default diagnostics state...");
55
56 Self { DiagnosticsMap:Arc::new(StandardMutex::new(HashMap::new())) }
57 }
58}
59
60impl DiagnosticsState {
61 pub fn GetAll(&self) -> HashMap<String, HashMap<String, Vec<MarkerDataDTO>>> {
63 self.DiagnosticsMap.lock().ok().map(|guard| guard.clone()).unwrap_or_default()
64 }
65
66 pub fn GetByOwner(&self, owner:&str) -> HashMap<String, Vec<MarkerDataDTO>> {
68 self.DiagnosticsMap
69 .lock()
70 .ok()
71 .and_then(|guard| guard.get(owner).cloned())
72 .unwrap_or_default()
73 }
74
75 pub fn GetByOwnerAndResource(&self, owner:&str, resource:&str) -> Vec<MarkerDataDTO> {
77 self.DiagnosticsMap
78 .lock()
79 .ok()
80 .and_then(|guard| guard.get(owner).and_then(|resources| resources.get(resource).cloned()))
81 .unwrap_or_default()
82 }
83
84 pub fn SetByOwner(&self, owner:String, diagnostics:HashMap<String, Vec<MarkerDataDTO>>) {
86 if let Ok(mut guard) = self.DiagnosticsMap.lock() {
87 guard.insert(owner, diagnostics);
88 debug!("[DiagnosticsState] Diagnostics updated for owner");
89 }
90 }
91
92 pub fn SetByOwnerAndResource(&self, owner:String, resource:String, markers:Vec<MarkerDataDTO>) {
94 if let Ok(mut guard) = self.DiagnosticsMap.lock() {
95 guard.entry(owner).or_insert_with(HashMap::new).insert(resource, markers);
96 debug!("[DiagnosticsState] Diagnostics updated for owner and resource");
97 }
98 }
99
100 pub fn ClearByOwner(&self, owner:&str) {
102 if let Ok(mut guard) = self.DiagnosticsMap.lock() {
103 guard.remove(owner);
104 debug!("[DiagnosticsState] Diagnostics cleared for owner: {}", owner);
105 }
106 }
107
108 pub fn ClearByOwnerAndResource(&self, owner:&str, resource:&str) {
110 if let Ok(mut guard) = self.DiagnosticsMap.lock() {
111 if let Some(resources) = guard.get_mut(owner) {
112 resources.remove(resource);
113 debug!("[DiagnosticsState] Diagnostics cleared for owner and resource");
114 }
115 }
116 }
117
118 pub fn ClearAll(&self) {
120 if let Ok(mut guard) = self.DiagnosticsMap.lock() {
121 guard.clear();
122 debug!("[DiagnosticsState] All diagnostics cleared");
123 }
124 }
125}