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
//! Provides debug metric of server state that can be used by anything that wants
//! to see what is happening in the server. State is updated by the server as sessions are added, removed,
//! and when subscriptions / monitored items are added, removed.

use opcua_types::DateTime;

use crate::{
    comms::transport::Transport,
    config,
    server,
    subscriptions::subscription::Subscription,
    diagnostics::ServerDiagnostics,
    state::ServerState,
};

#[derive(Serialize)]
pub struct ServerMetrics {
    pub server: Server,
    pub diagnostics: ServerDiagnostics,
    pub config: Option<config::ServerConfig>,
    pub connections: Vec<Connection>,
    pub runtime_components: Vec<String>,
}

#[derive(Serialize)]
pub struct Server {
    pub start_time: String,
    pub uptime_ms: i64,
}

#[derive(Serialize)]
pub struct Connection {
    pub id: String,
    // creation time
    // state
    pub client_address: String,
    pub transport_state: String,
    pub session_activated: bool,
    pub session_terminated: bool,
    pub session_terminated_at: String,
    pub subscriptions: Vec<Subscription>,
}

impl ServerMetrics {
    pub fn new() -> ServerMetrics {
        // Sample metrics
        ServerMetrics {
            server: Server {
                start_time: String::new(),
                uptime_ms: 0,
            },
            diagnostics: ServerDiagnostics::default(),
            config: None,
            connections: Vec::new(),
            runtime_components: Vec::new(),
        }
    }

    pub fn set_server_info(&mut self, server: &server::Server) {
        let server_state = trace_read_lock_unwrap!(server.server_state);
        let server_config = trace_read_lock_unwrap!(server_state.config);

        // For security, blank out user tokens
        let mut config = server_config.clone();
        config.user_tokens.clear();
        config.user_tokens.insert(String::new(), config::ServerUserToken {
            user: String::from("User identity tokens have been removed"),
            pass: None,
        });
        self.config = Some(config.clone());
    }

    // Update the server state metrics (uptime etc.)
    pub fn update_from_server_state(&mut self, server_state: &ServerState) {
        let start_time = &server_state.start_time;
        let now = DateTime::now();

        self.server.start_time = start_time.as_chrono().to_rfc3339();

        // Take a snapshot of the diagnostics
        {
            let diagnostics = trace_read_lock_unwrap!(server_state.diagnostics);
            self.diagnostics = diagnostics.clone();
        }

        let elapsed = now.as_chrono().signed_duration_since(start_time.as_chrono());
        self.server.uptime_ms = elapsed.num_milliseconds();
    }

    // Update the connection metrics which includes susbcriptions and monitored items
    pub fn update_from_connections(&mut self, connections: &server::Connections) {
        self.runtime_components = runtime_components!();

        self.connections = connections.iter().map(|c| {
            let connection = trace_read_lock_unwrap!(c);

            let session = connection.session();
            let session = trace_read_lock_unwrap!(session);

            // Subscriptions
            let subscriptions = session.subscriptions.subscriptions().iter().map(|subscription_pair| {
                let mut subscription = subscription_pair.1.clone();
                subscription.diagnostics_on_drop = false;
                subscription
            }).collect();

            // session.subscriptions.iterate ...
            let session_id = session.session_id.to_string();
            Connection {
                id: session_id,
                // creation time
                // state
                client_address: if connection.client_address().is_some() {
                    format!("{:?}", connection.client_address().as_ref().unwrap())
                } else {
                    String::new()
                },
                transport_state: format!("{:?}", connection.state()),
                session_activated: session.activated,
                session_terminated: session.terminated(),
                session_terminated_at: if session.terminated() {
                    session.terminated_at().to_rfc3339()
                } else {
                    String::new()
                },
                subscriptions,
            }
        }).collect();
    }
}