dmsc 0.1.9

Ri - A high-performance Rust middleware framework with modular architecture
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
//!
//! This file is part of Ri.
//! The Ri project belongs to the Dunimd Team.
//!
//! Licensed under the Apache License, Version 2.0 (the "License");
//! You may not use this file except in compliance with the License.
//! You may obtain a copy of the License at
//!
//!     http://www.apache.org/licenses/LICENSE-2.0
//!
//! Unless required by applicable law or agreed to in writing, software
//! distributed under the License is distributed on an "AS IS" BASIS,
//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//! See the License for the specific language governing permissions and
//! limitations under the License.

#![allow(non_snake_case)]

//! # Grafana Integration Module
//! 
//! This module provides data structures for creating and managing Grafana dashboards and panels.
//! It enables programmatic generation of Grafana dashboards with panels, queries, and layout information.
//! 
//! ## Key Components
//! 
//! - **RiGridPos**: Represents the grid position of a panel on a dashboard
//! - **RiGrafanaPanel**: Represents a single Grafana panel with title, query, type, and position
//! - **RiGrafanaDashboard**: Represents a Grafana dashboard with multiple panels
//! 
//! ## Design Principles
//! 
//! 1. **Serde Integration**: All structs implement Serialize and Deserialize for easy JSON conversion
//! 2. **Simple API**: Easy-to-use methods for creating dashboards and adding panels
//! 3. **Layout Support**: Built-in support for Grafana's grid layout system
//! 4. **Extensible**: Can be extended to support additional panel types and dashboard features
//! 5. **Type Safety**: Strongly typed structs for all Grafana components
//! 6. **JSON Compatibility**: Generates JSON that is compatible with Grafana's API
//! 
//! ## Usage
//! 
//! ```rust
//! use ri::prelude::*;
//! 
//! fn example() -> RiResult<()> {
//!     // Create a new dashboard
//!     let mut dashboard = RiGrafanaDashboard::new("Ri Metrics");
//!     
//!     // Create a panel
//!     let panel = RiGrafanaPanel {
//!         title: "Request Rate".to_string(),
//!         query: "rate(http_requests_total[5m])".to_string(),
//!         panel_type: "graph".to_string(),
//!         grid_pos: RiGridPos {
//!             h: 8,
//!             w: 12,
//!             x: 0,
//!             y: 0,
//!         },
//!     };
//!     
//!     // Add panel to dashboard
//!     dashboard.add_panel(panel)?;
//!     
//!     // Convert to JSON for Grafana API
//!     let json = dashboard.to_json()?;
//!     println!("Dashboard JSON: {}", json);
//!     
//!     Ok(())
//! }
//! ```

use serde::{Serialize, Deserialize};
use crate::core::RiResult;

/// Grafana target configuration for data sources
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiGrafanaTarget {
    pub expr: String,
    pub ref_id: String,
    pub legend_format: Option<String>,
    pub interval: Option<String>,
}

/// Grafana grid position configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiGridPos {
    pub h: i32,
    pub w: i32,
    pub x: i32,
    pub y: i32,
}

/// Grafana panel configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiGrafanaPanel {
    pub id: i32,
    pub title: String,
    pub type_: String,
    pub targets: Vec<RiGrafanaTarget>,
    pub grid_pos: RiGridPos,
    pub field_config: serde_json::Value,
    pub options: serde_json::Value,
    pub description: Option<String>,
    pub datasource: Option<String>,
}

/// Grafana time range configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiGrafanaTimeRange {
    pub from: String,
    pub to: String,
}

/// Grafana dashboard tag
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiGrafanaTag {
    pub term: String,
    pub color: Option<String>,
}

/// Grafana dashboard configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiGrafanaDashboard {
    pub title: String,
    pub panels: Vec<RiGrafanaPanel>,
    pub tags: Vec<RiGrafanaTag>,
    pub time: RiGrafanaTimeRange,
    pub refresh: String,
    pub timezone: String,
    pub schema_version: i32,
    pub uid: Option<String>,
    pub version: i32,
}

/// Grafana dashboard generator
pub struct RiGrafanaDashboardGenerator {
    next_panel_id: i32,
}

#[allow(dead_code)]
impl RiGrafanaDashboard {
    pub fn new(title: &str) -> Self {
        RiGrafanaDashboard {
            title: title.to_string(),
            panels: Vec::new(),
            tags: vec![RiGrafanaTag { term: "dms".to_string(), color: Some("#1F77B4".to_string()) }],
            time: RiGrafanaTimeRange { from: "now-1h".to_string(), to: "now".to_string() },
            refresh: "5s".to_string(),
            timezone: "browser".to_string(),
            schema_version: 38,
            uid: None,
            version: 1,
        }
    }
    
    pub fn add_panel(&mut self, panel: RiGrafanaPanel) -> RiResult<()> {
        self.panels.push(panel);
        Ok(())
    }
    
    pub fn to_json(&self) -> RiResult<String> {
        serde_json::to_string(self).map_err(|e| crate::core::RiError::Serde(e.to_string()))
    }
}

