| 1 |
//! Quality Tier Management |
| 2 |
//! |
| 3 |
//! Manages service quality levels based on contribution rather than payment |
| 4 |
|
| 5 |
use anyhow::Result; |
| 6 |
use serde::{Deserialize, Serialize}; |
| 7 |
use std::collections::HashMap; |
| 8 |
use chrono::{DateTime, Utc}; |
| 9 |
|
| 10 |
use crate::economics::PriorityLevel; |
| 11 |
|
| 12 |
/// Quality tier manager for contribution-based service levels |
| 13 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 14 |
pub struct QualityTierManager { |
| 15 |
/// Available quality tiers |
| 16 |
pub tiers: HashMap<QualityTier, TierConfiguration>, |
| 17 |
/// User tier assignments |
| 18 |
pub user_tiers: HashMap<String, UserTierAssignment>, |
| 19 |
/// Tier performance metrics |
| 20 |
pub tier_metrics: HashMap<QualityTier, TierMetrics>, |
| 21 |
} |
| 22 |
|
| 23 |
#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)] |
| 24 |
pub enum QualityTier { |
| 25 |
/// Basic service for deficit contributors |
| 26 |
Basic, |
| 27 |
/// Standard service for balanced contributors |
| 28 |
Standard, |
| 29 |
/// Premium service for surplus contributors |
| 30 |
Premium, |
| 31 |
/// Enterprise service for generous contributors |
| 32 |
Enterprise, |
| 33 |
} |
| 34 |
|
| 35 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 36 |
pub struct TierConfiguration { |
| 37 |
pub tier: QualityTier, |
| 38 |
pub name: String, |
| 39 |
pub description: String, |
| 40 |
pub requirements: TierRequirements, |
| 41 |
pub benefits: TierBenefits, |
| 42 |
pub service_level: ServiceLevel, |
| 43 |
pub enabled: bool, |
| 44 |
} |
| 45 |
|
| 46 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 47 |
pub struct TierRequirements { |
| 48 |
/// Minimum contribution score to access this tier |
| 49 |
pub min_contribution_score: f64, |
| 50 |
/// Minimum priority level required |
| 51 |
pub min_priority_level: PriorityLevel, |
| 52 |
/// Minimum storage contribution (GB) |
| 53 |
pub min_storage_contribution_gb: u64, |
| 54 |
/// Minimum account age (days) |
| 55 |
pub min_account_age_days: u32, |
| 56 |
/// Minimum uptime percentage |
| 57 |
pub min_uptime_percentage: f64, |
| 58 |
} |
| 59 |
|
| 60 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 61 |
pub struct TierBenefits { |
| 62 |
/// Storage allocation multiplier (1.0 = 100% of contribution) |
| 63 |
pub storage_multiplier: f64, |
| 64 |
/// Bandwidth allocation multiplier |
| 65 |
pub bandwidth_multiplier: f64, |
| 66 |
/// Priority in allocation queue (higher = better priority) |
| 67 |
pub allocation_priority: u32, |
| 68 |
/// SLA guarantees |
| 69 |
pub sla_guarantees: SLAGuarantees, |
| 70 |
/// Special features access |
| 71 |
pub features: Vec<TierFeature>, |
| 72 |
/// Support level |
| 73 |
pub support_level: SupportTier, |
| 74 |
} |
| 75 |
|
| 76 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 77 |
pub struct SLAGuarantees { |
| 78 |
pub uptime_guarantee_percent: f64, |
| 79 |
pub max_latency_ms: u32, |
| 80 |
pub min_throughput_mbps: f64, |
| 81 |
pub data_durability_percent: f64, |
| 82 |
pub recovery_time_objective_hours: u32, |
| 83 |
pub recovery_point_objective_hours: u32, |
| 84 |
} |
| 85 |
|
| 86 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 87 |
pub enum TierFeature { |
| 88 |
/// Priority customer support |
| 89 |
PrioritySupport, |
| 90 |
/// Advanced monitoring and analytics |
| 91 |
AdvancedMonitoring, |
| 92 |
/// Geographic replication options |
| 93 |
GeographicReplication, |
| 94 |
/// Custom SLA agreements |
| 95 |
CustomSLA, |
| 96 |
/// API access with higher rate limits |
| 97 |
EnhancedAPI, |
| 98 |
/// Beta feature access |
| 99 |
BetaFeatures, |
| 100 |
/// Dedicated technical account manager |
| 101 |
DedicatedAccountManager, |
| 102 |
} |
| 103 |
|
| 104 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 105 |
pub enum SupportTier { |
| 106 |
/// Community forum support only |
| 107 |
Community, |
| 108 |
/// Standard business hours email support |
| 109 |
Standard, |
| 110 |
/// Premium 24/7 support with phone |
| 111 |
Premium, |
| 112 |
/// Enterprise dedicated support team |
| 113 |
Enterprise, |
| 114 |
} |
| 115 |
|
| 116 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 117 |
pub struct ServiceLevel { |
| 118 |
pub availability_target: f64, |
| 119 |
pub performance_targets: PerformanceTargets, |
| 120 |
pub data_protection: DataProtection, |
| 121 |
pub monitoring_level: MonitoringLevel, |
| 122 |
} |
| 123 |
|
| 124 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 125 |
pub struct PerformanceTargets { |
| 126 |
pub response_time_p50_ms: u32, |
| 127 |
pub response_time_p95_ms: u32, |
| 128 |
pub response_time_p99_ms: u32, |
| 129 |
pub throughput_minimum_mbps: f64, |
| 130 |
pub concurrent_connections: u32, |
| 131 |
} |
| 132 |
|
| 133 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 134 |
pub struct DataProtection { |
| 135 |
pub backup_frequency_hours: u32, |
| 136 |
pub backup_retention_days: u32, |
| 137 |
pub geo_redundancy: bool, |
| 138 |
pub encryption_level: EncryptionLevel, |
| 139 |
pub compliance_standards: Vec<ComplianceStandard>, |
| 140 |
} |
| 141 |
|
| 142 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 143 |
pub enum EncryptionLevel { |
| 144 |
/// AES-128 encryption |
| 145 |
Standard, |
| 146 |
/// AES-256 encryption |
| 147 |
Enhanced, |
| 148 |
/// AES-256 with hardware security modules |
| 149 |
Enterprise, |
| 150 |
} |
| 151 |
|
| 152 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 153 |
pub enum ComplianceStandard { |
| 154 |
GDPR, |
| 155 |
HIPAA, |
| 156 |
SOC2, |
| 157 |
ISO27001, |
| 158 |
PCI_DSS, |
| 159 |
} |
| 160 |
|
| 161 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 162 |
pub enum MonitoringLevel { |
| 163 |
/// Basic uptime monitoring |
| 164 |
Basic, |
| 165 |
/// Standard performance monitoring |
| 166 |
Standard, |
| 167 |
/// Advanced monitoring with alerting |
| 168 |
Advanced, |
| 169 |
/// Enterprise monitoring with analytics |
| 170 |
Enterprise, |
| 171 |
} |
| 172 |
|
| 173 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 174 |
pub struct UserTierAssignment { |
| 175 |
pub user_id: String, |
| 176 |
pub assigned_tier: QualityTier, |
| 177 |
pub assigned_at: DateTime<Utc>, |
| 178 |
pub last_evaluated: DateTime<Utc>, |
| 179 |
pub next_evaluation: DateTime<Utc>, |
| 180 |
pub tier_history: Vec<TierChange>, |
| 181 |
pub current_benefits: TierBenefits, |
| 182 |
} |
| 183 |
|
| 184 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 185 |
pub struct TierChange { |
| 186 |
pub from_tier: Option<QualityTier>, |
| 187 |
pub to_tier: QualityTier, |
| 188 |
pub reason: String, |
| 189 |
pub changed_at: DateTime<Utc>, |
| 190 |
} |
| 191 |
|
| 192 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 193 |
pub struct TierMetrics { |
| 194 |
pub tier: QualityTier, |
| 195 |
pub active_users: u32, |
| 196 |
pub average_satisfaction_score: f64, |
| 197 |
pub sla_compliance_percent: f64, |
| 198 |
pub average_resource_utilization: f64, |
| 199 |
pub support_ticket_count: u32, |
| 200 |
pub last_updated: DateTime<Utc>, |
| 201 |
} |
| 202 |
|
| 203 |
impl QualityTierManager { |
| 204 |
pub fn new() -> Self { |
| 205 |
let mut tiers = HashMap::new(); |
| 206 |
|
| 207 |
// Basic Tier - for users with contribution deficits |
| 208 |
tiers.insert(QualityTier::Basic, TierConfiguration { |
| 209 |
tier: QualityTier::Basic, |
| 210 |
name: "Basic Service".to_string(), |
| 211 |
description: "Essential storage and bandwidth with basic guarantees".to_string(), |
| 212 |
requirements: TierRequirements { |
| 213 |
min_contribution_score: 0.0, |
| 214 |
min_priority_level: PriorityLevel::Deficit, |
| 215 |
min_storage_contribution_gb: 10, |
| 216 |
min_account_age_days: 0, |
| 217 |
min_uptime_percentage: 90.0, |
| 218 |
}, |
| 219 |
benefits: TierBenefits { |
| 220 |
storage_multiplier: 0.5, // Can use 50% of what they contribute |
| 221 |
bandwidth_multiplier: 0.5, |
| 222 |
allocation_priority: 100, |
| 223 |
sla_guarantees: SLAGuarantees { |
| 224 |
uptime_guarantee_percent: 95.0, |
| 225 |
max_latency_ms: 500, |
| 226 |
min_throughput_mbps: 1.0, |
| 227 |
data_durability_percent: 99.9, |
| 228 |
recovery_time_objective_hours: 24, |
| 229 |
recovery_point_objective_hours: 4, |
| 230 |
}, |
| 231 |
features: vec![], |
| 232 |
support_level: SupportTier::Community, |
| 233 |
}, |
| 234 |
service_level: ServiceLevel { |
| 235 |
availability_target: 95.0, |
| 236 |
performance_targets: PerformanceTargets { |
| 237 |
response_time_p50_ms: 200, |
| 238 |
response_time_p95_ms: 500, |
| 239 |
response_time_p99_ms: 1000, |
| 240 |
throughput_minimum_mbps: 1.0, |
| 241 |
concurrent_connections: 10, |
| 242 |
}, |
| 243 |
data_protection: DataProtection { |
| 244 |
backup_frequency_hours: 24, |
| 245 |
backup_retention_days: 30, |
| 246 |
geo_redundancy: false, |
| 247 |
encryption_level: EncryptionLevel::Standard, |
| 248 |
compliance_standards: vec![], |
| 249 |
}, |
| 250 |
monitoring_level: MonitoringLevel::Basic, |
| 251 |
}, |
| 252 |
enabled: true, |
| 253 |
}); |
| 254 |
|
| 255 |
// Standard Tier - for balanced contributors |
| 256 |
tiers.insert(QualityTier::Standard, TierConfiguration { |
| 257 |
tier: QualityTier::Standard, |
| 258 |
name: "Standard Service".to_string(), |
| 259 |
description: "Reliable storage and bandwidth with good performance guarantees".to_string(), |
| 260 |
requirements: TierRequirements { |
| 261 |
min_contribution_score: 1.0, |
| 262 |
min_priority_level: PriorityLevel::Balanced, |
| 263 |
min_storage_contribution_gb: 50, |
| 264 |
min_account_age_days: 7, |
| 265 |
min_uptime_percentage: 95.0, |
| 266 |
}, |
| 267 |
benefits: TierBenefits { |
| 268 |
storage_multiplier: 1.0, // Can use 100% of what they contribute |
| 269 |
bandwidth_multiplier: 1.0, |
| 270 |
allocation_priority: 500, |
| 271 |
sla_guarantees: SLAGuarantees { |
| 272 |
uptime_guarantee_percent: 99.0, |
| 273 |
max_latency_ms: 200, |
| 274 |
min_throughput_mbps: 10.0, |
| 275 |
data_durability_percent: 99.99, |
| 276 |
recovery_time_objective_hours: 4, |
| 277 |
recovery_point_objective_hours: 1, |
| 278 |
}, |
| 279 |
features: vec![TierFeature::AdvancedMonitoring], |
| 280 |
support_level: SupportTier::Standard, |
| 281 |
}, |
| 282 |
service_level: ServiceLevel { |
| 283 |
availability_target: 99.0, |
| 284 |
performance_targets: PerformanceTargets { |
| 285 |
response_time_p50_ms: 100, |
| 286 |
response_time_p95_ms: 200, |
| 287 |
response_time_p99_ms: 500, |
| 288 |
throughput_minimum_mbps: 10.0, |
| 289 |
concurrent_connections: 50, |
| 290 |
}, |
| 291 |
data_protection: DataProtection { |
| 292 |
backup_frequency_hours: 12, |
| 293 |
backup_retention_days: 90, |
| 294 |
geo_redundancy: true, |
| 295 |
encryption_level: EncryptionLevel::Enhanced, |
| 296 |
compliance_standards: vec![ComplianceStandard::GDPR], |
| 297 |
}, |
| 298 |
monitoring_level: MonitoringLevel::Standard, |
| 299 |
}, |
| 300 |
enabled: true, |
| 301 |
}); |
| 302 |
|
| 303 |
// Premium Tier - for surplus contributors |
| 304 |
tiers.insert(QualityTier::Premium, TierConfiguration { |
| 305 |
tier: QualityTier::Premium, |
| 306 |
name: "Premium Service".to_string(), |
| 307 |
description: "High-performance storage with premium support and features".to_string(), |
| 308 |
requirements: TierRequirements { |
| 309 |
min_contribution_score: 1.5, |
| 310 |
min_priority_level: PriorityLevel::Surplus, |
| 311 |
min_storage_contribution_gb: 200, |
| 312 |
min_account_age_days: 30, |
| 313 |
min_uptime_percentage: 98.0, |
| 314 |
}, |
| 315 |
benefits: TierBenefits { |
| 316 |
storage_multiplier: 1.5, // Can use 150% of what they contribute |
| 317 |
bandwidth_multiplier: 1.5, |
| 318 |
allocation_priority: 800, |
| 319 |
sla_guarantees: SLAGuarantees { |
| 320 |
uptime_guarantee_percent: 99.5, |
| 321 |
max_latency_ms: 100, |
| 322 |
min_throughput_mbps: 50.0, |
| 323 |
data_durability_percent: 99.999, |
| 324 |
recovery_time_objective_hours: 1, |
| 325 |
recovery_point_objective_hours: 0, |
| 326 |
}, |
| 327 |
features: vec![ |
| 328 |
TierFeature::PrioritySupport, |
| 329 |
TierFeature::AdvancedMonitoring, |
| 330 |
TierFeature::GeographicReplication, |
| 331 |
TierFeature::EnhancedAPI, |
| 332 |
], |
| 333 |
support_level: SupportTier::Premium, |
| 334 |
}, |
| 335 |
service_level: ServiceLevel { |
| 336 |
availability_target: 99.5, |
| 337 |
performance_targets: PerformanceTargets { |
| 338 |
response_time_p50_ms: 50, |
| 339 |
response_time_p95_ms: 100, |
| 340 |
response_time_p99_ms: 200, |
| 341 |
throughput_minimum_mbps: 50.0, |
| 342 |
concurrent_connections: 200, |
| 343 |
}, |
| 344 |
data_protection: DataProtection { |
| 345 |
backup_frequency_hours: 6, |
| 346 |
backup_retention_days: 365, |
| 347 |
geo_redundancy: true, |
| 348 |
encryption_level: EncryptionLevel::Enterprise, |
| 349 |
compliance_standards: vec![ |
| 350 |
ComplianceStandard::GDPR, |
| 351 |
ComplianceStandard::SOC2, |
| 352 |
ComplianceStandard::ISO27001, |
| 353 |
], |
| 354 |
}, |
| 355 |
monitoring_level: MonitoringLevel::Advanced, |
| 356 |
}, |
| 357 |
enabled: true, |
| 358 |
}); |
| 359 |
|
| 360 |
// Enterprise Tier - for generous contributors |
| 361 |
tiers.insert(QualityTier::Enterprise, TierConfiguration { |
| 362 |
tier: QualityTier::Enterprise, |
| 363 |
name: "Enterprise Service".to_string(), |
| 364 |
description: "Maximum performance with enterprise-grade guarantees and dedicated support".to_string(), |
| 365 |
requirements: TierRequirements { |
| 366 |
min_contribution_score: 2.0, |
| 367 |
min_priority_level: PriorityLevel::Generous, |
| 368 |
min_storage_contribution_gb: 1000, |
| 369 |
min_account_age_days: 90, |
| 370 |
min_uptime_percentage: 99.0, |
| 371 |
}, |
| 372 |
benefits: TierBenefits { |
| 373 |
storage_multiplier: 2.0, // Can use 200% of what they contribute |
| 374 |
bandwidth_multiplier: 2.0, |
| 375 |
allocation_priority: 1000, |
| 376 |
sla_guarantees: SLAGuarantees { |
| 377 |
uptime_guarantee_percent: 99.9, |
| 378 |
max_latency_ms: 50, |
| 379 |
min_throughput_mbps: 100.0, |
| 380 |
data_durability_percent: 99.9999, |
| 381 |
recovery_time_objective_hours: 0, |
| 382 |
recovery_point_objective_hours: 0, |
| 383 |
}, |
| 384 |
features: vec![ |
| 385 |
TierFeature::PrioritySupport, |
| 386 |
TierFeature::AdvancedMonitoring, |
| 387 |
TierFeature::GeographicReplication, |
| 388 |
TierFeature::CustomSLA, |
| 389 |
TierFeature::EnhancedAPI, |
| 390 |
TierFeature::BetaFeatures, |
| 391 |
TierFeature::DedicatedAccountManager, |
| 392 |
], |
| 393 |
support_level: SupportTier::Enterprise, |
| 394 |
}, |
| 395 |
service_level: ServiceLevel { |
| 396 |
availability_target: 99.9, |
| 397 |
performance_targets: PerformanceTargets { |
| 398 |
response_time_p50_ms: 25, |
| 399 |
response_time_p95_ms: 50, |
| 400 |
response_time_p99_ms: 100, |
| 401 |
throughput_minimum_mbps: 100.0, |
| 402 |
concurrent_connections: 1000, |
| 403 |
}, |
| 404 |
data_protection: DataProtection { |
| 405 |
backup_frequency_hours: 1, |
| 406 |
backup_retention_days: 2555, // 7 years |
| 407 |
geo_redundancy: true, |
| 408 |
encryption_level: EncryptionLevel::Enterprise, |
| 409 |
compliance_standards: vec![ |
| 410 |
ComplianceStandard::GDPR, |
| 411 |
ComplianceStandard::HIPAA, |
| 412 |
ComplianceStandard::SOC2, |
| 413 |
ComplianceStandard::ISO27001, |
| 414 |
ComplianceStandard::PCI_DSS, |
| 415 |
], |
| 416 |
}, |
| 417 |
monitoring_level: MonitoringLevel::Enterprise, |
| 418 |
}, |
| 419 |
enabled: true, |
| 420 |
}); |
| 421 |
|
| 422 |
Self { |
| 423 |
tiers, |
| 424 |
user_tiers: HashMap::new(), |
| 425 |
tier_metrics: HashMap::new(), |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
/// Assign appropriate tier based on user contribution |
| 430 |
pub async fn assign_user_tier(&mut self, user_id: String, contribution_score: f64, priority_level: PriorityLevel, storage_contribution_gb: u64, account_age_days: u32, uptime_percentage: f64) -> Result<QualityTier> { |
| 431 |
|
| 432 |
// Find the highest tier the user qualifies for |
| 433 |
let qualified_tier = if self.meets_requirements(&QualityTier::Enterprise, contribution_score, &priority_level, storage_contribution_gb, account_age_days, uptime_percentage) { |
| 434 |
QualityTier::Enterprise |
| 435 |
} else if self.meets_requirements(&QualityTier::Premium, contribution_score, &priority_level, storage_contribution_gb, account_age_days, uptime_percentage) { |
| 436 |
QualityTier::Premium |
| 437 |
} else if self.meets_requirements(&QualityTier::Standard, contribution_score, &priority_level, storage_contribution_gb, account_age_days, uptime_percentage) { |
| 438 |
QualityTier::Standard |
| 439 |
} else { |
| 440 |
QualityTier::Basic |
| 441 |
}; |
| 442 |
|
| 443 |
// Get current assignment if exists |
| 444 |
let previous_tier = self.user_tiers.get(&user_id).map(|assignment| assignment.assigned_tier.clone()); |
| 445 |
|
| 446 |
// Create or update tier assignment |
| 447 |
let benefits = self.tiers.get(&qualified_tier).unwrap().benefits.clone(); |
| 448 |
|
| 449 |
let assignment = UserTierAssignment { |
| 450 |
user_id: user_id.clone(), |
| 451 |
assigned_tier: qualified_tier.clone(), |
| 452 |
assigned_at: if previous_tier.is_some() { |
| 453 |
self.user_tiers.get(&user_id).unwrap().assigned_at |
| 454 |
} else { |
| 455 |
Utc::now() |
| 456 |
}, |
| 457 |
last_evaluated: Utc::now(), |
| 458 |
next_evaluation: Utc::now() + chrono::Duration::days(7), // Re-evaluate weekly |
| 459 |
tier_history: { |
| 460 |
let mut history = self.user_tiers.get(&user_id) |
| 461 |
.map(|a| a.tier_history.clone()) |
| 462 |
.unwrap_or_default(); |
| 463 |
|
| 464 |
if previous_tier != Some(qualified_tier.clone()) { |
| 465 |
history.push(TierChange { |
| 466 |
from_tier: previous_tier, |
| 467 |
to_tier: qualified_tier.clone(), |
| 468 |
reason: format!("Contribution score: {:.2}, Storage: {}GB", contribution_score, storage_contribution_gb), |
| 469 |
changed_at: Utc::now(), |
| 470 |
}); |
| 471 |
} |
| 472 |
history |
| 473 |
}, |
| 474 |
current_benefits: benefits, |
| 475 |
}; |
| 476 |
|
| 477 |
self.user_tiers.insert(user_id, assignment); |
| 478 |
|
| 479 |
Ok(qualified_tier) |
| 480 |
} |
| 481 |
|
| 482 |
/// Check if user meets tier requirements |
| 483 |
fn meets_requirements(&self, tier: &QualityTier, contribution_score: f64, priority_level: &PriorityLevel, storage_contribution_gb: u64, account_age_days: u32, uptime_percentage: f64) -> bool { |
| 484 |
if let Some(tier_config) = self.tiers.get(tier) { |
| 485 |
let reqs = &tier_config.requirements; |
| 486 |
|
| 487 |
contribution_score >= reqs.min_contribution_score && |
| 488 |
storage_contribution_gb >= reqs.min_storage_contribution_gb && |
| 489 |
account_age_days >= reqs.min_account_age_days && |
| 490 |
uptime_percentage >= reqs.min_uptime_percentage && |
| 491 |
self.priority_level_meets_requirement(priority_level, &reqs.min_priority_level) |
| 492 |
} else { |
| 493 |
false |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
fn priority_level_meets_requirement(&self, user_level: &PriorityLevel, required_level: &PriorityLevel) -> bool { |
| 498 |
let user_score = match user_level { |
| 499 |
PriorityLevel::Deficit => 0, |
| 500 |
PriorityLevel::Balanced => 1, |
| 501 |
PriorityLevel::Surplus => 2, |
| 502 |
PriorityLevel::Generous => 3, |
| 503 |
}; |
| 504 |
|
| 505 |
let required_score = match required_level { |
| 506 |
PriorityLevel::Deficit => 0, |
| 507 |
PriorityLevel::Balanced => 1, |
| 508 |
PriorityLevel::Surplus => 2, |
| 509 |
PriorityLevel::Generous => 3, |
| 510 |
}; |
| 511 |
|
| 512 |
user_score >= required_score |
| 513 |
} |
| 514 |
|
| 515 |
/// Get user's current tier assignment |
| 516 |
pub fn get_user_tier(&self, user_id: &str) -> Option<&UserTierAssignment> { |
| 517 |
self.user_tiers.get(user_id) |
| 518 |
} |
| 519 |
|
| 520 |
/// Get tier configuration |
| 521 |
pub fn get_tier_config(&self, tier: &QualityTier) -> Option<&TierConfiguration> { |
| 522 |
self.tiers.get(tier) |
| 523 |
} |
| 524 |
|
| 525 |
/// Get all available tiers |
| 526 |
pub fn get_available_tiers(&self) -> Vec<&TierConfiguration> { |
| 527 |
self.tiers.values().filter(|t| t.enabled).collect() |
| 528 |
} |
| 529 |
|
| 530 |
/// Update tier metrics |
| 531 |
pub async fn update_tier_metrics(&mut self) -> Result<()> { |
| 532 |
// Count users per tier and calculate metrics |
| 533 |
for tier in [QualityTier::Basic, QualityTier::Standard, QualityTier::Premium, QualityTier::Enterprise] { |
| 534 |
let active_users = self.user_tiers.values() |
| 535 |
.filter(|assignment| assignment.assigned_tier == tier) |
| 536 |
.count() as u32; |
| 537 |
|
| 538 |
let metrics = TierMetrics { |
| 539 |
tier: tier.clone(), |
| 540 |
active_users, |
| 541 |
average_satisfaction_score: 85.0, // Mock data - would be calculated from user feedback |
| 542 |
sla_compliance_percent: 99.2, // Mock data - would be calculated from actual SLA metrics |
| 543 |
average_resource_utilization: 75.0, // Mock data |
| 544 |
support_ticket_count: active_users / 10, // Mock ratio |
| 545 |
last_updated: Utc::now(), |
| 546 |
}; |
| 547 |
|
| 548 |
self.tier_metrics.insert(tier, metrics); |
| 549 |
} |
| 550 |
|
| 551 |
Ok(()) |
| 552 |
} |
| 553 |
|
| 554 |
/// Get tier metrics |
| 555 |
pub fn get_tier_metrics(&self, tier: &QualityTier) -> Option<&TierMetrics> { |
| 556 |
self.tier_metrics.get(tier) |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
impl Default for QualityTierManager { |
| 561 |
fn default() -> Self { |
| 562 |
Self::new() |
| 563 |
} |
| 564 |
} |