Skip to main content

rocketmq_model/common/running/
running_stats.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
15/// Stats tracking various runtime metrics of RocketMQ
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub enum RunningStats {
18    /// Maximum offset in the commit log
19    CommitLogMaxOffset,
20
21    /// Minimum offset in the commit log
22    CommitLogMinOffset,
23
24    /// Disk usage ratio for commit log
25    CommitLogDiskRatio,
26
27    /// Disk usage ratio for consume queue
28    ConsumeQueueDiskRatio,
29
30    /// Offset for scheduled messages
31    ScheduleMessageOffset,
32}
33
34impl RunningStats {
35    /// Get the string representation of the enum value
36    pub fn as_str(&self) -> &'static str {
37        match self {
38            RunningStats::CommitLogMaxOffset => "commitLogMaxOffset",
39            RunningStats::CommitLogMinOffset => "commitLogMinOffset",
40            RunningStats::CommitLogDiskRatio => "commitLogDiskRatio",
41            RunningStats::ConsumeQueueDiskRatio => "consumeQueueDiskRatio",
42            RunningStats::ScheduleMessageOffset => "scheduleMessageOffset",
43        }
44    }
45}
46
47impl std::fmt::Display for RunningStats {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        write!(f, "{}", self.as_str())
50    }
51}
52
53impl std::str::FromStr for RunningStats {
54    type Err = String;
55
56    fn from_str(s: &str) -> Result<Self, Self::Err> {
57        match s {
58            "commitLogMaxOffset" => Ok(RunningStats::CommitLogMaxOffset),
59            "commitLogMinOffset" => Ok(RunningStats::CommitLogMinOffset),
60            "commitLogDiskRatio" => Ok(RunningStats::CommitLogDiskRatio),
61            "consumeQueueDiskRatio" => Ok(RunningStats::ConsumeQueueDiskRatio),
62            "scheduleMessageOffset" => Ok(RunningStats::ScheduleMessageOffset),
63            _ => Err(format!("Unknown RunningStats value: {s}")),
64        }
65    }
66}