Skip to content

Commit

Permalink
More robust OCEL2 preset file selection
Browse files Browse the repository at this point in the history
  • Loading branch information
aarkue committed Mar 27, 2024
1 parent 464cf24 commit 38e48a1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 47 deletions.
58 changes: 24 additions & 34 deletions backend/web-server/src/load_ocel/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Option<Vec<&'static str>>>) {
return (
StatusCode::OK,
Json(Some(OCEL_PATHS.iter().map(|p| p.name).collect())),
);
pub async fn get_available_ocels() -> (StatusCode, Json<Option<Vec<String>>>) {
let mut ocel_names: Vec<String> = 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(
Expand Down Expand Up @@ -70,16 +68,8 @@ pub fn load_ocel_file_to_state(name: &str, state: &AppState) -> Option<OCELInfo>
}

pub fn load_ocel_file(name: &str) -> Result<OCEL, std::io::Error> {
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)
}
2 changes: 1 addition & 1 deletion frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<link rel="icon" type="image/svg+xml" href="/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>OCEDECLARE</title>
</head>
Expand Down
13 changes: 4 additions & 9 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,8 @@ function App() {
const [selectedOcel, setSelectedOcel] = useState<string>();
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 {
Expand Down Expand Up @@ -121,7 +116,7 @@ function App() {
<div className="bg-gray-50 border-r border-r-slate-200 px-2">
<img src="/favicon.png" className="w-[7rem] h-[7rem] mx-auto my-4" />
<div className="flex flex-col gap-2">
{ocelInfo !== undefined && (
{ocelInfo != null && (
<span className="flex flex-col items-center mx-auto text-xl">
<span className=" font-semibold text-green-700">
OCEL loaded
Expand All @@ -130,7 +125,7 @@ function App() {
<span>{ocelInfo.num_objects} Objects</span>
</span>
)}
{ocelInfo !== undefined && (
{ocelInfo != null && (
<>
<MenuLink to="/ocel-info">OCEL Info</MenuLink>
<MenuLink
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/BackendProviderContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ export const BackendProviderContext = createContext<BackendProvider>({

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 (
Expand Down
2 changes: 1 addition & 1 deletion tauri/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<link rel="icon" type="image/svg+xml" href="/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>OCEDECLARE</title>
</head>
Expand Down

0 comments on commit 38e48a1

Please sign in to comment.