From cee6627a983c3bbd35786af34701040cc1cc5a52 Mon Sep 17 00:00:00 2001 From: nareshkannasln Date: Wed, 9 Sep 2026 16:52:31 +0530 Subject: [PATCH 1/2] perf: batch employee metadata lookup --- ...yee_hours_utilization_based_on_timesheet.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/hrms/hr/report/employee_hours_utilization_based_on_timesheet/employee_hours_utilization_based_on_timesheet.py b/hrms/hr/report/employee_hours_utilization_based_on_timesheet/employee_hours_utilization_based_on_timesheet.py index 3a9a8b8165b..b15357548b5 100644 --- a/hrms/hr/report/employee_hours_utilization_based_on_timesheet/employee_hours_utilization_based_on_timesheet.py +++ b/hrms/hr/report/employee_hours_utilization_based_on_timesheet/employee_hours_utilization_based_on_timesheet.py @@ -168,10 +168,22 @@ def generate_stats_by_employee(self): self.stats_by_employee[emp]["non_billed_hours"] += flt(hours, 2) def set_employee_department_and_name(self): - for emp, stats in self.stats_by_employee.items(): - stats["employee_name"], stats["department"] = frappe.get_value( - "Employee", emp, ["employee_name", "department"] + if not self.stats_by_employee: + return + + employee_details = { + employee["name"]: employee + for employee in frappe.get_all( + "Employee", + filters={"name": ["in", list(self.stats_by_employee)]}, + fields=["name", "employee_name", "department"], ) + } + + for emp, stats in self.stats_by_employee.items(): + employee = employee_details.get(emp) + stats["employee_name"] = employee.get("employee_name") if employee else None + stats["department"] = employee.get("department") if employee else None def calculate_utilizations(self): TOTAL_HOURS = flt(self.standard_working_hours * self.day_span, 2) From a7de62c9da1ef5eca50fe9d80b8103af0d723fac Mon Sep 17 00:00:00 2001 From: nareshkannasln Date: Wed, 9 Sep 2026 16:52:39 +0530 Subject: [PATCH 2/2] test: cover batched employee metadata lookup --- .../test_employee_util.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/hrms/hr/report/employee_hours_utilization_based_on_timesheet/test_employee_util.py b/hrms/hr/report/employee_hours_utilization_based_on_timesheet/test_employee_util.py index 54c4871c217..41f27bd275f 100644 --- a/hrms/hr/report/employee_hours_utilization_based_on_timesheet/test_employee_util.py +++ b/hrms/hr/report/employee_hours_utilization_based_on_timesheet/test_employee_util.py @@ -1,3 +1,5 @@ +from unittest.mock import patch + import frappe from frappe.utils.make_random import get_random @@ -5,6 +7,7 @@ from erpnext.setup.doctype.employee.test_employee import make_employee from hrms.hr.report.employee_hours_utilization_based_on_timesheet.employee_hours_utilization_based_on_timesheet import ( + EmployeeHoursReport, execute, ) from hrms.tests.utils import HRMSTestSuite @@ -150,6 +153,17 @@ def test_report_summary_data(self): for i in range(4): self.assertEqual(summary[i]["value"], expected_summary_values[i]) + def test_employee_metadata_is_fetched_in_one_query(self): + report = EmployeeHoursReport.__new__(EmployeeHoursReport) + report.stats_by_employee = frappe._dict({self.test_emp1: {}, self.test_emp2: {}}) + + with patch.object(frappe, "get_all", wraps=frappe.get_all) as get_all: + report.set_employee_department_and_name() + + self.assertEqual(get_all.call_count, 1) + self.assertEqual(report.stats_by_employee[self.test_emp1]["employee_name"], "test1@employeeutil.com") + self.assertEqual(report.stats_by_employee[self.test_emp2]["employee_name"], "test2@employeeutil.com") + def get_expected_data_for_test_employees(self): emp1_data = frappe.get_doc("Employee", self.test_emp1) emp2_data = frappe.get_doc("Employee", self.test_emp2)