Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use setExtractionStatus action/reducer to propagate the message in th… #97

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/actions/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,30 @@ export function createUploadExtract(file, config) {
if (file_json !== undefined){
file_json["filename"] = file.name;
// submit uploaded file for extraction
dispatch(setExtractionStatus("Analyzing file"));
if (file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" || file.type =="application/msword"){
const word_pipeline_status = await wordPipeline(file_json, dataset_json, config, clientInfo);
if (word_pipeline_status) {
console.log("File extraction complete.");
dispatch(setExtractionStatus(SET_EXTRACTION_STATUS, true));
console.log("File extraction complete");
dispatch(setExtractionStatus("File extraction complete"));

}
else {
console.error("File extraction failed");
dispatch(setExtractionStatus(SET_EXTRACTION_STATUS, false));
dispatch(setExtractionStatus("File extraction failed"));
}

}
else if (file.type == "application/pdf") {
const pdf_pipeline_status = await pdfPipeline(file_json, dataset_json, config, clientInfo);
if (pdf_pipeline_status) {
console.log("File extraction complete.");
dispatch(setExtractionStatus(SET_EXTRACTION_STATUS, true));
dispatch(setExtractionStatus("File extraction complete"));

}
else {
console.error("File extraction failed");
dispatch(setExtractionStatus(SET_EXTRACTION_STATUS, false));
dispatch(setExtractionStatus("File extraction failed"));
}

// TODO add extracted output files to dataset state
Expand All @@ -75,20 +76,20 @@ export function createUploadExtract(file, config) {
else {
// TODO add error action
console.error("Error in file type");
dispatch(setExtractionStatus(SET_EXTRACTION_STATUS, false));
dispatch(setExtractionStatus("Error in file type"));
}
// after submitting uploaded file for extraction, add the file to dataset state
dispatch(addFileToDataset(ADD_FILE_TO_DATASET, file_json));
}
else {
console.error("Error in clowder upload of file ", file.name)
dispatch(setExtractionStatus(SET_EXTRACTION_STATUS, false));
dispatch(setExtractionStatus("Error in clowder upload of file " + file.name));
}
}
else {
console.error("Error in dataset creation");
dispatch(setExtractionStatus(SET_EXTRACTION_STATUS, false));
}
dispatch(setExtractionStatus("Error in dataset creation"));
}
};
}

4 changes: 2 additions & 2 deletions src/actions/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ export function fetchFileMetadataJsonld(id) {
}

