From f99aa533bd55a93c45ff7f294d6bfc62367d6a54 Mon Sep 17 00:00:00 2001 From: Hayden Flinner Date: Sat, 4 Jan 2025 19:26:11 -0500 Subject: [PATCH] added drain_handler --- src/components/drain_handler.rs | 79 +++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/components/drain_handler.rs diff --git a/src/components/drain_handler.rs b/src/components/drain_handler.rs new file mode 100644 index 0000000..c1d6838 --- /dev/null +++ b/src/components/drain_handler.rs @@ -0,0 +1,79 @@ +use bstr::{BStr, ByteSlice}; +use rustc_hash::FxHashMap; +use tracing::error; + +use crate::drainrs::{ParseTree, DrainState, RecordParsed, RecordsParsedIter, RecordsParsedResult}; + +use super::home::DispLine; + +struct LineData { + template_id: usize, +} + +#[derive(Default)] +pub struct DrainHandler { + drain_state: DrainState, + parsed: FxHashMap<(usize, usize), LineData>, + templates: Vec, +} + +impl DrainHandler { + pub fn new() -> Self { + Self { drain_state: DrainState::default(), parsed: FxHashMap::default(), templates: Vec::default() } + } + + pub fn parse(&mut self, logfile: &BStr, lines: &[DispLine]) { + // Note, cannot assume the display lines are contiguous in the file, may have been a bunch filtered out. + // let mut tree = ParseTree::default(); + let mut template_names = Vec::new(); + + for line in lines { + let mut handle_parse = |template_names: &[String], rp: &RecordParsed| { + // let typ = &self.templates[rp.template_id]; + // rp.values <-- actual values + self.parsed.insert(line.file_loc, LineData { template_id: (rp.template_id) } ); + true + }; + // TODO refactor to use &strs instead of start/end indexes within the mmap? + // Can also just pass the mmap in here. + // Then the below should just work; probably need better abstraction in the lib to shield users from the virtually guaranteed need + // to store the templates. + let str = &logfile[line.file_loc.0..line.file_loc.1].to_str_lossy(); + let mut rpi = RecordsParsedIter::from(&str, &mut self.drain_state.parse_tree); + loop { + let handle = |record| { + match record { + RecordsParsedResult::NewTemplate(template) => { + template_names.push( + template + .template + .iter() + .map(|t| t.to_string()) + .intersperse(" ".to_string()) + .collect::(), + ); + // handle_parse(&template_names, &template.first_parse); + true + } + RecordsParsedResult::RecordParsed(rp) => handle_parse(&template_names, &rp), + RecordsParsedResult::ParseError(e) => { + error!("err: {}", e); + false + } + RecordsParsedResult::UnparsedLine(line) => { + error!("unparsed: {}", line); + false + } + RecordsParsedResult::Done => { + log::info!("Done!"); + false + } + } + }; + if !rpi.next(handle) { + break; + } + } + } + } +} \ No newline at end of file