Skip to content

Commit

Permalink
fix: price estimate
Browse files Browse the repository at this point in the history
  • Loading branch information
pxseu committed May 27, 2023
1 parent 80e0381 commit 6c5a998
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 22 deletions.
5 changes: 1 addition & 4 deletions src/commands/ignite/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 17 additions & 8 deletions src/commands/ignite/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,23 @@ 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
};

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()
Expand Down Expand Up @@ -871,23 +878,25 @@ pub fn get_price_estimate(
) -> Result<String> {
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;
}
}

Expand Down
64 changes: 54 additions & 10 deletions src/commands/projects/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)))
}
}
Expand Down

0 comments on commit 6c5a998

Please sign in to comment.