Compare commits

..

11 Commits

Author SHA1 Message Date
17780c02d5 UI evolution
** Re-evaluating need for users to edit list of hosts at runtime **
2025-05-01 14:54:26 -04:00
141d0ee899 BUGFIX: Delete works for lists of hosts using popups 2025-05-01 09:54:09 -04:00
022359d3fd minor improvements for better 'rustifying' of it 2025-04-30 20:31:19 -04:00
21b41f5593 WIP: working on better ui interaction 2025-04-30 16:09:22 -04:00
9ff76954e0 BUGFIX: removes extra quotes from log entry time entries
FEATURE: Allows delete of target

UPDATE: UI Uses more popups to make the UI more 'layered'
2025-04-30 16:08:56 -04:00
8d0452d752 Fix version tag 2025-04-29 09:29:51 -04:00
49092d9ad7 Updates to use:
- if passed file doesnt exist
 - check for hosts.txt
- if no file passed
 - check for hosts.txt
- if hosts.txt missing
 - default hosts
2025-04-29 09:29:13 -04:00
0261914cda Updates label for VPN endpoints and added more hosts to check 2025-04-28 15:48:54 -04:00
f5fa9877b3 commented out glue code that isn't public build ready 2025-04-28 13:51:25 -04:00
7d830c4e3b ** RatatuiUi **
displays a preset list of hosts and their response state

Updates format of input file to CSV with <host ip>,<host name> as the format.
UI now has red/green for down/up hosts
duration_to_string now in lib
improves display of how long ago change happened
log events are now displayed on the monitoring page to show when a host comes up/goes down
Modularize ui better
Adds ability to return to monitoring or quit from editing page
table in place for displaying hosts

WIP: start of being able to go up/down through the list of hosts to eventually edit them.
2025-04-28 13:49:36 -04:00
6d55d96ca7 adds ability to read hosts.txt if no file is specified on the command line, using defaults if hosts.txt is missing 2025-04-23 15:03:16 -04:00
16 changed files with 1040 additions and 534 deletions

471
Cargo.lock generated
View File

