Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add annual cost estimation format #80

Merged
merged 2 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,33 @@ Here are few examples with different output formats. Note that `json` format wil
osc-cost --format=human # default
Summary:
╭───────────────────────┬──────────────╮
Account Id865343815873
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€ │
╰───────────────┴───────┴──────────────────────┴───────────────────────┴──────────────────────╯
```

```
Expand Down
5 changes: 5 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub enum InputSource {
pub enum OutputFormat {
Hour,
Month,
Year,
Json,
Csv,
Ods,
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 18 additions & 2 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ impl Resources {
Ok(self.cost_per_hour()? * HOURS_PER_MONTH)
}

pub fn cost_per_year(&self) -> Result<f32, ResourceError> {
Ok(self.cost_per_hour()? * HOURS_PER_MONTH * 12.0)
}

pub fn json(&self) -> serde_json::Result<String> {
let mut out = String::new();
for resource in &self.resources {
Expand Down Expand Up @@ -195,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() {
Expand Down Expand Up @@ -232,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
),
])
}
_ => {
Expand All @@ -245,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"),
Expand All @@ -254,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}"))
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let output: Vec<u8> = 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()?,
Expand Down