File size: 7,074 Bytes
d5bfab8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
use crate::common::{oeis_id_from_path, SimpleLog};
use loda_rust_core;
use super::{AnalyticsError, AnalyticsMode};
use loda_rust_core::parser::ParsedProgram;
use std::path::PathBuf;
use std::error::Error;
use std::fs;
use std::time::Instant;
use std::rc::Rc;
use core::cell::RefCell;
use console::Style;
use indicatif::{HumanDuration, ProgressBar};
#[cfg(feature = "loda-rust-arc")]
use crate::arc::RunWithProgram;
pub struct BatchProgramAnalyzerContext {
pub program_path: PathBuf,
pub program_id: u32,
pub parsed_program: ParsedProgram,
}
pub trait BatchProgramAnalyzerPlugin {
fn plugin_name(&self) -> &'static str;
fn analyze(&mut self, context: &BatchProgramAnalyzerContext) -> Result<(), Box<dyn Error>>;
fn save(&self) -> Result<(), Box<dyn Error>>;
fn human_readable_summary(&self) -> String;
fn format_summary(&self) -> String {
let name: &str = self.plugin_name();
let summary: String = self.human_readable_summary();
format!("\n{}\n{}\n", name.trim(), summary.trim())
}
}
pub type BatchProgramAnalyzerPluginItem = Rc<RefCell<dyn BatchProgramAnalyzerPlugin>>;
pub struct BatchProgramAnalyzer {
analytics_mode: AnalyticsMode,
plugin_vec: Vec<BatchProgramAnalyzerPluginItem>,
simple_log: SimpleLog,
program_paths: Vec<PathBuf>,
number_of_program_files_that_could_not_be_loaded: usize,
number_of_program_files_ignored: usize,
number_of_program_files_successfully_analyzed: usize,
}
impl BatchProgramAnalyzer {
pub fn new(
analytics_mode: AnalyticsMode,
plugin_vec: Vec<BatchProgramAnalyzerPluginItem>,
simple_log: SimpleLog,
program_paths: Vec<PathBuf>,
) -> Self {
Self {
analytics_mode,
plugin_vec,
simple_log,
program_paths,
number_of_program_files_that_could_not_be_loaded: 0,
number_of_program_files_ignored: 0,
number_of_program_files_successfully_analyzed: 0,
}
}
pub fn run(&mut self) -> anyhow::Result<()> {
println!("Run batch-program-analyzer");
self.simple_log.println("BatchProgramAnalyzer");
self.analyze_the_valid_program_files()
.map_err(|e| anyhow::anyhow!("BatchProgramAnalyzer.run. analyze_the_valid_program_files. error: {:?}", e))?;
self.save_result_files()
.map_err(|e| anyhow::anyhow!("BatchProgramAnalyzer.run. save_result_files. error: {:?}", e))?;
self.save_summary()
.map_err(|e| anyhow::anyhow!("BatchProgramAnalyzer.run. save_summary. error: {:?}", e))?;
Ok(())
}
fn analyze_the_valid_program_files(&mut self) -> Result<(), Box<dyn Error>> {
let number_of_paths = self.program_paths.len();
if number_of_paths <= 0 {
let message = "Expected 1 or more programs, but there are no programs to analyze";
return Err(Box::new(AnalyticsError::BatchProgramAnalyzer(message.to_string())));
}
let pb = ProgressBar::new(number_of_paths as u64);
let start = Instant::now();
let program_paths: Vec<PathBuf> = self.program_paths.clone();
for program_path in program_paths {
self.analyze_program_file(program_path)?;
pb.inc(1);
}
pb.finish_and_clear();
let green_bold = Style::new().green().bold();
println!(
"{:>12} batch-program-analyzer in {}",
green_bold.apply_to("Finished"),
HumanDuration(start.elapsed())
);
let content = format!("number of program files successfully analyzed: {:?}", self.number_of_program_files_successfully_analyzed);
self.simple_log.println(content);
let content = format!("number of program files that could not be loaded: {:?}", self.number_of_program_files_that_could_not_be_loaded);
self.simple_log.println(content);
let content = format!("number of program files that was ignored: {:?}", self.number_of_program_files_ignored);
self.simple_log.println(content);
Ok(())
}
fn analyze_program_file(
&mut self,
program_path: PathBuf,
) -> Result<(), Box<dyn Error>> {
let program_id: u32;
match self.analytics_mode {
AnalyticsMode::OEIS => {
// Extract OEIS id from program path
program_id = match oeis_id_from_path(&program_path) {
Some(oeis_id) => oeis_id.raw(),
None => {
debug!("Unable to extract program_id from {:?}", program_path);
self.number_of_program_files_that_could_not_be_loaded += 1;
return Ok(());
}
};
},
AnalyticsMode::ARC => {
// ARC programs use a filename ala `39a8645d-1.asm`, so it doesn't work with an integer `program_id`.
program_id = 1;
},
}
#[allow(unused_mut)]
let mut contents: String = match fs::read_to_string(&program_path) {
Ok(value) => value,
Err(error) => {
debug!("loading program_id: {:?}, something went wrong reading the file: {:?}", program_id, error);
self.number_of_program_files_that_could_not_be_loaded += 1;
return Ok(());
}
};
#[cfg(feature = "loda-rust-arc")]
if self.analytics_mode == AnalyticsMode::ARC {
// detect if it's a "simple" program, and wrap it in the "advanced" template
let is_simple: bool = contents.contains("Program Type: simple");
if is_simple {
contents = RunWithProgram::convert_simple_to_full(contents);
}
}
let parsed_program: ParsedProgram = match ParsedProgram::parse_program(&contents) {
Ok(value) => value,
Err(error) => {
debug!("loading program_id: {:?}, something went wrong parsing the program: {:?}", program_id, error);
self.number_of_program_files_that_could_not_be_loaded += 1;
return Ok(());
}
};
let context = BatchProgramAnalyzerContext {
program_path: program_path,
program_id: program_id,
parsed_program: parsed_program,
};
for plugin in self.plugin_vec.iter() {
plugin.borrow_mut().analyze(&context)?;
}
self.number_of_program_files_successfully_analyzed += 1;
Ok(())
}
fn save_result_files(&self) -> Result<(), Box<dyn Error>> {
for plugin in self.plugin_vec.iter() {
plugin.borrow().save()?;
}
Ok(())
}
fn save_summary(&mut self) -> Result<(), Box<dyn Error>> {
for plugin in self.plugin_vec.iter() {
let summary: String = plugin.borrow().format_summary();
self.simple_log.print(&summary)?;
}
Ok(())
}
}
|