Skip to main content

rocketmq_protocol/protocol/body/
queue_time_span.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 chrono::Datelike;
16use chrono::Local;
17use chrono::TimeZone;
18use chrono::Timelike;
19use rocketmq_model::message::MessageQueue;
20use serde::Deserialize;
21use serde::Serialize;
22
23fn time_millis_to_human_string2(timestamp: i64) -> String {
24    Local
25        .timestamp_millis_opt(timestamp)
26        .single()
27        .or_else(|| Local.timestamp_millis_opt(0).single())
28        .map(|value| {
29            format!(
30                "{:04}-{:02}-{:02} {:02}:{:02}:{:02},{:03}",
31                value.year(),
32                value.month(),
33                value.day(),
34                value.hour(),
35                value.minute(),
36                value.second(),
37                value.timestamp_subsec_millis()
38            )
39        })
40        .unwrap_or_default()
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize, Default)]
44pub struct QueueTimeSpan {
45    pub message_queue: Option<MessageQueue>,
46    pub min_time_stamp: i64,
47    pub max_time_stamp: i64,
48    pub consume_time_stamp: i64,
49    pub delay_time: i64,
50}
51
52impl QueueTimeSpan {
53    pub fn get_message_queue(&self) -> Option<MessageQueue> {
54        self.message_queue.clone()
55    }
56
57    pub fn set_message_queue(&mut self, message_queue: MessageQueue) {
58        self.message_queue = Some(message_queue);
59    }
60
61    pub fn get_min_time_stamp(&self) -> i64 {
62        self.min_time_stamp
63    }
64
65    pub fn set_min_time_stamp(&mut self, min_time_stamp: i64) {
66        self.min_time_stamp = min_time_stamp;
67    }
68
69    pub fn get_max_time_stamp(&self) -> i64 {
70        self.max_time_stamp
71    }
72
73    pub fn set_max_time_stamp(&mut self, max_time_stamp: i64) {
74        self.max_time_stamp = max_time_stamp;
75    }
76
77    pub fn get_consume_time_stamp(&self) -> i64 {
78        self.consume_time_stamp
79    }
80
81    pub fn set_consume_time_stamp(&mut self, consume_time_stamp: i64) {
82        self.consume_time_stamp = consume_time_stamp;
83    }
84
85    pub fn get_delay_time(&self) -> i64 {
86        self.delay_time
87    }
88
89    pub fn set_delay_time(&mut self, delay_time: i64) {
90        self.delay_time = delay_time;
91    }
92
93    pub fn get_min_time_stamp_str(&self) -> String {
94        time_millis_to_human_string2(self.min_time_stamp)
95    }
96
97    pub fn get_max_time_stamp_str(&self) -> String {
98        time_millis_to_human_string2(self.max_time_stamp)
99    }
100
101    pub fn get_consume_time_stamp_str(&self) -> String {
102        time_millis_to_human_string2(self.consume_time_stamp)
103    }
104}
105
106#[cfg(test)]
107mod tests {
108
109    use super::*;
110
111    #[test]
112    fn queue_span_serde_round_trip() {
113        let body: QueueTimeSpan = QueueTimeSpan {
114            message_queue: Some(MessageQueue::new()),
115            min_time_stamp: 1,
116            max_time_stamp: 2,
117            consume_time_stamp: 3,
118            delay_time: 4,
119        };
120
121        let json = serde_json::to_string(&body).unwrap();
122        assert_eq!(
123            json,
124            "{\"message_queue\":{\"topic\":\"\",\"brokerName\":\"\",\"queueId\":0},\"min_time_stamp\":1,\"\
125             max_time_stamp\":2,\"consume_time_stamp\":3,\"delay_time\":4}"
126        );
127        let deserialized: QueueTimeSpan = serde_json::from_str(&json).unwrap();
128        assert_eq!(deserialized.consume_time_stamp, 3);
129        assert_eq!(deserialized.delay_time, 4);
130        assert_eq!(deserialized.max_time_stamp, 2);
131        assert_eq!(deserialized.min_time_stamp, 1);
132        assert_eq!(deserialized.message_queue, Some(MessageQueue::new()),);
133    }
134    #[test]
135    fn queue_span_setters() {
136        let mut body: QueueTimeSpan = Default::default();
137        body.set_message_queue(MessageQueue::new());
138        body.set_min_time_stamp(1);
139        body.set_max_time_stamp(2);
140        body.set_consume_time_stamp(3);
141        body.set_delay_time(4);
142
143        assert_eq!(body.get_consume_time_stamp(), 3);
144        assert_eq!(body.get_delay_time(), 4);
145        assert_eq!(body.get_max_time_stamp(), 2);
146        assert_eq!(body.get_min_time_stamp(), 1);
147        assert_eq!(body.get_message_queue(), Some(MessageQueue::new()),);
148    }
149
150    #[test]
151    fn queue_span_time_str() {
152        let body: QueueTimeSpan = QueueTimeSpan {
153            message_queue: Some(MessageQueue::new()),
154            min_time_stamp: 1,
155            max_time_stamp: 2,
156            consume_time_stamp: 3,
157            delay_time: 4,
158        };
159
160        let expected_min = time_millis_to_human_string2(body.get_min_time_stamp());
161        let expected_max = time_millis_to_human_string2(body.get_max_time_stamp());
162        let expected_consume = time_millis_to_human_string2(body.get_consume_time_stamp());
163
164        assert_eq!(body.get_min_time_stamp_str(), expected_min);
165        assert_eq!(body.get_max_time_stamp_str(), expected_max);
166        assert_eq!(body.get_consume_time_stamp_str(), expected_consume);
167    }
168}