@ -32,6 +32,21 @@ version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "ansi_term" name = "ansi_term"
version = "0.12.1" version = "0.12.1"
@ -118,6 +133,12 @@ version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd"
[[package]]
name = "bumpalo"
version = "3.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf"
[[package]] [[package]]
name = "cassowary" name = "cassowary"
version = "0.3.0" version = "0.3.0"
@ -148,6 +169,20 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-link",
]
[[package]] [[package]]
name = "clap" name = "clap"
version = "4.5.37" version = "4.5.37"
@ -235,6 +270,12 @@ dependencies = [
"static_assertions", "static_assertions",
] ]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]] [[package]]
name = "crossterm" name = "crossterm"
version = "0.28.1" version = "0.28.1"
@ -243,7 +284,7 @@ checksum = "829d955a0bb380ef178a640b91779e3987da38c9aea133b20614cfed8cdea9c6"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"crossterm_winapi", "crossterm_winapi",
"mio 1.0.3", "mio",
"parking_lot", "parking_lot",
"rustix", "rustix",
"signal-hook", "signal-hook",
@ -383,95 +424,6 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
[[package]]
name = "futures"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
name = "futures-core"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
[[package]]
name = "futures-executor"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
[[package]]
name = "futures-macro"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-sink"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7"
[[package]]
name = "futures-task"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988"
[[package]]
name = "futures-util"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]] [[package]]
name = "gimli" name = "gimli"
version = "0.28.1" version = "0.28.1"
@ -495,6 +447,30 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "iana-time-zone"
version = "0.1.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0c919e5debc312ad217002b8048a17b7d83f80703865bbfcfebb0458b0b27d8"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"log",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "ident_case" name = "ident_case"
version = "1.0.1" version = "1.0.1"
@ -571,6 +547,16 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "js-sys"
version = "0.3.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
dependencies = [
"once_cell",
"wasm-bindgen",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.5.0" version = "1.5.0"
@ -629,18 +615,6 @@ dependencies = [
"adler", "adler",
] ]
[[package]]
name = "mio"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
dependencies = [
"libc",
"log",
"wasi",
"windows-sys 0.48.0",
]
[[package]] [[package]]
name = "mio" name = "mio"
version = "1.0.3" version = "1.0.3"
@ -653,6 +627,15 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "object" name = "object"
version = "0.32.2" version = "0.32.2"
@ -694,7 +677,7 @@ dependencies = [
"libc", "libc",
"redox_syscall", "redox_syscall",
"smallvec", "smallvec",
"windows-targets 0.52.6", "windows-targets",
] ]
[[package]] [[package]]
@ -709,25 +692,6 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b"
[[package]]
name = "pin-utils"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "ping-rs"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d873f038f84371f9c7fa13f6afea4d5f1fbcd5070ba8eb7af2a6d41c768eff8b"
dependencies = [
"futures",
"mio 0.8.11",
"paste",
"socket2",
"windows",
]
[[package]] [[package]]
name = "portable-atomic" name = "portable-atomic"
version = "1.11.0" version = "1.11.0"
@ -745,16 +709,16 @@ dependencies = [
[[package]] [[package]]
name = "pp" name = "pp"
version = "0.1.1" version = "0.2.2-PREVIEW"
dependencies = [ dependencies = [
"ansi_term", "ansi_term",
"chrono",
"clap", "clap",
"color-eyre", "color-eyre",
"crossterm", "crossterm",
"csv", "csv",
"env_logger", "env_logger",
"log", "log",
"ping-rs",
"ratatui", "ratatui",
] ]
@ -924,7 +888,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34db1a06d485c9142248b7a054f034b349b212551f3dfd19c94d45a754a217cd" checksum = "34db1a06d485c9142248b7a054f034b349b212551f3dfd19c94d45a754a217cd"
dependencies = [ dependencies = [
"libc", "libc",
"mio 1.0.3", "mio",
"signal-hook", "signal-hook",
] ]
@ -937,31 +901,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "slab"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "smallvec" name = "smallvec"
version = "1.15.0" version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8917285742e9f3e1683f0a9c4e6b57960b7314d0b08d30d1ecd426713ee2eee9" checksum = "8917285742e9f3e1683f0a9c4e6b57960b7314d0b08d30d1ecd426713ee2eee9"
[[package]]
name = "socket2"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d"
dependencies = [
"libc",
"winapi",
]
[[package]] [[package]]
name = "static_assertions" name = "static_assertions"
version = "1.1.0" version = "1.1.0"
@ -1111,6 +1056,64 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
dependencies = [
"bumpalo",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
dependencies = [
"unicode-ident",
]
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"
@ -1134,27 +1137,62 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]] [[package]]
name = "windows" name = "windows-core"
version = "0.43.0" version = "0.61.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04662ed0e3e5630dfa9b26e4cb823b817f1a9addda855d973a9458c236556244" checksum = "4763c1de310c86d75a878046489e2e5ba02c649d185f21c67d4cf8a56d098980"
dependencies = [ dependencies = [
"windows_aarch64_gnullvm 0.42.2", "windows-implement",
"windows_aarch64_msvc 0.42.2", "windows-interface",
"windows_i686_gnu 0.42.2", "windows-link",
"windows_i686_msvc 0.42.2", "windows-result",
"windows_x86_64_gnu 0.42.2", "windows-strings",
"windows_x86_64_gnullvm 0.42.2",
"windows_x86_64_msvc 0.42.2",
] ]
[[package]] [[package]]
name = "windows-sys" name = "windows-implement"
version = "0.48.0" version = "0.60.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" checksum = "a47fddd13af08290e67f4acabf4b459f647552718f683a7b415d290ac744a836"
dependencies = [ dependencies = [
"windows-targets 0.48.5", "proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-interface"
version = "0.59.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd9211b69f8dcdfa817bfd14bf1c97c9188afa36f4750130fcdf3f400eca9fa8"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-link"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76840935b766e1b0a05c0066835fb9ec80071d4c09a16f6bd5f7e655e3c14c38"
[[package]]
name = "windows-result"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c64fd11a4fd95df68efcfee5f44a294fe71b8bc6a91993e2791938abcc712252"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-strings"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a2ba9642430ee452d5a7aa78d72907ebe8cfda358e8cb7918a2050581322f97"
dependencies = [
"windows-link",
] ]
[[package]] [[package]]
@ -1163,7 +1201,7 @@ version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [ dependencies = [
"windows-targets 0.52.6", "windows-targets",
] ]
[[package]] [[package]]
@ -1172,22 +1210,7 @@ version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [ dependencies = [
"windows-targets 0.52.6", "windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc 0.48.5",
"windows_i686_gnu 0.48.5",
"windows_i686_msvc 0.48.5",
"windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc 0.48.5",
] ]
[[package]] [[package]]
@ -1196,64 +1219,28 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [ dependencies = [
"windows_aarch64_gnullvm 0.52.6", "windows_aarch64_gnullvm",
"windows_aarch64_msvc 0.52.6", "windows_aarch64_msvc",
"windows_i686_gnu 0.52.6", "windows_i686_gnu",
"windows_i686_gnullvm", "windows_i686_gnullvm",
"windows_i686_msvc 0.52.6", "windows_i686_msvc",
"windows_x86_64_gnu 0.52.6", "windows_x86_64_gnu",
"windows_x86_64_gnullvm 0.52.6", "windows_x86_64_gnullvm",
"windows_x86_64_msvc 0.52.6", "windows_x86_64_msvc",
] ]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]] [[package]]
name = "windows_aarch64_gnullvm" name = "windows_aarch64_gnullvm"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]] [[package]]
name = "windows_aarch64_msvc" name = "windows_aarch64_msvc"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]] [[package]]
name = "windows_i686_gnu" name = "windows_i686_gnu"
version = "0.52.6" version = "0.52.6"
@ -1266,72 +1253,24 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]] [[package]]
name = "windows_i686_msvc" name = "windows_i686_msvc"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]] [[package]]
name = "windows_x86_64_gnu" name = "windows_x86_64_gnu"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]] [[package]]
name = "windows_x86_64_gnullvm" name = "windows_x86_64_gnullvm"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]] [[package]]
name = "windows_x86_64_msvc" name = "windows_x86_64_msvc"
version = "0.52.6" version = "0.52.6"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "pp" name = "pp"
version = "0.1.1" version = "0.2.2-PREVIEW"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
@ -15,8 +15,7 @@ color-eyre = "0.6.3"
crossterm = "0.28.1" crossterm = "0.28.1"
ratatui = "0.29.0" ratatui = "0.29.0"
# Internal Ping # Time Display
ping-rs = "0.1" chrono = "0.4"
[[bin]] [[bin]]
name = "pp" name = "pp"

View File

View File

