oxigdal-observability 0.1.6

OpenTelemetry-based observability, monitoring, and alerting for OxiGDAL
Documentation
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! Automated reporting and notification systems.

use crate::error::Result;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Report generator for automated monitoring reports.
pub struct ReportGenerator {
    config: ReportConfig,
}

/// Report configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReportConfig {
    /// Name of the report.
    pub report_name: String,
    /// List of recipient email addresses.
    pub recipients: Vec<String>,
    /// Report generation schedule.
    pub schedule: ReportSchedule,
    /// Sections to include in the report.
    pub include_sections: Vec<ReportSection>,
}

/// Report schedule.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ReportSchedule {
    /// Generate report every hour.
    Hourly,
    /// Generate report daily.
    Daily,
    /// Generate report weekly.
    Weekly,
    /// Generate report monthly.
    Monthly,
}

/// Report section type.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ReportSection {
    /// High-level executive summary section.
    ExecutiveSummary,
    /// Performance metrics section with key indicators.
    PerformanceMetrics,
    /// SLO compliance status section.
    SloCompliance,
    /// Anomaly detection results section.
    AnomalyDetection,
    /// Resource utilization statistics section.
    ResourceUtilization,
    /// Error analysis and breakdown section.
    ErrorAnalysis,
    /// Trend analysis section.
    Trends,
    /// Recommendations section.
    Recommendations,
}

/// Generated report.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Report {
    /// Unique report identifier.
    pub id: String,
    /// Report name/title.
    pub name: String,
    /// Timestamp when the report was generated.
    pub generated_at: DateTime<Utc>,
    /// Start of the reporting period.
    pub period_start: DateTime<Utc>,
    /// End of the reporting period.
    pub period_end: DateTime<Utc>,
    /// Report sections with their data.
    pub sections: HashMap<String, ReportData>,
    /// Executive summary text.
    pub summary: String,
}

/// Report data type.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ReportData {
    /// Plain text content.
    Text(String),
    /// Collection of metric summaries.
    Metrics(Vec<MetricSummary>),
    /// Collection of chart visualizations.
    Charts(Vec<ChartData>),
    /// Collection of tabular data.
    Tables(Vec<TableData>),
}

/// Metric summary.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricSummary {
    /// Metric name.
    pub name: String,
    /// Current metric value.
    pub value: f64,
    /// Unit of measurement.
    pub unit: String,
    /// Percentage change from previous period.
    pub change_percent: f64,
    /// Trend direction.
    pub trend: Trend,
}

/// Trend direction.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum Trend {
    /// Upward trend.
    Up,
    /// Downward trend.
    Down,
    /// Stable/no change.
    Stable,
}

/// Chart data.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChartData {
    /// Chart title.
    pub title: String,
    /// Type of chart to render.
    pub chart_type: ChartType,
    /// Data points as (label, value) pairs.
    pub data_points: Vec<(String, f64)>,
}

/// Chart type.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ChartType {
    /// Line chart for time series.
    Line,
    /// Bar chart for comparisons.
    Bar,
    /// Pie chart for proportions.
    Pie,
    /// Area chart for cumulative data.
    Area,
}

/// Table data.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TableData {
    /// Table title.
    pub title: String,
    /// Column headers.
    pub headers: Vec<String>,
    /// Table rows (each row is a vector of cell values).
    pub rows: Vec<Vec<String>>,
}

impl ReportGenerator {
    /// Create a new report generator.
    pub fn new(config: ReportConfig) -> Self {
        Self { config }
    }

    /// Generate a report.
    pub fn generate(
        &self,
        period_start: DateTime<Utc>,
        period_end: DateTime<Utc>,
    ) -> Result<Report> {
        let mut sections = HashMap::new();

        for section in &self.config.include_sections {
            let data = match section {
                ReportSection::ExecutiveSummary => self.generate_executive_summary()?,
                ReportSection::PerformanceMetrics => self.generate_performance_metrics()?,
                ReportSection::SloCompliance => self.generate_slo_compliance()?,
                ReportSection::AnomalyDetection => self.generate_anomaly_section()?,
                ReportSection::ResourceUtilization => self.generate_resource_utilization()?,
                ReportSection::ErrorAnalysis => self.generate_error_analysis()?,
                ReportSection::Trends => self.generate_trends()?,
                ReportSection::Recommendations => self.generate_recommendations()?,
            };

            sections.insert(format!("{:?}", section), data);
        }

        Ok(Report {
            id: uuid::Uuid::new_v4().to_string(),
            name: self.config.report_name.clone(),
            generated_at: Utc::now(),
            period_start,
            period_end,
            sections,
            summary: self.generate_summary()?,
        })
    }

