use chrono::{DateTime, Duration, Utc};
use mockforge_core::contract_drift::forecasting::{
ChangeForecast, Forecaster, ForecastingConfig,
};
use mockforge_core::incidents::types::DriftIncident;
use std::sync::Arc;
#[derive(Clone)]
pub struct ForecastingService {
forecaster: Arc<Forecaster>,
config: ForecastingConfig,
}
impl ForecastingService {
pub fn new(config: ForecastingConfig) -> Self {
let forecaster = Arc::new(Forecaster::new(config.clone()));
Self { forecaster, config }
}
pub async fn generate_forecast(
&self,
incidents: &[DriftIncident],
workspace_id: Option<String>,
service_id: Option<String>,
service_name: Option<String>,
endpoint: String,
method: String,
forecast_window_days: u32,
) -> Option<ChangeForecast> {
self.forecaster.generate_forecast(
incidents,
workspace_id,
service_id,
service_name,
endpoint,
method,
forecast_window_days,
)
}
pub fn is_forecast_stale(&self, forecast: &ChangeForecast) -> bool {
Utc::now() >= forecast.expires_at
}
pub fn default_expiration(&self) -> DateTime<Utc> {
Utc::now() + Duration::hours(self.config.default_expiration_hours as i64)
}
}
impl Default for ForecastingService {
fn default() -> Self {
Self::new(ForecastingConfig::default())
}
}