Skip to content

Commit

Permalink
feat: filters on submission table (#1191)
Browse files Browse the repository at this point in the history
Co-authored-by: sujanadh <sujanadh07@gmail.com>
  • Loading branch information
Sujanadh and sujanadh authored Feb 15, 2024
1 parent 4e95163 commit 9201779
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
40 changes: 36 additions & 4 deletions src/backend/app/submissions/submission_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
from collections import Counter
from datetime import datetime, timedelta
from io import BytesIO
from typing import Optional

import sozipfile.sozipfile as zipfile
from asgiref.sync import async_to_sync
from dateutil import parser
from fastapi import HTTPException, Response
from fastapi.responses import FileResponse
from loguru import logger as log
Expand Down Expand Up @@ -765,7 +767,15 @@ async def get_submissions_by_date(
return response


async def get_submission_by_project(project_id: int, skip: 0, limit: 100, db: Session):
async def get_submission_by_project(
project_id: int,
skip: 0,
limit: 100,
db: Session,
submitted_by: Optional[str] = None,
review_state: Optional[str] = None,
submitted_date: Optional[str] = None,
):
"""Get submission by project.
Retrieves a paginated list of submissions for a given project.
Expand All @@ -775,6 +785,9 @@ async def get_submission_by_project(project_id: int, skip: 0, limit: 100, db: Se
skip (int): The number of submissions to skip.
limit (int): The maximum number of submissions to retrieve.
db (Session): The database session.
submitted_by: username of submitter.
review_state: reviewState of the submission.
submitted_date: date of submissions.
Returns:
Tuple[int, List]: A tuple containing the total number of submissions and
Expand All @@ -795,17 +808,36 @@ async def get_submission_by_project(project_id: int, skip: 0, limit: 100, db: Se

with zipfile.ZipFile(file, "r") as zip_ref:
with zip_ref.open("submissions.json") as file_in_zip:
content = file_in_zip.read()
content = json.loads(file_in_zip.read())
if submitted_by:
content = [
sub for sub in content if submitted_by.lower() in sub["username"].lower()
]
if review_state:
content = [
sub
for sub in content
if sub.get("__system", {}).get("reviewState") == review_state
]
if submitted_date:
content = [
sub
for sub in content
if parser.parse(sub.get("end")).date()
== parser.parse(submitted_date).date()
]

content = json.loads(content)
start_index = skip
end_index = skip + limit
paginated_content = content[start_index:end_index]
return len(content), paginated_content


async def get_submission_by_task(
project: db_models.DbProject, task_id: int, filters: dict, db: Session
project: db_models.DbProject,
task_id: int,
filters: dict,
db: Session,
):
"""Get submissions and count by task.
Expand Down
30 changes: 29 additions & 1 deletion src/backend/app/submissions/submission_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ async def submission_table(
project_id: int,
page: int = Query(1, ge=1),
results_per_page: int = Query(13, le=100),
submitted_by: Optional[str] = None,
review_state: Optional[str] = None,
submitted_date: Optional[str] = Query(
None, title="Submitted Date", description="Date in format (e.g., 'YYYY-MM-DD')"
),
db: Session = Depends(database.get_db),
current_user: AuthUser = Depends(mapper),
):
Expand All @@ -396,7 +401,7 @@ async def submission_table(
skip = (page - 1) * results_per_page
limit = results_per_page
count, data = await submission_crud.get_submission_by_project(
project_id, skip, limit, db
project_id, skip, limit, db, submitted_by, review_state, submitted_date
)
background_task_id = await project_crud.insert_background_task_into_database(
db, "sync_submission", project_id
Expand All @@ -419,6 +424,11 @@ async def task_submissions(
project: db_models.DbProject = Depends(project_deps.get_project_by_id),
page: int = Query(1, ge=1),
limit: int = Query(13, le=100),
submitted_by: Optional[str] = None,
review_state: Optional[str] = None,
submitted_date: Optional[str] = Query(
None, title="Submitted Date", description="Date in format (e.g., 'YYYY-MM-DD')"
),
db: Session = Depends(database.get_db),
current_user: AuthUser = Depends(mapper),
):
Expand All @@ -438,6 +448,24 @@ async def task_submissions(
"$wkt": True,
}

if submitted_date:
filters["$filter"] = (
"__system/submissionDate ge {}T00:00:00+00:00 "
"and __system/submissionDate le {}T23:59:59.999+00:00"
).format(submitted_date, submitted_date)

if submitted_by:
if "$filter" in filters:
filters["$filter"] += f"and (username eq '{submitted_by}')"
else:
filters["$filter"] = f"username eq '{submitted_by}'"

if review_state:
if "$filter" in filters:
filters["$filter"] += f" and (__system/reviewState eq '{review_state}')"
else:
filters["$filter"] = f"__system/reviewState eq '{review_state}'"

data, count = await submission_crud.get_submission_by_task(
project, task_id, filters, db
)
Expand Down

0 comments on commit 9201779

Please sign in to comment.