From 437c46c3699743c3dea4edfe16a33358bea037a7 Mon Sep 17 00:00:00 2001 From: Marijana Petojevic Date: Wed, 3 Jul 2024 15:06:08 +0200 Subject: [PATCH] Fix: Don't send requests to backend each time file list is loaded to check file remote status of each file in the directory. --- grader_labextension/handlers/assignment.py | 2 -- grader_labextension/handlers/lectures.py | 3 --- grader_labextension/handlers/submissions.py | 3 --- .../handlers/version_control.py | 4 ---- src/components/assignment/assignment.tsx | 11 +++++----- src/components/util/file-item.tsx | 20 ++++++++++--------- src/services/file.service.ts | 2 -- 7 files changed, 17 insertions(+), 28 deletions(-) diff --git a/grader_labextension/handlers/assignment.py b/grader_labextension/handlers/assignment.py index 9b7355e..7c6dc9a 100644 --- a/grader_labextension/handlers/assignment.py +++ b/grader_labextension/handlers/assignment.py @@ -23,7 +23,6 @@ class AssignmentBaseHandler(ExtensionBaseHandler): Tornado Handler class for http requests to /lectures/{lecture_id}/assignments. """ - #@cache(max_age=30) async def get(self, lecture_id: int): """Sends a GET request to the grader service and returns assignments of the lecture @@ -137,7 +136,6 @@ async def put(self, lecture_id: int, assignment_id: int): raise HTTPError(e.code, reason=e.response.reason) self.write(json.dumps(response)) - #@cache(max_age=30) async def get(self, lecture_id: int, assignment_id: int): """Sends a GET-request to the grader service to get a specific assignment diff --git a/grader_labextension/handlers/lectures.py b/grader_labextension/handlers/lectures.py index b26ae58..dab8c8c 100644 --- a/grader_labextension/handlers/lectures.py +++ b/grader_labextension/handlers/lectures.py @@ -19,7 +19,6 @@ class LectureBaseHandler(ExtensionBaseHandler): Tornado Handler class for http requests to /lectures. """ @web.authenticated - #@cache(max_age=60) async def get(self): """Sends a GET-request to the grader service and returns the autorized lectures """ @@ -83,7 +82,6 @@ async def put(self, lecture_id: int): self.write(json.dumps(response_data)) @web.authenticated - #@cache(max_age=60) async def get(self, lecture_id: int): """Sends a GET-request to the grader service and returns the lecture @@ -129,7 +127,6 @@ class LectureStudentsHandler(ExtensionBaseHandler): """ Tornado Handler class for http requests to /lectures/{lecture_id}/users. """ - #@cache(max_age=60) async def get(self, lecture_id: int): """ Sends a GET request to the grader service and returns attendants of lecture diff --git a/grader_labextension/handlers/submissions.py b/grader_labextension/handlers/submissions.py index 8d16f27..2cf6f51 100644 --- a/grader_labextension/handlers/submissions.py +++ b/grader_labextension/handlers/submissions.py @@ -26,7 +26,6 @@ class SubmissionHandler(ExtensionBaseHandler): Tornado Handler class for http requests to /lectures/{lecture_id}/assignments/{assignment_id}/submissions. """ - #@cache(max_age=15) async def get(self, lecture_id: int, assignment_id: int): """ Sends a GET-request to the grader service and returns submissions of a assignment @@ -58,7 +57,6 @@ async def get(self, lecture_id: int, assignment_id: int): path=r"\/lectures\/(?P\d*)\/assignments\/(?P\d*)\/submissions\/(" r"?P\d*)\/logs\/?") class SubmissionLogsHandler(ExtensionBaseHandler): - #@cache(max_age=15) async def get(self, lecture_id: int, assignment_id: int, submission_id: int): """Sends a GET-request to the grader service and returns the logs of a submission @@ -177,7 +175,6 @@ class SubmissionObjectHandler(ExtensionBaseHandler): Tornado Handler class for http requests to /lectures/{lecture_id}/assignments/{assignment_id}/submissions/{submission_id}. """ - #@cache(max_age=15) async def get(self, lecture_id: int, assignment_id: int, submission_id: int): """Sends a GET-request to the grader service and returns a submission diff --git a/grader_labextension/handlers/version_control.py b/grader_labextension/handlers/version_control.py index 234cd78..0feac66 100644 --- a/grader_labextension/handlers/version_control.py +++ b/grader_labextension/handlers/version_control.py @@ -101,7 +101,6 @@ class GitRemoteFileStatusHandler(ExtensionBaseHandler): Tornado Handler class for http requests to /lectures/{lecture_id}/assignments/{assignment_id}/remote-file-status/{repo}. """ - # @cache(max_age=15) async def get(self, lecture_id: int, assignment_id: int, repo: str): if repo not in {"assignment", "source", "release"}: self.log.error(HTTPStatus.NOT_FOUND) @@ -141,7 +140,6 @@ class GitRemoteStatusHandler(ExtensionBaseHandler): Tornado Handler class for http requests to /lectures/{lecture_id}/assignments/{assignment_id}/remote_status/{repo}. """ - #@cache(max_age=15) async def get(self, lecture_id: int, assignment_id: int, repo: str): if repo not in {"assignment", "source", "release"}: self.log.error(HTTPStatus.NOT_FOUND) @@ -178,8 +176,6 @@ class GitLogHandler(ExtensionBaseHandler): """ Tornado Handler class for http requests to /lectures/{lecture_id}/assignments/{assignment_id}/log/{repo}. """ - - #@cache(max_age=15) async def get(self, lecture_id: int, assignment_id: int, repo: str): """ Sends a GET request to the grader service to get the logs of a given repo. diff --git a/src/components/assignment/assignment.tsx b/src/components/assignment/assignment.tsx index 6d0e5dd..388a0dd 100644 --- a/src/components/assignment/assignment.tsx +++ b/src/components/assignment/assignment.tsx @@ -115,16 +115,17 @@ export const AssignmentComponent = () => { enabled: !!lecture && !!assignment, }); - const { data: asssignemntProperties } = useQuery({ - queryKey: ['assignmentProperties', lectureId, assignmentId], - queryFn: () => getAssignmentProperties(lecture.id, assignment.id).then(properties => { + React.useEffect(() => { + if (lecture && assignment) { + getAssignmentProperties(lecture.id, assignment.id).then(properties => { const gb = new GradeBook(properties); setFileList([ ...gb.getNotebooks().map(n => n + '.ipynb'), ...gb.getExtraFiles() ]); - }) - }); + }); + } + }, []) diff --git a/src/components/util/file-item.tsx b/src/components/util/file-item.tsx index fad35a3..edd7b2d 100644 --- a/src/components/util/file-item.tsx +++ b/src/components/util/file-item.tsx @@ -25,6 +25,7 @@ import { RepoType } from './repo-type'; import CompareArrowsIcon from '@mui/icons-material/CompareArrows'; import CheckIcon from '@mui/icons-material/Check'; import PublishRoundedIcon from '@mui/icons-material/PublishRounded'; +import { UseQueryOptions, useQuery } from '@tanstack/react-query'; interface IFileItemProps { file: File; @@ -54,21 +55,22 @@ const FileItem = ({ }; const [isSelected, setIsSelected] = React.useState(true); - const [fileRemoteStatus, setFileRemoteStatus] = React.useState( - null as 'up_to_date' | 'push_needed' | 'divergent' - ); - React.useEffect(() => { - getRemoteFileStatus( + + const fileStatusQueryOptions: UseQueryOptions<'up_to_date' | 'push_needed' | 'divergent', Error> = { + queryKey: ['fileStatus', lecture?.id, assignment?.id, file.path], + queryFn: () => getRemoteFileStatus( lecture, assignment, RepoType.SOURCE, getRelativePath(file.path, 'source'), true - ).then(status => { - setFileRemoteStatus(status as 'up_to_date' | 'push_needed' | 'divergent'); - }); - }, [assignment, lecture]); + ) as Promise<'up_to_date' | 'push_needed' | 'divergent'>, + enabled: !!lecture && !!assignment, + }; + + const { data: fileRemoteStatus } = useQuery(fileStatusQueryOptions); + const getFleRemoteStatusText = ( status: 'up_to_date' | 'push_needed' | 'divergent' diff --git a/src/services/file.service.ts b/src/services/file.service.ts index 838363d..298373f 100644 --- a/src/services/file.service.ts +++ b/src/services/file.service.ts @@ -6,7 +6,6 @@ import { FilterFileBrowserModel } from '@jupyterlab/filebrowser/lib/model'; import { GlobalObjects } from '../index'; -import { renameFile } from '@jupyterlab/docmanager'; import { Contents } from '@jupyterlab/services'; import { Assignment } from '../model/assignment'; import { HTTPMethod, request } from './request.service'; @@ -39,7 +38,6 @@ export interface File { content: File[]; } -// TODO: getFiles should return Promise export const getFiles = async (path: string): Promise => { if (path === null) { return [];