redis_enterprise/
stats.rs1use crate::client::RestClient;
62use crate::error::Result;
63use serde::{Deserialize, Serialize};
64use serde_json::Value;
65
66#[derive(Debug, Serialize)]
68pub struct StatsQuery {
69 #[serde(skip_serializing_if = "Option::is_none")]
71 pub interval: Option<String>,
72 #[serde(skip_serializing_if = "Option::is_none")]
74 pub stime: Option<String>,
75 #[serde(skip_serializing_if = "Option::is_none")]
77 pub etime: Option<String>,
78 #[serde(skip_serializing_if = "Option::is_none")]
80 pub metrics: Option<String>,
81}
82
83#[derive(Debug, Clone, Serialize, Deserialize)]
85pub struct StatsResponse {
86 pub intervals: Vec<StatsInterval>,
88
89 #[serde(flatten)]
90 pub extra: Value,
91}
92
93#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct StatsInterval {
96 pub time: String,
98 pub metrics: Value,
100}
101
102#[derive(Debug, Clone, Serialize, Deserialize)]
105pub struct LastStatsResponse {
106 pub stime: Option<String>,
108 pub etime: Option<String>,
110 pub interval: Option<String>,
112 #[serde(flatten)]
114 pub metrics: Value,
115}
116
117#[derive(Debug, Clone, Serialize, Deserialize)]
119pub struct AggregatedStatsResponse {
120 pub stats: Vec<ResourceStats>,
122 #[serde(flatten)]
123 pub extra: Value,
124}
125
126#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct ResourceStats {
129 pub uid: u32,
131 pub intervals: Vec<StatsInterval>,
133 #[serde(flatten)]
134 pub extra: Value,
135}
136
137pub struct StatsHandler {
139 client: RestClient,
140}
141
142impl StatsHandler {
143 pub fn new(client: RestClient) -> Self {
144 StatsHandler { client }
145 }
146
147 pub async fn cluster(&self, query: Option<StatsQuery>) -> Result<StatsResponse> {
149 if let Some(q) = query {
150 let query_str = serde_urlencoded::to_string(&q).unwrap_or_default();
151 self.client
152 .get(&format!("/v1/cluster/stats?{}", query_str))
153 .await
154 } else {
155 self.client.get("/v1/cluster/stats").await
156 }
157 }
158
159 pub async fn cluster_last(&self) -> Result<LastStatsResponse> {
161 self.client.get("/v1/cluster/stats/last").await
162 }
163
164 pub async fn node(&self, uid: u32, query: Option<StatsQuery>) -> Result<StatsResponse> {
168 if let Some(q) = query {
169 let query_str = serde_urlencoded::to_string(&q).unwrap_or_default();
170 self.client
171 .get(&format!("/v1/nodes/{}/stats?{}", uid, query_str))
172 .await
173 } else {
174 self.client.get(&format!("/v1/nodes/{}/stats", uid)).await
175 }
176 }
177
178 pub async fn node_last(&self, uid: u32) -> Result<LastStatsResponse> {
180 self.client
181 .get(&format!("/v1/nodes/{}/stats/last", uid))
182 .await
183 }
184
185 pub async fn nodes(&self, query: Option<StatsQuery>) -> Result<AggregatedStatsResponse> {
189 if let Some(q) = query {
190 let query_str = serde_urlencoded::to_string(&q).unwrap_or_default();
191 self.client
192 .get(&format!("/v1/nodes/stats?{}", query_str))
193 .await
194 } else {
195 self.client.get("/v1/nodes/stats").await
196 }
197 }
198
199 pub async fn nodes_last(&self) -> Result<AggregatedStatsResponse> {
203 self.client.get("/v1/nodes/stats/last").await
204 }
205
206 pub async fn node_alt(&self, uid: u32) -> Result<StatsResponse> {
210 self.client.get(&format!("/v1/nodes/stats/{}", uid)).await
211 }
212
213 pub async fn node_last_alt(&self, uid: u32) -> Result<LastStatsResponse> {
215 self.client
216 .get(&format!("/v1/nodes/stats/last/{}", uid))
217 .await
218 }
219
220 pub async fn database(&self, uid: u32, query: Option<StatsQuery>) -> Result<StatsResponse> {
222 if let Some(q) = query {
223 let query_str = serde_urlencoded::to_string(&q).unwrap_or_default();
224 self.client
225 .get(&format!("/v1/bdbs/{}/stats?{}", uid, query_str))
226 .await
227 } else {
228 self.client.get(&format!("/v1/bdbs/{}/stats", uid)).await
229 }
230 }
231
232 pub async fn database_last(&self, uid: u32) -> Result<LastStatsResponse> {
234 self.client
235 .get(&format!("/v1/bdbs/{}/stats/last", uid))
236 .await
237 }
238
239 pub async fn databases(&self, query: Option<StatsQuery>) -> Result<AggregatedStatsResponse> {
243 if let Some(q) = query {
244 let query_str = serde_urlencoded::to_string(&q).unwrap_or_default();
245 self.client
246 .get(&format!("/v1/bdbs/stats?{}", query_str))
247 .await
248 } else {
249 self.client.get("/v1/bdbs/stats").await
250 }
251 }
252
253 pub async fn databases_last(&self) -> Result<AggregatedStatsResponse> {
257 self.client.get("/v1/bdbs/stats/last").await
258 }
259
260 pub async fn database_alt(&self, uid: u32) -> Result<StatsResponse> {
264 self.client.get(&format!("/v1/bdbs/stats/{}", uid)).await
265 }
266
267 pub async fn database_last_alt(&self, uid: u32) -> Result<LastStatsResponse> {
269 self.client
270 .get(&format!("/v1/bdbs/stats/last/{}", uid))
271 .await
272 }
273
274 pub async fn shard(&self, uid: u32, query: Option<StatsQuery>) -> Result<StatsResponse> {
276 if let Some(q) = query {
277 let query_str = serde_urlencoded::to_string(&q).unwrap_or_default();
278 self.client
279 .get(&format!("/v1/shards/{}/stats?{}", uid, query_str))
280 .await
281 } else {
282 self.client.get(&format!("/v1/shards/{}/stats", uid)).await
283 }
284 }
285
286 pub async fn shards(&self, query: Option<StatsQuery>) -> Result<AggregatedStatsResponse> {
288 if let Some(q) = query {
289 let query_str = serde_urlencoded::to_string(&q).unwrap_or_default();
290 self.client
291 .get(&format!("/v1/shards/stats?{}", query_str))
292 .await
293 } else {
294 self.client.get("/v1/shards/stats").await
295 }
296 }
297
298 }