Skip to main content

linkrs_metrics/manager/
counters.rs

1//! Primitive counter store: global, per-space and per-index read/write.
2use std::collections::HashMap;
3use std::sync::Arc;
4
5use super::core::StatsManager;
6use super::metric_type::MetricType;
7use super::metric_value::MetricValue;
8
9impl StatsManager {
10    pub fn add_value(&self, metric_type: MetricType) {
11        let metric = self
12            .metrics
13            .entry(metric_type)
14            .or_insert_with(|| Arc::new(MetricValue::new(0)));
15        metric.increment();
16    }
17
18    pub fn add_value_with_amount(&self, metric_type: MetricType, amount: u64) {
19        let metric = self
20            .metrics
21            .entry(metric_type)
22            .or_insert_with(|| Arc::new(MetricValue::new(0)));
23        metric.add(amount);
24    }
25
26    pub fn dec_value(&self, metric_type: MetricType) {
27        if let Some(metric) = self.metrics.get(&metric_type) {
28            metric.decrement();
29        }
30    }
31
32    pub fn set_value(&self, metric_type: MetricType, value: u64) {
33        let metric = self
34            .metrics
35            .entry(metric_type)
36            .or_insert_with(|| Arc::new(MetricValue::new(0)));
37        metric.set(value);
38    }
39
40    pub fn add_space_metric(&self, space_name: &str, metric_type: MetricType) {
41        let space_map = self
42            .space_metrics
43            .entry(space_name.to_string())
44            .or_insert_with(|| Arc::new(dashmap::DashMap::new()));
45        let metric = space_map
46            .entry(metric_type)
47            .or_insert_with(|| Arc::new(MetricValue::new(0)));
48        metric.increment();
49    }
50
51    pub fn add_space_metric_with_amount(
52        &self,
53        space_name: &str,
54        metric_type: MetricType,
55        amount: u64,
56    ) {
57        let space_map = self
58            .space_metrics
59            .entry(space_name.to_string())
60            .or_insert_with(|| Arc::new(dashmap::DashMap::new()));
61        let metric = space_map
62            .entry(metric_type)
63            .or_insert_with(|| Arc::new(MetricValue::new(0)));
64        metric.add(amount);
65    }
66
67    pub fn set_space_metric_with_amount(
68        &self,
69        space_name: &str,
70        metric_type: MetricType,
71        amount: u64,
72    ) {
73        let space_map = self
74            .space_metrics
75            .entry(space_name.to_string())
76            .or_insert_with(|| Arc::new(dashmap::DashMap::new()));
77        let metric = space_map
78            .entry(metric_type)
79            .or_insert_with(|| Arc::new(MetricValue::new(0)));
80        metric.set(amount);
81    }
82
83    pub fn add_index_metric(&self, index_name: &str, metric_type: MetricType) {
84        let index_map = self
85            .index_metrics
86            .entry(index_name.to_string())
87            .or_insert_with(|| Arc::new(dashmap::DashMap::new()));
88        let metric = index_map
89            .entry(metric_type)
90            .or_insert_with(|| Arc::new(MetricValue::new(0)));
91        metric.increment();
92    }
93
94    pub fn add_index_metric_with_amount(
95        &self,
96        index_name: &str,
97        metric_type: MetricType,
98        amount: u64,
99    ) {
100        let index_map = self
101            .index_metrics
102            .entry(index_name.to_string())
103            .or_insert_with(|| Arc::new(dashmap::DashMap::new()));
104        let metric = index_map
105            .entry(metric_type)
106            .or_insert_with(|| Arc::new(MetricValue::new(0)));
107        metric.add(amount);
108    }
109
110    pub fn dec_space_metric(&self, space_name: &str, metric_type: MetricType) {
111        if let Some(space_map) = self.space_metrics.get(space_name) {
112            if let Some(metric) = space_map.get(&metric_type) {
113                metric.decrement();
114            }
115        }
116    }
117
118    pub fn get_value(&self, metric_type: MetricType) -> Option<u64> {
119        self.metrics.get(&metric_type).map(|metric| metric.get())
120    }
121
122    pub fn get_space_value(&self, space_name: &str, metric_type: MetricType) -> Option<u64> {
123        self.space_metrics
124            .get(space_name)
125            .and_then(|space_map| space_map.get(&metric_type).map(|metric| metric.get()))
126    }
127
128    pub fn get_index_value(&self, index_name: &str, metric_type: MetricType) -> Option<u64> {
129        self.index_metrics
130            .get(index_name)
131            .and_then(|index_map| index_map.get(&metric_type).map(|metric| metric.get()))
132    }
133
134    pub fn get_all_index_metrics(&self, index_name: &str) -> Option<HashMap<MetricType, u64>> {
135        self.index_metrics.get(index_name).map(|index_map| {
136            index_map
137                .iter()
138                .map(|entry| (*entry.key(), entry.value().get()))
139                .collect()
140        })
141    }
142
143    pub fn get_all_metrics(&self) -> HashMap<MetricType, u64> {
144        self.metrics
145            .iter()
146            .map(|entry| (*entry.key(), entry.value().get()))
147            .collect()
148    }
149
150    pub fn get_all_space_metrics(&self, space_name: &str) -> Option<HashMap<MetricType, u64>> {
151        self.space_metrics.get(space_name).map(|space_map| {
152            space_map
153                .iter()
154                .map(|entry| (*entry.key(), entry.value().get()))
155                .collect()
156        })
157    }
158
159    /// Every space-scoped metric set, keyed by space key.
160    pub fn all_space_metrics(&self) -> HashMap<String, HashMap<MetricType, u64>> {
161        self.space_metrics
162            .iter()
163            .map(|entry| {
164                (
165                    entry.key().clone(),
166                    entry
167                        .value()
168                        .iter()
169                        .map(|metric| (*metric.key(), metric.value().get()))
170                        .collect(),
171                )
172            })
173            .collect()
174    }
175
176    pub fn reset_metric(&self, metric_type: MetricType) {
177        if let Some(metric) = self.metrics.get(&metric_type) {
178            metric.set(0);
179        }
180    }
181
182    pub fn reset_all_metrics(&self) {
183        for metric in self.metrics.iter() {
184            metric.value().set(0);
185        }
186    }
187
188    pub fn reset_space_metrics(&self, space_name: &str) {
189        if let Some(space_map) = self.space_metrics.get(space_name) {
190            for metric in space_map.iter() {
191                metric.value().set(0);
192            }
193        }
194    }
195}