Skip to content

Commit

Permalink
feat: add percentage to ::get_costs function
Browse files Browse the repository at this point in the history
  • Loading branch information
csgui committed Dec 26, 2022
1 parent 69af1ac commit e41ae71
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion components/clarity-repl/src/repl/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,12 @@ impl Session {
};

if let Some(cost) = cost {
let headers = vec!["".to_string(), "Consumed".to_string(), "Limit".to_string()];
let headers = vec![
"".to_string(),
"Consumed".to_string(),
"Limit".to_string(),
"Percentage".to_string(),
];
let mut headers_cells = vec![];
for header in headers.iter() {
headers_cells.push(Cell::new(&header));
Expand All @@ -316,32 +321,54 @@ impl Session {
Cell::new("Runtime"),
Cell::new(&cost.total.runtime.to_string()),
Cell::new(&cost.limit.runtime.to_string()),
Cell::new(&(Self::get_costs_percentage(&cost.total.runtime, &cost.limit.runtime))),
]));
table.add_row(Row::new(vec![
Cell::new("Read count"),
Cell::new(&cost.total.read_count.to_string()),
Cell::new(&cost.limit.read_count.to_string()),
Cell::new(
&(Self::get_costs_percentage(&cost.total.read_count, &cost.limit.read_count)),
),
]));
table.add_row(Row::new(vec![
Cell::new("Read length (bytes)"),
Cell::new(&cost.total.read_length.to_string()),
Cell::new(&cost.limit.read_length.to_string()),
Cell::new(
&(Self::get_costs_percentage(&cost.total.read_length, &cost.limit.read_length)),
),
]));
table.add_row(Row::new(vec![
Cell::new("Write count"),
Cell::new(&cost.total.write_count.to_string()),
Cell::new(&cost.limit.write_count.to_string()),
Cell::new(
&(Self::get_costs_percentage(&cost.total.write_count, &cost.limit.write_count)),
),
]));
table.add_row(Row::new(vec![
Cell::new("Write length (bytes)"),
Cell::new(&cost.total.write_length.to_string()),
Cell::new(&cost.limit.write_length.to_string()),
Cell::new(
&(Self::get_costs_percentage(
&cost.total.write_length,
&cost.limit.write_length,
)),
),
]));
output.push(format!("{}", table));
}
output.append(&mut result);
}

fn get_costs_percentage(consumed: &u64, limit: &u64) -> String {
let calc = (*consumed as f64 / *limit as f64) * 100_f64;

format!("{calc:.2} %")
}

pub fn formatted_interpretation<'a, 'hooks>(
&'a mut self,
snippet: String,
Expand Down

0 comments on commit e41ae71

Please sign in to comment.