logutil 0.12.4

log Util is a Rust-based tool designed to help you monitor and analyze Nginx access logs in real-time.
Documentation
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
454
use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    widgets::{Block, Borders, Cell, Paragraph, Row, Table, TableState},
    Frame,
};
use std::path::PathBuf;

pub struct Settings {
    selected_file: PathBuf,
    settings: Vec<Setting>,
    table_state: TableState,
    selected_index: usize,
    input_mode: bool,
    current_input: String,
}

#[derive(Clone)]
pub struct Setting {
    pub name: String,
    pub value: String,
    pub description: String,
    pub input_type: InputType,
}

#[derive(Clone)]
pub enum InputType {
    Number,
    Text,
    Boolean,
    Regex,
}

impl Settings {
    pub fn new_with_args(selected_file: PathBuf, cli_args: &CliArgs) -> Self {
        let settings = vec![
            Setting {
                name: "Count".to_string(),
                value: cli_args.count.to_string(),
                description: "Number of lines to read from the end of the file (0 to start from the end, -1 to read the entire file)".to_string(),
                input_type: InputType::Number,
            },
            Setting {
                name: "Regex Pattern".to_string(),
                value: cli_args.regex.clone(),
                description: "Regular expression to parse the log entries".to_string(),
                input_type: InputType::Regex,
            },
            Setting {
                name: "Date Format".to_string(),
                value: cli_args.date_format.clone(),
                description: "Date format to parse the log entries".to_string(),
                input_type: InputType::Text,
            },
            Setting {
                name: "Top N".to_string(),
                value: cli_args.top.to_string(),
                description: "Number of top entries to display".to_string(),
                input_type: InputType::Number,
            },
            Setting {
                name: "Show URLs".to_string(),
                value: cli_args.show_urls.to_string(),
                description: "Show top URLs in console".to_string(),
                input_type: InputType::Boolean,
            },
            Setting {
                name: "Show IPs".to_string(),
                value: cli_args.show_ips.to_string(),
                description: "Show top IPs in console".to_string(),
                input_type: InputType::Boolean,
            },
            Setting {
                name: "Log to File".to_string(),
                value: cli_args.log_to_file.to_string(),
                description: "Enable logging to a file".to_string(),
                input_type: InputType::Boolean,
            },
            Setting {
                name: "Enable Security".to_string(),
                value: cli_args.enable_security.to_string(),
                description: "Enable Security tab (detect suspicious activity, attacks, etc.)".to_string(),
                input_type: InputType::Boolean,
            },
            Setting {
                name: "Enable Performance".to_string(),
                value: cli_args.enable_performance.to_string(),
                description: "Enable Performance tab (monitor response times, slow requests)".to_string(),
                input_type: InputType::Boolean,
            },
            Setting {
                name: "Enable Errors".to_string(),
                value: cli_args.enable_errors.to_string(),
                description: "Enable Errors tab (track error codes and failed requests)".to_string(),
                input_type: InputType::Boolean,
            },
            Setting {
                name: "Enable Bots".to_string(),
                value: cli_args.enable_bots.to_string(),
                description: "Enable Bots tab (detect bot activity and user agents)".to_string(),
                input_type: InputType::Boolean,
            },
            Setting {
                name: "Enable Sparkline".to_string(),
                value: cli_args.enable_sparkline.to_string(),
                description: "Enable Sparkline tab (show request trends over time)".to_string(),
                input_type: InputType::Boolean,
            },
            Setting {
                name: "Enable Heatmap".to_string(),
                value: cli_args.enable_heatmap.to_string(),
                description: "Enable Heatmap tab (show request distribution by time)".to_string(),
                input_type: InputType::Boolean,
            },
        ];

        let mut instance = Self {
            selected_file,
            settings,
            table_state: TableState::default(),
            selected_index: 0,
            input_mode: false,
            current_input: String::new(),
        };

        // Инициализируем выделение
        instance.table_state.select(Some(0));

        instance
    }

