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
use crate::api::_generic::handle_response;
use crate::api::overview::{RabbitMqContext, RabbitMqExchangeType};
use crate::errors::RabbitMqClientError;
use crate::RabbitMqClient;
use async_trait::async_trait;
use serde::Deserialize;

#[async_trait]
pub trait NodeApi {
    async fn list_nodes(&self) -> Result<Vec<RabbitMqNode>, RabbitMqClientError>;

    async fn get_node(&self, node: String) -> Result<RabbitMqNode, RabbitMqClientError>;

    async fn get_node_memory(
        &self,
        node: String,
    ) -> Result<RabbitMqNodeMemory, RabbitMqClientError>;
}

#[async_trait]
impl NodeApi for RabbitMqClient {
    async fn list_nodes(&self) -> Result<Vec<RabbitMqNode>, RabbitMqClientError> {
        let response = self
            .client
            .request(reqwest::Method::GET, format!("{}/api/nodes", self.api_url))
            .send()
            .await?;

        handle_response(response).await
    }

    async fn get_node(&self, node: String) -> Result<RabbitMqNode, RabbitMqClientError> {
        let response = self
            .client
            .request(
                reqwest::Method::GET,
                format!("{}/api/nodes/{}", self.api_url, node),
            )
            .send()
            .await?;

        handle_response(response).await
    }

    async fn get_node_memory(
        &self,
        node: String,
    ) -> Result<RabbitMqNodeMemory, RabbitMqClientError> {
        let response = self
            .client
            .request(
                reqwest::Method::GET,
                format!("{}/api/nodes/{}/memory", self.api_url, node),
            )
            .send()
            .await?;

        let wrapper: RabbitMqNodeMemoryWrapper = handle_response(response).await?;

        Ok(wrapper.memory)
    }
}

#[derive(Debug, Deserialize)]
pub struct RabbitMqNode {
    pub os_pid: String,
    pub fd_total: i64,
    pub sockets_total: i64,
    pub mem_limit: i64,
    pub mem_alarm: bool,
    pub disk_free_limit: i64,
    pub disk_free_alarm: bool,
    pub proc_total: i64,
    pub rates_mode: String,
    pub uptime: i64,
    pub run_queue: i64,
    pub processors: i64,
    pub exchange_types: Vec<RabbitMqExchangeType>,
    pub auth_mechanisms: Vec<RabbitMqNodeAuthMechanism>,
    pub applications: Vec<RabbitMqNodeApplication>,
    pub contexts: Vec<RabbitMqContext>,
    pub log_files: Vec<String>,
    pub db_dir: String,
    pub config_files: Vec<String>,
    pub net_ticktime: i64,
    pub enabled_plugins: Vec<String>,
    pub mem_calculation_strategy: String,
    pub name: String,
    pub running: bool,
    #[serde(rename = "type")]
    pub kind: String,
    pub mem_used: i64,
}

#[derive(Debug, Deserialize)]
pub struct RabbitMqNodeAuthMechanism {
    pub name: String,
    pub description: String,
    pub enabled: bool,
}

#[derive(Debug, Deserialize)]
pub struct RabbitMqNodeApplication {
    pub name: String,
    pub description: String,
    pub version: String,
}

#[derive(Debug, Deserialize)]
pub struct RabbitMqNodeMemoryWrapper {
    memory: RabbitMqNodeMemory,
}

#[derive(Debug, Deserialize)]
pub struct RabbitMqNodeMemory {
    pub connection_readers: i64,
    pub connection_writers: i64,
    pub connection_channels: i64,
    pub connection_other: i64,
    pub queue_procs: i64,
    pub queue_slave_procs: i64,
    pub quorum_queue_procs: i64,
    pub quorum_queue_dlx_procs: i64,
    pub stream_queue_procs: i64,
    pub stream_queue_replica_reader_procs: i64,
    pub stream_queue_coordinator_procs: i64,
    pub plugins: i64,
    pub other_proc: i64,
    pub metrics: i64,
    pub mgmt_db: i64,
    pub mnesia: i64,
    pub quorum_ets: i64,
    pub other_ets: i64,
    pub binary: i64,
    pub msg_index: i64,
    pub code: i64,
    pub atom: i64,
    pub other_system: i64,
    pub allocated_unused: i64,
    pub reserved_unallocated: i64,
    pub strategy: String,
    pub total: RabbitMqNodeMemoryTotal,
}

#[derive(Debug, Deserialize)]
pub struct RabbitMqNodeMemoryTotal {
    pub erlang: i64,
    pub rss: i64,
    pub allocated: i64,
}