Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get contributors by project #1062

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2411,4 +2411,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