From 5e0d54b0e7000a19433dbe8aed83e6e4b2d3decd Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sun, 16 Jun 2024 19:41:20 +0200 Subject: [PATCH] Add Inserted/Selected rows into summary --- src/interpreter/clickhouse.rs | 19 ++++++++++++++ src/view/summary_view.rs | 48 +++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/interpreter/clickhouse.rs b/src/interpreter/clickhouse.rs index 9d3ac58..e8bd31f 100644 --- a/src/interpreter/clickhouse.rs +++ b/src/interpreter/clickhouse.rs @@ -101,6 +101,11 @@ pub struct ClickHouseServerStorages { pub distributed_insert_files: u64, } #[derive(Default)] +pub struct ClickHouseServerRows { + pub selected: u64, + pub inserted: u64, +} +#[derive(Default)] pub struct ClickHouseServerSummary { pub processes: u64, pub merges: u64, @@ -109,6 +114,7 @@ pub struct ClickHouseServerSummary { pub replication_queue_tries: u64, pub fetches: u64, pub servers: u64, + pub rows: ClickHouseServerRows, pub storages: ClickHouseServerStorages, pub uptime: ClickHouseServerUptime, pub memory: ClickHouseServerMemory, @@ -396,6 +402,7 @@ impl ClickHouse { assumeNotNull(memory_dictionaries_) AS memory_dictionaries, asynchronous_metrics.*, + events.*, metrics.* FROM ( @@ -433,6 +440,12 @@ impl ClickHouse { CAST(anyLastIf(value, metric == 'AsynchronousMetricsUpdateInterval') AS UInt64) AS metrics_update_interval FROM {asynchronous_metrics} ) as asynchronous_metrics, + ( + SELECT + sumIf(CAST(value AS UInt64), event == 'SelectedRows') AS selected_rows, + sumIf(CAST(value AS UInt64), event == 'InsertedRows') AS inserted_rows + FROM {events} + ) as events, ( SELECT sumIf(CAST(value AS UInt64), metric == 'StorageBufferBytes') AS storage_buffer_bytes, @@ -474,6 +487,7 @@ impl ClickHouse { SETTINGS enable_global_with_statement=0 "#, metrics=self.get_table_name("system.metrics"), + events=self.get_table_name("system.events"), tables=self.get_table_name("system.tables"), processes=self.get_table_name("system.processes"), merges=self.get_table_name("system.merges"), @@ -503,6 +517,11 @@ impl ClickHouse { server: get("uptime"), }, + rows: ClickHouseServerRows { + selected: get("selected_rows"), + inserted: get("inserted_rows"), + }, + storages: ClickHouseServerStorages { buffer_bytes: get("storage_buffer_bytes"), distributed_insert_files: get("storage_distributed_insert_files"), diff --git a/src/view/summary_view.rs b/src/view/summary_view.rs index f74ee73..18d9d78 100644 --- a/src/view/summary_view.rs +++ b/src/view/summary_view.rs @@ -1,3 +1,4 @@ +use chrono::{DateTime, Local}; use cursive::{ event::{AnyCb, Event, EventResult}, theme::BaseColor, @@ -15,6 +16,9 @@ use crate::interpreter::{ }; pub struct SummaryView { + prev_summary: Option, + prev_update_time: Option>, + layout: views::LinearLayout, #[allow(unused)] @@ -149,7 +153,21 @@ impl SummaryView { BaseColor::Cyan.dark(), ))) .child(views::DummyView.fixed_width(1)) - .child(views::TextView::new("").with_name("disk_write")), + .child(views::TextView::new("").with_name("disk_write")) + .child(views::DummyView.fixed_width(1)) + .child(views::TextView::new(StyledString::styled( + "Selected rows:", + BaseColor::Cyan.dark(), + ))) + .child(views::DummyView.fixed_width(1)) + .child(views::TextView::new("").with_name("selected_rows")) + .child(views::DummyView.fixed_width(1)) + .child(views::TextView::new(StyledString::styled( + "Inserted rows:", + BaseColor::Cyan.dark(), + ))) + .child(views::DummyView.fixed_width(1)) + .child(views::TextView::new("").with_name("inserted_rows")), ) .child( views::LinearLayout::horizontal() @@ -181,7 +199,12 @@ impl SummaryView { let mut bg_runner = BackgroundRunner::new(delay, bg_runner_cv); bg_runner.start(update_callback); - return Self { layout, bg_runner }; + return Self { + prev_summary: None, + prev_update_time: None, + layout, + bg_runner, + }; } pub fn set_view_content(&mut self, view_name: &str, content: S) @@ -207,6 +230,13 @@ impl SummaryView { } else { 1 }; + let now = Local::now(); + let mut since_prev_us = (now - self.prev_update_time.unwrap_or(Local::now())) + .num_microseconds() + .unwrap_or_default() as u64; + if since_prev_us == 0 { + since_prev_us = 1; + } { let mut description: Vec = Vec::new(); @@ -311,6 +341,17 @@ impl SummaryView { fmt_ref.format((summary.blkdev.write_bytes / update_interval) as i64), ); + let mut selected_rows = summary.rows.selected / summary.uptime.server; + let mut inserted_rows = summary.rows.inserted / summary.uptime.server; + if let Some(prev_summary) = &self.prev_summary { + selected_rows = + (summary.rows.selected - prev_summary.rows.selected) * 1_000_000 / since_prev_us; + inserted_rows = + (summary.rows.inserted - prev_summary.rows.inserted) * 1_000_000 / since_prev_us; + } + self.set_view_content("selected_rows", fmt_ref.format(selected_rows as i64)); + self.set_view_content("inserted_rows", fmt_ref.format(inserted_rows as i64)); + self.set_view_content( "uptime", format_duration(Duration::from_secs(summary.uptime.server)).to_string(), @@ -389,6 +430,9 @@ impl SummaryView { ); self.set_view_content("storage_distributed_insert_files", content); } + + self.prev_summary = Some(summary); + self.prev_update_time = Some(now); } }