Skip to main content

mprobe_vis/
layout.rs

1//! Defines the structure and the layout of the data used for
2//! visualizing diagnostic metrics.
3
4mod data;
5mod iter;
6mod series;
7
8use std::fs;
9use std::path::Path;
10use std::path::PathBuf;
11
12use mprobe_diagnostics::DiagnosticData;
13
14use crate::error::Result;
15use crate::layout::data::DataEngine;
16use crate::layout::iter::ErrorHandlingIter;
17use crate::template::TemplateEngine;
18
19/// Coordinates the creation of the data visualization.
20///
21/// The data visualization directory is structured as follows:
22///
23/// ./vis/index.html
24///
25/// ./vis/views/view1.html
26/// ./vis/views/view2.html
27/// ./vis/views/...
28/// ./vis/views/viewN.html
29///
30/// ./vis/data/data1.js
31/// ./vis/data/data2.js
32/// ./vis/data/...
33/// ./vis/data/dataN.js
34///
35/// The __index__ file represents the entry point into the data visualization.
36/// The __data__ directory contains the chart data.
37/// The __view__ directory contains the chart visualizations.
38pub struct VisLayout {
39    index_file_path: PathBuf,
40    views_path: PathBuf,
41    data_path: PathBuf,
42}
43
44impl VisLayout {
45    const MAIN_DIR_NAME: &str = "vis";
46    const DATA_DIR_NAME: &str = "data";
47    const VIEWS_DIR_NAME: &str = "views";
48    const INDEX_FILE_NAME: &str = "index.html";
49
50    /// Initializes a directory for the data visualization and
51    /// creates a new instance of [VisLayout].
52    pub fn init(path: &Path) -> Result<VisLayout> {
53        let root_path = path.join(Self::MAIN_DIR_NAME);
54        let index_file_path = root_path.join(Self::INDEX_FILE_NAME);
55        let data_path = root_path.join(Self::DATA_DIR_NAME);
56        let views_path = root_path.join(Self::VIEWS_DIR_NAME);
57
58        if root_path.exists() {
59            fs::remove_dir_all(&root_path)?;
60        }
61        fs::create_dir(&root_path)?;
62
63        Ok(Self {
64            data_path,
65            index_file_path,
66            views_path,
67        })
68    }
69
70    /// Generates a visualization report based on the provided diagnostic data.
71    pub fn generate_report(&self, diagnostic_data: DiagnosticData) -> Result<()> {
72        let mut data_engine = DataEngine::new(&self.data_path);
73        let metrics = ErrorHandlingIter::new(diagnostic_data.into_iter());
74        let charts = data_engine.render(metrics)?;
75
76        let template = TemplateEngine::new(&self.index_file_path, &self.views_path);
77        template.render(&charts)
78    }
79}