-
Notifications
You must be signed in to change notification settings - Fork 7
/
ping_result_processor_csv_logger.rs
185 lines (175 loc) · 7.31 KB
/
ping_result_processor_csv_logger.rs
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
use crate::*;
use std::{fs::File, io, io::prelude::*, path::PathBuf};
use tracing;
pub struct PingResultProcessorCsvLogger {
log_path: PathBuf,
log_file: File,
}
impl PingResultProcessorCsvLogger {
#[tracing::instrument(name = "Creating ping result csv logger", level = "debug")]
pub fn new(log_path_buf: &PathBuf) -> PingResultProcessorCsvLogger {
return PingResultProcessorCsvLogger {
log_path: log_path_buf.clone(),
log_file: rnp_utils::create_log_file(log_path_buf),
};
}
fn log_result_as_csv(&mut self, ping_result: &PingResult) -> io::Result<()> {
let log_content = ping_result.format_as_csv_string();
self.log_file.write(log_content.as_bytes())?;
self.log_file.write("\n".as_bytes())?;
return Ok(());
}
}
impl PingResultProcessor for PingResultProcessorCsvLogger {
fn name(&self) -> &'static str {
"CsvLogger"
}
fn initialize(&mut self) {
// Writer CSV header
self.log_file
.write("UtcTime,WorkerId,Protocol,TargetIp,TargetPort,SourceIp,SourcePort,IsWarmup,IsSucceeded,RttInMs,IsTimedOut,PreparationError,PingError,HandshakeError,DisconnectError\n".as_bytes())
.expect(&format!(
"Failed to write logs to csv file! Path = {}",
self.log_path.display()
));
}
fn process_ping_result(&mut self, ping_result: &PingResult) {
self.log_result_as_csv(ping_result).expect(&format!(
"Failed to write logs to csv file! Path = {}",
self.log_path.display()
));
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ping_result_processors::ping_result_processor_test_common;
use crate::rnp_dto::PingResultCsvDto;
use chrono::{TimeZone, Utc};
use pretty_assertions::assert_eq;
#[test]
fn ping_result_process_csv_logger_should_work() {
let test_log_file_path = "tests_data\\test_log.csv";
let mut processor: Box<dyn PingResultProcessor + Send + Sync> = Box::new(
PingResultProcessorCsvLogger::new(&PathBuf::from(test_log_file_path)),
);
ping_result_processor_test_common::run_ping_result_processor_with_test_samples(
&mut processor,
);
let mut actual_logged_records = Vec::new();
{
let mut csv_reader = csv::Reader::from_path(test_log_file_path).unwrap();
for result in csv_reader.deserialize() {
let actual_record: PingResultCsvDto = result.unwrap();
actual_logged_records.push(actual_record);
}
}
assert_eq!(
vec![
PingResultCsvDto {
utc_time: Utc.ymd(2021, 7, 6).and_hms_milli(9, 10, 11, 12),
worker_id: 1,
protocol: "TCP".to_string(),
target_ip: "1.2.3.4".parse().unwrap(),
target_port: 443,
source_ip: "5.6.7.8".parse().unwrap(),
source_port: 8080,
is_warmup: true,
is_succeeded: true,
is_timed_out: false,
rtt_in_ms: 10f64,
preparation_error: "".to_string(),
ping_error: "".to_string(),
handshake_error: "".to_string(),
disconnect_error: "".to_string(),
},
PingResultCsvDto {
utc_time: Utc.ymd(2021, 7, 6).and_hms_milli(9, 10, 11, 12),
worker_id: 1,
protocol: "TCP".to_string(),
target_ip: "1.2.3.4".parse().unwrap(),
target_port: 443,
source_ip: "5.6.7.8".parse().unwrap(),
source_port: 8080,
is_warmup: false,
is_succeeded: false,
is_timed_out: true,
rtt_in_ms: 1000f64,
preparation_error: "".to_string(),
ping_error: "".to_string(),
handshake_error: "".to_string(),
disconnect_error: "".to_string(),
},
PingResultCsvDto {
utc_time: Utc.ymd(2021, 7, 6).and_hms_milli(9, 10, 11, 12),
worker_id: 1,
protocol: "TCP".to_string(),
target_ip: "1.2.3.4".parse().unwrap(),
target_port: 443,
source_ip: "5.6.7.8".parse().unwrap(),
source_port: 8080,
is_warmup: false,
is_succeeded: true,
is_timed_out: false,
rtt_in_ms: 20f64,
preparation_error: "".to_string(),
ping_error: "".to_string(),
handshake_error: "connect aborted".to_string(),
disconnect_error: "".to_string(),
},
PingResultCsvDto {
utc_time: Utc.ymd(2021, 7, 6).and_hms_milli(9, 10, 11, 12),
worker_id: 1,
protocol: "TCP".to_string(),
target_ip: "1.2.3.4".parse().unwrap(),
target_port: 443,
source_ip: "5.6.7.8".parse().unwrap(),
source_port: 8080,
is_warmup: false,
is_succeeded: true,
is_timed_out: false,
rtt_in_ms: 20f64,
preparation_error: "".to_string(),
ping_error: "".to_string(),
handshake_error: "".to_string(),
disconnect_error: "disconnect timeout".to_string(),
},
PingResultCsvDto {
utc_time: Utc.ymd(2021, 7, 6).and_hms_milli(9, 10, 11, 12),
worker_id: 1,
protocol: "TCP".to_string(),
target_ip: "1.2.3.4".parse().unwrap(),
target_port: 443,
source_ip: "5.6.7.8".parse().unwrap(),
source_port: 8080,
is_warmup: false,
is_succeeded: false,
is_timed_out: false,
rtt_in_ms: 0f64,
preparation_error: "".to_string(),
ping_error: "connect failed".to_string(),
handshake_error: "".to_string(),
disconnect_error: "".to_string(),
},
PingResultCsvDto {
utc_time: Utc.ymd(2021, 7, 6).and_hms_milli(9, 10, 11, 12),
worker_id: 1,
protocol: "TCP".to_string(),
target_ip: "1.2.3.4".parse().unwrap(),
target_port: 443,
source_ip: "5.6.7.8".parse().unwrap(),
source_port: 8080,
is_warmup: false,
is_succeeded: false,
is_timed_out: false,
rtt_in_ms: 0f64,
preparation_error: "address in use".to_string(),
ping_error: "".to_string(),
handshake_error: "".to_string(),
disconnect_error: "".to_string(),
},
],
actual_logged_records,
);
}
}