Skip to content

Commit

Permalink
perf: batch and commit auto attendance processing
Browse files Browse the repository at this point in the history
(cherry picked from commit 01db92e)
  • Loading branch information
ruchamahabal authored and mergify[bot] committed Nov 2, 2023
1 parent 6a5d41c commit 2353419
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
22 changes: 17 additions & 5 deletions hrms/hr/doctype/shift_type/shift_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import frappe
from frappe.model.document import Document
from frappe.utils import cint, get_datetime, get_time, getdate
from frappe.utils import cint, create_batch, get_datetime, get_time, getdate

from erpnext.setup.doctype.employee.employee import get_holiday_list_for_employee
from erpnext.setup.doctype.holiday_list.holiday_list import is_holiday
Expand All @@ -21,6 +21,8 @@
from hrms.utils import get_date_range
from hrms.utils.holiday_list import get_holiday_dates_between

EMPLOYEE_CHUNK_SIZE = 20


class ShiftType(Document):
@frappe.whitelist()
Expand Down Expand Up @@ -63,8 +65,18 @@ def process_auto_attendance(self):
self.name,
)

for employee in self.get_assigned_employees(self.process_attendance_after, True):
self.mark_absent_for_dates_with_no_attendance(employee)
# commit after processing checkin logs to avoid losing progress
frappe.db.commit() # nosemgrep

assigned_employees = self.get_assigned_employees(self.process_attendance_after, True)
for batch in create_batch(assigned_employees, EMPLOYEE_CHUNK_SIZE):
# mark absent in batches & commit to avoid losing progress
# since this tries to process remaining attendance
# right from "Process Attendance After" to "Last Sync of Checkin"
for employee in batch:
self.mark_absent_for_dates_with_no_attendance(employee)

frappe.db.commit() # nosemgrep

def get_employee_checkins(self) -> list[dict]:
return frappe.get_all(
Expand Down Expand Up @@ -225,7 +237,7 @@ def get_marked_attendance_dates_between(
)
).run(pluck=True)

def get_assigned_employees(self, from_date=None, consider_default_shift=False):
def get_assigned_employees(self, from_date=None, consider_default_shift=False) -> list[str]:
filters = {"shift_type": self.name, "docstatus": "1", "status": "Active"}
if from_date:
filters["start_date"] = (">=", from_date)
Expand All @@ -239,7 +251,7 @@ def get_assigned_employees(self, from_date=None, consider_default_shift=False):
# exclude inactive employees
inactive_employees = frappe.db.get_all("Employee", {"status": "Inactive"}, pluck="name")

return set(assigned_employees) - set(inactive_employees)
return list(set(assigned_employees) - set(inactive_employees))

def get_employees_with_default_shift(self, filters: dict) -> list:
default_shift_employees = frappe.get_all(
Expand Down
4 changes: 3 additions & 1 deletion hrms/hr/doctype/shift_type/test_shift_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,9 @@ def test_skip_absent_marking_for_a_fallback_default_shift(self):

default_shift = setup_shift_type()
employee = make_employee(
"[email protected]", company="_Test Company", default_shift=default_shift.name
"[email protected]",
company="_Test Company",
default_shift=default_shift.name,
)

assigned_shift = setup_shift_type(shift_type="Test Absent with no Attendance")
Expand Down

0 comments on commit 2353419

Please sign in to comment.