From bdd00a14e38039a062572d71ffb1dac2ed20ef48 Mon Sep 17 00:00:00 2001 From: Maxime Dufour Date: Thu, 9 Feb 2023 09:32:24 +0000 Subject: [PATCH 1/2] Add Annual cost estimation format --- src/args.rs | 5 +++++ src/core.rs | 4 ++++ src/main.rs | 1 + 3 files changed, 10 insertions(+) diff --git a/src/args.rs b/src/args.rs index e048a65..59c9996 100644 --- a/src/args.rs +++ b/src/args.rs @@ -47,6 +47,7 @@ pub enum InputSource { pub enum OutputFormat { Hour, Month, + Year, Json, Csv, Ods, @@ -83,6 +84,10 @@ impl Args { error!("cannot aggregate with month format"); 1 } + (true, OutputFormat::Year) => { + error!("cannot aggregate with annual format"); + 1 + } }; err_count += match (&self.output, &self.format) { (Some(_), _) => 0, diff --git a/src/core.rs b/src/core.rs index c32d78b..8405d51 100644 --- a/src/core.rs +++ b/src/core.rs @@ -148,6 +148,10 @@ impl Resources { Ok(self.cost_per_hour()? * HOURS_PER_MONTH) } + pub fn cost_per_year(&self) -> Result { + Ok(self.cost_per_hour()? * HOURS_PER_MONTH * 12.0) + } + pub fn json(&self) -> serde_json::Result { let mut out = String::new(); for resource in &self.resources { diff --git a/src/main.rs b/src/main.rs index 3f0bbe2..274b929 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,7 @@ fn main() -> Result<(), Box> { let output: Vec = match args.format { OutputFormat::Hour => format!("{}", resources.cost_per_hour()?).into_bytes(), OutputFormat::Month => format!("{}", resources.cost_per_month()?).into_bytes(), + OutputFormat::Year => format!("{}", resources.cost_per_year()?).into_bytes(), OutputFormat::Json => resources.json()?.into_bytes(), OutputFormat::Csv => resources.csv()?.into_bytes(), OutputFormat::Ods => resources.ods()?, From 838770e269fedeb78d24655c0d38ab6e89dadd89 Mon Sep 17 00:00:00 2001 From: Maxime Dufour Date: Thu, 9 Feb 2023 09:34:12 +0000 Subject: [PATCH 2/2] Update human format to include year cost estimation --- docs/README.md | 36 ++++++++++++++++++++++-------------- src/core.rs | 16 ++++++++++++++-- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/docs/README.md b/docs/README.md index 267856b..38a4061 100644 --- a/docs/README.md +++ b/docs/README.md @@ -58,25 +58,33 @@ Here are few examples with different output formats. Note that `json` format wil osc-cost --format=human # default Summary: ╭───────────────────────┬──────────────╮ -│ Account Id ┆ 865343815873 │ +│ Account Id ┆ 620346218618 │ ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ -│ Total price per hour ┆ 0.27983564$ │ +│ Total price per hour ┆ 2.2062643€ │ ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ -│ Total price per month ┆ 204.28001$ │ +│ Total price per month ┆ 1610.5729€ │ +├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ +│ Total price per year ┆ 19326.875€ │ ╰───────────────────────┴──────────────╯ Details: -╭───────────────┬───────┬──────────────────────┬───────────────────────╮ -│ Resource Type ┆ Count ┆ Total price per hour ┆ Total price per month │ -╞═══════════════╪═══════╪══════════════════════╪═══════════════════════╡ -│ Volume ┆ 1 ┆ 0.015068493$ ┆ 11$ │ -├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ -│ PublicIp ┆ 3 ┆ 0.01$ ┆ 7.2999997$ │ -├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ -│ Snapshot ┆ 46 ┆ 0.05876714$ ┆ 42.900017$ │ -├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ -│ Vm ┆ 1 ┆ 0.19600001$ ┆ 143.08$ │ -╰───────────────┴───────┴──────────────────────┴───────────────────────╯ +╭───────────────┬───────┬──────────────────────┬───────────────────────┬──────────────────────╮ +│ Resource Type ┆ Count ┆ Total price per hour ┆ Total price per month ┆ Total price per year │ +╞═══════════════╪═══════╪══════════════════════╪═══════════════════════╪══════════════════════╡ +│ Snapshot ┆ 23 ┆ 0.03164384€ ┆ 23.1€ ┆ 277.2€ │ +├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ +│ Vm ┆ 9 ┆ 1.7939999€ ┆ 1309.6199€ ┆ 15715.438€ │ +├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ +│ LoadBalancer ┆ 2 ┆ 0.06€ ┆ 43.8€ ┆ 525.6€ │ +├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ +│ NatServices ┆ 2 ┆ 0.1€ ┆ 73€ ┆ 876€ │ +├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ +│ Volume ┆ 12 ┆ 0.20554796€ ┆ 150.04999€ ┆ 1800.5999€ │ +├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ +│ PublicIp ┆ 7 ┆ 0.015€ ┆ 10.95€ ┆ 131.4€ │ +├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ +│ Oos ┆ 5 ┆ 0.00007237231€ ┆ 0.05283179€ ┆ 0.63398147€ │ +╰───────────────┴───────┴──────────────────────┴───────────────────────┴──────────────────────╯ ``` ``` diff --git a/src/core.rs b/src/core.rs index 8405d51..8c4ebf1 100644 --- a/src/core.rs +++ b/src/core.rs @@ -199,12 +199,13 @@ impl Resources { .load_preset(UTF8_FULL) .apply_modifier(UTF8_ROUND_CORNERS) .set_content_arrangement(ContentArrangement::Dynamic) - .set_width(80) + .set_width(100) .set_header(vec![ "Resource Type", "Count", "Total price per hour", "Total price per month", + "Total price per year", ]); for resource in self.resources.iter() { @@ -236,6 +237,13 @@ impl Resources { .ok_or("could not get the price_per_month")?, currency ), + format!( + "{}{}", + agg.price_per_month + .ok_or("could not get the price_per_year")? + * 12.0, + currency + ), ]) } _ => { @@ -249,7 +257,7 @@ impl Resources { .load_preset(UTF8_FULL) .apply_modifier(UTF8_ROUND_CORNERS) .set_content_arrangement(ContentArrangement::Dynamic) - .set_width(80) + .set_width(100) .add_row(vec![Cell::new("Account Id"), Cell::new(account_id)]) .add_row(vec![ Cell::new("Total price per hour"), @@ -258,6 +266,10 @@ impl Resources { .add_row(vec![ Cell::new("Total price per month"), Cell::new(format!("{}{}", self.cost_per_month()?, currency)), + ]) + .add_row(vec![ + Cell::new("Total price per year"), + Cell::new(format!("{}{}", self.cost_per_year()?, currency)), ]); Ok(format!("Summary:\n{table}\n\nDetails:\n{table_resource}"))