[IMP]hr_employee_stats_sheet:read the public holidays once per employee
Each day searched the public holidays of its own date. The days computed together now run one search per employee over their date range, then keep for each day the holidays the per-day search returned: those whose UTC range covers the day. The check in the employee time zone is unchanged, and two holidays on the same day still raise the same error. With this change, the number of SQL queries needed to compute the days of an employee no longer grows with the number of days. Assisted-by: Claude:claude-opus-5-5 Assisted-by: GPT:gpt-6.1-sol Assisted-by: GPT:gpt-6.2-astra-ultrafast
This commit is contained in:
@@ -206,13 +206,16 @@ class HrEmployeeStats(models.Model):
|
||||
|
||||
@api.depends("date","employee_id")
|
||||
def _compute_dayofweek(self):
|
||||
public_holidays = self._public_holidays_by_day()
|
||||
for stat in self:
|
||||
if not stat.date:
|
||||
stat.dayofweek = None
|
||||
stat.is_public_holiday = False
|
||||
continue
|
||||
stat.dayofweek = int(stat.date.strftime("%u")) - 1
|
||||
stat.is_public_holiday = stat._is_public_holiday_accordig_to_employe_tz()
|
||||
stat.is_public_holiday = stat._matches_public_holiday(
|
||||
public_holidays.get((stat.employee_id, stat.date))
|
||||
)
|
||||
|
||||
def _convert_to_employee_tz(self, date):
|
||||
"""Convert a UTC datetime to the employee's timezone datetime."""
|
||||
@@ -224,14 +227,28 @@ class HrEmployeeStats(models.Model):
|
||||
dt = pytz.utc.localize(date)
|
||||
return dt.astimezone(employee_tz)
|
||||
|
||||
def _public_holidays_by_day(self):
|
||||
"""Return {(employee, date): public holidays} with one search per
|
||||
employee: the holidays _get_public_holidays(date, date) returns."""
|
||||
public_holidays = {}
|
||||
days = self.filtered(lambda stat: stat.employee_id and stat.date)
|
||||
for employee, dates in days._dates_by_employee().items():
|
||||
holidays = employee._get_public_holidays(min(dates), max(dates))
|
||||
for date in dates:
|
||||
# the search compares the UTC datetimes with the whole day
|
||||
public_holidays[employee, date] = holidays.filtered(
|
||||
lambda holiday: holiday.date_from.date() <= date <= holiday.date_to.date()
|
||||
)
|
||||
return public_holidays
|
||||
|
||||
def _is_public_holiday_accordig_to_employe_tz(self):
|
||||
self.ensure_one()
|
||||
if not self.date or not self.employee_id:
|
||||
return False
|
||||
#get public holidays for the employee
|
||||
public_holidays = self.employee_id._get_public_holidays(
|
||||
self.date, self.date
|
||||
return self._matches_public_holiday(
|
||||
self._public_holidays_by_day().get((self.employee_id, self.date))
|
||||
)
|
||||
|
||||
def _matches_public_holiday(self, public_holidays):
|
||||
self.ensure_one()
|
||||
if not public_holidays:
|
||||
return False
|
||||
if len(public_holidays) > 1:
|
||||
|
||||
Reference in New Issue
Block a user