Skip to content

Commit 01d9cb4

Browse files
committed
style: make dashboard more concise
1 parent 77b8527 commit 01d9cb4

File tree

2 files changed

+32
-110
lines changed

2 files changed

+32
-110
lines changed

src/cmd/dashboard/app.rs

+25-101
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
const ITEM_HEIGHT: usize = 6;
22
const INFO_TEXT: [&str; 1] = ["(Esc|q) quit | (↑) move up | (↓) move down"];
33

4-
use crate::balancer::upstream_peer::UpstreamPeer;
5-
use crate::balancer::upstream_peer_pool::UpstreamPeerPool;
6-
use crate::errors::result::Result;
74
use chrono::{DateTime, Utc};
85
use io::Result as ioResult;
9-
use ratatui::layout::{Constraint, Layout, Margin, Rect};
10-
use ratatui::style::{Modifier, Style, Stylize};
11-
use ratatui::text::Text;
12-
use ratatui::widgets::{
13-
Block, BorderType, Cell, HighlightSpacing, Paragraph, Row, Scrollbar, ScrollbarOrientation,
14-
ScrollbarState, Table, TableState,
6+
use ratatui::{
7+
layout::{Constraint, Layout, Margin, Rect},
8+
style::{Modifier, Style, Stylize},
9+
text::Text,
10+
widgets::{
11+
Cell, HighlightSpacing, Paragraph, Row, Scrollbar, ScrollbarOrientation,
12+
ScrollbarState, Table, TableState,
13+
},
14+
Frame,
1515
};
16-
use ratatui::Frame;
1716
use std::{
1817
io,
1918
time::{SystemTime, UNIX_EPOCH},
2019
};
2120

2221
use super::ui::TableColors;
2322

23+
use crate::{
24+
balancer::{
25+
upstream_peer::UpstreamPeer,
26+
upstream_peer_pool::UpstreamPeerPool,
27+
},
28+
errors::result::Result,
29+
};
30+
2431
pub struct App {
2532
pub colors: TableColors,
2633
pub is_initial_load: bool,
2734
pub items: Option<Vec<UpstreamPeer>>,
28-
pub longest_item_lens: (u16, u16, u16, u16, u16, u16),
2935
pub scroll_state: ScrollbarState,
3036
pub state: TableState,
3137
pub ticks: u128,
@@ -38,7 +44,6 @@ impl App {
3844
colors: TableColors::new(),
3945
is_initial_load: true,
4046
items: None,
41-
longest_item_lens: (0, 0, 0, 0, 0, 0),
4247
scroll_state: ScrollbarState::new(0),
4348
state: TableState::default().with_selected(0),
4449
ticks: 0,
@@ -93,8 +98,8 @@ impl App {
9398
pub fn draw(&mut self, frame: &mut Frame) -> ioResult<()> {
9499
let vertical = &Layout::vertical([
95100
Constraint::Min(5),
96-
Constraint::Length(3),
97-
Constraint::Length(3),
101+
Constraint::Length(1),
102+
Constraint::Length(1),
98103
]);
99104
let rects = vertical.split(frame.area());
100105

@@ -116,12 +121,7 @@ impl App {
116121
.bg(self.colors.buffer_bg)
117122
.white(),
118123
)
119-
.centered()
120-
.block(
121-
Block::bordered()
122-
.border_type(BorderType::Double)
123-
.border_style(Style::new().fg(self.colors.footer_border_color)),
124-
);
124+
.centered();
125125
frame.render_widget(info_footer, area);
126126
}
127127

@@ -133,7 +133,7 @@ impl App {
133133

134134
frame.render_widget(t, area);
135135
} else {
136-
match self.items.clone() {
136+
match &self.items {
137137
Some(items) => match items.is_empty() {
138138
true => {
139139
let t = Paragraph::new("There are no agents registered. If agents are running, please give them a few seconds to register.".to_string().white())
@@ -177,35 +177,22 @@ impl App {
177177
items
178178
.into_iter()
179179
.map(|content| {
180-
Cell::from(Text::from(format!("\n{content}\n")).white())
180+
Cell::from(Text::from(content).white())
181181
})
182182
.collect::<Row>()
183183
.style(Style::new().fg(self.colors.row_fg).bg(color))
184-
.height(4)
184+
.height(1)
185185
});
186186

187-
self.longest_item_lens = constraint_len_calculator(items.clone())
188-
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;
189-
190187
let bar = " █ ";
191188
let t = Table::new(
192189
rows,
193-
[
194-
Constraint::Min(self.longest_item_lens.0),
195-
Constraint::Min(self.longest_item_lens.1),
196-
Constraint::Min(self.longest_item_lens.2),
197-
Constraint::Min(self.longest_item_lens.3),
198-
Constraint::Min(self.longest_item_lens.4),
199-
Constraint::Min(self.longest_item_lens.5),
200-
],
190+
[Constraint::Ratio(1, 6); 6],
201191
)
202192
.header(header)
203193
.row_highlight_style(selected_row_style)
204194
.highlight_symbol(Text::from(vec![
205-
"".into(),
206-
bar.into(),
207195
bar.into(),
208-
"".into(),
209196
]))
210197
.bg(self.colors.buffer_bg)
211198
.highlight_spacing(HighlightSpacing::Always)
@@ -254,12 +241,7 @@ impl App {
254241
.bg(self.colors.buffer_bg)
255242
.white(),
256243
)
257-
.centered()
258-
.block(
259-
Block::bordered()
260-
.border_type(BorderType::Double)
261-
.border_style(Style::new().fg(self.colors.footer_border_color)),
262-
);
244+
.centered();
263245
frame.render_widget(info_footer, area);
264246
}
265247

@@ -278,64 +260,6 @@ impl App {
278260
}
279261
}
280262

281-
fn constraint_len_calculator(items: Vec<UpstreamPeer>) -> Result<(u16, u16, u16, u16, u16, u16)> {
282-
let mut name = 0;
283-
for item in &items {
284-
if let Some(agent_name) = item.agent_name.clone() {
285-
if agent_name.len() > name {
286-
name += agent_name.len()
287-
}
288-
}
289-
}
290-
291-
let mut error = 0;
292-
for item in &items {
293-
if let Some(agent_error) = item.error.clone() {
294-
if agent_error.len() > error {
295-
error += agent_error.len()
296-
}
297-
}
298-
}
299-
300-
let mut addr = 0;
301-
for item in &items {
302-
if item.external_llamacpp_addr.to_string().len() > addr {
303-
addr += item.external_llamacpp_addr.to_string().len()
304-
}
305-
}
306-
307-
let mut slots_idle = 0;
308-
for item in &items {
309-
if item.slots_idle.to_string().len() > slots_idle {
310-
slots_idle += item.slots_idle.to_string().len()
311-
}
312-
}
313-
314-
let mut slots_processing = 0;
315-
for item in &items {
316-
if item.slots_processing.to_string().len() > slots_processing {
317-
slots_processing += item.slots_processing.to_string().len()
318-
}
319-
}
320-
321-
let mut last_update = 0;
322-
for item in &items {
323-
if systemtime_strftime(item.last_update)?.len() > last_update {
324-
last_update += systemtime_strftime(item.last_update)?.len()
325-
}
326-
}
327-
328-
#[allow(clippy::cast_possible_truncation)]
329-
Ok((
330-
name as u16,
331-
error as u16,
332-
addr as u16,
333-
last_update as u16,
334-
slots_idle as u16,
335-
slots_processing as u16,
336-
))
337-
}
338-
339263
fn ref_array(peer: UpstreamPeer) -> Result<[String; 6]> {
340264
let has_issue = match peer.error.clone() {
341265
Some(issue) => issue,

src/cmd/dashboard/ui.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@ pub struct TableColors {
55
pub buffer_bg: Color,
66
pub header_bg: Color,
77
pub header_fg: Color,
8+
pub normal_row_color: Color,
89
pub row_fg: Color,
910
pub selected_row_style_fg: Color,
10-
pub normal_row_color: Color,
11-
pub footer_border_color: Color,
1211
}
1312

1413
impl TableColors {
1514
pub const fn new() -> Self {
1615
Self {
17-
buffer_bg: tailwind::SLATE.c950,
18-
header_bg: tailwind::SLATE.c950,
19-
header_fg: tailwind::SLATE.c950,
20-
row_fg: tailwind::SLATE.c950,
21-
selected_row_style_fg: tailwind::GRAY.c400,
22-
normal_row_color: tailwind::SLATE.c950,
23-
footer_border_color: tailwind::SLATE.c950,
16+
buffer_bg: tailwind::NEUTRAL.c950,
17+
header_bg: tailwind::NEUTRAL.c950,
18+
header_fg: tailwind::NEUTRAL.c950,
19+
row_fg: tailwind::NEUTRAL.c950,
20+
selected_row_style_fg: tailwind::NEUTRAL.c400,
21+
normal_row_color: tailwind::NEUTRAL.c950,
2422
}
2523
}
2624
}

0 commit comments

Comments
 (0)