-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbackground_schedule_pool.rs
More file actions
120 lines (103 loc) · 3.41 KB
/
background_schedule_pool.rs
File metadata and controls
120 lines (103 loc) · 3.41 KB
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
use crate::{
interpreter::{ContextArc, WorkerEvent, options::ChDigViews},
view::{self, navigation::Navigation, provider::ViewProvider},
};
use cursive::{
Cursive,
view::{Nameable, Resizable},
views::Dialog,
};
use std::collections::HashMap;
pub struct BackgroundSchedulePoolViewProvider;
impl ViewProvider for BackgroundSchedulePoolViewProvider {
fn name(&self) -> &'static str {
"Background jobs"
}
fn view_type(&self) -> ChDigViews {
ChDigViews::BackgroundSchedulePool
}
fn show(&self, siv: &mut Cursive, context: ContextArc) {
if siv.has_view("background_schedule_pool") {
return;
}
let mut columns = vec![
"pool",
"database",
"table",
"log_name",
"query_id",
"elapsed_ms",
"executing",
"scheduled",
"delayed",
];
let cluster = context.lock().unwrap().options.clickhouse.cluster.is_some();
let columns_to_compare = if cluster {
columns.insert(0, "hostName() host");
vec!["host", "pool", "database", "table", "log_name"]
} else {
vec!["pool", "database", "table", "log_name"]
};
let dbtable = context
.lock()
.unwrap()
.clickhouse
.get_table_name("system", "background_schedule_pool");
let query = format!(
"SELECT {} FROM {} ORDER BY pool, database, table, log_name",
columns.join(", "),
dbtable,
);
siv.drop_main_view();
let mut view = view::SQLQueryView::new(
context.clone(),
"background_schedule_pool",
"elapsed_ms",
columns.clone(),
columns_to_compare,
query,
)
.unwrap_or_else(|_| panic!("Cannot get background_schedule_pool"));
let background_schedule_pool_logs_callback =
move |siv: &mut Cursive, columns: Vec<&'static str>, row: view::QueryResultRow| {
show_logs_for_background_schedule_pool_task(siv, columns, row);
};
view.get_inner_mut()
.set_on_submit(background_schedule_pool_logs_callback);
let view = view.with_name("background_schedule_pool").full_screen();
siv.set_main_view(Dialog::around(view).title("Background Schedule Pool"));
}
}
fn show_logs_for_background_schedule_pool_task(
siv: &mut Cursive,
columns: Vec<&'static str>,
row: view::QueryResultRow,
) {
let row = row.0;
let mut map = HashMap::<String, String>::new();
columns.iter().zip(row.iter()).for_each(|(c, r)| {
let value = r.to_string();
map.insert(c.to_string(), value);
});
let log_name = map
.get("log_name")
.map(|s| s.to_owned())
.unwrap_or_default();
let database = map
.get("database")
.map(|s| s.to_owned())
.unwrap_or_default();
let table = map.get("table").map(|s| s.to_owned()).unwrap_or_default();
let context = siv.user_data::<ContextArc>().unwrap().clone();
let view_options = context.clone().lock().unwrap().options.view.clone();
context.lock().unwrap().worker.send(
true,
WorkerEvent::BackgroundSchedulePoolLogs(
log_name,
database,
table,
view_options.start,
view_options.end,
),
);
}