Skip to content

Commit 7233cc9

Browse files
committed
data threshold notifications: represent amounts with bars
1 parent bc6c507 commit 7233cc9

7 files changed

Lines changed: 167 additions & 171 deletions

File tree

src/gui/pages/notifications_page.rs

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,32 @@ use iced::Length::FillPortion;
22
use iced::widget::scrollable::Direction;
33
use iced::widget::text::LineHeight;
44
use iced::widget::tooltip::Position;
5-
use iced::widget::{Column, Container, Row, Scrollable, Text, Tooltip};
5+
use iced::widget::{Column, Container, Row, Scrollable, Text, Tooltip, horizontal_space};
66
use iced::widget::{Space, button, vertical_space};
77
use iced::{Alignment, Font, Length, Padding};
88
use std::fmt::Write;
99

1010
use crate::chart::types::chart_type::ChartType;
11+
use crate::countries::country_utils::get_computer_tooltip;
1112
use crate::gui::components::header::get_button_settings;
1213
use crate::gui::components::tab::get_pages_tabs;
1314
use crate::gui::components::types::my_modal::MyModal;
14-
use crate::gui::pages::overview_page::host_bar;
15+
use crate::gui::pages::overview_page::{get_bars, get_bars_length, host_bar};
1516
use crate::gui::pages::types::settings_page::SettingsPage;
1617
use crate::gui::styles::container::ContainerType;
1718
use crate::gui::styles::scrollbar::ScrollbarType;
1819
use crate::gui::styles::style_constants::FONT_SIZE_FOOTER;
1920
use crate::gui::styles::text::TextType;
2021
use crate::gui::types::message::Message;
22+
use crate::networking::types::data_info::DataInfo;
23+
use crate::networking::types::traffic_type::TrafficType;
2124
use crate::notifications::types::logged_notification::{
22-
BytesThresholdExceeded, FavoriteTransmitted, LoggedNotification, PacketsThresholdExceeded,
25+
DataThresholdExceeded, FavoriteTransmitted, LoggedNotification,
2326
};
2427
use crate::translations::translations::{
25-
bytes_exceeded_translation, bytes_exceeded_value_translation, clear_all_translation,
26-
favorite_transmitted_translation, incoming_translation, no_notifications_received_translation,
27-
no_notifications_set_translation, only_last_30_translation, outgoing_translation,
28-
packets_exceeded_translation, packets_exceeded_value_translation, per_second_translation,
28+
bytes_exceeded_translation, clear_all_translation, favorite_transmitted_translation,
29+
no_notifications_received_translation, no_notifications_set_translation,
30+
only_last_30_translation, packets_exceeded_translation, per_second_translation,
2931
threshold_translation,
3032
};
3133
use crate::utils::types::icon::Icon;
@@ -140,7 +142,7 @@ fn body_no_notifications_received(
140142
}
141143

142144
fn packets_notification_log<'a>(
143-
logged_notification: PacketsThresholdExceeded,
145+
logged_notification: DataThresholdExceeded,
144146
language: Language,
145147
font: Font,
146148
) -> Container<'a, Message, StyleType> {
@@ -150,14 +152,6 @@ fn packets_notification_log<'a>(
150152
logged_notification.threshold,
151153
per_second_translation(language)
152154
);
153-
let mut incoming_str = " - ".to_string();
154-
incoming_str.push_str(incoming_translation(language));
155-
incoming_str.push_str(": ");
156-
incoming_str.push_str(&logged_notification.incoming.to_string());
157-
let mut outgoing_str = " - ".to_string();
158-
outgoing_str.push_str(outgoing_translation(language));
159-
outgoing_str.push_str(": ");
160-
outgoing_str.push_str(&logged_notification.outgoing.to_string());
161155
let content = Row::new()
162156
.align_y(Alignment::Center)
163157
.height(Length::Fill)
@@ -190,19 +184,13 @@ fn packets_notification_log<'a>(
190184
.font(font),
191185
),
192186
)
193-
.push(
194-
Column::new()
195-
.spacing(7)
196-
.push(
197-
Text::new(packets_exceeded_value_translation(
198-
language,
199-
logged_notification.incoming + logged_notification.outgoing,
200-
))
201-
.font(font),
202-
)
203-
.push(Text::new(incoming_str).font(font))
204-
.push(Text::new(outgoing_str).font(font)),
205-
);
187+
.push(threshold_bar(
188+
logged_notification.data_info,
189+
ChartType::Packets,
190+
logged_notification.data_info,
191+
font,
192+
language,
193+
));
206194
Container::new(content)
207195
.height(120)
208196
.width(Length::Fill)
@@ -211,7 +199,7 @@ fn packets_notification_log<'a>(
211199
}
212200

