diff options
-rw-r--r-- | src/gatherer.rs | 17 | ||||
-rw-r--r-- | src/griph/mod.rs | 32 | ||||
-rwxr-xr-x | src/ifc.rs | 112 | ||||
-rwxr-xr-x | src/main.rs | 1 |
4 files changed, 38 insertions, 124 deletions
diff --git a/src/gatherer.rs b/src/gatherer.rs index 53e46f1..c03a0a5 100644 --- a/src/gatherer.rs +++ b/src/gatherer.rs @@ -12,7 +12,7 @@ use time::OffsetDateTime; use crate::{ db::DbMeminfo, - griph::{self, Style}, + griph::{self, Style, TwoLineOrder}, AwakeState, }; @@ -159,6 +159,9 @@ pub fn make_net_graph(state: &AwakeState) { .map(|m| m.map(|n| n.tx_bytes_per_sec() as usize / 125).unwrap_or(0)) .collect(); + let rx_sum = rx_zeroed.iter().fold(0, |acc, x| acc + x); + let tx_sum = tx_zeroed.iter().fold(0, |acc, x| acc + x); + // Mixing the TX/RX delta so we can pick a range. let mut mixed = vec![0; 512]; mixed[..256].copy_from_slice(&rx_zeroed); @@ -166,12 +169,20 @@ pub fn make_net_graph(state: &AwakeState) { mixed.sort(); let kinda_highest = mixed[511 - 32]; - let high_bound = (kinda_highest as f32 / 256.0).ceil().max(1.0) as usize * 256; + let high_bound = (kinda_highest as f32 / 64.0).ceil().max(1.0) as usize * 64; state .netinfo_upper_bound .store(high_bound, Ordering::Release); - let gif = griph::make_2line(0, high_bound, &tx_deltas, &rx_deltas); + let order = if tx_sum > rx_sum { + TwoLineOrder::SeriesAFirst + } else { + TwoLineOrder::SeriesBFirst + }; + + tracing::debug!("tx_sum = {tx_sum} // rx_sum = {rx_sum} // order = {order:?}"); + + let gif = griph::make_2line(0, high_bound, &tx_deltas, &rx_deltas, order); let path = state.cache_path.join("current_hostnetinfo.gif"); gif.save(path).unwrap(); diff --git a/src/griph/mod.rs b/src/griph/mod.rs index 636ffd9..377ad90 100644 --- a/src/griph/mod.rs +++ b/src/griph/mod.rs @@ -35,7 +35,7 @@ pub enum Style { pub fn make_1line(min: usize, max: usize, values: &[Option<usize>], style: Style) -> Gif { let range = max - min; // this assumes a range of values that is >1 per pixel - let vpp = range / HEIGHT; + let vpp = range as f32 / HEIGHT as f32; let mut raster = vec![0; SIZE]; let mut standard = Gif::new(WIDTH as u16, HEIGHT as u16); @@ -64,20 +64,36 @@ pub fn make_1line(min: usize, max: usize, values: &[Option<usize>], style: Style standard } +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum TwoLineOrder { + SeriesAFirst, + SeriesBFirst, +} + pub fn make_2line( min: usize, max: usize, values1: &[Option<usize>], values2: &[Option<usize>], + draw_order: TwoLineOrder, ) -> Gif { let range = max - min; // this assumes a range of values that is >1 per pixel - let vpp = range / HEIGHT; + let vpp = range as f32 / HEIGHT as f32; let mut raster = vec![0; SIZE]; draw_grid(&mut raster); - draw_line_underfill(&mut raster, values1, vpp, LINE1, LINE1_FILL); - draw_line_underfill(&mut raster, values2, vpp, LINE2, LINE2_FILL); + + match draw_order { + TwoLineOrder::SeriesAFirst => { + draw_line_underfill(&mut raster, values1, vpp, LINE1, LINE1_FILL); + draw_line_underfill(&mut raster, values2, vpp, LINE2, LINE2_FILL); + } + TwoLineOrder::SeriesBFirst => { + draw_line_underfill(&mut raster, values2, vpp, LINE2, LINE2_FILL); + draw_line_underfill(&mut raster, values1, vpp, LINE1, LINE1_FILL); + } + } let mut standard = Gif::new(WIDTH as u16, HEIGHT as u16); standard.set_palette(Some(DARK_PALETTE.try_into().unwrap())); @@ -109,14 +125,14 @@ fn draw_grid(raster: &mut [u8]) { } } -fn draw_line(raster: &mut [u8], values: &[Option<usize>], vpp: usize, colour: u8) { +fn draw_line(raster: &mut [u8], values: &[Option<usize>], vpp: f32, colour: u8) { // Draw Line // this will be discontinuous and i think that's okay. we // could make it a proper line by keeping track what value // was last and drawing the whole vertical there for (x, maybe) in values.iter().enumerate() { if let Some(value) = maybe { - let value_height = value / vpp; + let value_height = (*value as f32 / vpp) as usize; if value_height > (HEIGHT - 1) { continue; } @@ -130,13 +146,13 @@ fn draw_line(raster: &mut [u8], values: &[Option<usize>], vpp: usize, colour: u8 fn draw_line_underfill( raster: &mut [u8], values: &[Option<usize>], - vpp: usize, + vpp: f32, colour: u8, colour_fill: u8, ) { for (x, maybe) in values.iter().enumerate() { if let Some(value) = maybe { - let value_height = value / vpp; + let value_height = (*value as f32 / vpp) as usize; if value_height > (HEIGHT - 1) { continue; } diff --git a/src/ifc.rs b/src/ifc.rs deleted file mode 100755 index d0a9f5f..0000000 --- a/src/ifc.rs +++ /dev/null @@ -1,112 +0,0 @@ -//! The international fixed calendar is a 13-month calendar with each month -//! containing exactly 28 days. There is an extra day at the end of the year -//! called the year day. -//! -//! In leap-years there is an extra day inserted at the end of June called the -//! leap day. It is directly after the fourth week of june and is given to june, -//! so it becomes June 29th. The day after June 29th starts the new month, Sol, -//! with Sol 1. -//! -//! [Wikipedia: International Fixed Calendar][wp-ifc] -//! [wp-ifc]: https://en.wikipedia.org/wiki/International_Fixed_Calendar - -use time::Date as TimeDate; - -const MONTHS: [[&str; 2]; 13] = [ - ["January", "Jan"], - ["February", "Feb"], - ["March", "Mar"], - ["April", "Apr"], - ["May", "May"], - ["June", "Jun"], - ["Sol", "Sol"], - ["July", "Jul"], - ["August", "Aug"], - ["September", "Sep"], - ["October", "Oct"], - ["November", "Nov"], - ["December", "Dec"], -]; - -pub struct Calendar { - pub year: usize, - pub ordinal: usize, -} - -impl Calendar { - pub fn from_year(year: usize) -> Self { - Self { year, ordinal: 0 } - } - - pub fn from_time_date(date: TimeDate) -> Self { - let year = date.year() as usize; - let ord = date.ordinal() as usize; - - Self { year, ordinal: ord } - } -} - -pub struct Date { - pub year: u32, - pub month: u8, - pub day: u8, -} - -impl Date { - pub fn from_time_date(date: TimeDate) -> Self { - let year = date.year() as u32; - let ord = date.ordinal(); - - if !year_leaps(year) || ord <= 168 { - // not a leap year path - // also the "leap year but before the leap-day" path - Self { - year, - month: (ord / 28) as u8, - day: (ord % 28) as u8, - } - } else if ord == 169 { - Self { - year, - month: 6, - day: 29, - } - } else { - todo!() - } - } - - pub fn is_leap(&self) -> bool { - year_leaps(self.year) - } -} - -/// Whether or not a year is a leap year -fn year_leaps(year: u32) -> bool { - let four = year % 4 == 0; - let hundreds = year % 100 == 0; - let fourhund = year % 400 == 0; - - // leap if: - // - four AND NOT hundred - // - four AND hundred AND fourhund - - // `fourhund` here checks `hundreds` by virtue of 100 being a multiple of 400 - four && (!hundreds || fourhund) -} - -mod test { - use crate::ifc::year_leaps; - - #[test] - fn leap_years() { - // the examples given by wikipedia - assert!(year_leaps(2000)); - assert!(!year_leaps(1700)); - assert!(!year_leaps(1800)); - assert!(!year_leaps(1900)); - - // testing the four rule - assert!(year_leaps(2024)); - } -} diff --git a/src/main.rs b/src/main.rs index d4faf9f..99ab22f 100755 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,6 @@ mod error; mod fs; mod gatherer; mod griph; -mod ifc; mod markup; mod settings; mod templated; |