implement base loop runner
This commit is contained in:
parent
2b079012f2
commit
6d3f360012
|
|
@ -1,13 +1,16 @@
|
||||||
mod config;
|
mod config;
|
||||||
mod observables;
|
mod observables;
|
||||||
|
mod runner;
|
||||||
|
|
||||||
use config::{Config, GraphConfig, SeriesConfig};
|
use config::{Config, GraphConfig, SeriesConfig};
|
||||||
use draw::{StackedConfig, StackedSeries, StackedSeriesConfig, stacked};
|
use draw::{StackedConfig, StackedSeriesConfig};
|
||||||
use observables::{Observable, ObservableCollection};
|
use observables::{Observable, ObservableCollection};
|
||||||
use protocol::{Protocol, ProtocolError};
|
use protocol::{Protocol, ProtocolError};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::{thread, time};
|
use std::{thread, time};
|
||||||
|
|
||||||
|
use crate::runner::{Graph, Runner};
|
||||||
|
|
||||||
fn create_debug_config() -> Config {
|
fn create_debug_config() -> Config {
|
||||||
let load_regexes = vec![Regex::new(".*\\/cpu-[0-9]+\\/cpu-(nice|steal|system|user)").unwrap()];
|
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 iowait_regexes = vec![Regex::new(".*\\/cpu-[0-9]+\\/cpu-wait").unwrap()];
|
||||||
|
|
@ -68,18 +71,16 @@ fn main() {
|
||||||
.collect::<Result<Vec<Observable>, ProtocolError>>()
|
.collect::<Result<Vec<Observable>, ProtocolError>>()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let collection = ObservableCollection::new(observables);
|
let collection = ObservableCollection::new(observables);
|
||||||
|
let graphs = vec![Graph::new(cfg, collection)];
|
||||||
|
|
||||||
let mut series = StackedSeries::new(cfg);
|
let mut runner = Runner::new(graphs, proto);
|
||||||
for _ in 1..10 {
|
|
||||||
let sample = collection.sample(&mut proto).unwrap();
|
|
||||||
series.push(sample);
|
|
||||||
|
|
||||||
let img = stacked(&series);
|
|
||||||
img.save("bzn.png").unwrap();
|
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let r = runner.tick();
|
||||||
|
if let Err(e) = r {
|
||||||
|
println!("{}", e.to_string());
|
||||||
|
}
|
||||||
thread::sleep(time::Duration::from_secs(1));
|
thread::sleep(time::Duration::from_secs(1));
|
||||||
|
|
||||||
// display_image("bzn", &img, 500, 500);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
panic!("sock not found!");
|
panic!("sock not found!");
|
||||||
|
|
|
||||||
|
|
@ -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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue