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

Minor fixes and CI cleanup #20

Merged
merged 7 commits into from
Feb 18, 2024
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
5 changes: 3 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ on:
tags:
- v*
branches:
- master
- main
pull_request:
workflow_dispatch:

name: CI

Expand All @@ -28,7 +29,7 @@ jobs:
env:
RUST_BACKTRACE: "1"
steps:
- uses: actions/checkout@master
- uses: actions/checkout@main
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
Expand Down
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/target
debug/
target/

.vs/
.vscode/
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "smag"
version = "0.6.0"
authors = ["Natan Yellin", "Tom Forbes <tom@tomforb.es>"]
version = "0.7.0"
authors = ["Natan Yellin", "Tom Forbes <tom@tomforb.es>", "Simon Vetter"]
edition = "2018"
repository = "https://github.com/orf/gping"
repository = "https://github.com/aantn/smag"
license = "MIT"
description = "Show Me A Graph - Command Line Graph Tool"

Expand Down
64 changes: 26 additions & 38 deletions src/datastore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use tui::text::Span;
pub struct DataStore {
pub styles: Vec<Style>,
pub data: Vec<ringbuffer::FixedRingBuffer<(f64, f64)>>,
args : Args,
args: Args,
window_min: Vec<f64>,
window_max: Vec<f64>,
}

impl DataStore {
pub fn new(args : Args) -> Self {
pub fn new(args: Args) -> Self {
let host_count = args.cmds.len();
DataStore {
styles: (0..host_count)
Expand All @@ -24,7 +24,7 @@ impl DataStore {
.collect(),
window_min: vec![0.0; host_count],
window_max: vec![args.buffer_size as f64; host_count],
args: args,
args,
}
}
pub fn update(&mut self, cmd_index: usize, x_index: u64, item: Option<f64>) {
Expand Down Expand Up @@ -71,14 +71,9 @@ impl DataStore {
]
}
pub fn y_axis_bounds(&self, chart_height: i32) -> ([f64; 2], i32) {
let iter = self
.data
.iter()
.map(|b| b.as_slice())
.flatten()
.map(|v| v.1);
let iter = self.data.iter().flat_map(|b| b.as_slice()).map(|v| v.1);
let min = iter.clone().fold(f64::INFINITY, |a, b| a.min(b));
let max = iter .fold(0f64, |a, b| a.max(b));
let max = iter.fold(0f64, |a, b| a.max(b));
let range = max - min;

// Parameters for automatic range and tick placement algorithm
Expand All @@ -91,31 +86,33 @@ impl DataStore {
} else {
2.0
};
let target_num_ticks : f64 = ((chart_height - 1) as f64 / target_lines_per_tick).max(2.0);
let preferred_increment : f64 = range_buffered / target_num_ticks;
let log10_times3 : i32 = (preferred_increment.log10() * 3.0).round() as i32;
let exponent : i32 = log10_times3 / 3;
let mut increment : f64 = 10_f64.powf(exponent as f64);
let target_num_ticks: f64 = ((chart_height - 1) as f64 / target_lines_per_tick).max(2.0);
let preferred_increment: f64 = range_buffered / target_num_ticks;
let log10_times3: i32 = (preferred_increment.log10() * 3.0).round() as i32;
let exponent: i32 = log10_times3 / 3;
let mut increment: f64 = 10_f64.powf(exponent as f64);
// Adjust increment to a power-of-ten multiple of 1, 2 or 5
match log10_times3 % 3 {
-2 => increment /= 5.0,
-1 => increment /= 2.0,
0 => (),
1 => increment *= 2.0,
2 => increment *= 5.0,
0 => (),
1 => increment *= 2.0,
2 => increment *= 5.0,
_ => increment = 1.0,
}

// Add buffer and round out to multiples of increment
let range_buffer_per_side = if range > 0.0 {
range_buffer_percent_per_side / 100.0 * range
} else { 1.0 };
} else {
1.0
};
let min_round = ((min - range_buffer_per_side) / increment).floor() * increment;
let max_round = ((max + range_buffer_per_side) / increment).ceil() * increment;
let max_round = ((max + range_buffer_per_side) / increment).ceil() * increment;
// Calculate number of ticks
let mut num_ticks : i32 = ((max_round - min_round) / increment).round() as i32 + 1;
if (((chart_height - 1) as f64 / num_ticks as f64)) < (0.3 * target_lines_per_tick) {
let mut num_ticks: i32 = ((max_round - min_round) / increment).round() as i32 + 1;

if ((chart_height - 1) as f64 / num_ticks as f64) < (0.3 * target_lines_per_tick) {
// Ticks are too close together, keep only min and max
num_ticks = 2;
}
Expand All @@ -124,20 +121,11 @@ impl DataStore {
}

fn format_tick(&self, increment: f64, value: f64) -> String {
if increment > 1.0 {
if increment >= 1.0 {
format!("{:.0}", value)
} else if increment < 1.0 && increment >= 0.1 {
format!("{:.1}", value)
} else if increment < 0.1 && increment >= 0.01 {
format!("{:.2}", value)
} else if increment < 0.01 && increment >= 0.001 {
format!("{:.3}", value)
} else if increment < 0.001 && increment >= 0.0001 {
format!("{:.4}", value)
} else if increment < 0.0001 && increment >= 0.00001 {
format!("{:.5}", value)
} else {
format!("{}", value)
let precision: usize = increment.log10().abs().ceil() as usize;
format!("{:.precision$}", value)
}
}

Expand All @@ -149,10 +137,10 @@ impl DataStore {

let y_label = &self.args.y_label;
let mut suffix = String::new();
suffix.push_str(" ");
suffix.push(' ');
if !y_label.is_empty() {
suffix.push_str(&y_label);
suffix.push_str(" ");
suffix.push_str(y_label);
suffix.push(' ');
}

(0..num_ticks)
Expand Down
4 changes: 2 additions & 2 deletions src/ringbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::fmt::Debug;
#[derive(Debug)]
pub struct FixedRingBuffer<T> {
buf: Vec<T>,
cap: usize, // Buffer size
head: usize, // Index of most recent value
cap: usize, // Buffer size
head: usize, // Index of most recent value
}

impl<T: Copy + Default> FixedRingBuffer<T> {
Expand Down
13 changes: 7 additions & 6 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,20 @@ pub fn draw_ui<T: tui::backend::Backend>(
panic!("Invalid range format. Please use 'min,max' or 'min,max,increment'");
}

let min : f64 = parts.next().unwrap().parse().unwrap();
let max : f64 = parts.next().unwrap().parse().unwrap();
let min: f64 = parts.next().unwrap().parse().unwrap();
let max: f64 = parts.next().unwrap().parse().unwrap();
if min >= max {
panic!("Invalid range format. Make sure min is less than max.");
}
let increment : f64 = if num_args == 3 {
let increment: f64 = if num_args == 3 {
parts.next().unwrap().parse().unwrap()
} else {
let target_lines_per_tick = 6.0;
let target_num_ticks : f64 = (chart_height - 1) as f64 / target_lines_per_tick;
let target_num_ticks: f64 = (chart_height - 1) as f64 / target_lines_per_tick;
(max - min) / target_num_ticks
}.min(max - min); // Make sure increment is not greater than range
let num_ticks : i32 = ((max - min) / increment).round() as i32 + 1;
}
.min(max - min); // Make sure increment is not greater than range
let num_ticks: i32 = ((max - min) / increment).round() as i32 + 1;

([min, max], num_ticks)
};
Expand Down
Loading