    pub fn draw(&mut self, frame: &mut Frame, area: Rect) {
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints(
                [
                    Constraint::Length(3), // Заголовок
                    Constraint::Length(3), // Описание
                    Constraint::Min(0),    // Список настроек
                    Constraint::Length(3), // Подсказки
                ]
                .as_ref(),
            )
            .split(area);

        // Заголовок
        let header_text = format!("⚙️ Settings for: {}", self.selected_file.display());
        frame.render_widget(
            Paragraph::new(header_text)
                .style(Style::new().fg(Color::White))
                .block(
                    Block::default()
                        .borders(Borders::ALL)
                        .border_type(ratatui::widgets::BorderType::Rounded)
                        .border_style(Style::new().fg(Color::Rgb(144, 238, 144)))
                        .title("🔧 Configuration"),
                ),
            chunks[0],
        );

        // Описание выбранной настройки
        if let Some(setting) = self.settings.get(self.selected_index) {
            let desc_text = format!("📝 {}", setting.description);
            frame.render_widget(
                Paragraph::new(desc_text)
                    .style(Style::new().fg(Color::White))
                    .block(
                        Block::default()
                            .borders(Borders::ALL)
                            .border_type(ratatui::widgets::BorderType::Rounded)
                            .border_style(Style::new().fg(Color::Rgb(144, 238, 144)))
                            .title("Description"),
                    ),
                chunks[1],
            );
        }

        // Список настроек
        let mut rows: Vec<Row> = self
            .settings
            .iter()
            .enumerate()
            .map(|(index, setting)| {
                let selected = index == self.selected_index;
                let name_style = if selected {
                    Style::new()
                        .fg(Color::Rgb(255, 255, 255))
                        .bg(Color::Rgb(144, 238, 144))
                        .add_modifier(Modifier::BOLD)
                } else {
                    Style::new().fg(Color::White)
                };
                let value_style = match setting.input_type {
                    InputType::Boolean => {
                        if selected {
                            Style::new()
                                .fg(Color::Yellow)
                                .bg(Color::Rgb(144, 238, 144))
                                .add_modifier(Modifier::BOLD)
                        } else {
                            Style::new().fg(Color::Yellow)
                        }
                    }
                    _ => {
                        if selected {
                            Style::new()
                                .fg(Color::Cyan)
                                .bg(Color::Rgb(144, 238, 144))
                                .add_modifier(Modifier::BOLD)
                        } else {
                            Style::new().fg(Color::Cyan)
                        }
                    }
                };
                let value_display = match setting.input_type {
                    InputType::Boolean => {
                        let checked = setting.value == "true";
                        if checked { "[x]" } else { "[ ]" }.to_string()
                    }
                    _ => {
                        if self.input_mode && index == self.selected_index {
                            format!("{}", self.current_input)
                        } else {
                            setting.value.clone()
                        }
                    }
                };
                Row::new(vec![
                    Cell::from(setting.name.clone()).style(name_style),
                    Cell::from(value_display).style(value_style),
                ])
            })
            .collect();

        // Добавляем пункт запуска анализа
        let start_style = if self.selected_index == self.settings.len() {
            Style::new()
                .fg(Color::Black)
                .bg(Color::Yellow)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::new().fg(Color::Yellow)
        };
        rows.push(Row::new(vec![
            Cell::from("▶ Start Analysis").style(start_style),
            Cell::from("").style(Style::default()),
        ]));

        // Создаем заголовок для таблицы
        let header = Row::new(vec![
            Cell::from("Setting").style(
                Style::new()
                    .fg(Color::Rgb(255, 255, 0))
                    .add_modifier(Modifier::BOLD),
            ),
            Cell::from("Value").style(
                Style::new()
                    .fg(Color::Rgb(0, 255, 255))
                    .add_modifier(Modifier::BOLD),
            ),
        ])
        .style(
            Style::new()
                .fg(Color::Rgb(0, 191, 255))
                .add_modifier(Modifier::BOLD),
        );

        frame.render_stateful_widget(
            Table::new(
                rows,
                [
                    Constraint::Length(30), // Setting
                    Constraint::Min(20),    // Value
                ],
            )
            .header(header)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_type(ratatui::widgets::BorderType::Rounded)
                    .border_style(Style::new().fg(Color::Rgb(144, 238, 144)))
                    .title("Settings"),
            )
            .row_highlight_style(
                Style::new()
                    .fg(Color::Rgb(255, 255, 255))
                    .bg(Color::Rgb(144, 238, 144))
                    .add_modifier(Modifier::BOLD),
            ),
            chunks[2],
            &mut self.table_state,
        );

