Skip to content

Commit

Permalink
refactor(backend,taxonomy-editor-frontend): Handle background task on…
Browse files Browse the repository at this point in the history
… frontend
  • Loading branch information
alice.juan committed Jan 10, 2024
1 parent 88475cb commit 88350fb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
2 changes: 2 additions & 0 deletions backend/editor/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ class StatusFilter(str, Enum):

OPEN = "OPEN"
CLOSED = "CLOSED"
LOADING = "LOADING"
FAILED = "FAILED"


@app.exception_handler(RequestValidationError)
Expand Down
17 changes: 16 additions & 1 deletion backend/editor/entries.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Database helper functions for API
"""
import asyncio
import re
import shutil
import tempfile
Expand Down Expand Up @@ -119,6 +120,10 @@ def parse_taxonomy(self, uploadfile=None):
except Exception as e:
raise TaxonomyParsingError() from e
except Exception as e:
# async with TransactionCtx():
# await self.set_project_status(status="FAILED")
# Call the asynchronous function to update status without awaiting it
asyncio.create_task(self.set_project_status_async(status="FAILED"))
raise TaxonomyImportError() from e

async def import_from_github(self, description, background_tasks: BackgroundTasks):
Expand All @@ -128,6 +133,9 @@ async def import_from_github(self, description, background_tasks: BackgroundTask
# Close current transaction to use the session variable in parser
await get_current_transaction().commit()

async with TransactionCtx():
await self.create_project(description) # Creates a "project node" in neo4j

# Add the task to background tasks
background_tasks.add_task(self.parse_taxonomy)
print("Background task added")
Expand All @@ -148,6 +156,9 @@ async def upload_taxonomy(
# Close current transaction to use the session variable in parser
await get_current_transaction().commit()
try:
async with TransactionCtx():
await self.create_project(description)

# Add the task to background tasks
background_tasks.add_task(self.parse_taxonomy, uploadfile)
print("Background task added")
Expand Down Expand Up @@ -263,10 +274,14 @@ async def create_project(self, description):
"taxonomy_name": self.taxonomy_name,
"branch_name": self.branch_name,
"description": description,
"status": "OPEN",
"status": "LOADING",
}
await get_current_transaction().run(query, params)

async def set_project_status_async(self, status):
async with TransactionCtx():
await self.set_project_status(status=status)

async def set_project_status(self, status):
"""
Helper function to update a Taxonomy Editor project status
Expand Down
13 changes: 11 additions & 2 deletions taxonomy-editor-frontend/src/pages/go-to-project/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,30 @@ const GoToProject = ({ clearNavBarLinks }: Props) => {
const navigate = useNavigate();

const { data, isPending, isError } = useFetch<ProjectsAPIResponse>(
`${API_URL}projects?status=OPEN`
`${API_URL}projects`
);

useEffect(() => {
let newProjects: ProjectType[] = [];

if (data) {
const backendProjects = data.map(
({ id, branch_name, taxonomy_name, description, errors_count }) => {
({
id,
branch_name,
taxonomy_name,
description,
errors_count,
status,
}) => {
return {
id, // needed by MaterialTable as key
projectName: id,
taxonomyName: toTitleCase(taxonomy_name),
branchName: branch_name,
description: description,
errors_count: errors_count,
status: status,
};
}
);
Expand Down Expand Up @@ -117,6 +125,7 @@ const GoToProject = ({ clearNavBarLinks }: Props) => {
}
},
},
{ title: "Status", field: "status" },
]}
options={{
actionsColumnIndex: -1,
Expand Down
8 changes: 7 additions & 1 deletion taxonomy-editor-frontend/src/pages/root-nodes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const RootNodes = ({
);
}

if (isPending || !nodes) {
if (isPending || !nodes || nodes.length === 0) {
return (
<Box
sx={{
Expand All @@ -93,6 +93,12 @@ const RootNodes = ({
}}
>
<CircularProgress />
<Typography sx={{ m: 5 }} variant="h6">
Taxonomy parsing may take several minutes, depending on the complexity
of the taxonomy being imported.
<br />
Kindly refresh the page to view the updated status of the project.
</Typography>
</Box>
);
}
Expand Down
6 changes: 4 additions & 2 deletions taxonomy-editor-frontend/src/pages/startproject/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const StartProject = ({ clearNavBarLinks }) => {
const baseUrl = createBaseURL(toSnakeCase(taxonomyName), branchName);
setLoading(true);
const dataToBeSent = { description: description };
let errorMessage: string = "Unable to import";

fetch(`${baseUrl}import`, {
method: "POST",
Expand All @@ -51,12 +52,13 @@ const StartProject = ({ clearNavBarLinks }) => {
.then(async (response) => {
const responseBody = await response.json();
if (!response.ok) {
throw new Error(responseBody?.detail ?? "Unable to import");
errorMessage = responseBody?.detail ?? "Unable to import";
throw new Error(errorMessage);
}
navigate(`/${toSnakeCase(taxonomyName)}/${branchName}/entry`);
})
.catch(() => {
setErrorMessage("Unable to import");
setErrorMessage(errorMessage);
})
.finally(() => setLoading(false));
};
Expand Down

0 comments on commit 88350fb

Please sign in to comment.