-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcontext.rs
More file actions
193 lines (174 loc) · 6.06 KB
/
context.rs
File metadata and controls
193 lines (174 loc) · 6.06 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use crate::interpreter::{options::ChDigOptions, ClickHouse, Worker};
use anyhow::Result;
use chdig::ActionDescription;
use chrono::Duration;
use cursive::{event::Event, event::EventResult, views::Dialog, views::OnEventView, Cursive, View};
use std::sync::{atomic, Arc, Condvar, Mutex};
pub type ContextArc = Arc<Mutex<Context>>;
type GlobalActionCallback = Arc<Box<dyn Fn(&mut Cursive) + Send + Sync>>;
pub struct GlobalAction {
pub description: ActionDescription,
pub callback: GlobalActionCallback,
}
type ViewActionCallback =
Arc<Box<dyn Fn(&mut dyn View) -> Result<Option<EventResult>> + Send + Sync>>;
pub struct ViewAction {
pub description: ActionDescription,
pub callback: ViewActionCallback,
}
pub struct Context {
pub options: ChDigOptions,
pub clickhouse: Arc<ClickHouse>,
pub server_version: String,
pub worker: Worker,
pub background_runner_cv: Arc<(Mutex<()>, Condvar)>,
pub background_runner_force: Arc<atomic::AtomicBool>,
pub background_runner_summary_force: Arc<atomic::AtomicBool>,
pub cb_sink: cursive::CbSink,
pub global_actions: Vec<GlobalAction>,
pub views_menu_actions: Vec<GlobalAction>,
pub view_actions: Vec<ViewAction>,
pub pending_view_callback: Option<ViewActionCallback>,
}
impl Context {
pub async fn new(
options: ChDigOptions,
clickhouse: Arc<ClickHouse>,
cb_sink: cursive::CbSink,
) -> Result<ContextArc> {
let server_version = clickhouse.version();
let worker = Worker::new();
let background_runner_cv = Arc::new((Mutex::new(()), Condvar::new()));
let background_runner_force = Arc::new(atomic::AtomicBool::new(false));
let background_runner_summary_force = Arc::new(atomic::AtomicBool::new(false));
let context = Arc::new(Mutex::new(Context {
options,
clickhouse,
server_version,
worker,
background_runner_cv,
background_runner_force,
background_runner_summary_force,
cb_sink,
global_actions: Vec::new(),
views_menu_actions: Vec::new(),
view_actions: Vec::new(),
pending_view_callback: None,
}));
context.lock().unwrap().worker.start(context.clone());
return Ok(context);
}
pub fn add_global_action<F, E>(
&mut self,
siv: &mut Cursive,
text: &'static str,
event: E,
cb: F,
) where
F: Fn(&mut Cursive) + Send + Sync + Copy + 'static,
E: Into<Event>,
{
let event = event.into();
let action = GlobalAction {
description: ActionDescription { text, event },
callback: Arc::new(Box::new(cb)),
};
siv.add_global_callback(action.description.event.clone(), cb);
self.global_actions.push(action);
}
pub fn add_global_action_without_shortcut<F>(
&mut self,
siv: &mut Cursive,
text: &'static str,
cb: F,
) where
F: Fn(&mut Cursive) + Send + Sync + Copy + 'static,
{
return self.add_global_action(siv, text, Event::Unknown(Vec::from([0u8])), cb);
}
pub fn add_view<F>(&mut self, text: &'static str, cb: F)
where
F: Fn(&mut Cursive) + Send + Sync + 'static,
{
let action = GlobalAction {
description: ActionDescription {
text,
event: Event::Unknown(Vec::from([0u8])),
},
callback: Arc::new(Box::new(cb)),
};
self.views_menu_actions.push(action);
}
pub fn add_view_action<F, E, V>(
&mut self,
view: &mut OnEventView<V>,
text: &'static str,
event: E,
cb: F,
) where
F: Fn(&mut dyn View) -> Result<Option<EventResult>> + Send + Sync + Copy + 'static,
E: Into<Event>,
V: View,
{
let event = event.into();
let action = ViewAction {
description: ActionDescription { text, event },
callback: Arc::new(Box::new(cb)),
};
let event = action.description.event.clone();
let cb = action.callback.clone();
view.set_on_event_inner(event, move |sub_view, _event| {
let result = cb.as_ref()(sub_view);
match result {
Err(err) => {
return Some(EventResult::with_cb_once(move |siv: &mut Cursive| {
siv.add_layer(Dialog::info(err.to_string()));
}));
}
Ok(event) => return event,
}
});
self.view_actions.push(action);
}
pub fn add_view_action_without_shortcut<F, V>(
&mut self,
view: &mut OnEventView<V>,
text: &'static str,
cb: F,
) where
F: Fn(&mut dyn View) -> Result<Option<EventResult>> + Send + Sync + Copy + 'static,
V: View,
{
return self.add_view_action(view, text, Event::Unknown(Vec::from([0u8])), cb);
}
pub fn trigger_view_refresh(&self) {
self.background_runner_force
.store(true, atomic::Ordering::SeqCst);
self.background_runner_summary_force
.store(true, atomic::Ordering::SeqCst);
self.background_runner_cv.1.notify_all();
}
pub fn shift_time_interval(&mut self, is_sub: bool, minutes: i64) {
let new_start = &mut self.options.view.start;
let new_end = &mut self.options.view.end;
if is_sub {
*new_start -= Duration::try_minutes(minutes).unwrap();
*new_end -= Duration::try_minutes(minutes).unwrap();
log::debug!(
"Set time frame to ({}, {}) ({} minutes backward)",
new_start,
new_end,
minutes
);
} else {
*new_start += Duration::try_minutes(minutes).unwrap();
*new_end += Duration::try_minutes(minutes).unwrap();
log::debug!(
"Set time frame to ({}, {}) ({} minutes forward)",
new_start,
new_end,
minutes
);
}
}
}