Skip to content

Commit

Permalink
Add server logs view
Browse files Browse the repository at this point in the history
  • Loading branch information
azat committed Mar 31, 2024
1 parent cc3ace7 commit b93875b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 10 deletions.
10 changes: 7 additions & 3 deletions src/interpreter/clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ impl ClickHouse {

pub async fn get_query_logs(
&self,
query_ids: &[String],
query_ids: &Option<Vec<String>>,
start_microseconds: DateTime<Local>,
end_microseconds: Option<DateTime<Local>>,
) -> Result<Columns> {
Expand Down Expand Up @@ -689,7 +689,7 @@ impl ClickHouse {
WHERE
event_date >= toDate(start_time_) AND event_time > toDateTime(start_time_) AND event_time_microseconds > start_time_
AND event_date <= toDate(end_time_) AND event_time <= toDateTime(end_time_) AND event_time_microseconds <= end_time_
AND query_id IN ('{}')
{}
// TODO: if query finished, add filter for event_time end range
ORDER BY event_date, event_time, event_time_microseconds
"#,
Expand All @@ -701,7 +701,11 @@ impl ClickHouse {
.timestamp_nanos_opt()
.ok_or(Error::msg("Invalid end time"))?,
dbtable,
query_ids.join("','"),
if let Some(query_ids) = query_ids {
format!("AND query_id IN ('{}')", query_ids.join("','"))
} else {
"".into()
}
)
.as_str(),
)
Expand Down
2 changes: 2 additions & 0 deletions src/interpreter/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ pub enum ChDigViews {
Backups,
/// Show information about dictionaries (system.dictionaries)
Dictionaries,
/// Show server logs (system.text_log)
ServerLogs,
}

#[derive(Parser, Clone)]
Expand Down
13 changes: 9 additions & 4 deletions src/interpreter/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ pub enum Event {
UpdateSlowQueryLog(String, DateTime<Local>, DateTime<Local>, u64),
// [filter, start, end, limit]
UpdateLastQueryLog(String, DateTime<Local>, DateTime<Local>, u64),
// ([query_ids], start, end)
GetQueryTextLog(Vec<String>, DateTime<Local>, Option<DateTime<Local>>),
// (view_name, [query_ids], start, end)
GetQueryTextLog(
&'static str,
Option<Vec<String>>,
DateTime<Local>,
Option<DateTime<Local>>,
),
// [bool (true - show in TUI, false - open in browser), type, start, end]
ShowServerFlameGraph(bool, TraceType, DateTime<Local>, DateTime<Local>),
// (type, bool (true - show in TUI, false - open in browser), start time, end time, [query_ids])
Expand Down Expand Up @@ -275,14 +280,14 @@ async fn process_event(context: ContextArc, event: Event, need_clear: &mut bool)
}))
.map_err(|_| anyhow!("Cannot send message to UI"))?;
}
Event::GetQueryTextLog(query_ids, start_microseconds, end_microseconds) => {
Event::GetQueryTextLog(view_name, query_ids, start_microseconds, end_microseconds) => {
let block = clickhouse
.get_query_logs(&query_ids, start_microseconds, end_microseconds)
.await?;
cb_sink
.send(Box::new(move |siv: &mut cursive::Cursive| {
siv.call_on_name_or_render_error(
"query_log",
view_name,
move |view: &mut view::TextLogView| {
return view.update(block);
},
Expand Down
37 changes: 36 additions & 1 deletion src/view/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
options::{parse_datetime, ChDigViews},
ContextArc, WorkerEvent,
},
view,
view::{self, TextLogView},
};
use anyhow::Result;
#[cfg(not(target_family = "windows"))]
Expand Down Expand Up @@ -78,6 +78,7 @@ pub trait Navigation {
fn show_clickhouse_errors(&mut self, context: ContextArc);
fn show_clickhouse_backups(&mut self, context: ContextArc);
fn show_clickhouse_dictionaries(&mut self, context: ContextArc);
fn show_clickhouse_server_logs(&mut self, context: ContextArc);

#[allow(clippy::too_many_arguments)]
fn show_query_result_view<F>(
Expand Down Expand Up @@ -262,6 +263,7 @@ impl Navigation for Cursive {
ChDigViews::Errors => self.show_clickhouse_errors(context.clone()),
ChDigViews::Backups => self.show_clickhouse_backups(context.clone()),
ChDigViews::Dictionaries => self.show_clickhouse_dictionaries(context.clone()),
ChDigViews::ServerLogs => self.show_clickhouse_server_logs(context.clone()),
}
}

Expand Down Expand Up @@ -372,6 +374,12 @@ impl Navigation for Cursive {
siv.show_clickhouse_dictionaries(ctx.clone())
});
}
{
let ctx = context.clone();
c.add_view("Server logs", move |siv| {
siv.show_clickhouse_server_logs(ctx.clone())
});
}
{
let ctx = context.clone();
c.add_view("Errors", move |siv| siv.show_clickhouse_errors(ctx.clone()));
Expand Down Expand Up @@ -968,6 +976,33 @@ impl Navigation for Cursive {
);
}

fn show_clickhouse_server_logs(&mut self, context: ContextArc) {
if self.has_view("server_logs") {
return;
}

let view_options = context.clone().lock().unwrap().options.view.clone();

self.drop_main_view();
self.set_main_view(
LinearLayout::vertical()
.child(TextView::new("Server logs:").center())
.child(DummyView.fixed_height(1))
.child(
TextLogView::new(
"server_logs",
context,
view_options.start,
Some(view_options.end),
None,
)
.with_name("server_logs")
.full_screen(),
),
);
self.focus_name("server_logs").unwrap();
}

fn show_query_result_view<F>(
&mut self,
context: ContextArc,
Expand Down
3 changes: 2 additions & 1 deletion src/view/processes_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,10 +956,11 @@ impl ProcessesView {
.child(views::NamedView::new(
"query_log",
TextLogView::new(
"query_log",
context_copy,
min_query_start_microseconds,
max_query_end_microseconds,
query_ids,
Some(query_ids),
),
)),
));
Expand Down
5 changes: 4 additions & 1 deletion src/view/text_log_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ const FLUSH_INTERVAL_MILLISECONDS: i64 = 7500;

impl TextLogView {
pub fn new(
view_name: &'static str,
context: ContextArc,
min_query_start_microseconds: DateTime64,
max_query_end_microseconds: Option<DateTime64>,
query_ids: Vec<String>,
query_ids: Option<Vec<String>>,
) -> Self {
let flush_interval_milliseconds =
Duration::try_milliseconds(FLUSH_INTERVAL_MILLISECONDS).unwrap();
Expand All @@ -52,6 +53,7 @@ impl TextLogView {
.unwrap()
.worker
.send(WorkerEvent::GetQueryTextLog(
view_name,
query_ids.clone(),
query_start_microseconds,
max_query_end_microseconds,
Expand All @@ -64,6 +66,7 @@ impl TextLogView {
move || {
update_callback_context.lock().unwrap().worker.send(
WorkerEvent::GetQueryTextLog(
view_name,
update_query_ids.clone(),
*update_last_event_time_microseconds.lock().unwrap(),
max_query_end_microseconds,
Expand Down

0 comments on commit b93875b

Please sign in to comment.