Get all projects the user has access to across all organizations they belong to. Includes organization information with each project. Optimized version that reduces unnecessary data loading.
(
*,
request: Request,
orm: Session = Depends(get_orm_session),
)
| 15 | |
| 16 | |
| 17 | async def get_projects( |
| 18 | *, |
| 19 | request: Request, |
| 20 | orm: Session = Depends(get_orm_session), |
| 21 | ) -> list[ProjectSummaryResponse]: |
| 22 | """ |
| 23 | Get all projects the user has access to across all organizations they belong to. |
| 24 | Includes organization information with each project. |
| 25 | |
| 26 | Optimized version that reduces unnecessary data loading. |
| 27 | """ |
| 28 | # Use a more efficient query that only loads what we need for the response |
| 29 | projects = ProjectModel.get_all_for_user_optimized(orm, request.state.session.user_id) |
| 30 | |
| 31 | # Only fetch trace counts if we have projects |
| 32 | if projects: |
| 33 | _projects_counts = await TraceCountsModel.select( |
| 34 | filters={'project_ids': [str(project.id) for project in projects]} |
| 35 | ) |
| 36 | projects_counts: dict[str, int] = {str(p.project_id): p for p in _projects_counts} |
| 37 | else: |
| 38 | projects_counts = {} |
| 39 | |
| 40 | project_responses = [] |
| 41 | for project in projects: |
| 42 | response = ProjectSummaryResponse.model_validate(project) |
| 43 | |
| 44 | # add trace metrics to the response |
| 45 | if counts := projects_counts.get(str(project.id)): |
| 46 | response.span_count = counts.span_count |
| 47 | response.trace_count = counts.trace_count |
| 48 | |
| 49 | project_responses.append(response) |
| 50 | |
| 51 | return project_responses |
| 52 | |
| 53 | |
| 54 | def get_project( |
searching dependent graphs…