213201
fn bytes_notification_log<'a>(
214-
logged_notification: BytesThresholdExceeded,
202+
logged_notification: DataThresholdExceeded,
215203
language: Language,
216204
font: Font,
217205
) -> Container<'a, Message, StyleType> {
@@ -220,20 +208,7 @@ fn bytes_notification_log<'a>(
220208
threshold_str.push_str(&ByteMultiple::formatted_string(
221209
(logged_notification.threshold).into(),
222210
));
223-
224211
let _ = write!(threshold_str, " {}", per_second_translation(language));
225-
let mut incoming_str = " - ".to_string();
226-
incoming_str.push_str(incoming_translation(language));
227-
incoming_str.push_str(": ");
228-
incoming_str.push_str(&ByteMultiple::formatted_string(u128::from(
229-
logged_notification.incoming,
230-
)));
231-
let mut outgoing_str = " - ".to_string();
232-
outgoing_str.push_str(outgoing_translation(language));
233-
outgoing_str.push_str(": ");
234-
outgoing_str.push_str(&ByteMultiple::formatted_string(u128::from(
235-
logged_notification.outgoing,
236-
)));
237212
let content = Row::new()
238213
.spacing(30)
239214
.align_y(Alignment::Center)
@@ -266,21 +241,13 @@ fn bytes_notification_log<'a>(
266241
.font(font),
267242
),
268243
)
269-
.push(
270-
Column::new()
271-
.spacing(7)
272-
.push(
273-
Text::new(bytes_exceeded_value_translation(
274-
language,
275-
&ByteMultiple::formatted_string(u128::from(
276-
logged_notification.incoming + logged_notification.outgoing,
277-
)),
278-
))
279-
.font(font),
280-
)
281-
.push(Text::new(incoming_str).font(font))
282-
.push(Text::new(outgoing_str).font(font)),
283-
);
244+
.push(threshold_bar(
245+
logged_notification.data_info,
246+
ChartType::Bytes,
247+
logged_notification.data_info,
248+
font,
249+
language,
250+
));
284251
Container::new(content)
285252
.height(120)
286253
.width(Length::Fill)
@@ -330,7 +297,7 @@ fn favorite_notification_log<'a>(
330297
.font(font),
331298
),
332299
)
333-
.push(Column::new().spacing(7).push(host_bar));
300+
.push(host_bar);
334301

335302
Container::new(content)
336303
.height(120)
@@ -387,3 +354,41 @@ fn logged_notifications<'a>(sniffer: &Sniffer) -> Column<'a, Message, StyleType>
387354
}
388355
ret_val
389356
}
357+
358+
fn threshold_bar<'a>(
359+
data_info: DataInfo,
360+
chart_type: ChartType,
361+
first_entry_data_info: DataInfo,
362+
font: Font,
363+
language: Language,
364+
) -> Row<'a, Message, StyleType> {
365+
let (incoming_bar_len, outgoing_bar_len) =
366+
get_bars_length(chart_type, &first_entry_data_info, &data_info);
367+
368+
Row::new()
369+
.align_y(Alignment::Center)
370+
.spacing(5)
371+
.push(get_computer_tooltip(
372+
true,
373+
true,
374+
None,
375+
TrafficType::Unicast,
376+
language,
377+
font,
378+
))
379+
.push(
380+
Column::new()
381+
.spacing(1)
382+
.push(
383+
Row::new().push(horizontal_space()).push(
384+
Text::new(if chart_type.eq(&ChartType::Packets) {
385+
data_info.tot_packets().to_string()
386+
} else {
387+
ByteMultiple::formatted_string(data_info.tot_bytes())
388+
})
389+
.font(font),
390+
),
391+
)
392+
.push(get_bars(incoming_bar_len, outgoing_bar_len)),
393+
)
394+
}

