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