Skip to content

Commit

Permalink
fix: add python and node interpreter options
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtIeSocks committed Dec 4, 2023
1 parent 762323b commit 75bece8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 30 deletions.
14 changes: 13 additions & 1 deletion client/src/components/drawer/Routing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,19 @@ export default function RoutingTab() {

<Divider sx={{ my: 2 }} />
<ListSubheader>Routing</ListSubheader>
<MultiOptionList field="sort_by" buttons={sortByOptions} type="select" />
<MultiOptionList
field="sort_by"
buttons={sortByOptions}
type="select"
itemLabel={(item) => {
if (item === 'tsp') return 'TSP'
if (item.includes('.')) {
const [plugin, ext] = item.split('.')
return `${plugin} (${ext})`
}
return item
}}
/>
<Collapse in={!SORT_BY.some((sort) => sort === sort_by)}>
<NumInput field="route_split_level" min={1} max={12} />
</Collapse>
Expand Down
18 changes: 10 additions & 8 deletions server/algorithms/src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ pub fn main(
SortBy::Random => clusters.sort_random(),
SortBy::Unset => clusters,
SortBy::Custom(plugin) => {
if let Ok(plugin_manager) =
PluginManager::new(plugin, route_split_level, radius, &clusters)
{
if let Ok(sorted_clusters) = plugin_manager.run() {
sorted_clusters
} else {
match PluginManager::new(plugin, route_split_level, radius, &clusters) {
Ok(plugin_manager) => match plugin_manager.run() {
Ok(sorted_clusters) => sorted_clusters,
Err(e) => {
log::error!("Error while running plugin: {}", e);
clusters
}
},
Err(e) => {
log::error!("Plugin not found: {}", e);
clusters
}
} else {
clusters
}
}
};
Expand Down
73 changes: 52 additions & 21 deletions server/algorithms/src/routing/plugin_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ use crate::s2::create_cell_map;
use crate::utils::{self, stringify_points};
use model::api::{point_array::PointArray, single_vec::SingleVec};

#[derive(Debug)]
pub struct PluginManager<'a> {
plugin: String,
plugin_path: String,
interpreter: String,
route_split_level: u64,
radius: f64,
clusters: &'a SingleVec,
Expand Down Expand Up @@ -46,25 +48,31 @@ impl<'a> PluginManager<'a> {
));
};

let interpreter = match plugin.split(".").last() {
Some("py") => "python3",
Some("js") => "node",
Some("ts") => "ts-node",
val => {
if plugin == val.unwrap_or("") {
""
} else {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Unrecognized plugin, please create a PR to add support for it",
));
}
}
};
Ok(PluginManager {
plugin: plugin.to_string(),
plugin_path,
interpreter: interpreter.to_string(),
route_split_level,
radius,
clusters,
})
}

// pub fn run(self) -> SingleVec {
// match self.error_wrapper() {
// Ok(result) => result,
// Err(err) => {
// log::error!("{} failed: {}", self.plugin, err);
// self.clusters
// }
// }
// }

pub fn run(self) -> Result<SingleVec, std::io::Error> {
log::info!("starting {}...", self.plugin);
let time = Instant::now();
Expand Down Expand Up @@ -148,7 +156,11 @@ impl<'a> PluginManager<'a> {
final_routes.append(current);
}

log::info!("full tsp time: {}", time.elapsed().as_secs_f32());
log::info!(
"full {} time: {}",
self.plugin,
time.elapsed().as_secs_f32()
);
Ok(final_routes)
}

Expand All @@ -157,7 +169,16 @@ impl<'a> PluginManager<'a> {
let time = Instant::now();
let clusters = points.clone().sort_s2();
let stringified_points = stringify_points(&clusters);
let mut child = match Command::new(&self.plugin_path)

let mut child = if self.interpreter.is_empty() {
Command::new(&self.plugin_path)
} else {
Command::new(&self.interpreter)
};
if !self.interpreter.is_empty() {
child.arg(&self.plugin_path);
}
let mut child = match child
.args(&["--input", &stringified_points])
.args(&["--radius", &self.radius.to_string()])
.args(&["--route_split_level", &self.route_split_level.to_string()])
Expand All @@ -174,7 +195,7 @@ impl<'a> PluginManager<'a> {
None => {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Failed to open stdin",
"failed to open stdin",
));
}
};
Expand All @@ -198,16 +219,26 @@ impl<'a> PluginManager<'a> {
Err(err) => return Err(err),
};
let output = String::from_utf8_lossy(&output.stdout);
let output = output
let output_indexes = output
.split(",")
.filter_map(|s| s.parse::<usize>().ok())
.filter_map(|s| s.trim().parse::<usize>().ok())
.collect::<Vec<usize>>();

log::info!(
"{} child process finished in {}s",
self.plugin,
time.elapsed().as_secs_f32()
);
Ok(output.into_iter().map(|i| clusters[i]).collect())
if output_indexes.is_empty() {
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!(
"no valid output from child process {}, output should return comma separated indexes of the input clusters in the order they should be routed",
output
),
))
} else {
log::info!(
"{} child process finished in {}s",
self.plugin,
time.elapsed().as_secs_f32()
);
Ok(output_indexes.into_iter().map(|i| clusters[i]).collect())
}
}
}

0 comments on commit 75bece8

Please sign in to comment.