redis_cloud/handlers/
metrics.rs

1//! Metrics operations handler
2
3use crate::{Result, client::CloudClient, models::CloudMetrics};
4use serde_json::Value;
5
6/// Handler for Cloud metrics operations
7pub struct CloudMetricsHandler {
8    client: CloudClient,
9}
10
11impl CloudMetricsHandler {
12    pub fn new(client: CloudClient) -> Self {
13        CloudMetricsHandler { client }
14    }
15
16    /// Get database metrics
17    pub async fn database(
18        &self,
19        subscription_id: u32,
20        database_id: u32,
21        metric_names: Vec<String>,
22        from: Option<String>,
23        to: Option<String>,
24    ) -> Result<CloudMetrics> {
25        let mut query_params = vec![];
26
27        for name in metric_names {
28            query_params.push(format!("metricSpecs={}", name));
29        }
30
31        if let Some(from_time) = from {
32            query_params.push(format!("from={}", from_time));
33        }
34
35        if let Some(to_time) = to {
36            query_params.push(format!("to={}", to_time));
37        }
38
39        let query_string = if query_params.is_empty() {
40            String::new()
41        } else {
42            format!("?{}", query_params.join("&"))
43        };
44
45        self.client
46            .get(&format!(
47                "/subscriptions/{}/databases/{}/metrics{}",
48                subscription_id, database_id, query_string
49            ))
50            .await
51    }
52
53    /// Get subscription metrics
54    pub async fn subscription(
55        &self,
56        subscription_id: u32,
57        from: Option<String>,
58        to: Option<String>,
59    ) -> Result<Value> {
60        let mut query_params = vec![];
61
62        if let Some(from_time) = from {
63            query_params.push(format!("from={}", from_time));
64        }
65
66        if let Some(to_time) = to {
67            query_params.push(format!("to={}", to_time));
68        }
69
70        let query_string = if query_params.is_empty() {
71            String::new()
72        } else {
73            format!("?{}", query_params.join("&"))
74        };
75
76        self.client
77            .get(&format!(
78                "/subscriptions/{}/metrics{}",
79                subscription_id, query_string
80            ))
81            .await
82    }
83}