impl RiGrafanaDashboardGenerator {
    pub fn new() -> Self {
        RiGrafanaDashboardGenerator {
            next_panel_id: 1,
        }
    }
    
    /// Convert a string to title case (first letter of each word uppercase)
    fn title_case(&self, s: &str) -> String {
        let mut result = String::new();
        let mut capitalize_next = true;
        
        for c in s.chars() {
            if c == '_' || c == ' ' {
                result.push(' ');
                capitalize_next = true;
            } else if capitalize_next {
                result.push(c.to_ascii_uppercase());
                capitalize_next = false;
            } else {
                result.push(c.to_ascii_lowercase());
            }
        }
        
        result
    }
    
    /// Create a new dashboard with default settings
    pub fn create_dashboard(&self, title: &str) -> RiGrafanaDashboard {
        RiGrafanaDashboard::new(title)
    }
    
    /// Create a Prometheus target
    pub fn create_prometheus_target(&self, expr: &str, ref_id: &str, legend_format: Option<&str>) -> RiGrafanaTarget {
        RiGrafanaTarget {
            expr: expr.to_string(),
            ref_id: ref_id.to_string(),
            legend_format: legend_format.map(|s| s.to_string()),
            interval: None,
        }
    }
    
    /// Create a new panel with default settings
    pub fn create_panel(&mut self, title: &str, panel_type: &str, x: i32, y: i32, w: i32, h: i32) -> RiGrafanaPanel {
        let panel_id = self.next_panel_id;
        self.next_panel_id += 1;
        
        RiGrafanaPanel {
            id: panel_id,
            title: title.to_string(),
            type_: panel_type.to_string(),
            targets: Vec::new(),
            grid_pos: RiGridPos { h, w, x, y },
            field_config: serde_json::json!({ "defaults": {}, "overrides": [] }),
            options: serde_json::json!({}),
            description: None,
            datasource: Some("Prometheus".to_string()),
        }
    }
    
    /// Create a graph panel for request rate
    pub fn create_request_rate_panel(&mut self, x: i32, y: i32, w: i32, h: i32) -> RiGrafanaPanel {
        let mut panel = self.create_panel("Request Rate", "timeseries", x, y, w, h);
        panel.targets.push(self.create_prometheus_target(
            "rate(dms_requests_total[5m])",
            "A",
            Some("{{instance}}")
        ));
        panel
    }
    
    /// Create a graph panel for request duration
    pub fn create_request_duration_panel(&mut self, x: i32, y: i32, w: i32, h: i32) -> RiGrafanaPanel {
        let mut panel = self.create_panel("Request Duration", "timeseries", x, y, w, h);
        panel.targets.push(self.create_prometheus_target(
            "histogram_quantile(0.95, sum(rate(dms_request_duration_seconds_bucket[5m])) by (le))",
            "A",
            Some("95th Percentile")
        ));
        panel.targets.push(self.create_prometheus_target(
            "histogram_quantile(0.5, sum(rate(dms_request_duration_seconds_bucket[5m])) by (le))",
            "B",
            Some("50th Percentile")
        ));
        panel
    }
    
    /// Create a stat panel for active connections
    pub fn create_active_connections_panel(&mut self, x: i32, y: i32, w: i32, h: i32) -> RiGrafanaPanel {
        let mut panel = self.create_panel("Active Connections", "stat", x, y, w, h);
        panel.targets.push(self.create_prometheus_target(
            "dms_active_connections",
            "A",
            None
        ));
        panel
    }
    
    /// Create a graph panel for error rate
    pub fn create_error_rate_panel(&mut self, x: i32, y: i32, w: i32, h: i32) -> RiGrafanaPanel {
        let mut panel = self.create_panel("Error Rate", "timeseries", x, y, w, h);
        panel.targets.push(self.create_prometheus_target(
            "rate(dms_errors_total[5m])",
            "A",
            Some("{{instance}}")
        ));
        panel
    }
    
    /// Create a graph panel for cache metrics
    pub fn create_cache_metrics_panel(&mut self, x: i32, y: i32, w: i32, h: i32) -> RiGrafanaPanel {
        let mut panel = self.create_panel("Cache Metrics", "timeseries", x, y, w, h);
        panel.targets.push(self.create_prometheus_target(
            "rate(dms_cache_hits_total[5m])",
            "A",
            Some("Hits")
        ));
        panel.targets.push(self.create_prometheus_target(
            "rate(dms_cache_misses_total[5m])",
            "B",
            Some("Misses")
        ));
        panel
    }
    
    /// Create a graph panel for database query time
    pub fn create_db_query_time_panel(&mut self, x: i32, y: i32, w: i32, h: i32) -> RiGrafanaPanel {
        let mut panel = self.create_panel("Database Query Time", "timeseries", x, y, w, h);
        panel.targets.push(self.create_prometheus_target(
            "histogram_quantile(0.95, sum(rate(dms_db_query_duration_seconds_bucket[5m])) by (le))",
            "A",
            Some("95th Percentile")
        ));
        panel
    }
    
