Skip to content

Commit

Permalink
feat: get contributors by project (#1062)
Browse files Browse the repository at this point in the history
Co-authored-by: sujanadh <[email protected]>
  • Loading branch information
Sujanadh and sujanadh authored Dec 27, 2023
1 parent fcd9855 commit 0a1c86a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
50 changes: 49 additions & 1 deletion src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
15 changes: 14 additions & 1 deletion src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1233,4 +1233,17 @@ async def project_dashboard(
ProjectDashboard: The project dashboard details.
"""

return await project_crud.get_dashboard_detail(project_id, db)
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

0 comments on commit 0a1c86a

Please sign in to comment.