#![allow(dead_code)]
use std::collections::{BTreeMap, HashMap, VecDeque};
use std::sync::{Arc, RwLock};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
use tokio::sync::RwLock as TokioRwLock;
use uuid::Uuid;
use super::super::{CloudProvider, ExecutionConfig, QuantumCloudConfig, WorkloadSpec};
use crate::{DeviceError, DeviceResult};
pub struct CostEstimationEngine {
pub config: CostEstimationConfig,
pub pricing_models: HashMap<CloudProvider, ProviderPricingModel>,
pub cost_predictors: HashMap<String, Box<dyn CostPredictor + Send + Sync>>,
pub budget_analyzer: Arc<TokioRwLock<BudgetAnalyzer>>,
pub cost_optimizer: Arc<TokioRwLock<CostOptimizer>>,
pub pricing_cache: Arc<TokioRwLock<PricingCache>>,
pub cost_history: Arc<TokioRwLock<CostHistory>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CostEstimationConfig {
pub enabled: bool,
pub estimation_accuracy_level: EstimationAccuracyLevel,
pub pricing_update_frequency: Duration,
pub include_hidden_costs: bool,
pub currency: String,
pub tax_rate: f64,
pub discount_thresholds: Vec<DiscountThreshold>,
pub cost_categories: Vec<CostCategory>,
pub predictive_modeling: PredictiveModelingConfig,
pub budget_tracking: BudgetTrackingConfig,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum EstimationAccuracyLevel {
Quick,
Standard,
Detailed,
Comprehensive,
RealTime,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiscountThreshold {
pub threshold_type: ThresholdType,
pub minimum_amount: f64,
pub discount_percentage: f64,
pub applicable_services: Vec<String>,
pub time_period: Duration,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ThresholdType {
Volume,
Frequency,
Duration,
Commitment,
Loyalty,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CostCategory {
Compute,
Storage,
Network,
Management,
Support,
Licensing,
Compliance,
DataTransfer,
Backup,
Security,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PredictiveModelingConfig {
pub enabled: bool,
pub model_types: Vec<PredictiveModelType>,
pub forecast_horizon: Duration,
pub confidence_intervals: bool,
pub seasonal_adjustments: bool,
pub trend_analysis: bool,
pub anomaly_detection: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum PredictiveModelType {
Linear,
TimeSeries,
MachineLearning,
StatisticalRegression,
NeuralNetwork,
EnsembleMethods,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BudgetTrackingConfig {
pub enabled: bool,
pub budget_periods: Vec<BudgetPeriod>,
pub alert_thresholds: Vec<f64>,
pub auto_scaling_on_budget: bool,
pub cost_allocation_tracking: bool,
pub variance_analysis: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum BudgetPeriod {
Daily,
Weekly,
Monthly,
Quarterly,
Yearly,
Custom(Duration),
}
#[derive(Debug, Clone)]
pub struct ProviderPricingModel {
pub provider: CloudProvider,
pub pricing_structure: PricingStructure,
pub service_pricing: HashMap<String, ServicePricing>,
pub volume_discounts: Vec<VolumeDiscount>,
pub promotional_offers: Vec<PromotionalOffer>,
pub regional_pricing: HashMap<String, RegionalPricingAdjustment>,
pub last_updated: SystemTime,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PricingStructure {
pub base_pricing_model: BasePricingModel,
pub billing_granularity: BillingGranularity,
pub minimum_charges: HashMap<String, f64>,
pub setup_fees: HashMap<String, f64>,
pub termination_fees: HashMap<String, f64>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum BasePricingModel {
PayPerUse,
Subscription,
Reserved,
Spot,
Hybrid,
Freemium,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum BillingGranularity {
Second,
Minute,
Hour,
Day,
Month,
Transaction,
Resource,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServicePricing {
pub service_name: String,
pub pricing_tiers: Vec<PricingTier>,
pub usage_metrics: Vec<UsageMetric>,
pub cost_components: Vec<CostComponent>,
pub billing_model: ServiceBillingModel,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PricingTier {
pub tier_name: String,
pub tier_level: usize,
pub usage_range: UsageRange,
pub unit_price: f64,
pub included_quota: Option<f64>,
pub overage_price: Option<f64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UsageRange {
pub min_usage: f64,
pub max_usage: Option<f64>,
pub unit: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UsageMetric {
pub metric_name: String,
pub metric_type: UsageMetricType,
pub unit: String,
pub measurement_interval: Duration,
pub aggregation_method: AggregationMethod,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum UsageMetricType {
Cumulative,
Peak,
Average,
Minimum,
Maximum,
Count,
Duration,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum AggregationMethod {
Sum,
Average,
Maximum,
Minimum,
Count,
Percentile(f64),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CostComponent {
pub component_name: String,
pub component_type: CostComponentType,
pub pricing_formula: PricingFormula,
pub dependencies: Vec<String>,
pub optional: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CostComponentType {
Fixed,
Variable,
Tiered,
StepFunction,
Custom,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PricingFormula {
pub formula_type: FormulaType,
pub parameters: HashMap<String, f64>,
pub variables: Vec<String>,
pub expression: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum FormulaType {
Linear,
Polynomial,
Exponential,
Logarithmic,
Piecewise,
Custom,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceBillingModel {
pub billing_cycle: BillingCycle,
pub payment_terms: PaymentTerms,
pub late_fees: Option<LateFees>,
pub refund_policy: RefundPolicy,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum BillingCycle {
RealTime,
Daily,
Weekly,
Monthly,
Quarterly,
Annually,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaymentTerms {
pub payment_due_days: u32,
pub early_payment_discount: Option<f64>,
pub accepted_payment_methods: Vec<PaymentMethod>,
pub automatic_payment: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum PaymentMethod {
CreditCard,
BankTransfer,
DigitalWallet,
Cryptocurrency,
InvoiceBilling,
PurchaseOrder,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LateFees {
pub late_fee_percentage: f64,
pub grace_period_days: u32,
pub maximum_late_fee: Option<f64>,
pub compound_interest: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RefundPolicy {
pub refund_eligibility_days: u32,
pub partial_refunds_allowed: bool,
pub refund_processing_time: Duration,
pub refund_conditions: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VolumeDiscount {
pub discount_name: String,
pub volume_threshold: f64,
pub discount_percentage: f64,
pub applicable_services: Vec<String>,
pub discount_cap: Option<f64>,
pub validity_period: Option<Duration>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromotionalOffer {
pub offer_name: String,
pub offer_type: OfferType,
pub discount_value: f64,
pub applicable_services: Vec<String>,
pub eligibility_criteria: Vec<String>,
pub start_date: SystemTime,
pub end_date: SystemTime,
pub usage_limit: Option<f64>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum OfferType {
PercentageDiscount,
FixedAmountDiscount,
FreeUsage,
UpgradePromotion,
BundleDiscount,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegionalPricingAdjustment {
pub region: String,
pub currency: String,
pub exchange_rate: f64,
pub tax_rate: f64,
pub regulatory_fees: f64,
pub local_adjustments: HashMap<String, f64>,
}
pub trait CostPredictor {
fn predict_cost(
&self,
workload: &WorkloadSpec,
config: &ExecutionConfig,
time_horizon: Duration,
) -> DeviceResult<CostPrediction>;
fn get_predictor_name(&self) -> String;
fn get_confidence_level(&self) -> f64;
}
#[derive(Debug, Clone)]
pub struct CostPrediction {
pub prediction_id: String,
pub workload_id: String,
pub predicted_cost: f64,
pub cost_breakdown: DetailedCostBreakdown,
pub confidence_interval: (f64, f64),
pub prediction_horizon: Duration,
pub assumptions: Vec<CostAssumption>,
pub risk_factors: Vec<RiskFactor>,
pub optimization_opportunities: Vec<CostOptimizationOpportunity>,
}
#[derive(Debug, Clone)]
pub struct DetailedCostBreakdown {
pub base_costs: HashMap<CostCategory, f64>,
pub variable_costs: HashMap<String, f64>,
pub fixed_costs: HashMap<String, f64>,
pub taxes_and_fees: f64,
pub discounts_applied: f64,
pub total_cost: f64,
pub cost_per_unit: HashMap<String, f64>,
}
#[derive(Debug, Clone)]
pub struct CostAssumption {
pub assumption_type: AssumptionType,
pub description: String,
pub impact_on_cost: f64,
pub confidence: f64,
pub sensitivity: f64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AssumptionType {
PricingStability,
UsagePattern,
ResourceAvailability,
PerformanceCharacteristics,
ExternalFactors,
}
#[derive(Debug, Clone)]
pub struct RiskFactor {
pub risk_type: RiskType,
pub description: String,
pub probability: f64,
pub potential_cost_impact: f64,
pub mitigation_strategies: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RiskType {
PriceVolatility,
DemandSpike,
ResourceScarcity,
TechnicalFailure,
PolicyChange,
MarketConditions,
}
#[derive(Debug, Clone)]
pub struct CostOptimizationOpportunity {
pub opportunity_id: String,
pub opportunity_type: OptimizationType,
pub description: String,
pub potential_savings: f64,
pub implementation_effort: ImplementationEffort,
pub implementation_time: Duration,
pub confidence: f64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OptimizationType {
ProviderSwitch,
ServiceTierChange,
UsageOptimization,
SchedulingOptimization,
VolumeDiscount,
ReservedCapacity,
SpotInstances,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ImplementationEffort {
Low,
Medium,
High,
VeryHigh,
}
pub struct BudgetAnalyzer {
pub current_budgets: HashMap<String, Budget>,
pub budget_performance: HashMap<String, BudgetPerformance>,
pub variance_analyzer: VarianceAnalyzer,
pub forecast_engine: BudgetForecastEngine,
}
#[derive(Debug, Clone)]
pub struct Budget {
pub budget_id: String,
pub budget_name: String,
pub budget_period: BudgetPeriod,
pub allocated_amount: f64,
pub spent_amount: f64,
pub remaining_amount: f64,
pub cost_centers: HashMap<String, f64>,
pub alert_thresholds: Vec<AlertThreshold>,
pub auto_adjustments: Vec<AutoAdjustment>,
}
#[derive(Debug, Clone)]
pub struct BudgetPerformance {
pub budget_id: String,
pub utilization_rate: f64,
pub spending_velocity: f64,
pub variance_from_plan: f64,
pub efficiency_score: f64,
pub trend_direction: TrendDirection,
pub performance_metrics: HashMap<String, f64>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TrendDirection {
Increasing,
Decreasing,
Stable,
Volatile,
}
#[derive(Debug, Clone)]
pub struct AlertThreshold {
pub threshold_type: ThresholdType,
pub threshold_value: f64,
pub alert_severity: AlertSeverity,
pub notification_channels: Vec<NotificationChannel>,
pub auto_actions: Vec<AutoAction>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AlertSeverity {
Info,
Warning,
Critical,
Emergency,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NotificationChannel {
Email,
SMS,
Slack,
Webhook,
Dashboard,
PagerDuty,
}
#[derive(Debug, Clone)]
pub struct AutoAction {
pub action_type: AutoActionType,
pub action_parameters: HashMap<String, String>,
pub conditions: Vec<String>,
pub approval_required: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AutoActionType {
ScaleDown,
SuspendJobs,
SwitchProvider,
NotifyAdministrator,
ActivateBackupPlan,
ApplyEmergencyBudget,
}
#[derive(Debug, Clone)]
pub struct AutoAdjustment {
pub adjustment_type: AdjustmentType,
pub trigger_conditions: Vec<String>,
pub adjustment_amount: f64,
pub maximum_adjustments: usize,
pub cool_down_period: Duration,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AdjustmentType {
BudgetIncrease,
BudgetDecrease,
Reallocation,
TemporaryExtension,
EmergencyFund,
}
pub struct VarianceAnalyzer {
pub variance_models: Vec<Box<dyn VarianceModel + Send + Sync>>,
pub statistical_analyzers: Vec<Box<dyn StatisticalAnalyzer + Send + Sync>>,
pub trend_detectors: Vec<Box<dyn TrendDetector + Send + Sync>>,
}
pub trait VarianceModel {
fn analyze_variance(
&self,
budget: &Budget,
actual_spending: &[SpendingRecord],
) -> DeviceResult<VarianceAnalysis>;
fn get_model_name(&self) -> String;
}
#[derive(Debug, Clone)]
pub struct VarianceAnalysis {
pub total_variance: f64,
pub variance_percentage: f64,
pub variance_components: HashMap<String, f64>,
pub variance_causes: Vec<VarianceCause>,
pub statistical_significance: f64,
pub recommendations: Vec<VarianceRecommendation>,
}
#[derive(Debug, Clone)]
pub struct VarianceCause {
pub cause_type: VarianceCauseType,
pub description: String,
pub contribution_percentage: f64,
pub controllability: ControllabilityLevel,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VarianceCauseType {
VolumeVariance,
RateVariance,
MixVariance,
EfficiencyVariance,
TimingVariance,
ExternalFactors,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ControllabilityLevel {
FullyControllable,
PartiallyControllable,
Influenceable,
Uncontrollable,
}
#[derive(Debug, Clone)]
pub struct VarianceRecommendation {
pub recommendation_type: VarianceRecommendationType,
pub description: String,
pub priority: RecommendationPriority,
pub expected_impact: f64,
pub implementation_timeline: Duration,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VarianceRecommendationType {
BudgetAdjustment,
ProcessImprovement,
CostControl,
ResourceOptimization,
ForecastRefinement,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RecommendationPriority {
Low,
Medium,
High,
Critical,
}
pub trait StatisticalAnalyzer {
fn analyze_spending_patterns(
&self,
spending_data: &[SpendingRecord],
) -> DeviceResult<StatisticalAnalysis>;
fn get_analyzer_name(&self) -> String;
}
#[derive(Debug, Clone)]
pub struct StatisticalAnalysis {
pub descriptive_statistics: DescriptiveStatistics,
pub correlation_analysis: CorrelationAnalysis,
pub regression_analysis: Option<RegressionAnalysis>,
pub anomaly_detection: AnomalyDetectionResult,
pub seasonal_decomposition: Option<SeasonalDecomposition>,
}
#[derive(Debug, Clone)]
pub struct DescriptiveStatistics {
pub mean: f64,
pub median: f64,
pub standard_deviation: f64,
pub variance: f64,
pub skewness: f64,
pub kurtosis: f64,
pub percentiles: HashMap<f64, f64>,
}
#[derive(Debug, Clone)]
pub struct CorrelationAnalysis {
pub correlationmatrix: Vec<Vec<f64>>,
pub variable_names: Vec<String>,
pub significant_correlations: Vec<(String, String, f64, f64)>, }
#[derive(Debug, Clone)]
pub struct RegressionAnalysis {
pub model_type: RegressionModelType,
pub coefficients: Vec<f64>,
pub r_squared: f64,
pub adjusted_r_squared: f64,
pub f_statistic: f64,
pub p_values: Vec<f64>,
pub residual_analysis: ResidualAnalysis,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RegressionModelType {
Linear,
Polynomial,
Logistic,
MultipleRegression,
StepwiseRegression,
}
#[derive(Debug, Clone)]
pub struct ResidualAnalysis {
pub residuals: Vec<f64>,
pub standardized_residuals: Vec<f64>,
pub residual_patterns: Vec<ResidualPattern>,
pub outliers: Vec<usize>,
pub normality_test: NormalityTest,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ResidualPattern {
Random,
Heteroscedastic,
AutoCorrelated,
NonLinear,
Seasonal,
}
#[derive(Debug, Clone)]
pub struct NormalityTest {
pub test_name: String,
pub test_statistic: f64,
pub p_value: f64,
pub is_normal: bool,
}
#[derive(Debug, Clone)]
pub struct AnomalyDetectionResult {
pub anomalies_detected: Vec<Anomaly>,
pub anomaly_score: f64,
pub detection_method: String,
pub confidence_level: f64,
}
#[derive(Debug, Clone)]
pub struct Anomaly {
pub anomaly_id: String,
pub timestamp: SystemTime,
pub anomaly_type: AnomalyType,
pub severity: f64,
pub description: String,
pub affected_metrics: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AnomalyType {
PointAnomaly,
ContextualAnomaly,
CollectiveAnomaly,
TrendAnomaly,
SeasonalAnomaly,
}
#[derive(Debug, Clone)]
pub struct SeasonalDecomposition {
pub trend_component: Vec<f64>,
pub seasonal_component: Vec<f64>,
pub residual_component: Vec<f64>,
pub seasonal_periods: Vec<Duration>,
pub trend_direction: TrendDirection,
}
pub trait TrendDetector {
fn detect_trends(&self, spending_data: &[SpendingRecord])
-> DeviceResult<TrendDetectionResult>;
fn get_detector_name(&self) -> String;
}
#[derive(Debug, Clone)]
pub struct TrendDetectionResult {
pub trends_detected: Vec<Trend>,
pub overall_trend_direction: TrendDirection,
pub trend_strength: f64,
pub trend_significance: f64,
}
#[derive(Debug, Clone)]
pub struct Trend {
pub trend_id: String,
pub trend_type: TrendType,
pub start_time: SystemTime,
pub end_time: Option<SystemTime>,
pub trend_direction: TrendDirection,
pub trend_magnitude: f64,
pub affected_categories: Vec<CostCategory>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TrendType {
Linear,
Exponential,
Cyclical,
Seasonal,
Irregular,
}
pub struct BudgetForecastEngine {
pub forecast_models: Vec<Box<dyn ForecastModel + Send + Sync>>,
pub scenario_generators: Vec<Box<dyn ScenarioGenerator + Send + Sync>>,
pub uncertainty_quantifiers: Vec<Box<dyn UncertaintyQuantifier + Send + Sync>>,
}
pub trait ForecastModel {
fn generate_forecast(
&self,
historical_data: &[SpendingRecord],
forecast_horizon: Duration,
) -> DeviceResult<BudgetForecast>;
fn get_model_name(&self) -> String;
fn get_model_accuracy(&self) -> f64;
}
#[derive(Debug, Clone)]
pub struct BudgetForecast {
pub forecast_id: String,
pub forecast_horizon: Duration,
pub forecasted_values: Vec<ForecastPoint>,
pub confidence_intervals: Vec<ConfidenceInterval>,
pub forecast_accuracy_metrics: ForecastAccuracyMetrics,
pub scenarios: Vec<ForecastScenario>,
}
#[derive(Debug, Clone)]
pub struct ForecastPoint {
pub timestamp: SystemTime,
pub predicted_value: f64,
pub prediction_interval: (f64, f64),
pub contributing_factors: HashMap<String, f64>,
}
#[derive(Debug, Clone)]
pub struct ConfidenceInterval {
pub confidence_level: f64,
pub lower_bound: f64,
pub upper_bound: f64,
pub interval_width: f64,
}
#[derive(Debug, Clone)]
pub struct ForecastAccuracyMetrics {
pub mean_absolute_error: f64,
pub mean_squared_error: f64,
pub mean_absolute_percentage_error: f64,
pub symmetric_mean_absolute_percentage_error: f64,
pub theil_u_statistic: f64,
}
#[derive(Debug, Clone)]
pub struct ForecastScenario {
pub scenario_name: String,
pub scenario_type: ScenarioType,
pub probability: f64,
pub forecasted_values: Vec<f64>,
pub scenario_assumptions: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScenarioType {
Optimistic,
Pessimistic,
MostLikely,
WorstCase,
BestCase,
Stress,
}
pub trait ScenarioGenerator {
fn generate_scenarios(
&self,
base_forecast: &BudgetForecast,
num_scenarios: usize,
) -> DeviceResult<Vec<ForecastScenario>>;
fn get_generator_name(&self) -> String;
}
pub trait UncertaintyQuantifier {
fn quantify_uncertainty(&self, forecast: &BudgetForecast) -> DeviceResult<UncertaintyAnalysis>;
fn get_quantifier_name(&self) -> String;
}
#[derive(Debug, Clone)]
pub struct UncertaintyAnalysis {
pub total_uncertainty: f64,
pub uncertainty_sources: Vec<UncertaintySource>,
pub uncertainty_propagation: UncertaintyPropagation,
pub sensitivity_analysis: SensitivityAnalysis,
}
#[derive(Debug, Clone)]
pub struct UncertaintySource {
pub source_name: String,
pub source_type: UncertaintySourceType,
pub contribution_percentage: f64,
pub uncertainty_distribution: UncertaintyDistribution,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UncertaintySourceType {
ModelUncertainty,
ParameterUncertainty,
InputDataUncertainty,
StructuralUncertainty,
ExternalFactors,
}
#[derive(Debug, Clone)]
pub struct UncertaintyDistribution {
pub distribution_type: DistributionType,
pub parameters: HashMap<String, f64>,
pub confidence_level: f64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DistributionType {
Normal,
LogNormal,
Uniform,
Triangular,
Beta,
Gamma,
Exponential,
}
#[derive(Debug, Clone)]
pub struct UncertaintyPropagation {
pub propagation_method: PropagationMethod,
pub correlation_effects: Vec<CorrelationEffect>,
pub amplification_factors: HashMap<String, f64>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PropagationMethod {
MonteCarlo,
LinearPropagation,
TaylorSeries,
Polynomial,
Sampling,
}
#[derive(Debug, Clone)]
pub struct CorrelationEffect {
pub source1: String,
pub source2: String,
pub correlation_coefficient: f64,
pub effect_magnitude: f64,
}
#[derive(Debug, Clone)]
pub struct SensitivityAnalysis {
pub sensitivity_indices: HashMap<String, f64>,
pub interaction_effects: Vec<InteractionEffect>,
pub robust_parameters: Vec<String>,
pub critical_parameters: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct InteractionEffect {
pub parameters: Vec<String>,
pub interaction_strength: f64,
pub effect_type: InteractionType,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InteractionType {
Synergistic,
Antagonistic,
Additive,
Multiplicative,
}
pub struct CostOptimizer {
pub optimization_strategies: Vec<Box<dyn CostOptimizationStrategy + Send + Sync>>,
pub recommendation_engine: RecommendationEngine,
pub savings_calculator: SavingsCalculator,
}
pub trait CostOptimizationStrategy {
fn optimize_costs(
&self,
cost_analysis: &CostAnalysis,
) -> DeviceResult<OptimizationRecommendation>;
fn get_strategy_name(&self) -> String;
fn get_potential_savings(&self, cost_analysis: &CostAnalysis) -> DeviceResult<f64>;
}
#[derive(Debug, Clone)]
pub struct CostAnalysis {
pub total_costs: f64,
pub cost_breakdown: DetailedCostBreakdown,
pub cost_trends: Vec<CostTrend>,
pub cost_drivers: Vec<CostDriver>,
pub benchmark_comparison: BenchmarkComparison,
pub inefficiencies: Vec<CostInefficiency>,
}
#[derive(Debug, Clone)]
pub struct CostTrend {
pub trend_id: String,
pub cost_category: CostCategory,
pub trend_direction: TrendDirection,
pub trend_rate: f64,
pub trend_duration: Duration,
pub projected_impact: f64,
}
#[derive(Debug, Clone)]
pub struct CostDriver {
pub driver_name: String,
pub driver_type: CostDriverType,
pub impact_magnitude: f64,
pub controllability: ControllabilityLevel,
pub optimization_potential: f64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CostDriverType {
Volume,
Complexity,
Quality,
Speed,
Flexibility,
Risk,
}
#[derive(Debug, Clone)]
pub struct BenchmarkComparison {
pub benchmark_type: BenchmarkType,
pub comparison_metrics: HashMap<String, BenchmarkMetric>,
pub relative_performance: f64,
pub improvement_opportunities: Vec<ImprovementOpportunity>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BenchmarkType {
IndustryAverage,
BestInClass,
Historical,
Internal,
Competitor,
}
#[derive(Debug, Clone)]
pub struct BenchmarkMetric {
pub metric_name: String,
pub current_value: f64,
pub benchmark_value: f64,
pub variance_percentage: f64,
pub performance_gap: f64,
}
#[derive(Debug, Clone)]
pub struct ImprovementOpportunity {
pub opportunity_id: String,
pub opportunity_area: String,
pub current_performance: f64,
pub target_performance: f64,
pub potential_savings: f64,
pub implementation_complexity: f64,
}
#[derive(Debug, Clone)]
pub struct CostInefficiency {
pub inefficiency_type: InefficiencyType,
pub description: String,
pub cost_impact: f64,
pub frequency: f64,
pub root_causes: Vec<String>,
pub remediation_options: Vec<RemediationOption>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InefficiencyType {
Overprovisioning,
Underutilization,
Redundancy,
ProcessInefficiency,
TechnologyMismatch,
VendorInefficiency,
}
#[derive(Debug, Clone)]
pub struct RemediationOption {
pub option_name: String,
pub implementation_effort: ImplementationEffort,
pub expected_savings: f64,
pub implementation_timeline: Duration,
pub success_probability: f64,
}
#[derive(Debug, Clone)]
pub struct OptimizationRecommendation {
pub recommendation_id: String,
pub recommendation_type: OptimizationType,
pub priority: RecommendationPriority,
pub description: String,
pub potential_savings: f64,
pub implementation_plan: ImplementationPlan,
pub risk_assessment: RiskAssessment,
pub roi_analysis: ROIAnalysis,
}
#[derive(Debug, Clone)]
pub struct ImplementationPlan {
pub phases: Vec<ImplementationPhase>,
pub total_duration: Duration,
pub resource_requirements: Vec<ResourceRequirement>,
pub dependencies: Vec<String>,
pub milestones: Vec<Milestone>,
}
#[derive(Debug, Clone)]
pub struct ImplementationPhase {
pub phase_name: String,
pub phase_duration: Duration,
pub phase_activities: Vec<String>,
pub deliverables: Vec<String>,
pub success_criteria: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct ResourceRequirement {
pub resource_type: String,
pub quantity: f64,
pub duration: Duration,
pub cost: f64,
pub availability: f64,
}
#[derive(Debug, Clone)]
pub struct Milestone {
pub milestone_name: String,
pub target_date: SystemTime,
pub completion_criteria: Vec<String>,
pub deliverables: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct RiskAssessment {
pub overall_risk_score: f64,
pub risk_factors: Vec<RiskFactor>,
pub mitigation_strategies: Vec<MitigationStrategy>,
pub contingency_plans: Vec<ContingencyPlan>,
}
#[derive(Debug, Clone)]
pub struct MitigationStrategy {
pub strategy_name: String,
pub risk_reduction: f64,
pub implementation_cost: f64,
pub effectiveness: f64,
}
#[derive(Debug, Clone)]
pub struct ContingencyPlan {
pub plan_name: String,
pub trigger_conditions: Vec<String>,
pub response_actions: Vec<String>,
pub resource_requirements: Vec<ResourceRequirement>,
}
#[derive(Debug, Clone)]
pub struct ROIAnalysis {
pub initial_investment: f64,
pub annual_savings: f64,
pub payback_period: Duration,
pub net_present_value: f64,
pub internal_rate_of_return: f64,
pub roi_percentage: f64,
}
pub struct RecommendationEngine {
pub recommendation_algorithms: Vec<Box<dyn RecommendationAlgorithm + Send + Sync>>,
pub scoring_models: Vec<Box<dyn ScoringModel + Send + Sync>>,
pub prioritization_engine: PrioritizationEngine,
}
pub trait RecommendationAlgorithm {
fn generate_recommendations(
&self,
analysis: &CostAnalysis,
) -> DeviceResult<Vec<OptimizationRecommendation>>;
fn get_algorithm_name(&self) -> String;
}
pub trait ScoringModel {
fn score_recommendation(
&self,
recommendation: &OptimizationRecommendation,
) -> DeviceResult<f64>;
fn get_model_name(&self) -> String;
}
pub struct PrioritizationEngine {
pub prioritization_criteria: Vec<PrioritizationCriterion>,
pub weighting_scheme: WeightingScheme,
pub decision_matrix: DecisionMatrix,
}
#[derive(Debug, Clone)]
pub struct PrioritizationCriterion {
pub criterion_name: String,
pub criterion_type: CriterionType,
pub weight: f64,
pub scoring_function: ScoringFunction,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CriterionType {
Financial,
Strategic,
Operational,
Risk,
Feasibility,
}
#[derive(Debug, Clone)]
pub struct ScoringFunction {
pub function_type: FunctionType,
pub parameters: HashMap<String, f64>,
pub range: (f64, f64),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FunctionType {
Linear,
Exponential,
Logarithmic,
Sigmoid,
Step,
}
#[derive(Debug, Clone)]
pub struct WeightingScheme {
pub scheme_type: WeightingSchemeType,
pub weights: HashMap<String, f64>,
pub normalization_method: NormalizationMethod,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WeightingSchemeType {
Equal,
Hierarchical,
Expert,
DataDriven,
Hybrid,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NormalizationMethod {
MinMax,
ZScore,
UnitVector,
Sum,
Max,
}
#[derive(Debug, Clone)]
pub struct DecisionMatrix {
pub alternatives: Vec<String>,
pub criteria: Vec<String>,
pub scores: Vec<Vec<f64>>,
pub weights: Vec<f64>,
pub aggregation_method: AggregationMethod,
}
pub struct SavingsCalculator {
pub calculation_methods: Vec<Box<dyn SavingsCalculationMethod + Send + Sync>>,
pub validation_rules: Vec<ValidationRule>,
pub adjustment_factors: AdjustmentFactors,
}
pub trait SavingsCalculationMethod {
fn calculate_savings(
&self,
baseline_cost: f64,
optimized_cost: f64,
implementation_cost: f64,
) -> DeviceResult<SavingsCalculation>;
fn get_method_name(&self) -> String;
}
#[derive(Debug, Clone)]
pub struct SavingsCalculation {
pub gross_savings: f64,
pub net_savings: f64,
pub savings_percentage: f64,
pub implementation_cost: f64,
pub ongoing_costs: f64,
pub payback_period: Duration,
pub cumulative_savings: Vec<(SystemTime, f64)>,
}
#[derive(Debug, Clone)]
pub struct ValidationRule {
pub rule_name: String,
pub rule_type: ValidationRuleType,
pub condition: String,
pub action: String,
pub severity: ValidationSeverity,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValidationRuleType {
BusinessRule,
TechnicalConstraint,
RegulatoryRequirement,
PolicyCompliance,
DataQuality,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValidationSeverity {
Warning,
Error,
Critical,
Info,
}
#[derive(Debug, Clone)]
pub struct AdjustmentFactors {
pub risk_adjustment: f64,
pub confidence_adjustment: f64,
pub market_adjustment: f64,
pub seasonal_adjustment: f64,
pub inflation_adjustment: f64,
}
pub struct PricingCache {
pub cache_entries: HashMap<String, PricingCacheEntry>,
pub cache_statistics: CacheStatistics,
pub eviction_policy: EvictionPolicy,
}
#[derive(Debug, Clone)]
pub struct PricingCacheEntry {
pub entry_id: String,
pub provider: CloudProvider,
pub service: String,
pub pricing_data: ServicePricing,
pub timestamp: SystemTime,
pub validity_period: Duration,
pub access_count: usize,
}
#[derive(Debug, Clone)]
pub struct CacheStatistics {
pub hit_rate: f64,
pub miss_rate: f64,
pub eviction_rate: f64,
pub average_lookup_time: Duration,
pub total_entries: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EvictionPolicy {
LRU,
LFU,
FIFO,
TTL,
Random,
}
pub struct CostHistory {
pub spending_records: Vec<SpendingRecord>,
pub aggregated_costs: HashMap<String, Vec<AggregatedCost>>,
pub cost_trends: HashMap<String, CostTrend>,
pub historical_analysis: HistoricalAnalysis,
}
#[derive(Debug, Clone)]
pub struct SpendingRecord {
pub record_id: String,
pub timestamp: SystemTime,
pub provider: CloudProvider,
pub service: String,
pub cost_amount: f64,
pub currency: String,
pub cost_category: CostCategory,
pub usage_metrics: HashMap<String, f64>,
pub billing_period: (SystemTime, SystemTime),
}
#[derive(Debug, Clone)]
pub struct AggregatedCost {
pub aggregation_period: (SystemTime, SystemTime),
pub total_cost: f64,
pub cost_breakdown: HashMap<CostCategory, f64>,
pub usage_summary: HashMap<String, f64>,
pub cost_per_unit: HashMap<String, f64>,
}
#[derive(Debug, Clone)]
pub struct HistoricalAnalysis {
pub cost_growth_rate: f64,
pub seasonal_patterns: Vec<SeasonalPattern>,
pub cost_volatility: f64,
pub efficiency_trends: Vec<EfficiencyTrend>,
pub comparative_analysis: ComparativeAnalysis,
}
#[derive(Debug, Clone)]
pub struct SeasonalPattern {
pub pattern_name: String,
pub period: Duration,
pub amplitude: f64,
pub phase_shift: Duration,
pub significance: f64,
}
#[derive(Debug, Clone)]
pub struct EfficiencyTrend {
pub metric_name: String,
pub trend_direction: TrendDirection,
pub improvement_rate: f64,
pub efficiency_score: f64,
}
#[derive(Debug, Clone)]
pub struct ComparativeAnalysis {
pub period_comparisons: Vec<PeriodComparison>,
pub provider_comparisons: Vec<ProviderComparison>,
pub service_comparisons: Vec<ServiceComparison>,
}
#[derive(Debug, Clone)]
pub struct PeriodComparison {
pub period1: (SystemTime, SystemTime),
pub period2: (SystemTime, SystemTime),
pub cost_difference: f64,
pub percentage_change: f64,
pub significant_changes: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct ProviderComparison {
pub provider1: CloudProvider,
pub provider2: CloudProvider,
pub cost_comparison: HashMap<String, f64>,
pub service_comparison: HashMap<String, f64>,
pub efficiency_comparison: HashMap<String, f64>,
}
#[derive(Debug, Clone)]
pub struct ServiceComparison {
pub service_name: String,
pub cost_trend: TrendDirection,
pub usage_trend: TrendDirection,
pub efficiency_trend: TrendDirection,
pub optimization_opportunities: Vec<String>,
}