tramex_tools/interface/interface_file/
file_handler.rs

1//! File Handler
2
3use crate::data::{AdditionalInfos, Data, Trace};
4use crate::errors::ErrorCode;
5use crate::errors::TramexError;
6use crate::interface::interface_types::InterfaceTrait;
7use crate::interface::layer::Layers;
8use crate::interface::parse_config::{FileMetadata, Technology};
9use crate::tramex_error;
10use std::path::PathBuf;
11//use std::collections::HashMap;
12
13use super::file_index::FileIndex;
14use super::utils_file::parse_one_block;
15
16/// The default number of log processed by batch
17/// Number of events parsed per batch
18pub const BATCH_SIZE: usize = 100;
19
20#[derive(Debug, Clone)]
21/// Data structure to store the file.
22pub struct File {
23    /// Path of the file.
24    pub file_path: PathBuf,
25
26    /// Content of the file.
27    pub file_content: Vec<String>,
28
29    /// Full read status of the file.
30    pub full_read: bool,
31
32    /// the number of log to read each batch
33    nb_read: usize,
34
35    /// The previous line number (deprecated - kept for compatibility)
36    index_line: usize,
37
38    /// Available
39    pub available: bool,
40
41    /// File index for efficient navigation (Option 3)
42    pub index: Option<FileIndex>,
43
44    /// Cache of parsed traces (index -> Trace)
45    //_parsed_cache: HashMap<usize, Trace>,
46
47    /// Current logical index in the file index
48    pub current_log_index: usize,
49}
50
51impl Default for File {
52    fn default() -> Self {
53        Self {
54            file_path: PathBuf::from(""),
55            file_content: vec![],
56            full_read: false,
57            nb_read: BATCH_SIZE,
58            index_line: 0,
59            available: true,
60            index: None,
61            //parsed_cache: HashMap::new(),
62            current_log_index: 0,
63        }
64    }
65}
66
67impl InterfaceTrait for File {
68    fn get_more_data(&mut self, _layer_list: Layers, data: &mut Data) -> Result<(), Vec<TramexError>> {
69        if self.full_read {
70            return Ok(());
71        }
72
73        // Parse metadata on first call and build index
74        if self.index.is_none() && data.events.is_empty() {
75            data.metadata = FileMetadata::parse_from_lines(&self.file_content);
76
77            // Build file index (Option 3)
78            log::info!("Building file index...");
79            match FileIndex::build_from_lines(&self.file_content) {
80                Ok(index) => {
81                    log::info!("File index built: {} logs found", index.total_count);
82                    self.index = Some(index);
83                }
84                Err(e) => {
85                    log::error!("Failed to build file index: {}", e.message);
86                    return Err(vec![e]);
87                }
88            }
89        }
90
91        // Use old batch processing for now (will be optimized later)
92        let (mut traces, err_processed) = self.process();
93
94        // Infer technology from RRC canal name if metadata is Unknown
95        if data.metadata.technology == Technology::Unknown {
96            for trace in &traces {
97                if let AdditionalInfos::RRCInfos(infos) = &trace.additional_infos {
98                    if infos.canal.ends_with("-NR") {
99                        data.metadata.technology = Technology::NR;
100                    } else {
101                        data.metadata.technology = Technology::LTE;
102                    }
103                    break; // Only need to check the first RRC trace
104                }
105            }
106        }
107
108        data.events.append(&mut traces);
109        if !err_processed.is_empty() {
110            let filtered: Vec<TramexError> = err_processed
111                .iter()
112                .filter(|tmx_err| !matches!(tmx_err.get_code(), ErrorCode::EndOfFile))
113                .filter(|tmx_err| !matches!(tmx_err.get_code(), ErrorCode::ParsingLayerNotImplemented))
114                .cloned()
115                .collect();
116            // Only return Err if there are real errors remaining
117            if !filtered.is_empty() {
118                return Err(filtered);
119            }
120        }
121        Ok(())
122    }
123
124    fn close(&mut self) -> Result<(), TramexError> {
125        Ok(())
126    }
127
128    fn supports_preloading(&self) -> bool {
129        true
130    }
131
132    fn get_total_event_count(&self) -> Option<usize> {
133        self.index.as_ref().map(|idx| idx.total_count)
134    }
135
136    fn is_fully_read(&self) -> bool {
137        self.full_read
138    }
139}
140
141impl File {
142    /// Create a new file.
143    pub fn new(file_path: PathBuf, file_content: String) -> Self {
144        Self {
145            file_path,
146            file_content: file_content.lines().map(|x| x.to_string()).collect(),
147            full_read: false,
148            nb_read: BATCH_SIZE,
149            index_line: 0,
150            available: true,
151            index: None,
152            //parsed_cache: HashMap::new(),
153            current_log_index: 0,
154        }
155    }
156
157    /// set file mode using a path and content
158    pub fn new_file_content(file_path: PathBuf, file_content: String) -> Self {
159        Self {
160            file_path,
161            file_content: file_content.lines().map(|x| x.to_string()).collect(),
162            full_read: false,
163            nb_read: BATCH_SIZE,
164            index_line: 0,
165            available: true,
166            index: None,
167            //parsed_cache: HashMap::new(),
168            current_log_index: 0,
169        }
170    }
171
172    /// To update the number of log to read per batch
173    pub fn change_nb_read(&mut self, toread: usize) {
174        self.nb_read = toread;
175    }
176
177    /// To process the file and parse a batch of log
178    pub fn process(&mut self) -> (Vec<Trace>, Vec<TramexError>) {
179        let (vec_trace, opt_err) = File::process_string(&self.file_content, self.nb_read, &mut self.index_line);
180        for one_error in &opt_err {
181            if matches!(one_error.get_code(), ErrorCode::EndOfFile) {
182                self.full_read = true;
183            }
184        }
185        (vec_trace, opt_err)
186    }
187    /// To process a string passed in argument, with index and batch to read
188    pub fn process_string(lines: &[String], nb_to_read: usize, ix: &mut usize) -> (Vec<Trace>, Vec<TramexError>) {
189        let mut traces = vec![];
190        let mut errors = vec![];
191        for _ in 0..nb_to_read {
192            if *ix >= lines.len() {
193                errors.push(tramex_error!("End of file".to_string(), ErrorCode::EndOfFile));
194                break;
195            }
196            match parse_one_block(&lines[*ix..], ix) {
197                Ok(trace) => {
198                    traces.push(trace);
199                }
200                Err(err) => {
201                    log::error!("{}, some events may be missing", err.message);
202                    errors.push(err);
203                }
204            };
205        }
206        (traces, errors)
207    }
208}