basic_usage/
basic_usage.rs

1//! Basic usage example of lab-resource-manager
2//!
3//! This example demonstrates how to use the library to monitor
4//! Google Calendar events and send Slack notifications.
5//!
6//! Run with:
7//! ```bash
8//! cargo run --example basic_usage
9//! ```
10
11use lab_resource_manager::{
12    GoogleCalendarUsageRepository, JsonFileIdentityLinkRepository, NotificationRouter,
13    NotifyFutureResourceUsageChangesUseCase, load_config,
14};
15use std::path::PathBuf;
16use std::sync::Arc;
17use std::time::Duration;
18
19#[tokio::main]
20async fn main() -> Result<(), Box<dyn std::error::Error>> {
21    println!("🚀 Starting resource usage watcher (basic example)");
22
23    // Load resource configuration
24    let config_path =
25        std::env::var("RESOURCE_CONFIG").unwrap_or_else(|_| "config/resources.toml".to_string());
26
27    let config = load_config(&config_path)?;
28    println!("✅ Configuration loaded");
29
30    // Create repository
31    let service_account_key = std::env::var("GOOGLE_SERVICE_ACCOUNT_KEY")
32        .expect("GOOGLE_SERVICE_ACCOUNT_KEY must be set");
33
34    let id_mappings_path = std::env::var("GOOGLE_CALENDAR_MAPPINGS_FILE")
35        .unwrap_or_else(|_| "data/google_calendar_mappings.json".to_string());
36
37    let repository = Arc::new(
38        GoogleCalendarUsageRepository::new(
39            &service_account_key,
40            config.clone(),
41            PathBuf::from(&id_mappings_path),
42        )
43        .await?,
44    );
45    println!("✅ Google Calendar repository initialized");
46
47    // Create identity link repository
48    let identity_links_path = std::env::var("IDENTITY_LINKS_FILE")
49        .unwrap_or_else(|_| "data/identity_links.json".to_string());
50    let identity_repo = Arc::new(JsonFileIdentityLinkRepository::new(PathBuf::from(
51        identity_links_path,
52    )));
53
54    // Create notification router (uses configured notification destinations and identity_repo)
55    let notifier = NotificationRouter::new(config, identity_repo);
56    println!("✅ Notification router initialized (using configured destinations)");
57
58    // Create use case
59    let usecase = NotifyFutureResourceUsageChangesUseCase::new(repository, notifier).await?;
60
61    // Run polling loop
62    let interval = Duration::from_secs(60);
63    println!("🔍 Starting monitoring loop (interval: {:?})", interval);
64    println!("Press Ctrl+C to stop\n");
65
66    loop {
67        match usecase.poll_once().await {
68            Ok(_) => {
69                println!("✅ Poll completed successfully");
70            }
71            Err(e) => {
72                eprintln!("❌ Polling error: {}", e);
73            }
74        }
75
76        tokio::time::sleep(interval).await;
77    }
78}