@ -1,13 +1,18 @@
address,name
10.3.100.1,Belleville Gateway 10.3.100.1,Belleville Gateway
10.11.31.3,Belleville VPN 11-31 10.11.31.1,Belleville VPN 11-31 (P)
10.12.32.1,Belleville VPN 12-32 10.11.31.3,Belleville VPN 11-31 (B)
10.12.32.1,Belleville VPN 12-32 (P)
10.12.32.3,Belleville VPN 12-32 (B)
192.186.110.6,Belleville Cogeco 192.186.110.6,Belleville Cogeco
129.222.197.36,Belleville Starlink 129.222.197.36,Belleville Starlink
10.2.100.1,Lindsay Gateway 10.2.100.1,Lindsay Gateway
24.143.184.98,Lindsay Cogeco 24.143.184.98,Lindsay Cogeco
192.168.1.50,Lindsay Sign 192.168.1.50,Lindsay Sign
10.11.21.1,Lindsay VPN 11-21 10.11.21.1,Lindsay VPN 11-21 (P)
10.12.22.1,Lindsay VPN 11-22 10.11.21.2,Lindsay VPN 11-21 (L)
10.11.21.1,Lindsay VPN 11-22 (P)
10.12.22.2,Lindsay VPN 11-22 (L)
8.8.8.8,Google DNS 8.8.8.8,Google DNS
1.1.1.1,1111 DNS 1.1.1.1,1111 DNS
192.168.0.1,Peterborough Gateway 192.168.0.1,Peterborough Gateway

10
src/app_settings.rs Normal file
View File

@ -0,0 +1,10 @@
use std::path::PathBuf;
use clap::Parser;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct AppSettings {
/// File of list of hosts
#[arg(short, long, default_value = None)]
pub ping_host_file: Option<PathBuf>,
}

View File

@ -1,244 +1,14 @@
use std::collections::BTreeMap;
use std::fs::File;
use std::io;
use std::io::BufRead;
use std::net::Ipv4Addr;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::mpsc;
use std::time::{Duration, SystemTime};
use clap::Parser; use clap::Parser;
use pp::ping_result::PingResult; use ratatui::widgets::TableState;
use pp::ping_request::PingRequest; use pp::app_settings::AppSettings;
use pp::manager::Manager; use pp::tui::ratatui_app::RatatuiApp;
use pp::SECONDS_BETWEEN_DISPLAY; fn main() -> color_eyre::Result<()> {
use pp::target_state::TargetState; // find out what file we are using to get our hosts
use std::{env, error::Error, ffi::OsString, process};
use color_eyre::owo_colors::OwoColorize;
use crossterm::style::Stylize;
use log::debug;
const SECONDS_IN_MINUTE: u32 = 60;
const SECONDS_IN_HOUR: u32 = SECONDS_IN_MINUTE * 60;
const SECONDS_IN_DAY: u32 = SECONDS_IN_HOUR * 24;
pub fn duration_to_string(to_convert: Duration) -> String {
let mut total_seconds = to_convert.as_secs() as u32;
let mut working_string = String::new();
if total_seconds > 86400 {
// days
let num_days = (total_seconds / SECONDS_IN_DAY) as u32;
working_string = format!("{} days", num_days);
total_seconds = total_seconds - (num_days * SECONDS_IN_DAY);
}
if total_seconds > 3600 {
// hours
let num_hours = (total_seconds / SECONDS_IN_HOUR) as u32;
if num_hours > 0 {
working_string = format!("{} {} hours", working_string, num_hours);
total_seconds = total_seconds - (num_hours * SECONDS_IN_HOUR);
}
}
if total_seconds > 60 {
let num_minutes = (total_seconds / SECONDS_IN_MINUTE) as u32;
if num_minutes > 0 {
working_string = format!("{} {} minutes", working_string, num_minutes);
total_seconds = total_seconds - (num_minutes * SECONDS_IN_MINUTE);
}
// minutes
}
working_string = format!("{} {} seconds", working_string, total_seconds);
working_string
}
struct PPState {}
impl PPState {
pub fn get_default_targets() -> BTreeMap<String, TargetState> {
let mut working = BTreeMap::new();
working.insert("Localhost".to_string(),
TargetState {
name: "Localhost".to_string(),
target: Ipv4Addr::new(127, 0, 0, 1),
..TargetState::default()
},
);
working.insert("Home Gateway".to_string(),
TargetState {
name: "Home Gateway".to_string(),
target: Ipv4Addr::new(172, 24, 0, 1),
..TargetState::default()
},
);
working.insert("1111 DNS".to_string(),
TargetState {
name: "1111 DNS".to_string(),
target: Ipv4Addr::new(1, 1, 1, 1),
..TargetState::default()
},
);
working.insert("Google DNS".to_string(),
TargetState {
name: "Google DNS".to_string(),
target: Ipv4Addr::new(8, 8, 8, 8),
..TargetState::default()
},
);
working.insert("Site IP 1".to_string(),
TargetState {
name: "Site IP 1".to_string(),
target: Ipv4Addr::new(216, 121, 247, 231),
..TargetState::default()
},
);
working.insert("Site IP 2".to_string(),
TargetState {
name: "Site IP 2".to_string(),
target: Ipv4Addr::new(216, 234, 202, 122),
..TargetState::default()
},
);
working.insert("Site IP 3".to_string(),
TargetState {
name: "Site IP 3".to_string(),
target: Ipv4Addr::new(24, 143, 184, 98),
..TargetState::default()
},
);
working
}
pub fn build_targets_from_file(filename: Option<PathBuf>) -> BTreeMap<String, TargetState> {
PPState::get_default_targets();
if let Some(file) = filename {
let mut working = BTreeMap::new();
if !&file.exists() {
debug!("Cant load hosts from {:?}. Using default host list.", file.clone().as_os_str());
// use
PPState::get_default_targets()
} else {
debug!("LOADING HOSTS FROM {:?}", file.to_str());
let file = File::open(file);
let mut rdr = csv::Reader::from_reader(file.unwrap());
for result in rdr.records() {
let record = result.unwrap();
working.insert(record[1].to_string(),
TargetState {
name: record[1].to_string(),
target: Ipv4Addr::from_str(&record[0]).unwrap(),
alive: false,
last_alive_change: SystemTime::now(),
last_rtt: 0,
});
}
working
}
} else {
PPState::get_default_targets()
}
}
}
fn ips_from_state(to_read_from: BTreeMap<String, TargetState>) -> Vec<Ipv4Addr> {
let mut working: Vec<Ipv4Addr> = vec![];
for current in to_read_from {
working.push(current.1.target);
}
working
}
/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct AppSettings {
/// File of list of hosts
#[arg(short, long)]
ping_host_file: Option<PathBuf>,
}
fn main() {
// Get App Settings
let settings = AppSettings::parse(); let settings = AppSettings::parse();
print!("Prep to load targets..."); color_eyre::install()?;
let file_to_check = match settings.ping_host_file { let terminal = ratatui::init();
None => { let result = RatatuiApp::new(settings.ping_host_file).run(terminal);
PathBuf::from("./hosts.txt") ratatui::restore();
} result
Some(actual) => {
actual
}
};
let mut targets = PPState::build_targets_from_file(Some(file_to_check));
// channel to send requests to ping
let (ping_response_sender, ping_response_listener) = mpsc::channel::<PingResult>();
println!("Setting up requests for {} hosts.", targets.len());
Manager::spawn_manager_thread(ips_from_state(targets.clone()), ping_response_sender.clone());
let mut display_loop_start = SystemTime::now();
let mut duration_since_last_loop = SystemTime::now().duration_since(display_loop_start).unwrap();
loop {
let now = SystemTime::now();
if let Ok(response) = ping_response_listener.recv_timeout(Duration::from_millis(100)) {
let local_targets = targets.clone();
for (_, (name, current_state)) in local_targets.iter().enumerate() {
if current_state.target == response.target {
let last_alive_change = if response.success == current_state.alive {
current_state.last_alive_change
} else {
SystemTime::now()
};
let new_state = TargetState {
name: current_state.name.clone(),
target: current_state.target,
alive: response.success,
last_rtt: response.rtt,
last_alive_change,
};
targets.insert(name.clone(), new_state);
}
}
}
duration_since_last_loop = now
.duration_since(display_loop_start)
.expect("unable to figure out how long ago we displayed stuff");
if duration_since_last_loop.as_secs() > SECONDS_BETWEEN_DISPLAY as u64 {
println!("DISPLAY LOOP");
println!("Host \t\t\t\t\t | Alive \t | RTT \t\t");
for (name, current_result) in targets.clone() {
let time_since_last_change = now
.duration_since(current_result.last_alive_change)
.unwrap_or(Duration::from_secs(0));
let mut target_string = format!("{} ({})", name, current_result.target);
while target_string.len() < 34 {
target_string.push(' ');
// target_string = format!("{} ", target_string);
}
target_string = if current_result.alive {
target_string.green().to_string()
} else {
target_string.red().to_string()
};
println!("{} \t | {} \t | {}\t | Changed {} ago",
target_string,
current_result.alive,
current_result.last_rtt,
duration_to_string(time_since_last_change)
);
}
display_loop_start = now;
}
}
} }

