From 38e48a1dd5b50eef86ba94df4ef95d8b9c53fd4d Mon Sep 17 00:00:00 2001 From: aarkue Date: Wed, 27 Mar 2024 16:31:24 +0100 Subject: [PATCH] More robust OCEL2 preset file selection --- backend/web-server/src/load_ocel/mod.rs | 58 ++++++++++--------------- frontend/index.html | 2 +- frontend/src/App.tsx | 13 ++---- frontend/src/BackendProviderContext.ts | 5 ++- tauri/index.html | 2 +- 5 files changed, 33 insertions(+), 47 deletions(-) diff --git a/backend/web-server/src/load_ocel/mod.rs b/backend/web-server/src/load_ocel/mod.rs index db46d4a..4c2177e 100644 --- a/backend/web-server/src/load_ocel/mod.rs +++ b/backend/web-server/src/load_ocel/mod.rs @@ -1,4 +1,7 @@ -use std::{fs::File, io::BufReader}; +use std::{ + fs::{self, File}, + io::BufReader, +}; use axum::{extract::State, http::StatusCode, Json}; use ocedeclare_shared::OCELInfo; @@ -19,28 +22,23 @@ pub struct OCELFilePath { path: &'static str, } -pub const DEFAULT_OCEL_FILE: &str = "order-management"; +pub const DEFAULT_OCEL_FILE: &str = "order-management.json"; +pub const DATA_PATH: &str = "./data/"; -pub const OCEL_PATHS: &[OCELFilePath] = &[ - OCELFilePath { - name: "ContainerLogistics", - path: "./data/ContainerLogistics.json", - }, - OCELFilePath { - name: "ocel2-p2p", - path: "./data/ocel2-p2p.json", - }, - OCELFilePath { - name: "order-management", - path: "./data/order-management.json", - }, -]; - -pub async fn get_available_ocels() -> (StatusCode, Json>>) { - return ( - StatusCode::OK, - Json(Some(OCEL_PATHS.iter().map(|p| p.name).collect())), - ); +pub async fn get_available_ocels() -> (StatusCode, Json>>) { + let mut ocel_names: Vec = Vec::new(); + if let Ok(paths) = fs::read_dir(DATA_PATH) { + for dir_entry_res in paths { + if let Ok(dir_entry) = dir_entry_res { + let path_buf = dir_entry.path(); + let path = path_buf.as_os_str().to_str().unwrap(); + if path.ends_with(".json") || path.ends_with(".xml") { + ocel_names.push(path.split("/").last().unwrap().to_string()) + } + } + } + } + return (StatusCode::OK, Json(Some(ocel_names))); } pub async fn load_ocel_file_req( @@ -70,16 +68,8 @@ pub fn load_ocel_file_to_state(name: &str, state: &AppState) -> Option } pub fn load_ocel_file(name: &str) -> Result { - match OCEL_PATHS.iter().find(|op| op.name == name) { - Some(ocel_path) => { - let file = File::open(ocel_path.path)?; - let reader = BufReader::new(file); - let ocel: OCEL = serde_json::from_reader(reader)?; - Ok(ocel) - } - None => Err(std::io::Error::new( - std::io::ErrorKind::Other, - "OCEL with that name not found", - )), - } + let file = File::open(format!("{DATA_PATH}{name}"))?; + let reader = BufReader::new(file); + let ocel: OCEL = serde_json::from_reader(reader)?; + Ok(ocel) } diff --git a/frontend/index.html b/frontend/index.html index 68676a8..78967a6 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -2,7 +2,7 @@ - + OCEDECLARE diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 5ab221d..a010f4c 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -35,13 +35,8 @@ function App() { const [selectedOcel, setSelectedOcel] = useState(); const backend = useContext(BackendProviderContext); useEffect(() => { - void toast - .promise(backend["ocel/info"](), { - loading: "Loading OCEL Info", - success: "Got OCEL info", - error: "Failed to load OCEL info", - }) - .then((info) => { + console.log({backend}); + void backend["ocel/info"]().then((info) => { if (info !== undefined) { setOcelInfo(info); } else { @@ -121,7 +116,7 @@ function App() {
- {ocelInfo !== undefined && ( + {ocelInfo != null && ( OCEL loaded @@ -130,7 +125,7 @@ function App() { {ocelInfo.num_objects} Objects )} - {ocelInfo !== undefined && ( + {ocelInfo != null && ( <> OCEL Info ({ export const API_WEB_SERVER_BACKEND_PROVIDER: BackendProvider = { "ocel/info": async () => { - return await ( + const res = ( await fetch("http://localhost:3000/ocel/info", { method: "get" }) - ).json(); + ); + return await res.json(); }, "ocel/available": async () => { return await ( diff --git a/tauri/index.html b/tauri/index.html index 68676a8..78967a6 100644 --- a/tauri/index.html +++ b/tauri/index.html @@ -2,7 +2,7 @@ - + OCEDECLARE