mockforge_analytics/lib.rs
1//! MockForge Analytics
2//!
3//! Provides comprehensive traffic analytics and metrics dashboard capabilities
4//! for MockForge, including:
5//!
6//! - Time-series metrics aggregation (minute/hour/day granularity)
7//! - Endpoint performance tracking
8//! - Error analysis and monitoring
9//! - Client analytics
10//! - Traffic pattern detection
11//! - Data export (CSV, JSON)
12//! - Configurable retention policies
13//!
14//! # Architecture
15//!
16//! The analytics system consists of several components:
17//!
18//! - **Database**: SQLite-based storage for aggregated metrics
19//! - **Aggregator**: Background service that queries Prometheus and stores metrics
20//! - **Queries**: High-level query API for dashboard data
21//! - **Export**: Data export functionality
22//! - **Retention**: Automatic cleanup of old data
23//!
24//! # Example
25//!
26//! ```no_run
27//! use mockforge_analytics::{AnalyticsDatabase, AnalyticsConfig, RetentionConfig};
28//! use std::path::PathBuf;
29//!
30//! # async fn example() -> anyhow::Result<()> {
31//! let config = AnalyticsConfig {
32//! enabled: true,
33//! database_path: PathBuf::from("analytics.db"),
34//! aggregation_interval_seconds: 60,
35//! rollup_interval_hours: 1,
36//! retention: RetentionConfig::default(),
37//! batch_size: 1000,
38//! max_query_results: 10000,
39//! };
40//!
41//! let db = AnalyticsDatabase::new(&config.database_path).await?;
42//! db.run_migrations().await?;
43//!
44//! // Query recent metrics
45//! let metrics = db.get_recent_metrics(3600).await?;
46//! println!("Total requests in last hour: {}", metrics.total_requests);
47//! # Ok(())
48//! # }
49//! ```
50
51pub mod aggregator;
52pub mod config;
53pub mod database;
54pub mod error;
55pub mod export;
56pub mod models;
57pub mod queries;
58pub mod retention;
59
60pub use config::{AnalyticsConfig, RetentionConfig};
61pub use database::AnalyticsDatabase;
62pub use error::{AnalyticsError, Result};
63pub use models::*;
64
65/// Initialize the analytics system with the given configuration
66pub async fn init(config: AnalyticsConfig) -> Result<AnalyticsDatabase> {
67 let db = AnalyticsDatabase::new(&config.database_path).await?;
68 db.run_migrations().await?;
69 Ok(db)
70}