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
use std::{fmt::Display, collections::HashMap};

pub const TITLE_NAMES: [&str; 17] = ["timeStamp", "elapsed", "label", "responseCode", "responseMessage", "threadName", "dataType", "success", "failureMessage", "bytes", "sentBytes", "grpThreads", "allThreads", "URL", "Latency", "IdleTime", "Connect"];

#[derive(Clone)]
pub struct RecordData {
    time_stamp: u128,
    elapsed: u64,
    label: String,
    response_code: u16,
    response_message: String,
    thread_name: String,
    data_type: String,
    success: bool,
    failure_message: Option<String>,
    bytes: u64,
    sent_bytes: u64,
    grp_threads: u32,
    all_threads: u32,
    url: String,
    latency: u64,
    idle_time: u64,
    connect: u64,
    response_result: Option<ResponseResult>,
}

#[derive(Clone)]
pub struct ResponseResult {
    response_headers: HashMap<String, String>,
    response_data: String,
}

impl ResponseResult {
    pub fn new(response_headers: HashMap<String, String>, response_data: String) -> Self {
        Self { response_headers, response_data }
    }

    pub fn get_headers(&self) -> HashMap<String, String> {
        self.response_headers.clone()
    }

    pub fn get_response_data(&self) -> String {
        self.response_data.clone()
    }
}

impl RecordData {
    pub fn new(
        time_stamp: u128,
        elapsed: u64,
        label: String,
        response_code: u16,
        response_message: String,
        thread_name: String,
        data_type: String,
        success: bool,
        failure_message: Option<String>,
        bytes: u64,
        sent_bytes: u64,
        grp_threads: u32,
        all_threads: u32,
        url: String,
        latency: u64,
        idle_time: u64,
        connect: u64,
        response_result: Option<ResponseResult>,
    ) -> Self {
        Self {
            time_stamp,
            elapsed,
            label,
            response_code,
            response_message,
            thread_name,
            data_type,
            success,
            failure_message,
            bytes,
            sent_bytes,
            grp_threads,
            all_threads,
            url,
            latency,
            idle_time,
            connect,
            response_result,
        }
    }

    pub fn thread_name(&mut self, thread_name: String) {
        self.thread_name = thread_name;
    }

    pub fn grp_threads(&mut self, grp_threads: u32) {
        self.grp_threads = grp_threads;
    }

    pub fn all_threads(&mut self, all_threads: u32) {
        self.all_threads = all_threads;
    }

    pub fn get_response_result(&self) -> Option<ResponseResult> {
        self.response_result.clone()
    }
}

impl Display for RecordData {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}", 
            self.time_stamp, 
            self.elapsed,
            self.label,
            self.response_code,
            self.response_message,
            self.thread_name,
            self.data_type,
            self.success,
            self.failure_message.clone().unwrap_or("".to_string()),
            self.bytes,
            self.sent_bytes,
            self.grp_threads,
            self.all_threads,
            self.url,
            self.latency,
            self.idle_time,
            self.connect,
        )
    }
}