Skip to main content

formualizer_eval/engine/
formula_ingest.rs

1use std::collections::BTreeMap;
2use std::sync::Arc;
3
4use super::FormulaPlaneMode;
5use super::arena::AstNodeId;
6
7#[derive(Clone, Debug)]
8pub struct FormulaIngestRecord {
9    pub row: u32,
10    pub col: u32,
11    pub ast_id: AstNodeId,
12    pub formula_text: Option<Arc<str>>,
13}
14
15impl FormulaIngestRecord {
16    pub fn new(row: u32, col: u32, ast_id: AstNodeId, formula_text: Option<Arc<str>>) -> Self {
17        Self {
18            row,
19            col,
20            ast_id,
21            formula_text,
22        }
23    }
24}
25
26#[derive(Clone, Debug)]
27pub struct FormulaIngestBatch {
28    pub sheet_name: String,
29    pub formulas: Vec<FormulaIngestRecord>,
30}
31
32impl FormulaIngestBatch {
33    pub fn new(sheet_name: impl Into<String>, formulas: Vec<FormulaIngestRecord>) -> Self {
34        Self {
35            sheet_name: sheet_name.into(),
36            formulas,
37        }
38    }
39
40    pub fn len(&self) -> usize {
41        self.formulas.len()
42    }
43
44    pub fn is_empty(&self) -> bool {
45        self.formulas.is_empty()
46    }
47}
48
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct FormulaIngestReport {
51    pub mode: FormulaPlaneMode,
52    pub formula_cells_seen: u64,
53    pub graph_formula_cells_materialized: u64,
54
55    pub shadow_candidate_cells: u64,
56    pub shadow_accepted_span_cells: u64,
57    pub shadow_fallback_cells: u64,
58    pub shadow_templates_interned: u64,
59    pub shadow_spans_created: u64,
60
61    pub graph_formula_vertices_avoided_shadow: u64,
62    pub ast_roots_avoided_shadow: u64,
63    pub edge_rows_avoided_shadow: u64,
64
65    pub graph_vertices_created: u64,
66    pub graph_edges_created: u64,
67
68    pub fallback_reasons: BTreeMap<String, u64>,
69}
70
71impl Default for FormulaIngestReport {
72    fn default() -> Self {
73        Self {
74            mode: FormulaPlaneMode::Off,
75            formula_cells_seen: 0,
76            graph_formula_cells_materialized: 0,
77            shadow_candidate_cells: 0,
78            shadow_accepted_span_cells: 0,
79            shadow_fallback_cells: 0,
80            shadow_templates_interned: 0,
81            shadow_spans_created: 0,
82            graph_formula_vertices_avoided_shadow: 0,
83            ast_roots_avoided_shadow: 0,
84            edge_rows_avoided_shadow: 0,
85            graph_vertices_created: 0,
86            graph_edges_created: 0,
87            fallback_reasons: BTreeMap::new(),
88        }
89    }
90}
91
92impl FormulaIngestReport {
93    pub(crate) fn with_mode(mode: FormulaPlaneMode) -> Self {
94        Self {
95            mode,
96            ..Self::default()
97        }
98    }
99
100    pub(crate) fn accumulate(&mut self, other: &Self) {
101        self.formula_cells_seen = self
102            .formula_cells_seen
103            .saturating_add(other.formula_cells_seen);
104        self.graph_formula_cells_materialized = self
105            .graph_formula_cells_materialized
106            .saturating_add(other.graph_formula_cells_materialized);
107        self.shadow_candidate_cells = self
108            .shadow_candidate_cells
109            .saturating_add(other.shadow_candidate_cells);
110        self.shadow_accepted_span_cells = self
111            .shadow_accepted_span_cells
112            .saturating_add(other.shadow_accepted_span_cells);
113        self.shadow_fallback_cells = self
114            .shadow_fallback_cells
115            .saturating_add(other.shadow_fallback_cells);
116        self.shadow_templates_interned = self
117            .shadow_templates_interned
118            .saturating_add(other.shadow_templates_interned);
119        self.shadow_spans_created = self
120            .shadow_spans_created
121            .saturating_add(other.shadow_spans_created);
122        self.graph_formula_vertices_avoided_shadow = self
123            .graph_formula_vertices_avoided_shadow
124            .saturating_add(other.graph_formula_vertices_avoided_shadow);
125        self.ast_roots_avoided_shadow = self
126            .ast_roots_avoided_shadow
127            .saturating_add(other.ast_roots_avoided_shadow);
128        self.edge_rows_avoided_shadow = self
129            .edge_rows_avoided_shadow
130            .saturating_add(other.edge_rows_avoided_shadow);
131        self.graph_vertices_created = self
132            .graph_vertices_created
133            .saturating_add(other.graph_vertices_created);
134        self.graph_edges_created = self
135            .graph_edges_created
136            .saturating_add(other.graph_edges_created);
137        for (reason, count) in &other.fallback_reasons {
138            *self.fallback_reasons.entry(reason.clone()).or_default() += count;
139        }
140    }
141}