Skip to content

Commit

Permalink
perf: use cache for most used queries
Browse files Browse the repository at this point in the history
- `get_doc` -> `get_cached_doc` / `get_value`

- `get_value` to `db.value_cache`

(cherry picked from commit 2971dc1)
  • Loading branch information
ruchamahabal authored and mergify[bot] committed May 16, 2023
1 parent 7f5062e commit 1a1b0fa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
4 changes: 2 additions & 2 deletions hrms/hr/doctype/employee_checkin/employee_checkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def mark_attendance_and_link_log(
return None

elif attendance_status in ("Present", "Absent", "Half Day"):
employee_doc = frappe.get_doc("Employee", employee)
company = frappe.db.get_value("Employee", employee, "company", cache=True)
duplicate = get_duplicate_attendance_record(employee, attendance_date, shift)
overlapping = get_overlapping_shift_attendance(employee, attendance_date, shift)

Expand All @@ -147,7 +147,7 @@ def mark_attendance_and_link_log(
"attendance_date": attendance_date,
"status": attendance_status,
"working_hours": working_hours,
"company": employee_doc.company,
"company": company,
"shift": shift,
"late_entry": late_entry,
"early_exit": early_exit,
Expand Down
15 changes: 13 additions & 2 deletions hrms/hr/doctype/shift_assignment/shift_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def get_employee_shift(
shift_details = get_shift_for_timestamp(employee, for_timestamp)

# if shift assignment is not found, consider default shift
default_shift = frappe.db.get_value("Employee", employee, "default_shift")
default_shift = frappe.db.get_value("Employee", employee, "default_shift", cache=True)
if not shift_details and consider_default_shift:
shift_details = get_shift_details(default_shift, for_timestamp)

Expand Down Expand Up @@ -462,7 +462,18 @@ def get_shift_details(shift_type_name: str, for_timestamp: datetime = None) -> D
if for_timestamp is None:
for_timestamp = now_datetime()

shift_type = frappe.get_doc("Shift Type", shift_type_name)
shift_type = frappe.get_cached_value(
"Shift Type",
shift_type_name,
[
"name",
"start_time",
"end_time",
"begin_check_in_before_shift_start_time",
"allow_check_out_after_shift_end_time",
],
as_dict=1,
)
shift_actual_start = shift_type.start_time - timedelta(
minutes=shift_type.begin_check_in_before_shift_start_time
)
Expand Down
6 changes: 3 additions & 3 deletions hrms/hr/doctype/shift_type/shift_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def get_start_and_end_dates(self, employee):
return: start date = max of `process_attendance_after` and DOJ
return: end date = min of shift before `last_sync_of_checkin` and Relieving Date
"""
date_of_joining, relieving_date, employee_creation = frappe.db.get_value(
date_of_joining, relieving_date, employee_creation = frappe.get_cached_value(
"Employee", employee, ["date_of_joining", "relieving_date", "creation"]
)

Expand Down Expand Up @@ -235,7 +235,7 @@ def should_mark_attendance(self, employee: str, attendance_date: str) -> bool:


def process_auto_attendance_for_all_shifts():
shift_list = frappe.get_all("Shift Type", "name", {"enable_auto_attendance": "1"}, as_list=True)
shift_list = frappe.get_all("Shift Type", filters={"enable_auto_attendance": "1"}, pluck="name")
for shift in shift_list:
doc = frappe.get_doc("Shift Type", shift[0])
doc = frappe.get_cached_doc("Shift Type", shift)
doc.process_auto_attendance()

0 comments on commit 1a1b0fa

Please sign in to comment.