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
186
187
188
189
190
191
192
193
194
use std::{borrow::Cow, fmt, time::Duration};

use serde::Serialize;

pub use crate::sdam::description::{server::ServerType, topology::TopologyType};
use crate::{
    bson::DateTime,
    error::Error,
    hello::HelloCommandResponse,
    options::ServerAddress,
    sdam::ServerDescription,
    selection_criteria::TagSet,
};

/// A description of the most up-to-date information known about a server. Further details can be
/// found in the [Server Discovery and Monitoring specification](https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst).
#[derive(Clone)]
pub struct ServerInfo<'a> {
    pub(crate) description: Cow<'a, ServerDescription>,
}

impl<'a> Serialize for ServerInfo<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.description.serialize(serializer)
    }
}

impl<'a> ServerInfo<'a> {
    pub(crate) fn new_borrowed(description: &'a ServerDescription) -> Self {
        Self {
            description: Cow::Borrowed(description),
        }
    }

    pub(crate) fn new_owned(description: ServerDescription) -> Self {
        Self {
            description: Cow::Owned(description),
        }
    }

    fn command_response_getter<T>(
        &'a self,
        f: impl Fn(&'a HelloCommandResponse) -> Option<T>,
    ) -> Option<T> {
        self.description
            .reply
            .as_ref()
            .ok()
            .and_then(|reply| reply.as_ref().and_then(|r| f(&r.command_response)))
    }

    /// Gets the address of the server.
    pub fn address(&self) -> &ServerAddress {
        &self.description.address
    }

    /// Gets the weighted average of the time it has taken for a server check to round-trip
    /// from the driver to the server.
    ///
    /// This is the value that the driver uses internally to determine the latency window as part of
    /// server selection.
    pub fn average_round_trip_time(&self) -> Option<Duration> {
        self.description.average_round_trip_time
    }

    /// Gets the last time that the driver's monitoring thread for the server updated the internal
    /// information about the server.
    pub fn last_update_time(&self) -> Option<DateTime> {
        self.description.last_update_time
    }

    /// Gets the maximum wire version that the server supports.
    pub fn max_wire_version(&self) -> Option<i32> {
        self.command_response_getter(|r| r.max_wire_version)
    }

    /// Gets the minimum wire version that the server supports.
    pub fn min_wire_version(&self) -> Option<i32> {
        self.command_response_getter(|r| r.min_wire_version)
    }

    /// Gets the name of the replica set that the server is part of.
    pub fn replica_set_name(&self) -> Option<&str> {
        self.command_response_getter(|r| r.set_name.as_deref())
    }

    /// Gets the version of the replica set that the server is part of.
    pub fn replica_set_version(&self) -> Option<i32> {
        self.command_response_getter(|r| r.set_version)
    }

    /// Get the type of the server.
    pub fn server_type(&self) -> ServerType {
        self.description.server_type
    }

    /// Gets the tags associated with the server.
    pub fn tags(&self) -> Option<&TagSet> {
        self.command_response_getter(|r| r.tags.as_ref())
    }

    /// Gets the error that caused the server's state to be transitioned to Unknown, if any.
    ///
    /// When the driver encounters certain errors during operation execution or server monitoring,
    /// it transitions the affected server's state to Unknown, rendering the server unusable for
    /// future operations until it is confirmed to be in healthy state again.
    pub fn error(&self) -> Option<&Error> {
        self.description.reply.as_ref().err()
    }
}

impl<'a> fmt::Debug for ServerInfo<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> {
        match self.description.reply {
            Ok(_) => f
                .debug_struct("Server Description")
                .field("Address", self.address())
                .field("Type", &self.server_type())
                .field("Average RTT", &self.average_round_trip_time())
                .field("Last Update Time", &self.last_update_time())
                .field("Max Wire Version", &self.max_wire_version())
                .field("Min Wire Version", &self.min_wire_version())
                .field("Replica Set Name", &self.replica_set_name())
                .field("Replica Set Version", &self.replica_set_version())
                .field("Tags", &self.tags())
                .field(
                    "Compatibility Error",
                    &self.description.compatibility_error_message(),
                )
                .finish(),
            Err(ref e) => f
                .debug_struct("Server Description")
                .field("Address", self.address())
                .field("Type", &self.server_type())
                .field("Error", e)
                .finish(),
        }
    }
}

impl<'a> fmt::Display for ServerInfo<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> {
        write!(
            f,
            "{{ Address: {}, Type: {:?}",
            self.address(),
            self.server_type()
        )?;

        match self.description.reply {
            Ok(_) => {
                if let Some(avg_rtt) = self.average_round_trip_time() {
                    write!(f, ", Average RTT: {:?}", avg_rtt)?;
                }

                if let Some(last_update_time) = self.last_update_time() {
                    write!(f, ", Last Update Time: {}", last_update_time)?;
                }

                if let Some(max_wire_version) = self.max_wire_version() {
                    write!(f, ", Max Wire Version: {}", max_wire_version)?;
                }

                if let Some(min_wire_version) = self.min_wire_version() {
                    write!(f, ", Min Wire Version: {}", min_wire_version)?;
                }

                if let Some(rs_name) = self.replica_set_name() {
                    write!(f, ", Replica Set Name: {}", rs_name)?;
                }

                if let Some(rs_version) = self.replica_set_version() {
                    write!(f, ", Replica Set Version: {}", rs_version)?;
                }

                if let Some(tags) = self.tags() {
                    write!(f, ", Tags: {:?}", tags)?;
                }

                if let Some(compatibility_error) = self.description.compatibility_error_message() {
                    write!(f, ", Compatiblity Error: {}", compatibility_error)?;
                }
            }
            Err(ref e) => {
                write!(f, ", Error: {}", e)?;
            }
        }

        write!(f, " }}")
    }
}