    fn generate_executive_summary(&self) -> Result<ReportData> {
        let summary =
            "System performance has been within expected parameters during the reporting period.";
        Ok(ReportData::Text(summary.to_string()))
    }

    fn generate_performance_metrics(&self) -> Result<ReportData> {
        let metrics = vec![
            MetricSummary {
                name: "Request Rate".to_string(),
                value: 1000.0,
                unit: "req/s".to_string(),
                change_percent: 5.2,
                trend: Trend::Up,
            },
            MetricSummary {
                name: "Average Latency".to_string(),
                value: 45.3,
                unit: "ms".to_string(),
                change_percent: -2.1,
                trend: Trend::Down,
            },
        ];

        Ok(ReportData::Metrics(metrics))
    }

    fn generate_slo_compliance(&self) -> Result<ReportData> {
        let rows = vec![
            vec![
                "Availability".to_string(),
                "99.95%".to_string(),
                "Pass".to_string(),
            ],
            vec![
                "Latency P95".to_string(),
                "98ms".to_string(),
                "Pass".to_string(),
            ],
        ];

        Ok(ReportData::Tables(vec![TableData {
            title: "SLO Compliance".to_string(),
            headers: vec![
                "Metric".to_string(),
                "Value".to_string(),
                "Status".to_string(),
            ],
            rows,
        }]))
    }

    fn generate_anomaly_section(&self) -> Result<ReportData> {
        Ok(ReportData::Text(
            "No significant anomalies detected.".to_string(),
        ))
    }

    fn generate_resource_utilization(&self) -> Result<ReportData> {
        let data_points = vec![
            ("CPU".to_string(), 45.0),
            ("Memory".to_string(), 62.0),
            ("Disk".to_string(), 38.0),
        ];

        Ok(ReportData::Charts(vec![ChartData {
            title: "Resource Utilization".to_string(),
            chart_type: ChartType::Bar,
            data_points,
        }]))
    }

    fn generate_error_analysis(&self) -> Result<ReportData> {
        Ok(ReportData::Text("Error rate: 0.03%".to_string()))
    }

    fn generate_trends(&self) -> Result<ReportData> {
        let data_points = vec![
            ("Mon".to_string(), 100.0),
            ("Tue".to_string(), 105.0),
            ("Wed".to_string(), 110.0),
            ("Thu".to_string(), 108.0),
            ("Fri".to_string(), 115.0),
        ];

        Ok(ReportData::Charts(vec![ChartData {
            title: "Request Volume Trend".to_string(),
            chart_type: ChartType::Line,
            data_points,
        }]))
    }

    fn generate_recommendations(&self) -> Result<ReportData> {
        Ok(ReportData::Text(
            "Consider scaling up compute resources to handle peak load.".to_string(),
        ))
    }

    fn generate_summary(&self) -> Result<String> {
        Ok("System operating normally with all SLOs met.".to_string())
    }