View File

@ -1,8 +1,51 @@
use std::time::Duration;
pub mod manager; pub mod manager;
pub mod ping_request; pub mod ping_request;
pub mod ping_result; pub mod ping_result;
pub mod target_state; pub mod target_state;
mod tui; pub mod tui;
pub mod app_settings;
pub const SECONDS_BETWEEN_DISPLAY: u32 = 1; pub const SECONDS_BETWEEN_DISPLAY: u32 = 1;
pub const SECONDS_BETWEEN_PING: u32 = 2; pub const SECONDS_BETWEEN_PING: u32 = 2;
const SECONDS_IN_MINUTE: u32 = 60;
const SECONDS_IN_HOUR: u32 = SECONDS_IN_MINUTE * 60;
const SECONDS_IN_DAY: u32 = SECONDS_IN_HOUR * 24;
pub fn duration_to_string(to_convert: Duration) -> String {
let mut total_seconds = to_convert.as_secs() as u32;
let mut working_string = String::new();
if total_seconds > 86400 {
// days
let num_days = (total_seconds / SECONDS_IN_DAY) as u32;
working_string = format!("{} days", num_days);
total_seconds = total_seconds - (num_days * SECONDS_IN_DAY);
}
if total_seconds > 3600 {
// hours
let num_hours = (total_seconds / SECONDS_IN_HOUR) as u32;
if num_hours > 0 {
working_string = format!("{} {} hours", working_string, num_hours);
total_seconds = total_seconds - (num_hours * SECONDS_IN_HOUR);
}
}
if total_seconds > 60 {
let num_minutes = (total_seconds / SECONDS_IN_MINUTE) as u32;
if num_minutes > 0 {
working_string = format!("{} {} minutes", working_string, num_minutes);
total_seconds = total_seconds - (num_minutes * SECONDS_IN_MINUTE);
}
// minutes
}
working_string = format!("{} {} seconds", working_string, total_seconds);
working_string
}

View File

