From 2b079012f2f11c1f240192ac1ea31b2ee74c363a Mon Sep 17 00:00:00 2001 From: Ondrej Novak Date: Sun, 14 Jun 2026 22:52:38 +0200 Subject: [PATCH] define conf structures --- waybar-collectd/src/config.rs | 19 +++++++ waybar-collectd/src/main.rs | 83 +++++++++++++++++++++++------- waybar-collectd/src/observables.rs | 33 +++++++----- 3 files changed, 103 insertions(+), 32 deletions(-) create mode 100644 waybar-collectd/src/config.rs diff --git a/waybar-collectd/src/config.rs b/waybar-collectd/src/config.rs new file mode 100644 index 0000000..c10dd4c --- /dev/null +++ b/waybar-collectd/src/config.rs @@ -0,0 +1,19 @@ +use regex::Regex; + +pub struct Config { + pub graphs: Vec, +} + +pub struct GraphConfig { + pub name: String, + pub width_px: u16, + pub height_px: u16, + pub samples: u16, + pub series: Vec, +} + +pub struct SeriesConfig { + pub name: String, + pub color: (u8, u8, u8), + pub metrics: Vec, +} diff --git a/waybar-collectd/src/main.rs b/waybar-collectd/src/main.rs index 76f5079..25e1780 100644 --- a/waybar-collectd/src/main.rs +++ b/waybar-collectd/src/main.rs @@ -1,34 +1,77 @@ +mod config; mod observables; +use config::{Config, GraphConfig, SeriesConfig}; use draw::{StackedConfig, StackedSeries, StackedSeriesConfig, stacked}; -use observables::{Observable, ObservableConfig}; -use protocol::Protocol; +use observables::{Observable, ObservableCollection}; +use protocol::{Protocol, ProtocolError}; +use regex::Regex; use std::{thread, time}; +fn create_debug_config() -> Config { + let load_regexes = vec![Regex::new(".*\\/cpu-[0-9]+\\/cpu-(nice|steal|system|user)").unwrap()]; + let iowait_regexes = vec![Regex::new(".*\\/cpu-[0-9]+\\/cpu-wait").unwrap()]; + let irq_regexes = vec![Regex::new(".*\\/cpu-[0-9]+\\/cpu-(interrupt|softirq)").unwrap()]; + let idle_regexes = vec![Regex::new(".*\\/cpu-[0-9]+\\/cpu-idle").unwrap()]; + let series = vec![ + SeriesConfig { + name: "load".to_string(), + color: (2, 154, 219), + metrics: load_regexes, + }, + SeriesConfig { + name: "io wait".to_string(), + color: (68, 200, 229), + metrics: iowait_regexes, + }, + SeriesConfig { + name: "irq".to_string(), + color: (143, 232, 252), + metrics: irq_regexes, + }, + SeriesConfig { + name: "idle".to_string(), + color: (0, 0, 0), + metrics: idle_regexes, + }, + ]; + let graphs = vec![GraphConfig { + name: "cpu load".to_string(), + width_px: 480, + height_px: 98, + samples: 60, + series: series, + }]; + + Config { graphs: graphs } +} + fn main() { - let cfg = StackedConfig { - width_px_per_sample: 4, - height_px: 100, - series: vec![ - StackedSeriesConfig { color: (255, 0, 0) }, - StackedSeriesConfig { color: (0, 255, 0) }, - StackedSeriesConfig { color: (0, 0, 255) }, - ], - max_samples: 10, - }; - if let Ok(mut proto) = Protocol::new("/var/run/collectd-unixsock") { - // loop { + let config = create_debug_config(); - let cpu_load_cfg = ObservableConfig { - name: "cpu".to_string(), - norm: 1., - include: vec![Regex::new()], + let cfg = StackedConfig { + height_px: config.graphs[0].height_px as u32, + max_samples: config.graphs[0].samples as u32, + width_px_per_sample: (config.graphs[0].width_px / config.graphs[0].samples) as u32, + series: config.graphs[0] + .series + .iter() + .map(|s| StackedSeriesConfig { color: s.color }) + .collect(), }; - let cpu = Observable::discover(&mut proto).unwrap(); + + let observables = config.graphs[0] + .series + .iter() + .map(|s| Observable::discover(&mut proto, &s.name, &s.metrics)) + .collect::, ProtocolError>>() + .unwrap(); + let collection = ObservableCollection::new(observables); + let mut series = StackedSeries::new(cfg); for _ in 1..10 { - let sample = cpu.sample(&mut proto).unwrap(); + let sample = collection.sample(&mut proto).unwrap(); series.push(sample); let img = stacked(&series); diff --git a/waybar-collectd/src/observables.rs b/waybar-collectd/src/observables.rs index d78e496..0465854 100644 --- a/waybar-collectd/src/observables.rs +++ b/waybar-collectd/src/observables.rs @@ -1,32 +1,25 @@ use protocol::{Protocol, ProtocolError}; use regex::Regex; -pub struct ObservableConfig { - pub name: String, - pub norm: f32, - pub include: Vec, -} - pub struct Observable { pub name: String, - norm: f32, metrics: Vec, } impl Observable { pub fn discover( proto: &mut Protocol, - cfg: ObservableConfig, + name: &str, + includes: &Vec, ) -> Result { let all_values = proto.list()?; let filtered_values = all_values .into_iter() - .filter(|v| cfg.include.iter().any(|i| i.is_match(v))) + .filter(|v| includes.iter().any(|i| i.is_match(v))) .collect(); Ok(Observable { - name: cfg.name, - norm: cfg.norm, + name: name.to_string(), metrics: filtered_values, }) } @@ -38,6 +31,22 @@ impl Observable { .map(|f| proto.get(f.into(), "value".to_string())) .collect::, ProtocolError>>()?; - Ok(results.into_iter().sum::() / self.norm) + Ok(results.into_iter().sum::()) + } +} + +pub struct ObservableCollection { + observables: Vec, +} + +impl ObservableCollection { + pub fn new(observables: Vec) -> ObservableCollection { + ObservableCollection { + observables: observables, + } + } + + pub fn sample(&self, proto: &mut Protocol) -> Result, ProtocolError> { + self.observables.iter().map(|o| o.sample(proto)).collect() } }