Eliminate N+1 queries in team timesheet compact view API#780
Open
Copilot wants to merge 5 commits intoversion-16-hotfixfrom
Open
Eliminate N+1 queries in team timesheet compact view API#780Copilot wants to merge 5 commits intoversion-16-hotfixfrom
Copilot wants to merge 5 commits intoversion-16-hotfixfrom
Conversation
Co-authored-by: mrrobot47 <[email protected]>
Co-authored-by: mrrobot47 <[email protected]>
Co-authored-by: mrrobot47 <[email protected]>
Copilot
AI
changed the title
[WIP] Fix N+1 issue in get_compact_view_data API
Eliminate N+1 queries in team timesheet compact view API
Jan 12, 2026
Comment on lines
+247
to
+274
| def batch_get_timesheet_states(employee_names, start_date, end_date): | ||
| """Batch fetch timesheet states for all employees. | ||
|
|
||
| Returns a dictionary mapping employee names to their timesheet status. | ||
| If an employee has no timesheets or all their timesheets have None status, | ||
| they won't be in the returned dict (caller will use "Not Submitted" as default). | ||
| """ | ||
| if not employee_names: | ||
| return {} | ||
|
|
||
| # Get all timesheets for all employees in one query | ||
| timesheets = get_all( | ||
| "Timesheet", | ||
| filters={ | ||
| "employee": ["in", employee_names], | ||
| "start_date": [">=", start_date], | ||
| "end_date": ["<=", end_date], | ||
| "docstatus": ["!=", 2], | ||
| }, | ||
| fields=["employee", "custom_weekly_approval_status"], | ||
| ) | ||
|
|
||
| # Build a map of employee to their status | ||
| # If an employee has multiple timesheets, use the first non-None status found | ||
| states = {} | ||
| for ts in timesheets: | ||
| if ts.employee not in states and ts.custom_weekly_approval_status is not None: | ||
| states[ts.employee] = ts.custom_weekly_approval_status |
Contributor
There was a problem hiding this comment.
In our code we get one value per employee ,, here we get the first value which is not None , hence there is a mismatch in "status" field of the API Endpoint
def get_timesheet_state(employee: str, start_date: str, end_date: str):
status = frappe.db.get_value(
"Timesheet",
{
"employee": employee,
"start_date": [">=", getdate(start_date)],
"end_date": ["<=", getdate(end_date)],
"docstatus": ["!=", 2],
},
"custom_weekly_approval_status",
)
if status:
return status
return "Not Submitted"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #770
The
get_compact_view_data()endpoint was making 5 database queries per employee in a loop, resulting in ~50 queries for a page of 10 employees.Changes
Replaced per-employee queries with batch operations:
get_all()for all employees instead of cached individual callsbatch_get_timesheet_states()helper replacing individual lookupsBefore:
After:
Additional improvements:
WORK_DAYS_PER_WEEKconstantImpact
Original prompt
This section details on the original issue you should resolve
<issue_title>N+1 in get_compact_view_data API</issue_title>
<issue_description>
Severity: High
Category: Database/API
Location
next_pms/timesheet/api/team.pyget_compact_view_data()Description
The
get_compact_view_data()API endpoint, which powers the team timesheet compact view, loops through employees and makes multiple database queries per employee. This is called frequently as users navigate the timesheet interface.Problematic Code
Impact
get_employee_working_hours()= 10 queries (may be cached)get_employee_daily_working_norm()= 10 queries (may be cached)get_timesheet_state()= 10 queriesget_employee_leaves()= 10 queriesget_holidays()= 10 queriesRecommended Fix