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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use std::fs;
use log::{debug, error, warn};
use serde::{Deserialize, Serialize};
use similar::{ChangeTag, TextDiff};
use ratatui::widgets::ListState;
use self::actions::Actions;
use self::state::AppState;
use crate::app::actions::Action;
use crate::inputs::key::Key;
use crate::io::IoEvent;
pub mod actions;
pub mod state;
pub mod ui;
const DB_PATH: &str = "./data.json";
const CHECKSTYLE_SCORE: isize = 10;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Test {
pub id: usize,
pub name: String,
pub status: String,
pub log: String,
pub time_normal: f64,
pub time_valgrind: f64,
pub timeout: u64,
pub test_score: usize,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Data {
tests: Vec<Vec<Test>>,
test_path: String,
exec_name: Vec<String>,
valgrind_enabled: bool,
}
#[derive(Debug, PartialEq, Eq)]
pub enum AppReturn {
Exit,
Continue,
}
/// The main application, containing the state
pub struct App {
/// We could dispatch an IO event
io_tx: tokio::sync::mpsc::Sender<IoEvent>,
/// Contextual actions
actions: Actions,
/// State
is_loading: bool,
pub unwritten_data: bool,
state: AppState,
test_num: usize,
pub selected_tab: usize,
pub test_list: Vec<Vec<Test>>,
pub test_list_state: ListState,
windows_list_state: ListState,
pub log_list_state: ListState,
pub valgrind_enabled: bool,
pub test_path: String,
pub exec_name: Vec<String>,
pub current_ref: String,
pub checkstyle: String,
pub vmchecker_out: String,
pub diff: Vec<(&'static str, String)>,
pub errors: Vec<i32>
}
impl App {
pub fn new(io_tx: tokio::sync::mpsc::Sender<IoEvent>) -> Self {
let actions = vec![Action::Quit].into();
let is_loading = false;
let state = AppState::default();
let db_content = fs::read_to_string(DB_PATH).unwrap();
let json: Data = serde_json::from_str::<Data>(&db_content).unwrap();
let test_list = json.tests;
let test_path = json.test_path;
let exec_name = json.exec_name;
let mut test_list_state = ListState::default();
test_list_state.select(Some(0));
let mut windows_list_state = ListState::default();
windows_list_state.select(Some(0));
let mut log_list_state = ListState::default();
log_list_state.select(None);
let valgrind_enabled = json.valgrind_enabled;
let selected_tab = 0usize;
let unwritten_data = false;
let test_num = test_list.iter().map(|list| list.len()).sum();
let mut errors = vec![0, 0, 0];
let current_ref = fs::read_to_string(format!(
"{}ref/{:02}-{}.ref",
test_path, test_list[0][0].id, exec_name[0]
))
.unwrap();
let checkstyle = fs::read_to_string(format!("{}checkstyle.txt", test_path)).unwrap();
let vmchecker_out = String::new();
checkstyle
.lines().for_each(|line| {
match line {
_ if line.contains("CHECK") => errors[0] += 1,
_ if line.contains("WARNING") => errors[1] += 1,
_ if line.contains("ERROR") => errors[2] += 1,
_ => (),
};
});
let diff = TextDiff::from_lines(¤t_ref, &test_list[0][0].log)
.iter_all_changes()
.map(|item| {
let sign = match item.tag() {
ChangeTag::Delete => "-",
ChangeTag::Insert => "+",
ChangeTag::Equal => " ",
};
match item.missing_newline() {
true => (sign, format!("{}", item)),
false => (sign, format!("{}⏎", item)),
}
})
.collect();
Self {
io_tx,
actions,
is_loading,
unwritten_data,
state,
test_num,
selected_tab,
test_list,
test_list_state,
windows_list_state,
log_list_state,
valgrind_enabled,
test_path,
exec_name,
current_ref,
checkstyle,
vmchecker_out,
diff,
errors,
}
}
/// Handle a user action
pub async fn do_action(&mut self, key: Key) -> AppReturn {
if let Some(action) = self.actions.find(key) {
debug!("Run action [{:?}]", action);
match action {
Action::Quit => AppReturn::Exit,
Action::Run => {
self.dispatch(IoEvent::RunAll(self.test_num)).await;
AppReturn::Continue
}
Action::RunTaskOne => {
let mut task_one = Vec::new();
for test in self.test_list[0].iter() {
task_one.push((test.id, 0));
}
self.dispatch(IoEvent::RunFailed(task_one)).await;
AppReturn::Continue
}
// Action::RunTaskTwo => {
// let mut task_two = Vec::new();
// for test in self.test_list[1].iter() {
// task_two.push((test.id, 1));
// }
// self.dispatch(IoEvent::RunFailed(task_two)).await;
// AppReturn::Continue
// }
// Action::RunTaskThree => {
// let mut task_three = Vec::new();
// for test in self.test_list[2].iter() {
// task_three.push((test.id, 2));
// }
// self.dispatch(IoEvent::RunFailed(task_three)).await;
// AppReturn::Continue
// }
Action::RunFailed => {
let mut failed = Vec::new();
for (index, execs) in self.test_list.iter().enumerate() {
for test in execs {
// TODO make this more eficient
if test.status == "0"
|| test.status == "TIMEOUT"
|| test.status == "CRASHED"
|| test.status == "MEMLEAKS"
|| test.status == "ERROR"
{
failed.push((test.id, index));
}
}
}
self.dispatch(IoEvent::RunFailed(failed)).await;
AppReturn::Continue
}
Action::RunCurrent => {
self.dispatch(IoEvent::Make).await;
if let Some(index) = self.test_list_state.selected() {
let (test_index, exec_index) = get_list_index(&self.test_list, index);
self.test_list[exec_index][test_index].status.clear();
self.test_list[exec_index][test_index]
.status
.push_str("RUNNING");
self.dispatch(IoEvent::RunTest(test_index, exec_index))
.await;
} else {
warn!("No test selected");
}
AppReturn::Continue
}
Action::RightList => {
if let Some(index) = self.windows_list_state.selected() {
if index == 1 {
self.windows_list_state.select(Some(0));
} else {
self.windows_list_state.select(Some(1));
}
}
AppReturn::Continue
}
Action::LeftList => {
if let Some(index) = self.windows_list_state.selected() {
if index == 1 {
self.windows_list_state.select(Some(0));
} else {
self.windows_list_state.select(Some(1));
}
}
AppReturn::Continue
}
Action::UpList => {
// State based on which tab I am on
if let Some(window_index) = self.windows_list_state.selected() {
match window_index {
0 => {
if let Some(selected) = self.test_list_state.selected() {
if selected > 0 {
self.test_list_state.select(Some(selected - 1));
} else {
self.test_list_state.select(Some(self.test_num - 1));
}
self.dispatch(IoEvent::UpdateRef).await;
}
}
1 => {
if let Some(selected) = self.log_list_state.selected() {
let size = self.state().get_diffsize().unwrap_or(0);
if selected > 0 {
self.log_list_state.select(Some(selected - 1));
} else {
self.log_list_state.select(Some(size - 1));
}
}
}
_ => return AppReturn::Continue,
}
}
AppReturn::Continue
}
Action::DownList => {
if let Some(window_index) = self.windows_list_state.selected() {
match window_index {
0 => {
if let Some(selected) = self.test_list_state.selected() {
if selected >= self.test_num - 1 {
self.test_list_state.select(Some(0));
} else {
self.test_list_state.select(Some(selected + 1));
}
self.dispatch(IoEvent::UpdateRef).await;
}
}
1 => {
if let Some(selected) = self.log_list_state.selected() {
let size = self.state().get_diffsize().unwrap_or(0);
if selected >= size - 1 {
self.log_list_state.select(Some(0));
} else {
self.log_list_state.select(Some(selected + 1));
}
}
}
_ => return AppReturn::Continue,
}
}
AppReturn::Continue
}
Action::ActivateValgrind => {
self.valgrind_enabled = !self.valgrind_enabled;
AppReturn::Continue
}
Action::RunCheckstyle => {
self.state.update_checkstyle();
if let Some(true) = self.state.get_checkstyle() {
self.dispatch(IoEvent::LoadChecksyle).await;
}
AppReturn::Continue
} // Action::SendVMChecker => {
// info!("Preparing vmchecker send");
// self.dispatch(IoEvent::SendVMChecker).await;
// AppReturn::Continue
// }
// Action::OpenVMChecker => {
// self.state.update_vmcheck();
// self.vmchecker_out.push_str("Waiting for server response");
// if let Some(true) = self.state.get_vmcheck() {
// self.dispatch(IoEvent::LoadVMChecker).await;
// }
// AppReturn::Continue
// }
}
} else {
warn!("No action accociated to {}", key);
AppReturn::Continue
}
}
/// We could update the app or dispatch event on tick
pub async fn update_on_tick(&mut self) -> AppReturn {
// here we just increment a counter
self.state.incr_tick();
if self.unwritten_data && self.state.count_tick().unwrap() % 100 == 0 {
let data = self.save_data();
self.dispatch(IoEvent::SaveData(data)).await;
self.unwritten_data = false;
}
AppReturn::Continue
}
/// Send a network event to the IO thread
pub async fn dispatch(&mut self, action: IoEvent) {
// `is_loading` will be set to false again after the async action has finished in io/handler.rs
self.is_loading = true;
if let Err(e) = self.io_tx.send(action).await {
self.is_loading = false;
error!("Error from dispatch {}", e);
};
}
pub fn actions(&self) -> &Actions {
&self.actions
}
pub fn state(&self) -> &AppState {
&self.state
}
pub fn is_loading(&self) -> bool {
self.is_loading
}
pub fn initialized(&mut self) {
// Update contextual actions
self.actions = vec![
Action::Quit,
Action::Run,
Action::RunFailed,
Action::RunCurrent,
Action::RightList,
Action::LeftList,
Action::UpList,
Action::DownList,
Action::ActivateValgrind,
Action::RunCheckstyle,
Action::RunTaskOne,
// Action::RunTaskTwo,
// Action::RunTaskThree,
// Action::SendVMChecker,
// Action::OpenVMChecker,
]
.into();
self.state = AppState::initialized()
}
pub fn loaded(&mut self) {
self.is_loading = false;
}
pub fn calculate_score(&self) -> isize {
let mut score = 0isize;
for execs in self.test_list.iter() {
for test in execs {
score += test.status.parse::<isize>().unwrap_or(0);
}
}
if self.errors[0] >= 10 {
score -= CHECKSTYLE_SCORE / 2;
}
if self.errors[1] >= 5 {
score -= CHECKSTYLE_SCORE / 2;
}
if self.errors[2] > 0 {
score -= CHECKSTYLE_SCORE;
}
score
}
pub fn save_data(&mut self) -> Data {
Data {
tests: self.test_list.to_vec(),
test_path: self.test_path.clone(),
exec_name: self.exec_name.clone(),
valgrind_enabled: self.valgrind_enabled,
}
}
}
pub fn get_list_index(lists: &Vec<Vec<Test>>, index: usize) -> (usize, usize) {
let mut cumulative_index = 0;
let mut list_index = 0;
for list in lists {
cumulative_index += list.len();
if cumulative_index > index {
let index_in_list = index - (cumulative_index - list.len());
return (index_in_list, list_index);
}
list_index += 1;
}
return (0, 0);
}