Print stats about routing table size in UI
[dnsseed-rust] / src / printer.rs
1 use std::sync::atomic::Ordering;
2 use std::collections::LinkedList;
3 use std::sync::{Arc, Mutex};
4 use std::io::Write;
5
6 use crate::datastore::{Store, AddressState, U64Setting, RegexSetting};
7
8 use crate::START_SHUTDOWN;
9
10 pub enum Stat {
11         HeaderCount(u64),
12         NewConnection,
13         ConnectionClosed,
14         V4RoutingTableSize(usize),
15         V6RoutingTableSize(usize),
16 }
17
18 struct Stats {
19         lines: LinkedList<String>,
20         header_count: u64,
21         connection_count: u64,
22         v4_table_size: usize,
23         v6_table_size: usize,
24 }
25
26 pub struct Printer {
27         stats: Arc<Mutex<Stats>>,
28 }
29
30 impl Printer {
31         pub fn new(store: &'static Store) -> Printer {
32                 let stats: Arc<Mutex<Stats>> = Arc::new(Mutex::new(Stats {
33                         lines: LinkedList::new(),
34                         header_count: 0,
35                         connection_count: 0,
36                         v4_table_size: 0,
37                         v6_table_size: 0,
38                 }));
39                 let thread_arc = Arc::clone(&stats);
40                 std::thread::spawn(move || {
41                         loop {
42                                 std::thread::sleep(std::time::Duration::from_secs(1));
43
44                                 let stdout = std::io::stdout();
45                                 let mut out = stdout.lock();
46
47                                 let stats = thread_arc.lock().unwrap();
48                                 if START_SHUTDOWN.load(Ordering::Relaxed) && stats.connection_count == 0 {
49                                         break;
50                                 }
51
52                                 out.write_all(b"\x1b[2J\x1b[;H\n").expect("stdout broken?");
53                                 for line in stats.lines.iter() {
54                                         out.write_all(line.as_bytes()).expect("stdout broken?");
55                                         out.write_all(b"\n").expect("stdout broken?");
56                                 }
57
58                                 out.write_all(b"\nNode counts by status:\n").expect("stdout broken?");
59                                 for i in 0..AddressState::get_count() {
60                                         out.write_all(format!("{:22}: {}\n", AddressState::from_num(i).unwrap().to_str(),
61                                                         store.get_node_count(AddressState::from_num(i).unwrap())
62                                                         ).as_bytes()).expect("stdout broken?");
63                                 }
64
65                                 out.write_all(format!(
66                                                 "\nCurrent connections open/in progress: {}\n", stats.connection_count).as_bytes()).expect("stdout broken?");
67                                 out.write_all(format!(
68                                                 "Connections opened each second: {} (\"c x\" to change to x seconds)\n", store.get_u64(U64Setting::ConnsPerSec)
69                                                 ).as_bytes()).expect("stdout broken?");
70                                 out.write_all(format!(
71                                                 "Current block count: {}\n", stats.header_count).as_bytes()).expect("stdout broken?");
72
73                                 out.write_all(format!(
74                                                 "Timeout for full run (in seconds): {} (\"t x\" to change to x seconds)\n", store.get_u64(U64Setting::RunTimeout)
75                                                 ).as_bytes()).expect("stdout broken?");
76                                 out.write_all(format!(
77                                                 "Minimum protocol version: {} (\"v x\" to change value to x)\n", store.get_u64(U64Setting::MinProtocolVersion)
78                                                 ).as_bytes()).expect("stdout broken?");
79                                 out.write_all(format!(
80                                                 "Subversion match regex: {} (\"s x\" to change value to x)\n", store.get_regex(RegexSetting::SubverRegex).as_str()
81                                                 ).as_bytes()).expect("stdout broken?");
82
83                                 out.write_all(b"\nRetry times (in seconds):\n").expect("stdout broken?");
84                                 for i in 0..AddressState::get_count() {
85                                         let scan_secs = store.get_u64(U64Setting::RescanInterval(AddressState::from_num(i).unwrap()));
86                                         out.write_all(format!(
87                                                         "{:22} ({:2}): {:5} (ie {} hrs, {} min)\n", AddressState::from_num(i).unwrap().to_str(), i,
88                                                         scan_secs, scan_secs / 60 / 60, (scan_secs / 60) % 60,
89                                                         ).as_bytes()).expect("stdout broken?");
90                                 }
91
92                                 out.write_all(format!(
93                                                 "\nBGP Routing Table: {} v4 paths, {} v6 paths\n",
94                                                 stats.v4_table_size, stats.v6_table_size).as_bytes()).expect("stdout broken?");
95
96                                 out.write_all(b"\nCommands:\n").expect("stdout broken?");
97                                 out.write_all(b"q: quit\n").expect("stdout broken?");
98                                 out.write_all(format!(
99                                                 "r x y: Change retry time for status x (int value, see retry times section for name mappings) to y (in seconds)\n"
100                                                 ).as_bytes()).expect("stdout broken?");
101                                 out.write_all(format!(
102                                                 "w x: Change the amount of time a node is considered WAS_GOOD after it fails to x from {} (in seconds)\n",
103                                                 store.get_u64(U64Setting::WasGoodTimeout)
104                                                 ).as_bytes()).expect("stdout broken?");
105                                 out.write_all(b"a x: Scan node x\n").expect("stdout broken?");
106                                 out.write_all(b"b x: BGP Lookup IP x\n").expect("stdout broken?");
107                                 out.write_all(b"\x1b[s").expect("stdout broken?"); // Save cursor position and provide a blank line before cursor
108                                 out.write_all(b"\x1b[;H\x1b[2K").expect("stdout broken?");
109                                 out.write_all(b"Most recent log:\n").expect("stdout broken?");
110                                 out.write_all(b"\x1b[u").expect("stdout broken?"); // Restore cursor position and go up one line
111
112                                 out.flush().expect("stdout broken?");
113                         }
114                 });
115                 Printer {
116                         stats,
117                 }
118         }
119
120         pub fn add_line(&self, line: String, err: bool) {
121                 let mut stats = self.stats.lock().unwrap();
122                 if err {
123                         stats.lines.push_back("\x1b[31m".to_string() + &line + "\x1b[0m");
124                 } else {
125                         stats.lines.push_back(line);
126                 }
127                 if stats.lines.len() > 75 {
128                         stats.lines.pop_front();
129                 }
130         }
131
132         pub fn set_stat(&self, s: Stat) {
133                 match s {
134                         Stat::HeaderCount(c) => self.stats.lock().unwrap().header_count = c,
135                         Stat::NewConnection => self.stats.lock().unwrap().connection_count += 1,
136                         Stat::ConnectionClosed => self.stats.lock().unwrap().connection_count -= 1,
137                         Stat::V4RoutingTableSize(c) => self.stats.lock().unwrap().v4_table_size = c,
138                         Stat::V6RoutingTableSize(c) => self.stats.lock().unwrap().v6_table_size = c,
139                 }
140         }
141 }