        // Подсказки
        let help_text = if self.input_mode {
            "Type value and press Enter to save | Esc to cancel | Ctrl+C: Exit"
        } else {
            "↑/↓: Navigate | Enter: Edit | F10: Start Analysis | Esc: Back | Ctrl+C: Exit"
        };
        frame.render_widget(
            Paragraph::new(help_text)
                .style(Style::new().fg(Color::White))
                .block(
                    Block::default()
                        .borders(Borders::ALL)
                        .border_type(ratatui::widgets::BorderType::Rounded)
                        .border_style(Style::new().fg(Color::Rgb(144, 238, 144)))
                        .title("Help"),
                ),
            chunks[3],
        );
    }

    pub fn handle_input(&mut self, key: KeyEvent) -> SettingsAction {
        if self.input_mode {
            self.handle_input_mode(key)
        } else {
            self.handle_navigation_mode(key)
        }
    }

    fn handle_input_mode(&mut self, key: KeyEvent) -> SettingsAction {
        match key.code {
            KeyCode::Enter => {
                if let Some(setting) = self.settings.get_mut(self.selected_index) {
                    setting.value = self.current_input.clone();
                }
                self.input_mode = false;
                self.current_input.clear();
                SettingsAction::Continue
            }
            KeyCode::Esc => {
                self.input_mode = false;
                self.current_input.clear();
                SettingsAction::Continue
            }
            KeyCode::Char('c')
                if key
                    .modifiers
                    .contains(crossterm::event::KeyModifiers::CONTROL) =>
            {
                // Ctrl+C для завершения программы
                SettingsAction::Exit
            }
            KeyCode::Char(c) => {
                self.current_input.push(c);
                SettingsAction::Continue
            }
            KeyCode::Backspace => {
                self.current_input.pop();
                SettingsAction::Continue
            }
            _ => SettingsAction::Continue,
        }
    }

    fn handle_navigation_mode(&mut self, key: KeyEvent) -> SettingsAction {
        match key.code {
            KeyCode::Up => {
                if self.selected_index > 0 {
                    self.selected_index -= 1;
                    self.table_state.select(Some(self.selected_index));
                }
                SettingsAction::Continue
            }
            KeyCode::Down => {
                if self.selected_index <= self.settings.len() {
                    self.selected_index += 1;
                    self.table_state.select(Some(self.selected_index));
                }
                SettingsAction::Continue
            }
            KeyCode::Enter => {
                if self.selected_index >= self.settings.len() {
                    // Запуск анализа
                    return SettingsAction::StartAnalysis(self.get_cli_args());
                }
                if let Some(setting) = self.settings.get_mut(self.selected_index) {
                    match setting.input_type {
                        InputType::Boolean => {
                            // Переключаем значение
                            setting.value = if setting.value == "true" {
                                "false".to_string()
                            } else {
                                "true".to_string()
                            };
                        }
                        _ => {
                            self.current_input = setting.value.clone();
                            self.input_mode = true;
                        }
                    }
                }
                SettingsAction::Continue
            }
            KeyCode::Esc => SettingsAction::Back,
            KeyCode::Char('c')
                if key
                    .modifiers
                    .contains(crossterm::event::KeyModifiers::CONTROL) =>
            {
                // Ctrl+C для завершения программы
                SettingsAction::Exit
            }
            _ => SettingsAction::Continue,
        }
    }

    pub fn get_cli_args(&self) -> CliArgs {
        CliArgs {
            file: Some(self.selected_file.clone()),
            regex: self.settings[1].value.clone(),
            date_format: self.settings[2].value.clone(),
            count: self.settings[0].value.parse().unwrap_or(0),
            top: self.settings[3].value.parse().unwrap_or(10),
            show_urls: self.settings[4].value.parse().unwrap_or(false),
            show_ips: self.settings[5].value.parse().unwrap_or(false),
            log_to_file: self.settings[6].value.parse().unwrap_or(false),
            enable_security: self.settings[7].value.parse().unwrap_or(false),
            enable_performance: self.settings[8].value.parse().unwrap_or(false),
            enable_errors: self.settings[9].value.parse().unwrap_or(false),
            enable_bots: self.settings[10].value.parse().unwrap_or(false),
            enable_sparkline: self.settings[11].value.parse().unwrap_or(false),
            enable_heatmap: self.settings[12].value.parse().unwrap_or(false),
        }
    }
}

#[derive(Debug)]
pub enum SettingsAction {
    Continue,
    Back,
    StartAnalysis(CliArgs),
    Exit,
}

#[derive(Debug, Clone)]
pub struct CliArgs {
    pub file: Option<PathBuf>,
    pub regex: String,
    pub date_format: String,
    pub count: isize,
    pub top: usize,
    pub show_urls: bool,
    pub show_ips: bool,
    pub log_to_file: bool,
    pub enable_security: bool,
    pub enable_performance: bool,
    pub enable_errors: bool,
    pub enable_bots: bool,
    pub enable_sparkline: bool,
    pub enable_heatmap: bool,
}