export const SET_EXTRACTION_STATUS = "SET_EXTRACTION_STATUS";
export function setExtractionStatus(type, status) {
export function setExtractionStatus(status) {
return (dispatch) => {
dispatch({
type: type,
type: SET_EXTRACTION_STATUS,
extractionStatus: status
});
};
Expand Down
31 changes: 15 additions & 16 deletions src/components/childComponents/CreateAndUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {getClientInfo} from "../../utils/common";
import Dropfile from "./Dropfile";
import {createUploadExtract} from "../../actions/client";
import {getDatasetMetadata, getFileInDataset} from "../../utils/dataset";
import {fetchFilePreviews} from "../../actions/file";
import { fetchFilePreviews, SET_EXTRACTION_STATUS, setExtractionStatus } from "../../actions/file";
import {SET_DATASET_METADATA, setDatasetMetadata} from "../../actions/dataset";
import {SET_STATEMENT_TYPE, setStatement} from '../../actions/statement';
import config from "../../app.config";
Expand Down Expand Up @@ -55,13 +55,13 @@ export default function CreateAndUpload() {

// useEffect on extractionStatus for preview generation
useEffect(async () => {
if (extractionStatus !== null && extractionStatus === true) {
if (extractionStatus !== null) {
setLoadingText(extractionStatus);
const clientInfo = await getClientInfo();
const file_name = filename.replace(/\.[^/.]+$/, ""); // get filename without extension;
const dataset_id = datasets[0].id;
// check extraction status and html file generation in loop
const html_file_loop = async () => {
setLoadingText("Checking extraction status");
const highlights_filename = file_name + '_highlights' + '.json'
const highlightsFile = await getFileInDataset(dataset_id, "application/json", highlights_filename, clientInfo);
if (highlightsFile !== null && typeof highlightsFile.id === "string") {
Expand All @@ -86,7 +86,6 @@ export default function CreateAndUpload() {
}
datasetMetadata(metadata);

setLoadingText("Extraction completed");
setPreview(false) // Continue button activated
setSpinner(false); // stop display of spinner
} else {
Expand All @@ -102,7 +101,7 @@ export default function CreateAndUpload() {
}
}
else if (extractionStatus === false){
setLoadingText("Error in extraction");
dispatch(setExtractionStatus("Error in extraction"));
setSpinner(false); // stop display of spinner
}
}, [extractionStatus]);
Expand All @@ -111,7 +110,7 @@ export default function CreateAndUpload() {
// onDrop function to trigger createUploadExtract action dispatch
const onDrop = useCallback((acceptedFiles, rejectedFiles) => {
if (!statementTypeSelected) {
setLoadingText("Please select a statement type (Trial results or Trial protocol) first");
dispatch(setExtractionStatus("Please select a statement type (Trial results or Trial protocol) first"));
setSpinner(false);
return;
}
Expand All @@ -122,12 +121,12 @@ export default function CreateAndUpload() {
onDropFile(file)
})
rejectedFiles.map(file => {
setLoadingText("File rejected");
dispatch(setExtractionStatus("File rejected"));
setSpinner(false);
})
}
catch(error) {
setLoadingText("Upload failed", error)
dispatch(setExtractionStatus("Upload failed"));
setSpinner(false);
}
}, [mouseHover, statementTypeSelected]);
Expand All @@ -144,23 +143,23 @@ export default function CreateAndUpload() {
<Box className="createupload">
<LoadingOverlay active={loading} text={loading_text} spinner={spinner}>
<div className="mousehoverdrop" onMouseEnter={() => setMouseHover(true)}>
<Dropfile
onDrop={onDrop}
<Dropfile
onDrop={onDrop}
accept={{
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": [".docx"],
"application/msword": [".doc"],
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": [".docx"],
"application/msword": [".doc"],
"application/pdf": [".pdf"]
}}
message={!statementTypeSelected ?
"Please select a statement type above before uploading files" :
message={!statementTypeSelected ?
"Please select a statement type above before uploading files" :
"Drag and drop files here"}
/>
</div>
</LoadingOverlay>

<div className="radio-buttons-group-div">
<RadioGroup
name="radio-buttons-group"
<RadioGroup
name="radio-buttons-group"
row
onChange={handleStatementChange}
>
Expand Down
2 changes: 1 addition & 1 deletion src/components/childComponents/FilePreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function FilePreview() {
if (filePreviews !== undefined && filePreviews.length > 0) {
const previewsTemp = [];
// get either pdf preview / html preview
if (filePreviews.length === 1){
if (filePreviews.length > 0){
console.log("filePreviews:", filePreviews);
const fileId = filePreviews[0][0].file_id;
const previewsList = filePreviews[0][0].previews;
Expand Down
18 changes: 9 additions & 9 deletions src/utils/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,26 +206,26 @@ export async function setDatasetMetadata(dataset_id, content) {
export async function getDatasetMetadataLoop(dataset_id, extractor_name, clientInfo){
/**
* Asynchronously retrieves dataset metadata by repeatedly checking until it's available.
*
*
* @param {string} dataset_id - The ID of the dataset to retrieve metadata for.
* @param {string} extractor_name - The name of the extractor used (currently unused in the function).
* @param {Object} clientInfo - Client information required for API calls.
* @returns {Promise<Object|Array>} The dataset metadata content if successful, or an empty array if unsuccessful.
*
*
* This function implements a polling mechanism to check for dataset metadata:
* 1. It attempts to retrieve the metadata using the provided dataset ID and client info.
* 2. If metadata is available, it returns the content of the first metadata item.
* 3. If metadata is not yet available, it waits for 30 seconds before trying again.
* 4. This process repeats until metadata is found or an error occurs.
*
*
*/

const extractor_metadata_loop = async () => {
// get dataset metadata as json-ld
const metadata = await getDatasetMetadata(dataset_id, clientInfo);
if (metadata !== null && metadata.length > 0) {
// Filter metadata for the specified extractor
const relevantMetadata = metadata.filter(item =>
const relevantMetadata = metadata.filter(item =>
item.content && item.content.extractor === extractor_name
);

Expand All @@ -235,13 +235,13 @@ export async function getDatasetMetadataLoop(dataset_id, extractor_name, clientI
return relevantMetadata[0].content;
} else {
console.log(`No metadata found for extractor: ${extractor_name}`);
console.log("Waiting 30 seconds before checking again...");
await sleep(30000);
console.log("Waiting 5 seconds before checking again...");
await sleep(5000);
return await extractor_metadata_loop();
}
} else {
console.log("check for extraction metadata after 30s");
await sleep(30000);
console.log("check for extraction metadata after 5s");
await sleep(5000);
await extractor_metadata_loop();
}
};
Expand Down