@ -1,15 +1,17 @@
use std::net::{IpAddr, Ipv4Addr}; use std::net::Ipv4Addr;
use std::process::Command; use std::process::Command;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
use std::thread; use std::thread;
use std::time::{Duration, SystemTime}; use std::time::{Duration, SystemTime};
use ping_rs::send_ping;
use crate::ping_request::PingRequest; use crate::ping_request::PingRequest;
use crate::ping_result::PingResult; use crate::ping_result::PingResult;
use crate::SECONDS_BETWEEN_PING; use crate::SECONDS_BETWEEN_PING;
pub struct Manager; pub struct Manager;
const WIN_PING_SUCCESS: i32 = 1;
const LIN_PING_SUCCESS: i32 = 0;
impl Manager { impl Manager {
pub fn spawn_manager_thread(targets: Vec<Ipv4Addr>, sender: Sender<PingResult>) { pub fn spawn_manager_thread(targets: Vec<Ipv4Addr>, sender: Sender<PingResult>) {
let local_targets = targets.clone(); let local_targets = targets.clone();
@ -32,25 +34,27 @@ impl Manager {
let mut result = 0; let mut result = 0;
let mut success = true; let mut success = true;
let start_time = SystemTime::now(); let start_time = SystemTime::now();
#[cfg(any(target_os="windows"))] {
let target_address = IpAddr::from(request.target); result = Command::new("c:/windows/system32/ping.exe").arg(request.target.to_string())
.arg("-n 1")
let options = ping_rs::PingOptions { ttl: 128, dont_fragment: false }; .arg(format!("-w {}", crate::SECONDS_BETWEEN_PING))
let data = [1,2,3,4]; .arg("-4")
let result = send_ping(&target_address, Duration::from_secs(10), &data, Some(&options)); .output().unwrap().status.code().unwrap_or(255);
success = result == WIN_PING_SUCCESS as i32;;
}
#[cfg(any(target_os="linux"))] {
result = Command::new("/usr/bin/ping").arg(request.target.to_string())
.arg("-c 1")
.arg("-4")
.arg(format!("-w {}", SECONDS_BETWEEN_PING))
.arg("-W 1")
.output().unwrap().status.code().unwrap_or(255);
success = result == LIN_PING_SUCCESS as i32;
}
let ping_duration = SystemTime::now() let ping_duration = SystemTime::now()
.duration_since(start_time) .duration_since(start_time)
.unwrap_or(Duration::from_secs(600)); .unwrap_or(Duration::from_secs(600));
let success = ping_duration.as_millis() >= SECONDS_BETWEEN_PING as u128 && success;
let success = match &result {
Ok(reply) => {
println!("Good reply -> {}/{}/{}", reply.address, data.len(), reply.rtt);
true
},
Err(e) => { println!("ERROR {:?}", e); false }
};
sender.send(PingResult { sender.send(PingResult {
target: local_request.target, target: local_request.target,
success, success,

View File

@ -0,0 +1,7 @@
pub mod mode_editing;
pub mod mode_monitoring;
pub mod ratatui_app;
pub mod target_state_widget;
mod ratatui_screens;
mod mode_deleting;
mod mode_adding;

34
src/tui/mode_adding.rs Normal file
View File

@ -0,0 +1,34 @@
use std::time::Duration;
use ratatui::Frame;
use crate::tui::ratatui_app::RatatuiApp;
use color_eyre::Result;
use crossterm::event;
use crossterm::event::{Event, KeyCode, KeyEventKind};
use ratatui::widgets::{Block, Paragraph};
use crate::tui::ratatui_screens::RatatuiScreens::Monitoring;
pub struct RatatuiAddingMode {}
impl RatatuiAddingMode {
pub fn render(frame: &mut Frame, state: &mut RatatuiApp) {
frame.render_widget(
Paragraph::new("Not Yet Written.")
.block(Block::bordered())
.centered(),
frame.area()
);
}
pub fn handle_crossterm_events(app: &mut RatatuiApp) -> Result<()> {
if event::poll(Duration::from_millis(100))? {
match event::read()? {
Event::Key(key) if key.kind == KeyEventKind::Press => match key.code {
KeyCode::Esc => app.set_screen(Monitoring),
_ => {}
}
_ => {}
}
}
Ok(())
}
}

59
src/tui/mode_deleting.rs Normal file
View File

@ -0,0 +1,59 @@
use std::time::Duration;
use crossterm::event;
use crossterm::event::{Event, KeyCode, KeyEventKind};
use ratatui::Frame;
use ratatui::style::Stylize;
use ratatui::text::Line;
use ratatui::widgets::{Block, Paragraph};
use crate::tui::ratatui_app::RatatuiApp;
use crate::tui::ratatui_screens::RatatuiScreens::Monitoring;
use color_eyre::Result;
pub struct RatatuiDeletingMode {}
impl RatatuiDeletingMode {
pub fn render(frame: &mut Frame, state: &mut RatatuiApp) {
let title = Line::from("Delete Host").bold().red().centered();
let as_list = state.state.clone();
let mut nice_status = None;
for (index, (label, data)) in as_list.iter().enumerate() {
if index == state.selected_host {
nice_status = Some(data);
}
}
let mut body = format!("Do you really want to delete {} (Y/N)", nice_status.unwrap().name);
frame.render_widget(
Paragraph::new(body)
.block(Block::bordered().title(title))
.centered(),
frame.area()
);
}
pub fn handle_crossterm_events(app: &mut RatatuiApp) -> Result<()> {
if event::poll(Duration::from_millis(100))? {
match event::read()? {
Event::Key(key) if key.kind == KeyEventKind::Press => match key.code {
KeyCode::Enter | KeyCode::Char('y') | KeyCode::Char('Y') => {
let as_list = &app.state.clone();
for (index, (label, _)) in as_list.iter().enumerate() {
if index == app.selected_host {
app.state.remove(label).unwrap();
app.set_screen(Monitoring)
}
}
}
KeyCode::Esc | KeyCode::Char('n') | KeyCode::Char('N') => {
app.set_screen(Monitoring)
}
_ => {}
}
_ => {}
}
}
Ok(())
}
}

62
src/tui/mode_editing.rs Normal file
View File

@ -0,0 +1,62 @@
use std::collections::BTreeMap;
use std::time::Duration;
use crossterm::event;
use crossterm::event::{Event, KeyCode, KeyEventKind};
use ratatui::Frame;
use ratatui::prelude::Line;
use ratatui::style::Stylize;
use ratatui::widgets::{Block, Paragraph};
use crate::target_state::TargetState;
use crate::tui::ratatui_app::RatatuiApp;
use color_eyre::Result;
use crate::tui::ratatui_screens::RatatuiScreens::Monitoring;
pub struct RatatuiEditingMode {}
impl RatatuiEditingMode {
pub fn handle_crossterm_events(app: &mut RatatuiApp) -> Result<()> {
if event::poll(Duration::from_millis(100))? {
match event::read()? {
Event::Key(key) if key.kind == KeyEventKind::Press => match key.code {
KeyCode::Up => {
println!("UP")
}
KeyCode::Down => {
println!("Down")
}
KeyCode::Char('+') => {
println!("ADD HOST")
}
KeyCode::Char('-') => {
println!("Delete Host")
}
KeyCode::Char('q') => {
app.set_running(false);
}
KeyCode::Esc => {
app.set_screen(Monitoring);
}
KeyCode::Char('s') => {
println!("Save to existing file.");
}
_ => {}
},
_ => {}
}
}
Ok(())
}
pub fn render(frame: &mut Frame, state: &mut BTreeMap<String, TargetState>) {
let title = Line::from("Editing Hosts").bold().blue().centered();
let body_text = "This is the body text for editing hosts\n'q' to exit <esc> to return to monitoring";
frame.render_widget(
Paragraph::new(body_text)
.centered()
.block(Block::new().title(title).blue()),
frame.area(),
);
}
}

295
src/tui/mode_monitoring.rs Normal file
View File

@ -0,0 +1,295 @@
use crate::duration_to_string;
use crate::target_state::TargetState;
use crate::tui::ratatui_app::RatatuiApp;
use color_eyre::Result;
use crossterm::event;
use crossterm::event::{Event, KeyCode, KeyEventKind};
use ratatui::Frame;
use ratatui::layout::Flex;
use ratatui::prelude::*;
use ratatui::widgets::{
Block, Borders, Cell, Clear, List, ListItem, Paragraph, Row, Table, TableState,
};
use std::time::{Duration, SystemTime};
pub struct RatatuiMonitoringMode {}
impl RatatuiMonitoringMode {
fn render_hosts_list(frame: &mut Frame, state: &mut RatatuiApp, area: Rect) {
// Headers
let headers = ["Host", "RTT", "Last Change"]
.iter()
.map(|h| Cell::from(*h));
let header = Row::new(headers)
.style(Style::default().fg(Color::Yellow).bold())
.bottom_margin(1);
// Rows
let mut rows = vec![];
for (_, current) in state.state.iter() {
let name_field = format!(
"{:<40}",
format!("{} ({})", current.name.clone(), current.target.clone())
);
let name_style = if current.alive {
Style::default().fg(Color::Green)
} else {
Style::default().fg(Color::Red)
};
rows.push(Row::new(vec![
Cell::from(name_field).style(name_style),
Cell::from(current.last_rtt.to_string()),
Cell::from(format!(
"{} ago.",
duration_to_string(
SystemTime::now()
.duration_since(current.last_alive_change)
.unwrap()
)
)),
]));
}
// Table Builder
let table = Table::new(rows, vec![
Constraint::Min(30),
Constraint::Min(6),
Constraint::Min(5),
Constraint::Min(30),
])
.header(header)
.block(
Block::default()
.title(Line::from(format!("PP v{}", env!("CARGO_PKG_VERSION"))))
.borders(Borders::ALL),
)
.widths(&[
Constraint::Fill(2), // Host
Constraint::Min(4), // RTT
Constraint::Min(30), // TIme Since
])
.row_highlight_style(
Style::default()
.bg(Color::Blue)
.fg(Color::White)
.add_modifier(Modifier::BOLD),
)
.highlight_symbol(">> ");
// frame.render_widget(table, layouts[0]);
frame.render_stateful_widget(table, area, &mut make_hosts_list_state(state.selected_host));
}
fn render_logs(frame: &mut Frame, state: &mut RatatuiApp, logs_layout: Rect) {
let list_items: Vec<ListItem> = state
.get_log_entries(10)
.iter()
.map(|entry| ListItem::new(entry.clone()))
.collect();
// Log
frame.render_widget(
List::new(list_items)
.block(Block::default().title("Logs").borders(Borders::ALL))
.highlight_style(
Style::default()
.fg(Color::Yellow)
.add_modifier(Modifier::BOLD),
),
logs_layout,
);
}
fn render_footer_block(frame: &mut Frame, footer_layout: Rect) {
frame.render_widget(
Paragraph::new(
"Press <ESC> or q to exit | Press d to delete host | Press a to add host",
)
.block(Block::bordered())
.centered(),
footer_layout,
);
}
pub fn render(frame: &mut Frame, state: &mut RatatuiApp) {
let layouts = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Fill(1), // Top Bar
Constraint::Length(10), // Hosts
Constraint::Length(3), // Logs
])
.split(frame.area());
Self::render_hosts_list(frame, state, layouts[0]);
Self::render_logs(frame, state, layouts[1]);
Self::render_footer_block(frame, layouts[2]);
Self::add_popup(frame, state);
Self::exit_popup(frame, state);
Self::delete_popup(frame, state);
}
fn add_popup(frame: &mut Frame, state: &RatatuiApp) {
if state.showing_add_popup {
let area = RatatuiMonitoringMode::popup_area(frame.area(), 10, 40);
frame.render_widget(Clear, area);
frame.render_widget(
Paragraph::new(format!("Host Address : {}", state.add_host_name))
.block(Block::bordered().title("Label")),
area,
);
}
}
fn exit_popup(frame: &mut Frame, state: &RatatuiApp) {
if state.trying_to_exit {
let area = RatatuiMonitoringMode::popup_area(frame.area(), 5, 20);
frame.render_widget(Clear, area);
frame.render_widget(
Paragraph::new("Are you sure? (Y/N)").block(Block::bordered().title("Exit")),
area,
);
}
}
fn delete_popup(frame: &mut Frame, state: &RatatuiApp) {
if state.trying_to_delete {
let as_list = state.state.clone();
let mut host_name = String::new();
for (index, (label, _)) in as_list.iter().enumerate() {
if index == state.selected_host {
host_name = label.clone();
}
}
let block = Paragraph::new(format!("Delete {}", host_name))
.centered()
.block(Block::bordered());
frame.render_widget(block, frame.area());
}
}
fn popup_area(area: Rect, percent_x: u16, percent_y: u16) -> Rect {
let vertical = Layout::vertical([Constraint::Percentage(percent_x)]).flex(Flex::Center);
let horizontal = Layout::horizontal([Constraint::Percentage(percent_y)]).flex(Flex::Center);
let [area] = vertical.areas(area);
let [area] = horizontal.areas(area);
area
}
fn handle_add_popup_inputs(app: &mut RatatuiApp, key: KeyCode) {
match key {
KeyCode::Esc => {
app.showing_add_popup = false;
}
KeyCode::Backspace => {
app.add_host_name.remove(app.add_host_name.len() - 1);
}
KeyCode::Enter => {
app.state.insert(app.add_host_name.clone(), TargetState {
name: app.add_host_name.clone(),
..Default::default()
});
}
KeyCode::Down => {
dbg!("Move down to next field");
}
KeyCode::Up => {
dbg!("Move up to previous field");
}
_ => {
app.add_host_name = format!("{}{}", app.add_host_name, key);
//dbg!(format!("ADD_HOST_NAME: {}", app.add_host_name.clone()));
}
}
}
fn handle_exit_popup_inputs(app: &mut RatatuiApp, key: KeyCode) {
match key {
KeyCode::Char('y') | KeyCode::Char('Y') => {
app.set_running(false);
}
KeyCode::Char('n') | KeyCode::Char('N') => {
app.trying_to_exit = false;
}
_ => {}
}
}
fn handle_monitoring_screen_inputs(app: &mut RatatuiApp, key: KeyCode) {
// Default monitoring Screen
match key {
KeyCode::Down => {
if app.selected_host + 1 == app.state.len() {
app.selected_host = 0;
} else {
app.selected_host += 1;
}
}
KeyCode::Up => {
if app.selected_host == 0 {
app.selected_host = app.state.len() - 1;
} else {
app.selected_host -= 1;
}
}
KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('Q') => {
app.trying_to_exit = true;
}
KeyCode::Char('a') | KeyCode::Char('A') => {
app.showing_add_popup = true;
}
KeyCode::Char('d') | KeyCode::Char('D') => {
app.trying_to_delete = true;
}
_ => {}
}
}
fn handle_delete_popup_inputs(app: &mut RatatuiApp, key: KeyCode) {
match key {
KeyCode::Char('y') | KeyCode::Char('Y') => {
app.remove_selected_host();
app.trying_to_delete = false;
}
KeyCode::Char('n') | KeyCode::Char('N') => {
dbg!("Do not delete this item.");
app.trying_to_delete = false;
}
_ => {}
}
}
pub fn handle_crossterm_events(app: &mut RatatuiApp) -> Result<()> {
if event::poll(Duration::from_millis(100))? {
match event::read()? {
Event::Key(key) if key.kind == KeyEventKind::Press => {
// adding mode
if app.showing_add_popup {
Self::handle_add_popup_inputs(app, key.code);
} else if app.trying_to_exit {
Self::handle_exit_popup_inputs(app, key.code);
} else if app.trying_to_delete {
Self::handle_delete_popup_inputs(app, key.code);
} else {
Self::handle_monitoring_screen_inputs(app, key.code);
}
}
_ => {}
}
}
Ok(())
}
}
pub fn make_hosts_list_state(selected: usize) -> TableState {
let mut state = TableState::default();
state.select(Some(selected));
state
}

259
src/tui/ratatui_app.rs Normal file
View File

@ -0,0 +1,259 @@
use crate::manager::Manager;
use crate::ping_result::PingResult;
use crate::target_state::TargetState;
use crate::tui::mode_adding::RatatuiAddingMode;
use crate::tui::mode_deleting::RatatuiDeletingMode;
use crate::tui::mode_editing::RatatuiEditingMode;
use crate::tui::mode_monitoring::RatatuiMonitoringMode;
use crate::tui::ratatui_screens::RatatuiScreens;
use chrono::{DateTime, Local};
use color_eyre::Result;
use log::debug;
use ratatui::prelude::*;
use ratatui::{DefaultTerminal, Frame};
use std::collections::BTreeMap;
use std::fs::File;
use std::net::Ipv4Addr;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::mpsc;
use std::sync::mpsc::Receiver;
use std::thread;
use std::time::{Duration, SystemTime};
const LOG_ENTRIES_DEFAULT_COUNT: u32 = 10;
#[derive(Default)]
pub struct RatatuiApp {
running: bool,
pub state: BTreeMap<String, TargetState>,
current_screen: RatatuiScreens,
log_entries: Vec<String>,
filename: Option<String>,
pub selected_host: usize,
pub showing_add_popup: bool,
pub trying_to_exit: bool,
pub trying_to_delete: bool,
pub add_host_cursor_position: usize,
pub add_host_name: String,
pub num_log_entries: u32
}
/// Private Methods
impl RatatuiApp {
pub fn remove_selected_host(self: &mut RatatuiApp) {
for (index, (name, data)) in self.state.clone().iter().enumerate() {
if index == self.selected_host {
self.state.remove(name).unwrap();
}
}
}
pub fn set_filename(mut self, new_filename: String) {
self.filename = Some(new_filename)
}
pub fn get_filename(self) -> String {
self.filename.unwrap_or(String::new())
}
pub fn add_new_host(mut self, new_target: TargetState) -> Result<()> {
self.state.insert(new_target.name.clone(), new_target);
Ok(())
}
pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
self.running = true;
// start the 'manager' thread that spawns its ping threads as needed
let (sender, receiver) = mpsc::channel::<PingResult>();
Manager::spawn_manager_thread(self.targets_as_vec(), sender);
while self.running {
// check for any waiting ping results...
self.consume_waiting_results(&receiver);
terminal.draw(|frame| RatatuiMonitoringMode::render(frame, &mut self))?;
RatatuiMonitoringMode::handle_crossterm_events(&mut self)?;
}
Ok(())
}
fn consume_waiting_results(&mut self, receiver: &Receiver<PingResult>) {
let mut local_state = self.state.clone();
if let Ok(new_message) = receiver.recv_timeout(Duration::from_millis(10)) {
// find the right TargetState
for (name, mut current_state) in local_state.clone() {
if current_state.target == new_message.target {
let did_change = new_message.success != current_state.alive;
let last_alive_change = if did_change {
SystemTime::now()
} else {
current_state.last_alive_change
};
let new_state = TargetState {
name: current_state.name.clone(),
target: current_state.target,
alive: new_message.success,
last_rtt: new_message.rtt,
last_alive_change,
..TargetState::default()
};
local_state.insert(name.clone(), new_state.clone());
let success_message = if new_state.alive {
"Success"
} else {
"Failure"
};
let current_time: DateTime<Local> = SystemTime::now().into();
if did_change {
self.log_entries.push(format!(
"{} {} for {}",
current_time.format("%Y-%m-%d %H:%M:%S:").to_string(),
success_message,
new_state.name.clone()
));
}
}
}
}
self.state = local_state;
}
fn targets_as_vec(&self) -> Vec<Ipv4Addr> {
let mut working = vec![];
for (_, current) in &self.state {
working.push(current.target);
}
working
}
fn render(&mut self, frame: &mut Frame) {
RatatuiMonitoringMode::render(frame, self)
}
fn quit(&mut self) {
self.running = false;
}
fn get_default_targets() -> BTreeMap<String, TargetState> {
let mut working = BTreeMap::new();
working.insert(
"1111 DNS".to_string(),
TargetState {
name: "1111 DNS".to_string(),
target: Ipv4Addr::new(1, 1, 1, 1),
..TargetState::default()
},
);
working.insert(
"Google DNS".to_string(),
TargetState {
name: "Google DNS".to_string(),
target: Ipv4Addr::new(8, 8, 8, 8),
..TargetState::default()
},
);
working.insert(
"Test Site 1".to_string(),
TargetState {
name: "Test Site 1".to_string(),
target: Ipv4Addr::new(216, 234, 202, 122),
..TargetState::default()
},
);
working
}
}
/// Public Methods
impl RatatuiApp {
fn load_hosts_from_file(file_to_load_from: PathBuf) -> BTreeMap<String, TargetState> {
let mut working = BTreeMap::new();
let the_file = File::open(file_to_load_from.clone());
let mut rdr = csv::Reader::from_reader(the_file.unwrap());
for result in rdr.records() {
let record = result.unwrap();
working.insert(
record[1].to_string(),
TargetState {
name: record[1].to_string(),
target: Ipv4Addr::from_str(&record[0]).unwrap(),
..TargetState::default()
},
);
}
working
}
pub fn new(option: Option<PathBuf>) -> Self {
let mut working = Self::default();
let targets = if let Some(file) = option {
if file.exists() {
working.filename = Some(file.as_os_str().to_string_lossy().parse().unwrap());
} else {
// working.log_event("Passed file doesnt exist looking for hosts.txt".to_string());
if PathBuf::from_str("hosts.txt").unwrap().exists() {
// working.log_event("Found hosts.txt. using it as the default".to_string());
working.filename = Some("hosts.txt".to_string());
} else {
// working.log_event("Didnt find hosts.txt".to_string());
working.filename = None;
}
}
if let Some(file) = working.filename.clone() {
RatatuiApp::load_hosts_from_file(PathBuf::from_str(file.as_str()).unwrap())
} else {
RatatuiApp::get_default_targets()
}
} else {
// none was passed for our parameter.
working.filename = Some("hosts.txt".to_string());
if let Some(ref hosts_file) = working.filename {
if PathBuf::from_str(hosts_file).unwrap().exists() {
RatatuiApp::load_hosts_from_file(
PathBuf::from_str(&*hosts_file.clone()).unwrap(),
)
} else {
working.filename = None;
RatatuiApp::get_default_targets()
}
} else {
RatatuiApp::get_default_targets()
}
};
working.state = targets.clone();
working.num_log_entries = LOG_ENTRIES_DEFAULT_COUNT;
working
}
pub fn set_screen(&mut self, new_screen: RatatuiScreens) {
self.current_screen = new_screen
}
pub fn set_running(&mut self, new_state: bool) {
self.running = new_state
}
pub fn clear_log(&mut self) {
self.log_entries.clear();
}
pub fn log_event(&mut self, to_log: String) {
self.log_entries.push(to_log);
}
pub fn get_log_entries(&self, how_many: u32) -> Vec<String> {
self.log_entries
.iter()
.rev()
.take(how_many as usize)
.cloned()
.collect()
}
}

View File

@ -0,0 +1,9 @@
#[derive(Default)]
pub enum RatatuiScreens {
#[default]
Monitoring,
Editing,
Deleting,
Adding
}

View File

@ -0,0 +1,11 @@
use ratatui::buffer::Cell;
use ratatui::prelude::*;
pub struct TargetStateWidget;
impl Widget for TargetStateWidget {
fn render(self, area: Rect, buf: &mut Buffer)
{
}
}