From e41ae71585a432d21cc16c109d2858f9e1d8e22b Mon Sep 17 00:00:00 2001 From: Chris Guimaraes Date: Thu, 22 Dec 2022 21:56:38 +0000 Subject: [PATCH] feat: add percentage to ::get_costs function --- components/clarity-repl/src/repl/session.rs | 29 ++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/components/clarity-repl/src/repl/session.rs b/components/clarity-repl/src/repl/session.rs index 23f1dceed..3044fd080 100644 --- a/components/clarity-repl/src/repl/session.rs +++ b/components/clarity-repl/src/repl/session.rs @@ -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)); @@ -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,