[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:
Valentin Lab
2026-11-19 14:09:26 +01:00
parent 92dc0e2a58
commit 567a0a1d0d
2 changed files with 71 additions and 6 deletions

View File

@@ -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:

View File

@@ -462,6 +462,54 @@ class TestHrEmployeeStatsRecovery(TransactionCase):
{'employee_id': self.employee.id, 'date': day} for day in days
])
def _count_queries(self, records, fnames):
"""SQL queries needed to read fnames of records from an empty cache."""
self.env.invalidate_all()
records.read(fnames) # fills the caches that invalidate_all keeps
self.env.invalidate_all()
before = self.env.cr.sql_log_count
records.read(fnames)
return self.env.cr.sql_log_count - before
def test_query_count_does_not_grow_with_days(self):
start = Date.to_date("2025-03-03")
self.env['hr.employee.calendar'].create({
'employee_id': self.employee.id,
'date_start': start,
'calendar_id': self.base_calendar.id,
})
days = self._create_days(*(start + timedelta(days=i) for i in range(14)))
self.env['account.analytic.line'].create([{
'employee_id': self.employee.id, 'date': day.date, 'unit_amount': 3,
'account_id': self.analytic_account.id, 'name': 'Work Entry',
} for day in days])
fnames = ['total_hours', 'total_planned_hours', 'total_leave_hours',
'total_recovery_hours', 'gap_hours', 'is_public_holiday']
self.assertLessEqual(self._count_queries(days, fnames),
self._count_queries(days[:2], fnames))
self.assertEqual(days.mapped('total_hours'), [3] * 14)
def test_public_holidays_read_together(self):
# Stored in UTC, this holiday is May 1st 00:00 to May 2nd 01:00 in
# Paris. A day counts only if it is in the UTC range AND in the
# employee's range: April 30 and May 2 do not count.
self.env['resource.calendar.leaves'].create({
'name': 'Late finish',
'date_from': '2025-04-30 22:00:00',
'date_to': '2025-05-01 23:00:00',
})
days = self._create_days('2025-04-30', '2025-05-01', '2025-05-02')
self.assertEqual(days.mapped('is_public_holiday'), [False, True, False])
# April 30 now has two holidays in its UTC range: that is an anomaly.
self.env['resource.calendar.leaves'].create({
'name': 'Earlier',
'date_from': '2025-04-30 08:00:00',
'date_to': '2025-04-30 09:00:00',
})
self.env.invalidate_all()
with self.assertRaisesRegex(UserError, '2025-04-30'):
days.mapped('is_public_holiday')
def test_planned_hours_read_together_follow_calendar_history(self):
two_weeks = self.env['resource.calendar'].create({
'name': 'Alternating Mondays',