rocketmq_remoting/protocol/body/
timer_metrics_serialize_wrapper.rs1use std::collections::HashMap;
18use std::fmt::Display;
19use std::sync::atomic::AtomicU64;
20
21use cheetah_string::CheetahString;
22use rocketmq_common::TimeUtils::get_current_millis;
23
24use crate::protocol::DataVersion;
25
26#[derive(Debug, serde::Serialize, serde::Deserialize)]
27#[serde(rename_all = "camelCase")]
28pub struct TimerMetricsSerializeWrapper {
29 timing_count: HashMap<CheetahString, Metric>,
30 data_version: DataVersion,
31}
32
33impl TimerMetricsSerializeWrapper {
34 pub fn new() -> Self {
35 TimerMetricsSerializeWrapper::default()
36 }
37
38 pub fn with_timing_count(mut self, timing_count: HashMap<CheetahString, Metric>) -> Self {
39 self.timing_count = timing_count;
40 self
41 }
42
43 pub fn with_data_version(mut self, data_version: DataVersion) -> Self {
44 self.data_version = data_version;
45 self
46 }
47
48 pub fn timing_count(&self) -> &HashMap<CheetahString, Metric> {
49 &self.timing_count
50 }
51
52 pub fn data_version(&self) -> &DataVersion {
53 &self.data_version
54 }
55
56 pub fn data_version_mut(&mut self) -> &mut DataVersion {
57 &mut self.data_version
58 }
59
60 pub fn timing_count_mut(&mut self) -> &mut HashMap<CheetahString, Metric> {
61 &mut self.timing_count
62 }
63
64 pub fn insert_metric(&mut self, key: CheetahString, metric: Metric) {
65 self.timing_count.insert(key, metric);
66 }
67
68 pub fn get_metric(&self, key: &CheetahString) -> Option<&Metric> {
69 self.timing_count.get(key)
70 }
71
72 pub fn get_metric_mut(&mut self, key: &CheetahString) -> Option<&mut Metric> {
73 self.timing_count.get_mut(key)
74 }
75}
76
77impl Default for TimerMetricsSerializeWrapper {
78 fn default() -> Self {
79 TimerMetricsSerializeWrapper {
80 timing_count: HashMap::with_capacity(1024),
81 data_version: DataVersion::default(),
82 }
83 }
84}
85
86#[derive(Debug, serde::Serialize, serde::Deserialize)]
87#[serde(rename_all = "camelCase")]
88pub struct Metric {
89 pub count: AtomicU64,
90 pub time_stamp: u64,
91}
92
93impl Default for Metric {
94 fn default() -> Self {
95 Metric {
96 count: AtomicU64::new(0),
97 time_stamp: get_current_millis(),
98 }
99 }
100}
101
102impl Display for Metric {
103 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
104 write!(
105 f,
106 "Metric {{ count: {}, time_stamp: {} }}",
107 self.count.load(std::sync::atomic::Ordering::Relaxed),
108 self.time_stamp
109 )
110 }
111}