Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: index shift type & employee in shift assignment #622

Merged
merged 2 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions hrms/hr/doctype/shift_assignment/shift_assignment.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"fieldtype": "Link",
"label": "Employee",
"options": "Employee",
"reqd": 1
"reqd": 1,
"search_index": 1
},
{
"fetch_from": "employee.employee_name",
Expand All @@ -48,7 +49,8 @@
"in_list_view": 1,
"label": "Shift Type",
"options": "Shift Type",
"reqd": 1
"reqd": 1,
"search_index": 1
},
{
"fieldname": "column_break_3",
Expand Down Expand Up @@ -88,27 +90,24 @@
"allow_on_submit": 1,
"fieldname": "end_date",
"fieldtype": "Date",
"label": "End Date",
"show_days": 1,
"show_seconds": 1
"label": "End Date"
},
{
"allow_on_submit": 1,
"default": "Active",
"fieldname": "status",
"fieldtype": "Select",
"label": "Status",
"options": "Active\nInactive",
"show_days": 1,
"show_seconds": 1
"options": "Active\nInactive"
}
],
"is_submittable": 1,
"links": [],
"modified": "2020-06-15 14:27:54.310773",
"modified": "2023-06-23 12:08:23.247882",
"modified_by": "Administrator",
"module": "HR",
"name": "Shift Assignment",
"naming_rule": "Expression (old style)",
"owner": "Administrator",
"permissions": [
{
Expand Down Expand Up @@ -150,6 +149,7 @@
],
"sort_field": "modified",
"sort_order": "DESC",
"states": [],
"title_field": "employee_name",
"track_changes": 1
}
19 changes: 10 additions & 9 deletions hrms/hr/doctype/shift_assignment/shift_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.query_builder import Criterion, Interval
from frappe.query_builder.functions import Date
from frappe.utils import cstr, get_link_to_form, get_time, getdate, now_datetime
from frappe.query_builder import Criterion
from frappe.utils import add_days, cstr, get_link_to_form, get_time, getdate, now_datetime

from hrms.hr.utils import validate_active_employee

Expand Down Expand Up @@ -258,24 +257,26 @@ def _adjust_overlapping_shifts(shifts: dict):

def get_shifts_for_date(employee: str, for_timestamp: datetime) -> List[Dict[str, str]]:
"""Returns list of shifts with details for given date"""
assignment = frappe.qb.DocType("Shift Assignment")
for_date = for_timestamp.date()
prev_day = add_days(for_date, -1)

assignment = frappe.qb.DocType("Shift Assignment")
return (
frappe.qb.from_(assignment)
.select(assignment.name, assignment.shift_type, assignment.start_date, assignment.end_date)
.where(
(assignment.employee == employee)
& (assignment.docstatus == 1)
& (assignment.status == "Active")
& (assignment.start_date <= getdate(for_timestamp.date()))
& (assignment.start_date <= for_date)
& (
Criterion.any(
[
assignment.end_date.isnull(),
(
assignment.end_date.isnotnull()
# for midnight shifts, valid assignments are upto 1 day prior
& (Date(for_timestamp) - Interval(days=1) <= assignment.end_date)
& (prev_day <= assignment.end_date)
),
]
)
Expand Down Expand Up @@ -463,7 +464,7 @@ def get_shift_details(shift_type_name: str, for_timestamp: datetime = None) -> D
:param for_timestamp (datetime, optional): Datetime value of checkin, if not provided considers current datetime
"""
if not shift_type_name:
return {}
return frappe._dict()

if for_timestamp is None:
for_timestamp = now_datetime()
Expand All @@ -489,13 +490,13 @@ def get_shift_details(shift_type_name: str, for_timestamp: datetime = None) -> D
if get_time(for_timestamp.time()) >= get_time(shift_actual_start):
# if for_timestamp is greater than start time, it's within the first day
start_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.start_time
for_timestamp = for_timestamp + timedelta(days=1)
for_timestamp += timedelta(days=1)
end_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.end_time

elif get_time(for_timestamp.time()) < get_time(shift_actual_start):
# if for_timestamp is less than start time, it's within the second day
end_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.end_time
for_timestamp = for_timestamp + timedelta(days=-1)
for_timestamp += timedelta(days=-1)
start_datetime = datetime.combine(for_timestamp, datetime.min.time()) + shift_type.start_time
else:
# start and end timings fall on the same day
Expand Down