Implemented full system browsing support with logs, events, and tasks.
This commit is contained in:
+80
-101
@@ -1,31 +1,45 @@
|
||||
use crate::ui::utils::{layout_block_top_border, style_help, style_primary, style_secondary};
|
||||
use crate::ui::{draw_table, TableProps};
|
||||
use crate::models::radarr_models::Task;
|
||||
use crate::ui::radarr_ui::radarr_ui_utils::{
|
||||
convert_to_minutes_hours_days, determine_log_style_by_level,
|
||||
};
|
||||
use crate::ui::utils::{layout_block_top_border, style_help, style_primary};
|
||||
use crate::ui::{draw_table, ListProps, TableProps};
|
||||
use crate::{
|
||||
app::{radarr::ActiveRadarrBlock, App},
|
||||
models::Route,
|
||||
ui::{
|
||||
draw_list_box,
|
||||
utils::{horizontal_chunks, style_default, style_failure, title_block, vertical_chunks},
|
||||
utils::{horizontal_chunks, title_block, vertical_chunks},
|
||||
DrawUi,
|
||||
},
|
||||
};
|
||||
use chrono::Utc;
|
||||
use std::ops::Sub;
|
||||
use tui::layout::Alignment;
|
||||
use tui::style::Modifier;
|
||||
use tui::text::{Span, Text};
|
||||
use tui::widgets::{Cell, Paragraph, Row};
|
||||
use tui::{
|
||||
backend::Backend,
|
||||
layout::{Constraint, Rect},
|
||||
style::{Color, Style},
|
||||
widgets::ListItem,
|
||||
Frame,
|
||||
};
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "system_ui_tests.rs"]
|
||||
mod system_ui_tests;
|
||||
pub(super) const TASK_TABLE_HEADERS: [&str; 5] = [
|
||||
"Name",
|
||||
"Interval",
|
||||
"Last Execution",
|
||||
"Last Duration",
|
||||
"Next Execution",
|
||||
];
|
||||
|
||||
pub(super) const TASK_TABLE_CONSTRAINTS: [Constraint; 5] = [
|
||||
Constraint::Percentage(30),
|
||||
Constraint::Percentage(12),
|
||||
Constraint::Percentage(18),
|
||||
Constraint::Percentage(18),
|
||||
Constraint::Percentage(22),
|
||||
];
|
||||
|
||||
pub(super) struct SystemUi {}
|
||||
|
||||
@@ -40,7 +54,11 @@ impl DrawUi for SystemUi {
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_system_ui_layout<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect) {
|
||||
pub(super) fn draw_system_ui_layout<B: Backend>(
|
||||
f: &mut Frame<'_, B>,
|
||||
app: &mut App<'_>,
|
||||
area: Rect,
|
||||
) {
|
||||
let vertical_chunks = vertical_chunks(
|
||||
vec![
|
||||
Constraint::Ratio(1, 2),
|
||||
@@ -56,54 +74,31 @@ fn draw_system_ui_layout<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, ar
|
||||
);
|
||||
|
||||
draw_tasks(f, app, horizontal_chunks[0]);
|
||||
draw_events(f, app, horizontal_chunks[1]);
|
||||
draw_queued_events(f, app, horizontal_chunks[1]);
|
||||
draw_logs(f, app, vertical_chunks[1]);
|
||||
draw_help(f, app, vertical_chunks[2]);
|
||||
}
|
||||
|
||||
fn draw_tasks<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect) {
|
||||
let block = title_block("Tasks");
|
||||
draw_table(
|
||||
f,
|
||||
area,
|
||||
block,
|
||||
title_block("Tasks"),
|
||||
TableProps {
|
||||
content: &mut app.data.radarr_data.tasks,
|
||||
table_headers: vec![
|
||||
"Name",
|
||||
"Interval",
|
||||
"Last Execution",
|
||||
"Last Duration",
|
||||
"Next Execution",
|
||||
],
|
||||
constraints: vec![
|
||||
Constraint::Percentage(30),
|
||||
Constraint::Percentage(12),
|
||||
Constraint::Percentage(18),
|
||||
Constraint::Percentage(18),
|
||||
Constraint::Percentage(22),
|
||||
],
|
||||
table_headers: TASK_TABLE_HEADERS.to_vec(),
|
||||
constraints: TASK_TABLE_CONSTRAINTS.to_vec(),
|
||||
help: None,
|
||||
},
|
||||
|task| {
|
||||
let interval = convert_to_minutes_hours_days(*task.interval.as_i64().as_ref().unwrap());
|
||||
let last_duration = &task.last_duration[..8];
|
||||
let next_execution =
|
||||
convert_to_minutes_hours_days(task.next_execution.sub(Utc::now()).num_minutes());
|
||||
let last_execution =
|
||||
convert_to_minutes_hours_days(Utc::now().sub(task.last_execution).num_minutes());
|
||||
let last_execution_string = if last_execution != "now" {
|
||||
format!("{} ago", last_execution)
|
||||
} else {
|
||||
last_execution
|
||||
};
|
||||
let task_props = extract_task_props(task);
|
||||
|
||||
Row::new(vec![
|
||||
Cell::from(task.name.clone()),
|
||||
Cell::from(interval),
|
||||
Cell::from(last_execution_string),
|
||||
Cell::from(last_duration.to_owned()),
|
||||
Cell::from(next_execution),
|
||||
Cell::from(task_props.name),
|
||||
Cell::from(task_props.interval),
|
||||
Cell::from(task_props.last_execution),
|
||||
Cell::from(task_props.last_duration),
|
||||
Cell::from(task_props.next_execution),
|
||||
])
|
||||
.style(style_primary())
|
||||
},
|
||||
@@ -112,14 +107,13 @@ fn draw_tasks<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect) {
|
||||
);
|
||||
}
|
||||
|
||||
fn draw_events<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect) {
|
||||
let block = title_block("Events");
|
||||
pub(super) fn draw_queued_events<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect) {
|
||||
draw_table(
|
||||
f,
|
||||
area,
|
||||
block,
|
||||
title_block("Queued Events"),
|
||||
TableProps {
|
||||
content: &mut app.data.radarr_data.events,
|
||||
content: &mut app.data.radarr_data.queued_events,
|
||||
table_headers: vec!["Trigger", "Status", "Name", "Queued", "Started", "Duration"],
|
||||
constraints: vec![
|
||||
Constraint::Percentage(13),
|
||||
@@ -151,7 +145,11 @@ fn draw_events<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect)
|
||||
String::new()
|
||||
};
|
||||
|
||||
let duration = &event.duration[..8];
|
||||
let duration = if event.duration.is_some() {
|
||||
&event.duration.as_ref().unwrap()[..8]
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
Row::new(vec![
|
||||
Cell::from(event.trigger.clone()),
|
||||
@@ -172,31 +170,20 @@ fn draw_logs<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect) {
|
||||
draw_list_box(
|
||||
f,
|
||||
area,
|
||||
&mut app.data.radarr_data.logs,
|
||||
"Logs",
|
||||
|log| {
|
||||
let log_line = if log.exception.is_some() {
|
||||
Text::from(Span::raw(format!(
|
||||
"{}|{}|{}|{}|{}",
|
||||
log.time,
|
||||
log.level.as_ref().unwrap().to_uppercase(),
|
||||
log.logger.as_ref().unwrap(),
|
||||
log.exception_type.as_ref().unwrap(),
|
||||
log.exception.as_ref().unwrap()
|
||||
)))
|
||||
} else {
|
||||
Text::from(Span::raw(format!(
|
||||
"{}|{}|{}|{}",
|
||||
log.time,
|
||||
log.level.as_ref().unwrap().to_uppercase(),
|
||||
log.logger.as_ref().unwrap(),
|
||||
log.message.as_ref().unwrap()
|
||||
)))
|
||||
};
|
||||
let log_line = log.to_string();
|
||||
let level = log_line.split('|').collect::<Vec<&str>>()[1];
|
||||
let style = determine_log_style_by_level(level);
|
||||
|
||||
ListItem::new(log_line).style(determine_log_style_by_level(log.level.as_ref().unwrap()))
|
||||
ListItem::new(Text::from(Span::raw(log_line))).style(style)
|
||||
},
|
||||
ListProps {
|
||||
content: &mut app.data.radarr_data.logs,
|
||||
title: "Logs",
|
||||
is_loading: app.is_loading,
|
||||
is_popup: false,
|
||||
help: None,
|
||||
},
|
||||
app.is_loading,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -218,40 +205,32 @@ fn draw_help<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect) {
|
||||
f.render_widget(help_paragraph, area);
|
||||
}
|
||||
|
||||
fn determine_log_style_by_level(level: &str) -> Style {
|
||||
match level.to_lowercase().as_str() {
|
||||
"trace" => Style::default().fg(Color::Gray),
|
||||
"debug" => Style::default().fg(Color::Blue),
|
||||
"info" => style_default(),
|
||||
"warn" => style_secondary(),
|
||||
"error" => style_failure(),
|
||||
"fatal" => style_failure().add_modifier(Modifier::BOLD),
|
||||
_ => style_default(),
|
||||
}
|
||||
pub(super) struct TaskProps {
|
||||
pub(super) name: String,
|
||||
pub(super) interval: String,
|
||||
pub(super) last_execution: String,
|
||||
pub(super) last_duration: String,
|
||||
pub(super) next_execution: String,
|
||||
}
|
||||
|
||||
fn convert_to_minutes_hours_days(time: i64) -> String {
|
||||
if time < 60 {
|
||||
if time == 0 {
|
||||
"now".to_owned()
|
||||
} else if time == 1 {
|
||||
format!("{} minute", time)
|
||||
} else {
|
||||
format!("{} minutes", time)
|
||||
}
|
||||
} else if time / 60 < 24 {
|
||||
let hours = time / 60;
|
||||
if hours == 1 {
|
||||
format!("{} hour", hours)
|
||||
} else {
|
||||
format!("{} hours", hours)
|
||||
}
|
||||
pub(super) fn extract_task_props(task: &Task) -> TaskProps {
|
||||
let interval = convert_to_minutes_hours_days(*task.interval.as_i64().as_ref().unwrap());
|
||||
let last_duration = &task.last_duration[..8];
|
||||
let next_execution =
|
||||
convert_to_minutes_hours_days((task.next_execution - Utc::now()).num_minutes());
|
||||
let last_execution =
|
||||
convert_to_minutes_hours_days((Utc::now() - task.last_execution).num_minutes());
|
||||
let last_execution_string = if last_execution != "now" {
|
||||
format!("{} ago", last_execution)
|
||||
} else {
|
||||
let days = time / (60 * 24);
|
||||
if days == 1 {
|
||||
format!("{} day", days)
|
||||
} else {
|
||||
format!("{} days", days)
|
||||
}
|
||||
last_execution
|
||||
};
|
||||
|
||||
TaskProps {
|
||||
name: task.name.clone(),
|
||||
interval,
|
||||
last_execution: last_execution_string,
|
||||
last_duration: last_duration.to_owned(),
|
||||
next_execution,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user