-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcel.rs
197 lines (184 loc) · 8.71 KB
/
excel.rs
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
190
191
192
193
194
195
196
197
use crate::commands::{ConflictResolutions, NodeCommands, NodeResponses};
use crate::model::sedaroml::{read_model, Model, ModelDiff};
use crate::nodes::traits::Exchangeable;
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use pyo3::prelude::*;
use std::borrow::{Borrow, BorrowMut};
use crate::utils::python_signal_handler;
use std::sync::mpsc;
use std::thread::{self};
use log::{debug, error};
use notify_debouncer_mini::{
notify::RecursiveMode,
new_debouncer,
DebounceEventResult,
};
use tempfile::NamedTempFile;
#[derive(Clone)]
pub struct Excel {
identifier: String,
pub excel_filename: String,
sedaroml_filename: String,
rep: Option<Model>,
tx: mpsc::Sender<NodeCommands>,
rx: Arc<Mutex<mpsc::Receiver<NodeResponses>>>,
}
impl Excel {
pub fn new(identifier: String, filename: String) -> Arc<Mutex<Excel>> {
let mut sedaroml_filename = filename.to_string();
sedaroml_filename.push_str(".json");
let sedaroml_filename_clone = sedaroml_filename.clone();
let identifier_clone = identifier.to_string().clone();
let excel_filename = filename.to_string();
let (tx_to_node, rx_in_node) = mpsc::channel::<NodeCommands>();
let (tx_to_exchange, rx_in_exchange) = mpsc::channel::<NodeResponses>();
thread::spawn(move || {
// Setup
let _excel_filename = excel_filename.clone();
let _sedaroml_filename = sedaroml_filename_clone.clone();
let _identifier = identifier_clone.clone();
let mut excel_watcher = new_debouncer(Duration::from_millis(5), move |res: DebounceEventResult| {
match res {
Ok(_event) => {
excel_to_sedaroml(&_excel_filename, &_sedaroml_filename).unwrap_or_else(
|e| panic!("{}: Failed to convert Excel to SedaroML: {}", _identifier, e)
);
},
Err(e) => error!("Watch error: {:?}", e),
}
}).unwrap_or_else(|_| panic!("Failed to create excel watcher"));
let watcher = excel_watcher.watcher();
loop {
match rx_in_node.recv_timeout(Duration::from_millis(100)) {
Ok(command) => {
debug!("{}: Received command: {:?}", identifier_clone, command);
match command {
NodeCommands::Start => {
if !Path::exists(Path::new(&sedaroml_filename_clone)) {
debug!("{}: SedaroML file doesn't exist. Generating from: {}", identifier_clone, &excel_filename);
excel_to_sedaroml(&excel_filename, &sedaroml_filename_clone).unwrap_or_else(
|e| panic!("{}: Failed to convert Excel to SedaroML: {}", identifier_clone, e)
);
} else {
// Check for changes since exchange was last run
let current_rep = read_model(&sedaroml_filename_clone).unwrap_or_else(
|e| panic!("{}: Failed to read SedaroML: {:?}", identifier_clone, e)
);
let temp = NamedTempFile::new().unwrap();
excel_to_sedaroml(&excel_filename, temp.path().to_str().unwrap()).unwrap_or_else(
|e| panic!("{}: Failed to convert Excel to SedaroML: {}", identifier_clone, e)
);
let temp_rep = read_model(temp.path().to_str().unwrap()).unwrap_or_else(
|e| panic!("{}: Failed to read SedaroML: {:?}", identifier_clone, e)
);
let diff = current_rep.diff(&temp_rep);
if !diff.is_empty() {
tx_to_exchange.send(NodeResponses::Conflict(diff)).unwrap();
continue;
}
}
watcher.watch(&Path::new(&excel_filename), RecursiveMode::Recursive).unwrap_or_else(|e| panic!("Failed to watch path: {}: {}", excel_filename, e));
tx_to_exchange.send(NodeResponses::Started).unwrap();
},
NodeCommands::ResolveConflict(resolution_strategy) => {
let t = Instant::now();
match resolution_strategy {
ConflictResolutions::KeepRep => {
sedaroml_to_excel(&sedaroml_filename_clone, &excel_filename).unwrap_or_else(
|e| panic!("{}: Failed to convert SedaroML to Excel: {}", identifier_clone, e)
);
},
ConflictResolutions::UpdateRep => {
excel_to_sedaroml(&excel_filename, &sedaroml_filename_clone).unwrap_or_else(
|e| panic!("{}: Failed to convert Excel to SedaroML: {}", identifier_clone, e)
);
},
}
tx_to_exchange.send(NodeResponses::ConflictResolved(t.elapsed())).unwrap();
watcher.watch(&Path::new(&excel_filename), RecursiveMode::Recursive).unwrap_or_else(|e| panic!("Failed to watch path: {}: {}", excel_filename, e));
tx_to_exchange.send(NodeResponses::Started).unwrap();
},
NodeCommands::Stop => { tx_to_exchange.send(NodeResponses::Stopped).unwrap() },
NodeCommands::Changed(diff) => {
let t = Instant::now();
reconcile_diff_to_excel(&sedaroml_filename_clone, &diff, &excel_filename).unwrap_or_else(
|e| panic!("{}: Failed to convert SedaroML ModelDiff to Excel: {}", identifier_clone, e)
);
tx_to_exchange.send(NodeResponses::Done(t.elapsed())).unwrap();
},
NodeCommands::Done => {},
}
},
Err(_) => {},
};
python_signal_handler().unwrap();
}
});
let exchangeable = Excel {
identifier: identifier.into(),
excel_filename: filename.into(),
sedaroml_filename: sedaroml_filename.clone(),
rep: None,
tx: tx_to_node,
rx: Arc::new(Mutex::new(rx_in_exchange)),
};
Arc::new(Mutex::new(exchangeable))
}
}
impl Exchangeable for Excel {
fn identifier(&self) -> String { self.identifier.clone() }
fn sedaroml_filename(&self) -> String { self.sedaroml_filename.clone() }
fn rep(&self) -> &Model {
match self.rep.borrow() {
Some(rep) => rep,
None => panic!("{}: Representation not initialized", self.identifier()),
}
}
fn rep_mut(&mut self) -> &mut Model {
let iden = self.identifier();
match self.rep.borrow_mut() {
Some(rep) => rep,
None => panic!("{}: Representation not initialized", iden),
}
}
fn tx(&self) -> &mpsc::Sender<NodeCommands> { &self.tx }
fn rx(&self) -> &Arc<Mutex<mpsc::Receiver<NodeResponses>>> { &self.rx }
fn refresh_rep(&mut self) {
self.rep = Some(read_model(&self.sedaroml_filename()).unwrap_or_else(
|e| panic!("{}: Failed to read SedaroML: {:?}", self.identifier(), e)
));
}
}
fn excel_to_sedaroml(excel_filename: &str, sedaroml_filename: &str) -> PyResult<()> {
Python::with_gil(|py| {
let sys = py.import_bound("sys")?;
sys.getattr("path")?.call_method1("append", ("/Users/sebastianwelsh/Development/sedaro/modex/.venv/lib/python3.12/site-packages",))?;
sys.getattr("path")?.call_method1("append", ("/Users/sebastianwelsh/Development/sedaro/modex/.venv/lib/python3.12/site-packages/aeosa",))?; // TODO: Ewwww!!! How to do this better??
let module = PyModule::import_bound(py, "modex.excel")?;
module.getattr("excel_to_sedaroml")?.call1((excel_filename, sedaroml_filename))?;
Ok(())
})
}
fn sedaroml_to_excel(sedaroml_filename: &str, excel_filename: &str) -> PyResult<()> {
Python::with_gil(|py| {
let sys = py.import_bound("sys")?;
sys.getattr("path")?.call_method1("append", ("/Users/sebastianwelsh/Development/sedaro/modex/.venv/lib/python3.12/site-packages",))?;
sys.getattr("path")?.call_method1("append", ("/Users/sebastianwelsh/Development/sedaro/modex/.venv/lib/python3.12/site-packages/aeosa",))?; // TODO: Ewwww!!! How to do this better??
let module = PyModule::import_bound(py, "modex.excel")?;
module.getattr("sedaroml_to_excel")?.call1((sedaroml_filename, excel_filename))?;
Ok(())
})
}
fn reconcile_diff_to_excel(sedaroml_filename: &str, diff: &ModelDiff, excel_filename: &str) -> PyResult<()> {
let diff_str = serde_json::to_string(diff).unwrap();
Python::with_gil(|py| {
let sys = py.import_bound("sys")?;
sys.getattr("path")?.call_method1("append", ("/Users/sebastianwelsh/Development/sedaro/modex/.venv/lib/python3.12/site-packages",))?;
sys.getattr("path")?.call_method1("append", ("/Users/sebastianwelsh/Development/sedaro/modex/.venv/lib/python3.12/site-packages/aeosa",))?; // TODO: Ewwww!!! How to do this better??
let module = PyModule::import_bound(py, "modex.excel")?;
module.getattr("reconcile_diff_to_excel")?.call1((sedaroml_filename, diff_str, excel_filename))?;
Ok(())
})
}