-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathflamegraph.rs
More file actions
100 lines (88 loc) · 2.99 KB
/
flamegraph.rs
File metadata and controls
100 lines (88 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use crate::interpreter::clickhouse::Columns;
use crate::pastila;
use anyhow::{Error, Result};
use crossterm::event::{self, Event as CrosstermEvent, KeyEventKind};
use flamelens::app::{App, AppResult};
use flamelens::flame::FlameGraph;
use flamelens::handler::handle_key_events;
use flamelens::ui;
use ratatui::Terminal;
use ratatui::backend::CrosstermBackend;
use std::io;
pub fn show(block: Columns) -> AppResult<()> {
let data = block
.rows()
.map(|x| {
[
x.get::<String, _>(0).unwrap(),
x.get::<u64, _>(1).unwrap().to_string(),
]
.join(" ")
})
.collect::<Vec<String>>()
.join("\n");
if data.trim().is_empty() {
return Err(Error::msg("Flamegraph is empty").into());
}
let flamegraph = FlameGraph::from_string(data, true);
let mut app = App::with_flamegraph("Query", flamegraph);
let backend = CrosstermBackend::new(io::stderr());
let mut terminal = Terminal::new(backend)?;
let timeout = std::time::Duration::from_secs(1);
terminal.clear()?;
// Start the main loop.
while app.running {
terminal.draw(|frame| {
ui::render(&mut app, frame);
if let Some(input_buffer) = &app.input_buffer
&& let Some(cursor) = input_buffer.cursor
{
frame.set_cursor_position((cursor.0, cursor.1));
}
})?;
// FIXME: note, right now I cannot use EventHandle with Tui, since EventHandle is not
// terminated gracefully
if event::poll(timeout).expect("failed to poll new events") {
match event::read().expect("unable to read event") {
CrosstermEvent::Key(e) => {
if e.kind == KeyEventKind::Press {
handle_key_events(e, &mut app)?
}
}
CrosstermEvent::Mouse(_e) => {}
CrosstermEvent::Resize(_w, _h) => {}
CrosstermEvent::FocusGained => {}
CrosstermEvent::FocusLost => {}
CrosstermEvent::Paste(_) => {}
}
}
}
terminal.clear()?;
// ratatui's Terminal::drop may shows the cursor, re-hide it for cursive
drop(terminal);
crossterm::execute!(io::stderr(), crossterm::cursor::Hide)?;
Ok(())
}
pub async fn share(
block: Columns,
pastila_clickhouse_host: &str,
pastila_url: &str,
) -> Result<String> {
let data = block
.rows()
.map(|x| {
[
x.get::<String, _>(0).unwrap(),
x.get::<u64, _>(1).unwrap().to_string(),
]
.join(" ")
})
.collect::<Vec<String>>()
.join("\n");
if data.trim().is_empty() {
return Err(Error::msg("Flamegraph is empty"));
}
let pastila_url =
pastila::upload_encrypted(&data, pastila_clickhouse_host, pastila_url).await?;
return Ok(format!("https://whodidit.you/#profileURL={}", pastila_url));
}