Skip to content

Commit

Permalink
Fix: Don't send requests to backend each time file list is loaded to …
Browse files Browse the repository at this point in the history
…check file remote status of each file in the directory.
  • Loading branch information
mpetojevic committed Jul 3, 2024
1 parent eb80422 commit 437c46c
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 28 deletions.
2 changes: 0 additions & 2 deletions grader_labextension/handlers/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions grader_labextension/handlers/lectures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions grader_labextension/handlers/submissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,7 +57,6 @@ async def get(self, lecture_id: int, assignment_id: int):
path=r"\/lectures\/(?P<lecture_id>\d*)\/assignments\/(?P<assignment_id>\d*)\/submissions\/("
r"?P<submission_id>\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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions grader_labextension/handlers/version_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 6 additions & 5 deletions src/components/assignment/assignment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
]);
})
});
});
}
}, [])



Expand Down
20 changes: 11 additions & 9 deletions src/components/util/file-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'
Expand Down
2 changes: 0 additions & 2 deletions src/services/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -39,7 +38,6 @@ export interface File {
content: File[];
}

// TODO: getFiles should return Promise<File[]>
export const getFiles = async (path: string): Promise<File[]> => {
if (path === null) {
return [];
Expand Down

0 comments on commit 437c46c

Please sign in to comment.