From 0a1c86ae2da2ba3c30873067ff11b0687cd45908 Mon Sep 17 00:00:00 2001 From: Sujan Adhikari <109404840+Sujanadh@users.noreply.github.com> Date: Wed, 27 Dec 2023 15:49:01 +0545 Subject: [PATCH] feat: get contributors by project (#1062) Co-authored-by: sujanadh --- src/backend/app/projects/project_crud.py | 50 +++++++++++++++++++++- src/backend/app/projects/project_routes.py | 15 ++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/backend/app/projects/project_crud.py b/src/backend/app/projects/project_crud.py index b18dd696d3..1a95050961 100644 --- a/src/backend/app/projects/project_crud.py +++ b/src/backend/app/projects/project_crud.py @@ -2413,4 +2413,52 @@ async def get_dashboard_detail(project_id: int, db: Session): 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) - return project \ No newline at end of file + return project + +async def get_project_users(db:Session, project_id:int): + """ + Get the users and their contributions for a project. + + Args: + db (Session): The database session. + project_id (int): The ID of the project. + + Returns: + List[Dict[str, Union[str, int]]]: A list of dictionaries containing the username and the number of contributions made by each user for the specified project. + """ + + 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} + response = [] + + for user_id in unique_user_ids: + contributions = count_user_contributions(db, user_id, project_id) + db_user = await user_crud.get_user(db, user_id) + response.append({"user":db_user.username, "contributions":contributions}) + + response = sorted(response, key=lambda x: x["contributions"], reverse=True) + return response + +def count_user_contributions(db: Session, user_id: int, project_id: int) -> int: + """ + Count contributions for a specific user. + + Args: + db (Session): The database session. + user_id (int): The ID of the user. + project_id (int): The ID of the project. + + Returns: + int: The number of contributions made by the user for the specified project. + """ + + contributions_count = ( + db.query(func.count(db_models.DbTaskHistory.user_id)) + .filter( + db_models.DbTaskHistory.user_id == user_id, + db_models.DbTaskHistory.project_id == project_id + ) + .scalar() + ) + + return contributions_count \ No newline at end of file diff --git a/src/backend/app/projects/project_routes.py b/src/backend/app/projects/project_routes.py index 11fb9d9146..e948db821d 100644 --- a/src/backend/app/projects/project_routes.py +++ b/src/backend/app/projects/project_routes.py @@ -1233,4 +1233,17 @@ async def project_dashboard( ProjectDashboard: The project dashboard details. """ - return await project_crud.get_dashboard_detail(project_id, db) \ No newline at end of file + return await project_crud.get_dashboard_detail(project_id, db) + +@router.get("/contributors/{project_id}") +async def get_contributors(project_id: int, db: Session = Depends(database.get_db)): + """Get contributors of a project. + + Args: + project_id (int): ID of project. + + Returns: + list[project_schemas.ProjectUser]: List of project users. + """ + project_users = await project_crud.get_project_users(db, project_id) + return project_users \ No newline at end of file