diff --git a/src/commands/ignite/types.rs b/src/commands/ignite/types.rs index a1f08cc..1971b25 100644 --- a/src/commands/ignite/types.rs +++ b/src/commands/ignite/types.rs @@ -466,11 +466,8 @@ impl Display for StorageUsage { /// 512 -> 512MB /// 1 -> 1MB fn get_size(size: u64) -> String { - // really silly but matches dont like simple expressions - const LESS_THAN_KB: u64 = unit_multiplier::KB - 1; - match size { - 1..=LESS_THAN_KB => format!("{}MB", size), + 1..=unit_multiplier::KB => format!("{}MB", size), _ => { format!("{:.2}GB", size as f64 / unit_multiplier::KB as f64) diff --git a/src/commands/ignite/utils.rs b/src/commands/ignite/utils.rs index 1676d69..df67458 100644 --- a/src/commands/ignite/utils.rs +++ b/src/commands/ignite/utils.rs @@ -244,8 +244,8 @@ pub async fn update_deployment_config( } else { let (applied, billabe) = quota.get_free_tier_billable(&config.resources, &config.volume)?; - if !applied { - log::warn!("Free tier has been applied to some of the resources"); + if applied { + log::warn!("Free tier has been applied to the resources"); } billabe @@ -253,7 +253,14 @@ pub async fn update_deployment_config( let price = get_price_estimate(&skus, &resources, &volume_size)?; - log::info!("Estimated monthly price per container: {price}$"); + log::info!( + "Estimated monthly price{}: {price}$", + if config.type_ == Some(ContainerType::Stateful) { + "" + } else { + " per container" + } + ); if is_visual && !dialoguer::Confirm::new() @@ -871,23 +878,25 @@ pub fn get_price_estimate( ) -> Result { let mut total = 0.0; - for sku in skus.iter().filter(|sku| sku.id.starts_with("ignite_")) { + for sku in skus.iter().filter(|sku| sku.product == "ignite") { let mut price = sku.price; match sku.id.as_str() { - "ignite_cpu_per_min" => { + "ignite_vcpu_per_min" => { price *= resources.vcpu; } // per 100 MB "ignite_ram_per_min" => { - price *= (parse_size(&resources.ram)? / (100 * unit_multiplier::MB)) as f64; + price *= parse_size(&resources.ram)? as f64; + price /= (100 * unit_multiplier::MB) as f64; } - // per 100 MB + // per 1MB "ignite_volume_per_min" => { if let Some(size) = &volume { - price *= (parse_size(size)? / (100 * unit_multiplier::MB)) as f64; + price *= parse_size(size)? as f64; + price /= unit_multiplier::MB as f64; } } diff --git a/src/commands/projects/types.rs b/src/commands/projects/types.rs index 37edce7..4f626d7 100644 --- a/src/commands/projects/types.rs +++ b/src/commands/projects/types.rs @@ -65,6 +65,18 @@ pub struct Quotas { pub usage: Quota, } +impl std::ops::Sub for Quota { + type Output = Quota; + + fn sub(self, rhs: Self) -> Self::Output { + Quota { + vcpu: self.vcpu - rhs.vcpu, + ram: self.ram - rhs.ram, + volume: self.volume - rhs.volume, + } + } +} + impl Quotas { pub fn total_quota(&self) -> Quota { Quota { @@ -145,30 +157,62 @@ impl Quotas { let mut billable_resources = Resources::default(); let mut billable_volume = None; - let free = self.free_quota(); - let usage = self.usage_quota(); + let left_free = self.free_quota() - self.usage_quota(); - if usage.vcpu + resources.vcpu > free.vcpu { - billable_resources.vcpu = usage.vcpu + resources.vcpu - free.vcpu; + if left_free.vcpu > 0f64 { free_tier_applicable = true; + billable_resources.vcpu = if resources.vcpu > left_free.vcpu { + resources.vcpu - left_free.vcpu + } else { + 0f64 + }; + } else { + billable_resources.vcpu = resources.vcpu; } - let ram = parse_size(&resources.ram)?; + if left_free.ram > 0 { + let ram = parse_size(&resources.ram)?; - if usage.ram + ram > free.ram { - billable_resources.ram = format!("{}B", usage.ram + ram - free.ram); free_tier_applicable = true; + billable_resources.ram = format!( + "{}B", + if ram > left_free.ram { + ram - left_free.ram + } else { + 0 + } + ); + + log::debug!("{} {}", left_free.ram, ram); + } else { + billable_resources.ram = resources.ram.clone(); } if let Some(volume) = volume { - let volume = parse_size(&volume.size)?; + if left_free.volume > 0 { + let volume = parse_size(&volume.size)?; - if usage.volume + volume > free.volume { - billable_volume = Some(format!("{}B", usage.volume + volume - free.volume)); free_tier_applicable = true; + billable_volume = Some(format!( + "{}B", + if volume > left_free.volume { + volume - left_free.volume + } else { + 0 + } + )); + } else { + billable_volume = Some(volume.size.clone()); } } + log::debug!( + "free_tier_applicable: {}, billable_resources: {:?}, billable_volume: {:?}", + free_tier_applicable, + billable_resources, + billable_volume + ); + Ok((free_tier_applicable, (billable_resources, billable_volume))) } }