Skip to main content

rocketmq_protocol/protocol/subscription/
broker_stats_data.rs

1// Copyright 2023 The RocketMQ Rust Authors
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 serde::Deserialize;
16use serde::Serialize;
17
18use crate::protocol::body::broker_stats_item::BrokerStatsItem;
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
21#[serde(rename_all = "camelCase")]
22/// Represents broker statistics over different time periods (minute, hour, day)
23pub struct BrokerStatsData {
24    /// Statistics for the last minute
25    stats_minute: BrokerStatsItem,
26    /// Statistics for the last hour
27    stats_hour: BrokerStatsItem,
28    /// Statistics for the last day
29    stats_day: BrokerStatsItem,
30}
31
32impl BrokerStatsData {
33    pub fn new(stats_minute: BrokerStatsItem, stats_hour: BrokerStatsItem, stats_day: BrokerStatsItem) -> Self {
34        Self {
35            stats_minute,
36            stats_hour,
37            stats_day,
38        }
39    }
40
41    pub fn get_stats_minute(&self) -> &BrokerStatsItem {
42        &self.stats_minute
43    }
44
45    pub fn set_stats_minute(&mut self, stats_minute: BrokerStatsItem) {
46        self.stats_minute = stats_minute;
47    }
48
49    pub fn get_stats_hour(&self) -> &BrokerStatsItem {
50        &self.stats_hour
51    }
52
53    pub fn set_stats_hour(&mut self, stats_hour: BrokerStatsItem) {
54        self.stats_hour = stats_hour;
55    }
56
57    pub fn get_stats_day(&self) -> &BrokerStatsItem {
58        &self.stats_day
59    }
60
61    pub fn set_stats_day(&mut self, stats_day: BrokerStatsItem) {
62        self.stats_day = stats_day;
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn methods_and_serde_preserve_broker_stats_periods() {
72        let stats_minute = BrokerStatsItem::new(100, 12.5, 8.3);
73        let stats_hour = BrokerStatsItem::new(500, 15.0, 9.0);
74        let stats_day = BrokerStatsItem::new(1000, 20.0, 10.0);
75        let mut broker_stats = BrokerStatsData::new(stats_minute, stats_hour, stats_day);
76
77        assert_eq!(broker_stats.get_stats_minute().get_sum(), 100);
78        assert_eq!(broker_stats.get_stats_hour().get_sum(), 500);
79        assert_eq!(broker_stats.get_stats_day().get_sum(), 1000);
80
81        broker_stats.set_stats_minute(BrokerStatsItem::new(200, 25.0, 12.0));
82        broker_stats.set_stats_hour(BrokerStatsItem::new(600, 18.0, 10.0));
83        broker_stats.set_stats_day(BrokerStatsItem::new(1200, 22.0, 11.0));
84
85        let json = serde_json::to_string(&broker_stats).unwrap();
86        assert_eq!(
87            json,
88            r#"{"statsMinute":{"sum":200,"tps":25.0,"avgpt":12.0},"statsHour":{"sum":600,"tps":18.0,"avgpt":10.0},"statsDay":{"sum":1200,"tps":22.0,"avgpt":11.0}}"#
89        );
90
91        let decoded: BrokerStatsData = serde_json::from_str(&json).unwrap();
92        assert_eq!(decoded.get_stats_minute().get_sum(), 200);
93        assert_eq!(decoded.get_stats_hour().get_tps(), 18.0);
94        assert_eq!(decoded.get_stats_day().get_avgpt(), 11.0);
95    }
96}