Skip to content

Commit

Permalink
refactor(backend): correct username of contributors (#1751)
Browse files Browse the repository at this point in the history
* refactor: correct username of contributors using sql in single db transaction

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Sujanadh and pre-commit-ci[bot] authored Aug 13, 2024
1 parent d8eb188 commit ce1b3b4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
34 changes: 14 additions & 20 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -1525,37 +1525,31 @@ async def get_dashboard_detail(
return project


async def get_project_users(db: Session, project_id: int, db_user: db_models.DbUser):
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.
db_user (DbUser): User that called the endpoint.
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.
"""
# TODO refactor this
# TODO it could probably just be a single raw SQL statement
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)
response.append({"user": db_user.username, "contributions": contributions})

response = sorted(response, key=lambda x: x["contributions"], reverse=True)
return response
query = text("""
SELECT u.username, COUNT(th.user_id) as contributions
FROM users u
JOIN task_history th ON u.id = th.user_id
WHERE th.project_id = :project_id
GROUP BY u.username
ORDER BY contributions DESC
""")
result = db.execute(query, {"project_id": project_id}).fetchall()

return [
{"user": row.username, "contributions": row.contributions} for row in result
]


def count_user_contributions(db: Session, user_id: int, project_id: int) -> int:
Expand Down
6 changes: 2 additions & 4 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1187,17 +1187,15 @@ async def project_dashboard(

@router.get("/contributors/{project_id}")
async def get_contributors(
project_id: int,
db: Session = Depends(database.get_db),
project_user: ProjectUserDict = Depends(mapper),
):
"""Get contributors of a project.
TODO use a pydantic model for return type
"""
db_user = project_user.get("user")
project_users = await project_crud.get_project_users(db, project_id, db_user)
return project_users
project = project_user.get("project")
return await project_crud.get_project_users(db, project.id)


@router.post("/add-manager/")
Expand Down

0 comments on commit ce1b3b4

Please sign in to comment.