Adds some of the graph gen functionnalities
This commit is contained in:
parent
fb6aee97d9
commit
2ece26aac0
|
|
@ -1,2 +0,0 @@
|
|||
[build]
|
||||
rustflags = ["-Clink-arg=-fuse-ld=lld", "-Ctarget-cpu=native", "-Zthreads=24"]
|
||||
20
Cargo.toml
20
Cargo.toml
|
|
@ -1,4 +1,4 @@
|
|||
cargo-features = ["codegen-backend"]
|
||||
# cargo-features = ["codegen-backend"]
|
||||
[package]
|
||||
name = "ravenous-tools"
|
||||
version = "0.1.4"
|
||||
|
|
@ -8,6 +8,7 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
anyhow = "1.0.86"
|
||||
charts-rs = { version = "0.3.11", features = ["image-encoder"] }
|
||||
clap = { version = "4.5.7", features = ["derive"] }
|
||||
console = "0.15.8"
|
||||
csv = "1.3.0"
|
||||
|
|
@ -26,7 +27,7 @@ toml = "0.8.14"
|
|||
|
||||
[profile.dev]
|
||||
debug = 0
|
||||
codegen-backend = "cranelift"
|
||||
# codegen-backend = "cranelift"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
|
@ -34,21 +35,12 @@ codegen-units = 1
|
|||
panic = "abort"
|
||||
strip = "none"
|
||||
|
||||
[profile.windows]
|
||||
inherits = "release"
|
||||
opt-level = "z"
|
||||
debug = false
|
||||
strip = true
|
||||
lto = true
|
||||
codegen-units = 1
|
||||
incremental = false
|
||||
panic = "abort"
|
||||
|
||||
[profile.base]
|
||||
[profile.fast]
|
||||
inherits = "dev"
|
||||
opt-level = 0
|
||||
debug = 0
|
||||
strip = "none"
|
||||
lto = false
|
||||
codegen-units = 256
|
||||
incremental = false
|
||||
incremental = true
|
||||
panic = "abort"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,215 @@
|
|||
//! Export graphs
|
||||
|
||||
use charts_rs::{Box, Color, THEME_ANT};
|
||||
use charts_rs::{get_or_try_init_fonts, RadarChart, RadarIndicator, Series};
|
||||
use std::process::Command;
|
||||
use glob::glob;
|
||||
use std::{f32, fs::File, io::Write, iter, sync::LazyLock};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use clap::Args;
|
||||
use phf::phf_map;
|
||||
|
||||
use crate::Config;
|
||||
|
||||
#[derive(Args, Debug, Clone)]
|
||||
/// The configuration struct for the PdfMaker
|
||||
pub(super) struct GraphConfig {
|
||||
/// Special target
|
||||
#[arg(help = format!("The target to execute [{}]", *HELP_STR_TO_GRAPH))]
|
||||
target: GraphTargetType,
|
||||
|
||||
/// Alternative origin
|
||||
#[arg(short, long, default_value = "Images/Radar/radar.csv")]
|
||||
origin: String,
|
||||
|
||||
/// Alternative destination
|
||||
#[arg(short, long, default_value = "Images/Radar")]
|
||||
destination: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
/// All target type
|
||||
pub(super) enum GraphTargetType {
|
||||
/// Make all graphs
|
||||
Generate,
|
||||
/// Clean all graphs
|
||||
Remove,
|
||||
}
|
||||
|
||||
/// Main action dispatch for the graph make
|
||||
pub(super) fn make_graphs(args: &GraphConfig, _config: &Config) -> Result<()> {
|
||||
// Choose action to do
|
||||
match args.target {
|
||||
GraphTargetType::Generate => gen_graph(args),
|
||||
GraphTargetType::Remove => del_graphs(args),
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete the existing graphs
|
||||
fn del_graphs(_args: &GraphConfig) -> Result<()> {
|
||||
let mut cmd = Command::new("rm");
|
||||
cmd.args(glob("Images/Radar/*.svg")?
|
||||
.map(|v| v.unwrap().to_str().unwrap().to_string())
|
||||
.collect::<Vec<String>>());
|
||||
cmd.args(glob("Images/Radar/*.png")?
|
||||
.map(|v| v.unwrap().to_str().unwrap().to_string())
|
||||
.collect::<Vec<String>>());
|
||||
|
||||
if let Err(e) = cmd.output() {
|
||||
println!(
|
||||
"[ERRR] An error has occured when executing the remove target \n {:#?}",
|
||||
e
|
||||
)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Generate the graph and save it
|
||||
fn gen_graph(args: &GraphConfig) -> Result<()> {
|
||||
// Import CSV
|
||||
|
||||
let data = include_bytes!("../data/lmroman8-regular.ttf") as &[u8];
|
||||
get_or_try_init_fonts(Some(vec![data])).unwrap();
|
||||
|
||||
let csv = import_csv(args)?;
|
||||
|
||||
let maxv = &csv.max;
|
||||
|
||||
for graph in csv.values {
|
||||
// println!("{:?} {:#?}", maxv, graph);
|
||||
// let path = format!("{}/{}.png", args.destination, &graph.name);
|
||||
let path = format!("{}/{}.svg", args.destination, &graph.name);
|
||||
println!("[INFO] Processing {}",path);
|
||||
|
||||
let mut f = File::create(path)?;
|
||||
let mut r = RadarChart::new_with_theme([graph].to_vec(), maxv.to_vec(), THEME_ANT);
|
||||
r.legend_show = Some(false);
|
||||
r.margin = Box{ left: -90.0, top: -20.0, right: -90.0, bottom: -30.0 };
|
||||
r.width = 800.0;
|
||||
r.height = 600.0;
|
||||
r.series_label_font_size = 20.0;
|
||||
|
||||
// r.grid_stroke_color = vec![Color{ r: 75,g: 114, b: 110, a: 0 }];
|
||||
r.series_colors = vec![Color{ r: 0,g:127, b:142, a: 255}];
|
||||
// r.series_colors = vec![Color{ r: 140,g:170, b:161, a: 255}];
|
||||
// r.series_colors = vec![Color{ r:150,g:251, b:199, a: 255 }];
|
||||
|
||||
r.font_family = "LMRoman8".to_string();
|
||||
|
||||
// f.write_all(&charts_rs::svg_to_png(r.svg().context("Converting to svg")?.as_str())?)?;
|
||||
f.write_all(r.svg().context("Converting to SVG")?.as_bytes())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Import the default csv
|
||||
fn import_csv(args: &GraphConfig) -> Result<GraphValues> {
|
||||
let file = File::open(&args.origin)
|
||||
.context("Opening the radar file")
|
||||
.unwrap();
|
||||
let rdr = csv::ReaderBuilder::new().from_reader(file);
|
||||
|
||||
cleanup_csv(rdr)
|
||||
}
|
||||
|
||||
/// The data for all graphs
|
||||
struct GraphValues {
|
||||
/// The max values of a graph
|
||||
// max: Vec<(String, f32)>,
|
||||
max: Vec<RadarIndicator>,
|
||||
|
||||
/// The list of all character and associated values
|
||||
// values: Vec<(String, Vec<f32>)>,
|
||||
values: Vec<Series>,
|
||||
}
|
||||
|
||||
/// Cleanup the given csv to output usable data
|
||||
fn cleanup_csv(mut rdr: csv::Reader<File>) -> Result<GraphValues> {
|
||||
let headers = rdr.headers().context("Opening headers")?;
|
||||
// Making the max/base vec
|
||||
|
||||
// Gathering all the different category names
|
||||
let headers = headers
|
||||
.iter()
|
||||
.filter(|v| !v.is_empty())
|
||||
.skip(1)
|
||||
.map(|v| v.to_string())
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
// Gather all the different categories
|
||||
let mut rdr = rdr.records();
|
||||
|
||||
// Calculate the max values
|
||||
let maxvals: Vec<f32> = rdr
|
||||
.next()
|
||||
.context("Gathering max values")??
|
||||
.iter()
|
||||
.map(|v| v.parse::<f32>().unwrap_or(0.0))
|
||||
.skip(1)
|
||||
.collect();
|
||||
|
||||
// Calculate all the values of all graphs
|
||||
let mut res = Vec::new();
|
||||
for line in rdr {
|
||||
let line = line.context("Parsing table lines")?;
|
||||
let name = line
|
||||
.get(0)
|
||||
.context("Fetching name for character")?
|
||||
.to_string()
|
||||
.replace(" ", "_")
|
||||
.replace("î", "i")
|
||||
.to_lowercase()
|
||||
.replace("é", "e");
|
||||
let vals: Vec<f32> = line
|
||||
.iter()
|
||||
.skip(1)
|
||||
.enumerate()
|
||||
.map(|(i, v)| {
|
||||
v.parse::<f32>().unwrap_or(0.0) * 5.0
|
||||
/ maxvals.get(i).context("Fetching max value").unwrap()
|
||||
})
|
||||
.collect();
|
||||
res.push(Series::new(name, vals));
|
||||
}
|
||||
|
||||
Ok(GraphValues {
|
||||
max: headers
|
||||
.iter()
|
||||
.map(|v| v.to_owned())
|
||||
.zip(iter::repeat(5.0f32))
|
||||
.map(|(name, max)| RadarIndicator { name, max })
|
||||
.collect::<Vec<RadarIndicator>>(),
|
||||
values: res,
|
||||
})
|
||||
}
|
||||
|
||||
/// Link from user str input to the action to perform
|
||||
static STR_TO_GRAPH_TARGET_TYPE: phf::Map<&'static str, GraphTargetType> = phf_map! {
|
||||
"generate" => GraphTargetType::Generate,
|
||||
"gen" => GraphTargetType::Generate,
|
||||
"remove" => GraphTargetType::Remove,
|
||||
"del" => GraphTargetType::Remove,
|
||||
};
|
||||
|
||||
/// Help listing all the actions availables
|
||||
pub(super) static HELP_STR_TO_GRAPH: LazyLock<String> = LazyLock::new(|| {
|
||||
let targets = STR_TO_GRAPH_TARGET_TYPE
|
||||
.keys()
|
||||
.map(|v| v.to_string())
|
||||
.collect::<Vec<String>>();
|
||||
targets.join(", ").to_string()
|
||||
});
|
||||
|
||||
impl From<&str> for GraphTargetType {
|
||||
fn from(value: &str) -> Self {
|
||||
if let Some(target) = STR_TO_GRAPH_TARGET_TYPE.get(&value.to_lowercase()).cloned() {
|
||||
target
|
||||
} else {
|
||||
panic!(
|
||||
"Invalid target {}. The target available are : [{}]",
|
||||
value, *HELP_STR_TO_GRAPH
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@
|
|||
// #![allow(dead_code)]
|
||||
#![warn(missing_docs)]
|
||||
#![warn(clippy::missing_docs_in_private_items)]
|
||||
mod graphs;
|
||||
mod makepdf;
|
||||
mod makepdf_functions;
|
||||
mod makepdf_misc;
|
||||
|
|
@ -25,6 +26,7 @@ use std::{
|
|||
|
||||
use anyhow::{Context, Result};
|
||||
use clap::{Args, Parser, Subcommand};
|
||||
use graphs::{make_graphs, GraphConfig};
|
||||
use makepdf::{make_pdf, MakeConfig};
|
||||
use serde::Deserialize;
|
||||
use stats::{stat_solver, StatConfig};
|
||||
|
|
@ -52,6 +54,8 @@ enum Commands {
|
|||
Stats(StatConfig),
|
||||
/// Make a pdf via pdflatex
|
||||
Make(MakeConfig),
|
||||
/// Make all graphs
|
||||
Graphs(GraphConfig),
|
||||
}
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
|
|
@ -98,6 +102,7 @@ fn main() -> Result<()> {
|
|||
Commands::Config(args) => write_config(&args, &config, config_str),
|
||||
Commands::Stats(args) => stat_solver(&args, &config),
|
||||
Commands::Make(args) => make_pdf(&args, &config),
|
||||
Commands::Graphs(args) => make_graphs(&args, &config),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -35,15 +35,15 @@ pub(super) struct MakeConfig {
|
|||
pub(super) filename: String,
|
||||
|
||||
/// Special target
|
||||
#[arg(short, long, help = format!("Build with a special target. {}", HELP_BUILD_TARGET_LIST.as_str()))]
|
||||
#[arg(short, long, help = format!("Build with a special target. [{}]", HELP_BUILD_TARGET_LIST.as_str()))]
|
||||
pub(super) target: Option<MakeTargets>,
|
||||
|
||||
/// The verbosity level
|
||||
#[arg(short, long, default_value = "warning", help = format!("The verbosity level. {}",HELP_VERBOSITY_LEVEL.as_str()))]
|
||||
#[arg(short, long, default_value = "warning", help = format!("The verbosity level. [{}]",HELP_VERBOSITY_LEVEL.as_str()))]
|
||||
pub(super) verbosity: VerboseLevel,
|
||||
|
||||
/// The progress bar style
|
||||
#[arg(short, long, default_value = "class", help = format!("The progress bar style. {}",HELP_BLOCK_SELECTION.as_str()))]
|
||||
#[arg(short, long, default_value = "class", help = format!("The progress bar style. [{}]",HELP_BLOCK_SELECTION.as_str()))]
|
||||
pub(super) pbar: String,
|
||||
|
||||
/// switch to non interactive mode
|
||||
|
|
@ -67,7 +67,7 @@ pub(super) struct MakeConfig {
|
|||
pub(super) fn make_pdf(args: &MakeConfig, _config: &Config) -> Result<()> {
|
||||
let Ok(Some(_)) = BLOCK_PROGRESSION.get_key(&args.pbar) else {
|
||||
panic!(
|
||||
"Progressbar {} doesn't exists. Use one of the following {:?}",
|
||||
"Progressbar {} doesn't exists. Use one of the following [{:?}]",
|
||||
args.pbar, HELP_BLOCK_SELECTION
|
||||
);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -25,22 +25,31 @@ static STR_TO_BUILD_TARGET: phf::Map<&'static str, MakeTargets> = phf_map! {
|
|||
|
||||
/// List all blocktype
|
||||
pub(super) static HELP_BLOCK_SELECTION: LazyLock<String> = LazyLock::new(|| {
|
||||
let mut targets = BLOCK_PROGRESSION.keys().collect::<Vec<&String>>();
|
||||
let mut targets = BLOCK_PROGRESSION
|
||||
.keys()
|
||||
.map(|v| v.to_owned())
|
||||
.collect::<Vec<String>>();
|
||||
targets.sort();
|
||||
format!("{:?}", targets)
|
||||
targets.join(", ").to_string()
|
||||
});
|
||||
|
||||
/// List all targets
|
||||
pub(super) static HELP_VERBOSITY_LEVEL: LazyLock<String> = LazyLock::new(|| {
|
||||
let mut targets = STR_TO_VERBOSITY.values().collect::<Vec<&VerboseLevel>>();
|
||||
let mut targets = STR_TO_VERBOSITY
|
||||
.values()
|
||||
.map(|v| format!("{:?}", v).to_lowercase())
|
||||
.collect::<Vec<String>>();
|
||||
targets.sort();
|
||||
format!("{:?}", targets)
|
||||
targets.join(", ").to_string()
|
||||
});
|
||||
|
||||
/// List all targets
|
||||
pub(super) static HELP_BUILD_TARGET_LIST: LazyLock<String> = LazyLock::new(|| {
|
||||
let targets = STR_TO_BUILD_TARGET.values().collect::<Vec<&MakeTargets>>();
|
||||
format!("{:?}", targets)
|
||||
let targets = STR_TO_BUILD_TARGET
|
||||
.values()
|
||||
.map(|v| format!("{:?}", v).to_lowercase())
|
||||
.collect::<Vec<String>>();
|
||||
targets.join(", ").to_string()
|
||||
});
|
||||
|
||||
impl From<&str> for VerboseLevel {
|
||||
|
|
@ -49,7 +58,7 @@ impl From<&str> for VerboseLevel {
|
|||
target
|
||||
} else {
|
||||
panic!(
|
||||
"Invalid debug level {}. The debug level available are (from highest verbosity to lowest): {:?}",
|
||||
"Invalid debug level {}. The debug level available are (from highest verbosity to lowest): [{:?}]",
|
||||
value, HELP_VERBOSITY_LEVEL
|
||||
)
|
||||
}
|
||||
|
|
@ -62,7 +71,7 @@ impl From<&str> for MakeTargets {
|
|||
target
|
||||
} else {
|
||||
panic!(
|
||||
"Invalid build target {}. The build target available are {:?}",
|
||||
"Invalid build target {}. The build target available are [{:?}]",
|
||||
value, HELP_BUILD_TARGET_LIST
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue