Skip to content

Commit d2e818d

Browse files
authored
feat: implement table information collection (#19)
1 parent 90f9cc0 commit d2e818d

File tree

10 files changed

+1000
-27
lines changed

10 files changed

+1000
-27
lines changed

src/tools/be/memz.rs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::be_http_client;
22
use crate::config::Config;
33
use crate::error::Result;
4+
use crate::tools::common::format_utils;
45
use crate::tools::{ExecutionResult, Tool};
56
use crate::ui;
67
use chrono::Utc;
@@ -102,23 +103,6 @@ impl Tool for MemzGlobalTool {
102103
}
103104
}
104105

105-
/// Format bytes to a human-readable string
106-
fn format_bytes(bytes: u64) -> String {
107-
const KB: u64 = 1024;
108-
const MB: u64 = KB * 1024;
109-
const GB: u64 = MB * 1024;
110-
111-
if bytes >= GB {
112-
format!("{:.2} GB ({bytes} bytes)", bytes as f64 / GB as f64)
113-
} else if bytes >= MB {
114-
format!("{:.2} MB ({bytes} bytes)", bytes as f64 / MB as f64)
115-
} else if bytes >= KB {
116-
format!("{:.2} KB ({bytes} bytes)", bytes as f64 / KB as f64)
117-
} else {
118-
format!("{bytes} bytes")
119-
}
120-
}
121-
122106
/// Extract memory metrics from the HTML response
123107
fn extract_memory_metrics(html_content: &str) -> (String, String) {
124108
let re = Regex::new(r"Allocated: (\d+), active: (\d+), metadata: (\d+).*?, resident: (\d+), mapped: (\d+), retained: (\d+)").unwrap();
@@ -141,27 +125,27 @@ fn extract_memory_metrics(html_content: &str) -> (String, String) {
141125
{
142126
let caps = re.captures(html_content).unwrap();
143127
if let Some(bytes) = caps.get(1).and_then(|m| m.as_str().parse::<u64>().ok()) {
144-
allocated = format_bytes(bytes);
128+
allocated = format_utils::format_bytes(bytes, 2, true);
145129
}
146130

147131
if let Some(bytes) = caps.get(2).and_then(|m| m.as_str().parse::<u64>().ok()) {
148-
active = format_bytes(bytes);
132+
active = format_utils::format_bytes(bytes, 2, true);
149133
}
150134

151135
if let Some(bytes) = caps.get(3).and_then(|m| m.as_str().parse::<u64>().ok()) {
152-
metadata = format_bytes(bytes);
136+
metadata = format_utils::format_bytes(bytes, 2, true);
153137
}
154138

155139
if let Some(bytes) = caps.get(4).and_then(|m| m.as_str().parse::<u64>().ok()) {
156-
resident = format_bytes(bytes);
140+
resident = format_utils::format_bytes(bytes, 2, true);
157141
}
158142

159143
if let Some(bytes) = caps.get(5).and_then(|m| m.as_str().parse::<u64>().ok()) {
160-
mapped = format_bytes(bytes);
144+
mapped = format_utils::format_bytes(bytes, 2, true);
161145
}
162146

163147
if let Some(bytes) = caps.get(6).and_then(|m| m.as_str().parse::<u64>().ok()) {
164-
retained = format_bytes(bytes);
148+
retained = format_utils::format_bytes(bytes, 2, true);
165149
}
166150
}
167151

@@ -170,15 +154,15 @@ fn extract_memory_metrics(html_content: &str) -> (String, String) {
170154
.and_then(|caps| caps.get(1))
171155
.and_then(|m| m.as_str().parse::<u64>().ok())
172156
{
173-
thread_cache = format_bytes(bytes);
157+
thread_cache = format_utils::format_bytes(bytes, 2, true);
174158
}
175159

176160
if let Some(bytes) = dirty_pages_re
177161
.captures(html_content)
178162
.and_then(|caps| caps.get(1))
179163
.and_then(|m| m.as_str().parse::<u64>().ok())
180164
{
181-
dirty_pages = format_bytes(bytes);
165+
dirty_pages = format_utils::format_bytes(bytes, 2, true);
182166
}
183167

184168
let table = format!(

src/tools/common/format_utils.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// Format bytes to a human-readable string with customizable precision and format
2+
pub fn format_bytes(bytes: u64, precision: usize, show_original: bool) -> String {
3+
const KB: f64 = 1024.0;
4+
const MB: f64 = KB * 1024.0;
5+
const GB: f64 = MB * 1024.0;
6+
7+
if bytes >= GB as u64 {
8+
let formatted = format!("{:.precision$} GB", bytes as f64 / GB);
9+
if show_original {
10+
format!("{} ({bytes} bytes)", formatted)
11+
} else {
12+
formatted
13+
}
14+
} else if bytes >= MB as u64 {
15+
let formatted = format!("{:.precision$} MB", bytes as f64 / MB);
16+
if show_original {
17+
format!("{} ({bytes} bytes)", formatted)
18+
} else {
19+
formatted
20+
}
21+
} else if bytes >= KB as u64 {
22+
let formatted = format!("{:.precision$} KB", bytes as f64 / KB);
23+
if show_original {
24+
format!("{} ({bytes} bytes)", formatted)
25+
} else {
26+
formatted
27+
}
28+
} else if show_original {
29+
format!("{bytes} bytes")
30+
} else {
31+
format!("{} B", bytes)
32+
}
33+
}

src/tools/common/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
pub mod format_utils;
12
pub mod fs_utils;
23
pub mod jmap;

src/tools/fe/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ mod jmap;
22
mod jstack;
33
mod profiler;
44
pub mod routine_load;
5+
pub mod table_info;
56

67
pub use jmap::{JmapDumpTool, JmapHistoTool};
78
pub use jstack::JstackTool;
89
pub use profiler::FeProfilerTool;
910
pub use routine_load::{RoutineLoadJobLister, get_routine_load_tools};
11+
pub use table_info::{FeTableInfoTool, TableIdentity, TableInfoReport};

0 commit comments

Comments
 (0)