src/gui/sniffer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,11 +1158,12 @@ mod tests {
11581158
use crate::gui::styles::types::gradient_type::GradientType;
11591159
use crate::gui::types::message::Message;
11601160
use crate::gui::types::timing_events::TimingEvents;
1161+
use crate::networking::types::data_info::DataInfo;
11611162
use crate::networking::types::host::Host;
11621163
use crate::networking::types::info_traffic::InfoTrafficMessage;
11631164
use crate::networking::types::traffic_direction::TrafficDirection;
11641165
use crate::notifications::types::logged_notification::{
1165-
LoggedNotification, PacketsThresholdExceeded,
1166+
DataThresholdExceeded, LoggedNotification,
11661167
};
11671168
use crate::notifications::types::notifications::{
11681169
BytesNotification, FavoriteNotification, Notification, Notifications, PacketsNotification,
@@ -1922,10 +1923,9 @@ mod tests {
19221923
let mut sniffer = Sniffer::new(Configs::default());
19231924
sniffer.logged_notifications =
19241925
VecDeque::from([LoggedNotification::PacketsThresholdExceeded(
1925-
PacketsThresholdExceeded {
1926+
DataThresholdExceeded {
19261927
threshold: 0,
1927-
incoming: 0,
1928-
outgoing: 0,
1928+
data_info: DataInfo::default(),
19291929
timestamp: "".to_string(),
19301930
},
19311931
)]);

src/networking/types/data_info.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ impl DataInfo {
9696
self.final_instant = rhs.final_instant;
9797
}
9898

99+
pub fn subtract(&mut self, rhs: Self) {
100+
self.incoming_packets -= rhs.incoming_packets;
101+
self.outgoing_packets -= rhs.outgoing_packets;
102+
self.incoming_bytes -= rhs.incoming_bytes;
103+
self.outgoing_bytes -= rhs.outgoing_bytes;
104+
}
105+
99106
pub fn compare(&self, other: &Self, sort_type: SortType, chart_type: ChartType) -> Ordering {
100107
match chart_type {
101108
ChartType::Packets => match sort_type {

src/notifications/notify_and_log.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::InfoTraffic;
22
use crate::networking::types::capture_context::CaptureSource;
33
use crate::notifications::types::logged_notification::{
4-
BytesThresholdExceeded, FavoriteTransmitted, LoggedNotification, PacketsThresholdExceeded,
4+
DataThresholdExceeded, FavoriteTransmitted, LoggedNotification,
55
};
66
use crate::notifications::types::notifications::Notifications;
77
use crate::notifications::types::sound::{Sound, play};
@@ -20,23 +20,20 @@ pub fn notify_and_log(
2020
let mut sound_to_play = Sound::None;
2121
let mut emitted_notifications = 0;
2222
let timestamp = info_traffic.last_packet_timestamp;
23+
let mut data_info_delta = info_traffic.tot_data_info;
24+
data_info_delta.subtract(info_traffic.tot_data_info_prev);
2325
// packets threshold
2426
if let Some(threshold) = notifications.packets_notification.threshold {
25-
let sent_packets_entry = info_traffic.tot_data_info.outgoing_packets()
26-
- info_traffic.tot_data_info_prev.outgoing_packets();
27-
let received_packets_entry = info_traffic.tot_data_info.incoming_packets()
28-
- info_traffic.tot_data_info_prev.incoming_packets();
29-
if received_packets_entry + sent_packets_entry > u128::from(threshold) {
27+
if data_info_delta.tot_packets() > u128::from(threshold) {
3028
// log this notification
3129
emitted_notifications += 1;
3230
if logged_notifications.len() >= 30 {
3331
logged_notifications.pop_back();
3432
}
3533
logged_notifications.push_front(LoggedNotification::PacketsThresholdExceeded(
36-
PacketsThresholdExceeded {
34+
DataThresholdExceeded {
3735
threshold: notifications.packets_notification.previous_threshold,
38-
incoming: received_packets_entry.try_into().unwrap_or_default(),
39-
outgoing: sent_packets_entry.try_into().unwrap_or_default(),
36+
data_info: data_info_delta,
4037
timestamp: get_formatted_timestamp(timestamp),
4138
},
4239
));
@@ -47,21 +44,16 @@ pub fn notify_and_log(
4744
}
4845
// bytes threshold
4946
if let Some(threshold) = notifications.bytes_notification.threshold {
50-
let sent_bytes_entry = info_traffic.tot_data_info.outgoing_bytes()
51-
- info_traffic.tot_data_info_prev.outgoing_bytes();
52-
let received_bytes_entry = info_traffic.tot_data_info.incoming_bytes()
53-
- info_traffic.tot_data_info_prev.incoming_bytes();
54-
if received_bytes_entry + sent_bytes_entry > u128::from(threshold) {
47+
if data_info_delta.tot_bytes() > u128::from(threshold) {
5548
//log this notification
5649
emitted_notifications += 1;
5750
if logged_notifications.len() >= 30 {
5851
logged_notifications.pop_back();
5952
}
6053
logged_notifications.push_front(LoggedNotification::BytesThresholdExceeded(
61-
BytesThresholdExceeded {
54+
DataThresholdExceeded {
6255
threshold: notifications.bytes_notification.previous_threshold,
63-
incoming: received_bytes_entry.try_into().unwrap_or_default(),
64-
outgoing: sent_bytes_entry.try_into().unwrap_or_default(),
56+
data_info: data_info_delta,
6557
timestamp: get_formatted_timestamp(timestamp),
6658
},
6759
));

src/notifications/types/logged_notification.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
1+
use crate::networking::types::data_info::DataInfo;
12
use crate::networking::types::data_info_host::DataInfoHost;
23
use crate::networking::types::host::Host;
34

45
/// Enum representing the possible notification events.
56
pub enum LoggedNotification {
67
/// Packets threshold exceeded
7-
PacketsThresholdExceeded(PacketsThresholdExceeded),
8+
PacketsThresholdExceeded(DataThresholdExceeded),
89
/// Byte threshold exceeded
9-
BytesThresholdExceeded(BytesThresholdExceeded),
10+
BytesThresholdExceeded(DataThresholdExceeded),
1011
/// Favorite connection exchanged data
1112
FavoriteTransmitted(FavoriteTransmitted),
1213
}
1314

1415
#[derive(Clone)]
15-
pub struct PacketsThresholdExceeded {
16-
pub(crate) threshold: u32,
17-
pub(crate) incoming: u32,
18-
pub(crate) outgoing: u32,
19-
pub(crate) timestamp: String,
20-
}
21-
22-
#[derive(Clone)]
23-
pub struct BytesThresholdExceeded {
16+
pub struct DataThresholdExceeded {
2417
pub(crate) threshold: u64,
25-
pub(crate) incoming: u32,
26-
pub(crate) outgoing: u32,
18+
pub(crate) data_info: DataInfo,
2719
pub(crate) timestamp: String,
2820
}
2921

src/notifications/types/notifications.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ pub enum Notification {
3737
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize, Debug, Copy)]
3838
pub struct PacketsNotification {
3939
/// Threshold of received + sent packets; if exceeded a notification is emitted
40-
pub threshold: Option<u32>,
40+
pub threshold: Option<u64>,
4141
/// The sound to emit
4242
pub sound: Sound,
4343
/// The last used Some value for the threshold field
44-
pub previous_threshold: u32,
44+
pub previous_threshold: u64,
4545
}
4646

4747
impl Default for PacketsNotification {

0 commit comments

Comments
 (0)