Skip to content

Commit

Permalink
fix: optimize the performance of project dashboard (#1079)
Browse files Browse the repository at this point in the history
* fix: optimze the performance of project dashboard

* updated project schema

---------

Co-authored-by: sujanadh <sujanadh07@gmail.com>
  • Loading branch information
Sujanadh and sujanadh authored Jan 3, 2024
1 parent 1940d1b commit 43fe756
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
29 changes: 20 additions & 9 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -2397,21 +2397,32 @@ async def get_dashboard_detail(project_id: int, db: Session):
db_organization = await organization_crud.get_organisation_by_id(db, project.organisation_id)

s3_project_path = f"/{project.organisation_id}/{project_id}"
s3_submission_path = f"/{s3_project_path}/submissions.meta.json"
s3_submission_path = f"/{s3_project_path}/submission.zip"
s3_submission_meta_path = f"/{s3_project_path}/submissions.meta.json"

try:
file = get_obj_from_bucket(settings.S3_BUCKET_NAME, s3_submission_path)
project.last_active = (json.loads(file.getvalue()))["last_submission"]
submission = get_obj_from_bucket(settings.S3_BUCKET_NAME, s3_submission_path)
with zipfile.ZipFile(submission, "r") as zip_ref:
with zip_ref.open("submissions.json") as file_in_zip:
content = file_in_zip.read()
content = json.loads(content)
project.total_submission = len(content)
submission_meta = get_obj_from_bucket(settings.S3_BUCKET_NAME, s3_submission_meta_path)
project.last_active = (json.loads(submission_meta.getvalue()))["last_submission"]
except ValueError:
project.total_submission = 0
pass

contributors = db.query(db_models.DbTaskHistory).filter(db_models.DbTaskHistory.project_id==project_id).all()
unique_user_ids = {user.user_id for user in contributors if user.user_id is not None}
contributors = db.query(
db_models.DbTaskHistory.user_id
).filter(
db_models.DbTaskHistory.project_id == project_id,
db_models.DbTaskHistory.user_id.isnot(None)
).distinct().count()

project.organization = db_organization.name
project.organization_logo = db_organization.logo
project.total_contributors = len(unique_user_ids)
project.total_submission = await submission_crud.get_submission_count_of_a_project(db, project_id)
project.total_tasks = await tasks_crud.get_task_count_in_project(db, project_id)
project.organization, project.organization_logo = db_organization.name, db_organization.logo
project.total_contributors = contributors

return project

Expand Down
14 changes: 12 additions & 2 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from sqlalchemy.orm import Session

from app.auth.osm import AuthUser, login_required
from app.submission import submission_crud

from ..central import central_crud
from ..db import database, db_models
Expand Down Expand Up @@ -1220,7 +1221,9 @@ async def get_template_file(

@router.get("/project_dashboard/{project_id}", response_model=project_schemas.ProjectDashboard)
async def project_dashboard(
project_id: int, db: Session = Depends(database.get_db)
project_id: int,
background_tasks: BackgroundTasks,
db: Session = Depends(database.get_db)
):
"""
Get the project dashboard details.
Expand All @@ -1232,8 +1235,15 @@ async def project_dashboard(
Returns:
ProjectDashboard: The project dashboard details.
"""
data = await project_crud.get_dashboard_detail(project_id, db)
background_task_id = await project_crud.insert_background_task_into_database(
db, "sync_submission", project_id
)

return await project_crud.get_dashboard_detail(project_id, db)
background_tasks.add_task(
submission_crud.update_submission_in_s3, db, project_id, background_task_id
)
return data

@router.get("/contributors/{project_id}")
async def get_contributors(project_id: int, db: Session = Depends(database.get_db)):
Expand Down
6 changes: 3 additions & 3 deletions src/backend/app/projects/project_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ class BackgroundTaskStatus(BaseModel):
class ProjectDashboard(BaseModel):
project_name_prefix: str
organization: str
organization_logo: Optional[str] = None
total_tasks: int
total_submission: int
total_contributors: int
created: datetime
organization_logo: Optional[str] = None
total_submission: Optional[int] = None
total_contributors: Optional[int] = None
last_active: Optional[str] = None

@field_validator("created", mode="before")
Expand Down

0 comments on commit 43fe756

Please sign in to comment.