From a74eba146dd924946f4e3e10e89cc3a61ceb87a3 Mon Sep 17 00:00:00 2001 From: Luigi Marini Date: Mon, 9 Dec 2024 15:04:49 -0600 Subject: [PATCH] Use setExtractionStatus action/reducer to propagate the message in the drag and drop box. For some reason we have a duplicate preview in the state. --- src/actions/client.js | 21 +++++++------ src/actions/file.js | 4 +-- .../childComponents/CreateAndUpload.js | 31 +++++++++---------- src/components/childComponents/FilePreview.js | 2 +- src/utils/dataset.js | 18 +++++------ 5 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/actions/client.js b/src/actions/client.js index 2964afa..a451a52 100644 --- a/src/actions/client.js +++ b/src/actions/client.js @@ -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 @@ -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")); + } }; } diff --git a/src/actions/file.js b/src/actions/file.js index 5ec6bb3..d309205 100644 --- a/src/actions/file.js +++ b/src/actions/file.js @@ -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 }); }; diff --git a/src/components/childComponents/CreateAndUpload.js b/src/components/childComponents/CreateAndUpload.js index b5fff12..204345b 100644 --- a/src/components/childComponents/CreateAndUpload.js +++ b/src/components/childComponents/CreateAndUpload.js @@ -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"; @@ -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") { @@ -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 { @@ -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]); @@ -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; } @@ -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]); @@ -144,23 +143,23 @@ export default function CreateAndUpload() {
setMouseHover(true)}> -
- diff --git a/src/components/childComponents/FilePreview.js b/src/components/childComponents/FilePreview.js index f8687e0..188e53b 100644 --- a/src/components/childComponents/FilePreview.js +++ b/src/components/childComponents/FilePreview.js @@ -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; diff --git a/src/utils/dataset.js b/src/utils/dataset.js index 912f20a..18d6dae 100644 --- a/src/utils/dataset.js +++ b/src/utils/dataset.js @@ -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} 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 ); @@ -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(); } };