    /// Generate a default Ri dashboard with common metrics panels
    pub fn generate_default_dashboard(&mut self) -> RiResult<RiGrafanaDashboard> {
        let mut dashboard = self.create_dashboard("Ri Default Dashboard");
        
        // First row: Request metrics
        dashboard.add_panel(self.create_request_rate_panel(0, 0, 12, 8))?;
        dashboard.add_panel(self.create_request_duration_panel(12, 0, 12, 8))?;
        
        // Second row: Error and connection metrics
        dashboard.add_panel(self.create_error_rate_panel(0, 8, 12, 8))?;
        dashboard.add_panel(self.create_active_connections_panel(12, 8, 6, 8))?;
        
        // Third row: Cache and database metrics
        dashboard.add_panel(self.create_cache_metrics_panel(0, 16, 12, 8))?;
        dashboard.add_panel(self.create_db_query_time_panel(12, 16, 12, 8))?;
        
        Ok(dashboard)
    }
    
    /// Generate a dashboard automatically based on available metrics
    /// 
    /// This method analyzes available metrics and generates an appropriate dashboard
    /// with panels matching the metric types and values.
    /// 
    /// # Parameters
    /// 
    /// - `metrics`: List of available metric names
    /// - `dashboard_title`: Title for the generated dashboard
    /// 
    /// # Returns
    /// 
    /// A Grafana dashboard automatically generated based on the provided metrics
    pub fn generate_auto_dashboard(&mut self, metrics: Vec<&str>, dashboard_title: &str) -> RiResult<RiGrafanaDashboard> {
        let mut dashboard = self.create_dashboard(dashboard_title);
        
        // Analyze metrics and group by type
        let mut counter_metrics = Vec::with_capacity(4);
        let mut gauge_metrics = Vec::with_capacity(4);
        let mut histogram_metrics = Vec::with_capacity(4);
        
        for metric in metrics {
            if metric.ends_with("_total") || metric.contains("count") {
                counter_metrics.push(metric);
            } else if metric.ends_with("_seconds") || metric.ends_with("_bytes") || metric.contains("time") {
                histogram_metrics.push(metric);
            } else {
                gauge_metrics.push(metric);
            }
        }
        
        // Generate panels based on metric types
        let mut current_row = 0;
        
        // Add counter panels
        for (i, metric) in counter_metrics.iter().enumerate() {
            let panel = self.create_counter_panel(*metric, i as i32 * 12, current_row, 12, 8);
            dashboard.add_panel(panel)?;
            if (i + 1) % 2 == 0 {
                current_row += 8;
            }
        }
        
        if counter_metrics.len() % 2 != 0 {
            current_row += 8;
        }
        
        // Add gauge panels
        for (i, metric) in gauge_metrics.iter().enumerate() {
            let panel = self.create_gauge_panel(*metric, i as i32 * 12, current_row, 12, 8);
            dashboard.add_panel(panel)?;
            if (i + 1) % 2 == 0 {
                current_row += 8;
            }
        }
        
        if gauge_metrics.len() % 2 != 0 {
            current_row += 8;
        }
        
        // Add histogram panels
        for (i, metric) in histogram_metrics.iter().enumerate() {
            let panel = self.create_histogram_panel(*metric, i as i32 * 12, current_row, 12, 8);
            dashboard.add_panel(panel)?;
            if (i + 1) % 2 == 0 {
                current_row += 8;
            }
        }
        
        Ok(dashboard)
    }
    
    /// Create a counter panel for a metric
    pub fn create_counter_panel(&mut self, metric_name: &str, x: i32, y: i32, w: i32, h: i32) -> RiGrafanaPanel {
        let title = self.title_case(metric_name);
        let query = format!("rate({}[5m])", metric_name);
        
        let mut panel = self.create_panel(&title, "timeseries", x, y, w, h);
        panel.targets.push(self.create_prometheus_target(&query, "A", Some("{{instance}}")));
        panel
    }
    
    /// Create a gauge panel for a metric
    pub fn create_gauge_panel(&mut self, metric_name: &str, x: i32, y: i32, w: i32, h: i32) -> RiGrafanaPanel {
        let title = self.title_case(metric_name);
        
        let mut panel = self.create_panel(&title, "stat", x, y, w, h);
        panel.targets.push(self.create_prometheus_target(metric_name, "A", None));
        panel
    }
    
    /// Create a histogram panel for a metric
    pub fn create_histogram_panel(&mut self, metric_name: &str, x: i32, y: i32, w: i32, h: i32) -> RiGrafanaPanel {
        let title = self.title_case(metric_name);
        let query_95 = format!("histogram_quantile(0.95, sum(rate({}_bucket[5m])) by (le))", metric_name);
        let query_50 = format!("histogram_quantile(0.5, sum(rate({}_bucket[5m])) by (le))", metric_name);
        
        let mut panel = self.create_panel(&title, "timeseries", x, y, w, h);
        panel.targets.push(self.create_prometheus_target(&query_95, "A", Some("95th Percentile")));
        panel.targets.push(self.create_prometheus_target(&query_50, "B", Some("50th Percentile")));
        panel
    }
}