    /// Export report as HTML.
    pub fn export_html(&self, report: &Report) -> Result<String> {
        let mut html = format!(
            r#"<!DOCTYPE html>
<html>
<head>
    <title>{}</title>
    <style>
        body {{ font-family: Arial, sans-serif; margin: 20px; }}
        h1 {{ color: #333; }}
        table {{ border-collapse: collapse; width: 100%; margin: 20px 0; }}
        th, td {{ border: 1px solid #ddd; padding: 8px; text-align: left; }}
        th {{ background-color: #f2f2f2; }}
    </style>
</head>
<body>
    <h1>{}</h1>
    <p>Generated: {}</p>
    <p>Period: {} to {}</p>
    <h2>Summary</h2>
    <p>{}</p>
"#,
            report.name,
            report.name,
            report.generated_at.format("%Y-%m-%d %H:%M:%S"),
            report.period_start.format("%Y-%m-%d %H:%M:%S"),
            report.period_end.format("%Y-%m-%d %H:%M:%S"),
            report.summary
        );

        for (section_name, data) in &report.sections {
            html.push_str(&format!("<h2>{}</h2>", section_name));

            match data {
                ReportData::Text(text) => {
                    html.push_str(&format!("<p>{}</p>", text));
                }
                ReportData::Metrics(metrics) => {
                    html.push_str("<table><tr><th>Metric</th><th>Value</th><th>Change</th></tr>");
                    for metric in metrics {
                        html.push_str(&format!(
                            "<tr><td>{}</td><td>{} {}</td><td>{:+.1}%</td></tr>",
                            metric.name, metric.value, metric.unit, metric.change_percent
                        ));
                    }
                    html.push_str("</table>");
                }
                ReportData::Tables(tables) => {
                    for table in tables {
                        html.push_str("<table>");
                        html.push_str("<tr>");
                        for header in &table.headers {
                            html.push_str(&format!("<th>{}</th>", header));
                        }
                        html.push_str("</tr>");
                        for row in &table.rows {
                            html.push_str("<tr>");
                            for cell in row {
                                html.push_str(&format!("<td>{}</td>", cell));
                            }
                            html.push_str("</tr>");
                        }
                        html.push_str("</table>");
                    }
                }
                ReportData::Charts(_) => {
                    html.push_str("<p>[Chart visualization would be rendered here]</p>");
                }
            }
        }

        html.push_str("</body></html>");
        Ok(html)
    }

    /// Export report as JSON.
    pub fn export_json(&self, report: &Report) -> Result<String> {
        serde_json::to_string_pretty(report)
            .map_err(crate::error::ObservabilityError::Serialization)
    }
}

/// Notification system for alerts and reports.
pub struct NotificationSystem {
    channels: Vec<NotificationChannel>,
}

/// Notification channel.
pub enum NotificationChannel {
    /// Email notification channel.
    Email {
        /// List of email addresses to send notifications to.
        addresses: Vec<String>,
    },
    /// Slack notification channel.
    Slack {
        /// Slack incoming webhook URL.
        webhook_url: String,
    },
    /// PagerDuty notification channel.
    PagerDuty {
        /// PagerDuty integration key.
        integration_key: String,
    },
    /// Generic webhook notification channel.
    Webhook {
        /// URL to POST notification data to.
        url: String,
    },
}

impl NotificationSystem {
    /// Create a new notification system.
    pub fn new() -> Self {
        Self {
            channels: Vec::new(),
        }
    }

    /// Add a notification channel.
    pub fn add_channel(&mut self, channel: NotificationChannel) {
        self.channels.push(channel);
    }

    /// Send a notification.
    pub async fn send(&self, message: &str) -> Result<()> {
        for channel in &self.channels {
            match channel {
                NotificationChannel::Webhook { url } => {
                    let client = reqwest::Client::new();
                    let _ = client
                        .post(url)
                        .json(&serde_json::json!({ "message": message }))
                        .send()
                        .await?;
                }
                NotificationChannel::Slack { webhook_url } => {
                    let client = reqwest::Client::new();
                    let _ = client
                        .post(webhook_url)
                        .json(&serde_json::json!({ "text": message }))
                        .send()
                        .await?;
                }
                _ => {
                    // Placeholder for other channels
                }
            }
        }

        Ok(())
    }
}

impl Default for NotificationSystem {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_report_generation() {
        let config = ReportConfig {
            report_name: "Test Report".to_string(),
            recipients: vec!["test@example.com".to_string()],
            schedule: ReportSchedule::Daily,
            include_sections: vec![
                ReportSection::ExecutiveSummary,
                ReportSection::PerformanceMetrics,
            ],
        };

        let generator = ReportGenerator::new(config);
        let report = generator.generate(Utc::now(), Utc::now());
        assert!(report.is_ok());
    }

    #[test]
    fn test_html_export() {
        let config = ReportConfig {
            report_name: "Test Report".to_string(),
            recipients: vec![],
            schedule: ReportSchedule::Daily,
            include_sections: vec![ReportSection::ExecutiveSummary],
        };

        let generator = ReportGenerator::new(config);
        let report = generator
            .generate(Utc::now(), Utc::now())
            .expect("Failed to generate report");
        let html = generator.export_html(&report);
        assert!(html.is_ok());
        assert!(html.expect("No HTML").contains("<!DOCTYPE html>"));
    }
}