implement base loop runner

This commit is contained in:
Ondrej Novak 2026-06-17 10:19:15 +02:00
parent 2b079012f2
commit 6d3f360012
Signed by: handy
SSH Key Fingerprint: SHA256:jlNm8ijkaMl4X7+nn3zBFTJufdQgT4unKJoZVH6kYTM
2 changed files with 79 additions and 10 deletions

View File

@ -1,13 +1,16 @@
mod config;
mod observables;
mod runner;
use config::{Config, GraphConfig, SeriesConfig};
use draw::{StackedConfig, StackedSeries, StackedSeriesConfig, stacked};
use draw::{StackedConfig, StackedSeriesConfig};
use observables::{Observable, ObservableCollection};
use protocol::{Protocol, ProtocolError};
use regex::Regex;
use std::{thread, time};
use crate::runner::{Graph, Runner};
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()];
@ -68,18 +71,16 @@ fn main() {
.collect::<Result<Vec<Observable>, ProtocolError>>()
.unwrap();
let collection = ObservableCollection::new(observables);
let graphs = vec![Graph::new(cfg, collection)];
let mut series = StackedSeries::new(cfg);
for _ in 1..10 {
let sample = collection.sample(&mut proto).unwrap();
series.push(sample);
let img = stacked(&series);
img.save("bzn.png").unwrap();
let mut runner = Runner::new(graphs, proto);
loop {
let r = runner.tick();
if let Err(e) = r {
println!("{}", e.to_string());
}
thread::sleep(time::Duration::from_secs(1));
// display_image("bzn", &img, 500, 500);
}
} else {
panic!("sock not found!");

View File

@ -0,0 +1,68 @@
use core::fmt;
use draw::{StackedConfig, StackedSeries, stacked};
use protocol::Protocol;
use crate::observables::ObservableCollection;
pub struct Graph {
pub observable: ObservableCollection,
pub series: StackedSeries,
}
impl Graph {
pub fn new(config: StackedConfig, observable: ObservableCollection) -> Graph {
Graph {
observable: observable,
series: StackedSeries::new(config),
}
}
}
#[derive(PartialEq, Debug)]
pub enum RunnerError {
CouldNotSaveImage(String),
CouldNotGetData(String),
}
impl fmt::Display for RunnerError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let message = match self {
Self::CouldNotSaveImage(err) => {
format!("Could not save image: {}", err)
}
Self::CouldNotGetData(err) => {
format!("Could not read: {}", err)
}
};
f.write_str(&message)
}
}
pub struct Runner {
graphs: Vec<Graph>,
proto: Protocol,
}
impl Runner {
pub fn new(graphs: Vec<Graph>, proto: Protocol) -> Runner {
Runner {
graphs: graphs,
proto: proto,
}
}
pub fn tick(&mut self) -> Result<(), RunnerError> {
for g in &mut self.graphs {
let sample = g
.observable
.sample(&mut self.proto)
.map_err(|e| RunnerError::CouldNotGetData(e.to_string()))?;
g.series.push(sample);
let img = stacked(&g.series);
img.save("cpu.png".to_string())
.map_err(|e| RunnerError::CouldNotSaveImage(e.to_string()))?;
}
Ok(())
}
}