summaryrefslogtreecommitdiffstats
path: root/src/display/ui.rs
blob: a3f9b58ea7ca212c7ae11dd9f884ad107c47ae31 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use ::std::collections::HashMap;
use ::std::fmt;
use ::tui::backend::Backend;
use ::tui::layout::{Constraint, Direction, Layout, Rect};
use ::tui::style::{Color, Style};
use ::tui::terminal::Frame;
use ::tui::widgets::{Block, Borders, Row, Table, Widget};
use ::tui::Terminal;

use crate::display::{Bandwidth, UIState};
use crate::network::{Connection, Utilization};

use ::std::net::Ipv4Addr;

struct DisplayBandwidth(f64);

impl fmt::Display for DisplayBandwidth {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.0 > 999_999_999.0 {
            write!(f, "{:.2}GBps", self.0 / 1_000_000_000.0)
        } else if self.0 > 999_999.0 {
            write!(f, "{:.2}MBps", self.0 / 1_000_000.0)
        } else if self.0 > 999.0 {
            write!(f, "{:.2}KBps", self.0 / 1000.0)
        } else {
            write!(f, "{}Bps", self.0)
        }
    }
}

fn display_ip_or_host(ip: Ipv4Addr, ip_to_host: &HashMap<Ipv4Addr, String>) -> String {
    match ip_to_host.get(&ip) {
        Some(host) => host.clone(),
        None => ip.to_string(),
    }
}

fn create_table<'a>(
    title: &'a str,
    column_names: &'a [&'a str],
    rows: impl Iterator<Item = Vec<String>> + 'a,
    widths: &'a [u16],
) -> impl Widget + 'a {
    let table_rows =
        rows.map(|row| Row::StyledData(row.into_iter(), Style::default().fg(Color::White)));
    Table::new(column_names.iter(), table_rows)
        .block(Block::default().title(title).borders(Borders::ALL))
        .header_style(Style::default().fg(Color::Yellow))
        .widths(widths)
        .style(Style::default().fg(Color::White))
        .column_spacing(1)
}

fn format_row_data(
    first_cell: String,
    second_cell: String,
    bandwidth: &impl Bandwidth,
) -> Vec<String> {
    vec![
        first_cell,
        second_cell,
        format!(
            "{}/{}",
            DisplayBandwidth(bandwidth.get_total_bytes_uploaded() as f64),
            DisplayBandwidth(bandwidth.get_total_bytes_downloaded() as f64)
        ),
    ]
}

fn split(direction: Direction, rect: Rect) -> Vec<Rect> {
    Layout::default()
        .direction(direction)
        .margin(0)
        .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
        .split(rect)
}

fn render_process_table(state: &UIState, frame: &mut Frame<impl Backend>, rect: Rect) {
    let rows = state
        .processes
        .iter()
        .map(|(process_name, data_for_process)| {
            format_row_data(
                process_name.to_string(),
                data_for_process.connection_count.to_string(),
                data_for_process,
            )
        });
    let mut table = create_table(
        "Utilization by process name",
        &["Process", "Connection Count", "Total Bytes"],
        rows,
        &[30, 30, 30],
    );
    table.render(frame, rect);
}

fn render_connections_table(
    state: &UIState,
    frame: &mut Frame<impl Backend>,
    rect: Rect,
    ip_to_host: &HashMap<Ipv4Addr, String>,
) {
    let rows = state
        .connections
        .iter()
        .map(|(connection, connection_data)| {
            let connection_string = format!(
                "{}:{} => {}:{} ({})",
                display_ip_or_host(connection.local_socket.ip, ip_to_host),
                connection.local_socket.port,
                display_ip_or_host(connection.remote_socket.ip, ip_to_host),
                connection.remote_socket.port,
                connection.protocol,
            );
            format_row_data(
                connection_string,
                connection_data.process_name.to_string(),
                connection_data,
            )
        });
    let mut table = create_table(
        "Utilization by connection",
        &["Connection", "Processes", "Total Bytes Up/Down"],
        rows,
        &[50, 20, 20],
    );
    table.render(frame, rect);
}

fn render_remote_ip_table(
    state: &UIState,
    frame: &mut Frame<impl Backend>,
    rect: Rect,
    ip_to_host: &HashMap<Ipv4Addr, String>,
) {
    let rows = state
        .remote_ips
        .iter()
        .map(|(remote_ip, data_for_remote_ip)| {
            let remote_ip = display_ip_or_host(*remote_ip, &ip_to_host);
            format_row_data(
                remote_ip,
                data_for_remote_ip.connection_count.to_string(),
                data_for_remote_ip,
            )
        });
    let mut table = create_table(
        "Utilization by remote ip",
        &["Remote Address", "Connection Count", "Total Bytes"],
        rows,
        &[50, 20, 20],
    );
    table.render(frame, rect);
}

pub struct Ui<B>
where
    B: Backend,
{
    terminal: Terminal<B>,
    state: UIState,
    ip_to_host: HashMap<Ipv4Addr, String>,
}

impl<B> Ui<B>
where
    B: Backend,
{
    pub fn new(terminal_backend: B) -> Self {
        let mut terminal = Terminal::new(terminal_backend).unwrap();
        terminal.clear().unwrap();
        terminal.hide_cursor().unwrap();
        Ui {
            terminal,
            state: Default::default(),
            ip_to_host: Default::default(),
        }
    }
    pub fn draw(&mut self) {
        let state = &self.state;
        let ip_to_host = &self.ip_to_host;
        self.terminal
            .draw(|mut f| {
                let screen_horizontal_halves = split(Direction::Horizontal, f.size());
                let right_side_vertical_halves =
                    split(Direction::Vertical, screen_horizontal_halves[1]);
                render_connections_table(state, &mut f, screen_horizontal_halves[0], ip_to_host);
                render_process_table(state, &mut f, right_side_vertical_halves[0]);
                render_remote_ip_table(state, &mut f, right_side_vertical_halves[1], ip_to_host);
            })
            .unwrap();
    }
    pub fn update_state(
        &mut self,
        connections_to_procs: HashMap<Connection, String>,
        utilization: Utilization,
        ip_to_host: HashMap<Ipv4Addr, String>,
    ) {
        self.state = UIState::new(connections_to_procs, utilization);
        self.ip_to_host = ip_to_host;
    }
    pub fn end(&mut self) {
        // TODO: destroy?
        self.terminal.clear().unwrap();
        self.terminal.show_cursor().unwrap();
    }
}