Skip to content

Commit

Permalink
fix: Negative resolution time
Browse files Browse the repository at this point in the history
  • Loading branch information
rutwikhdev committed Dec 14, 2023
1 parent 15d849a commit e2dcc7f
Showing 1 changed file with 38 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
time_diff_in_seconds,
to_timedelta,
)
from datetime import timedelta

Check warning on line 15 in helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py

View check run for this annotation

Codecov / codecov/patch

helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py#L15

Added line #L15 was not covered by tests

from helpdesk.utils import get_context

Expand Down Expand Up @@ -275,46 +276,51 @@ def calc_time(
res = add_to_date(res, seconds=time_required, as_datetime=True)
return res

def calc_elapsed_time(self, start_at, end_at) -> float:
def get_working_days(self) -> dict[str, dict]:
workdays = []
for row in self.support_and_resolution:
workdays.append(row.workday)
return workdays

Check warning on line 283 in helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py

View check run for this annotation

Codecov / codecov/patch

helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py#L279-L283

Added lines #L279 - L283 were not covered by tests

def get_working_hours(self) -> dict[str, dict]:
res = {}
for row in self.support_and_resolution:
res[row.workday] = (row.start_time, row.end_time)
return res

Check warning on line 289 in helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py

View check run for this annotation

Codecov / codecov/patch

helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py#L285-L289

Added lines #L285 - L289 were not covered by tests

def is_working_time(self, date_time, working_hours):
day_of_week = get_weekdays()[date_time.weekday()]
start_time, end_time = working_hours.get(day_of_week, (0, 0))
date_time = timedelta(hours=date_time.hour, minutes=date_time.minute, seconds=date_time.second)

Check failure on line 294 in helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py

View workflow job for this annotation

GitHub Actions / Black - Reviewdog

[black-format] reported by reviewdog 🐶 Raw Output: helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py:294:- date_time = timedelta(hours=date_time.hour, minutes=date_time.minute, seconds=date_time.second) helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py:294:+ date_time = timedelta( helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py:295:+ hours=date_time.hour, minutes=date_time.minute, seconds=date_time.second helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py:296:+ )
return start_time <= date_time < end_time

Check warning on line 295 in helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py

View check run for this annotation

Codecov / codecov/patch

helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py#L291-L295

Added lines #L291 - L295 were not covered by tests

def calc_elapsed_time(self, start_time, end_time) -> float:

Check warning on line 297 in helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py

View check run for this annotation

Codecov / codecov/patch

helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py#L297

Added line #L297 was not covered by tests
"""
Get took from start to end, excluding non-working hours
:param start_at: Date at which calculation starts
:param end_at: Date at which calculation ends
:return: Number of seconds
"""
start_at = getdate(start_at)
end_at = getdate(end_at)
time_took = 0
holidays = self.get_holidays()
weekdays = get_weekdays()
workdays = self.get_workdays()
while getdate(start_at) <= getdate(end_at):
today = start_at
today_day = getdate(today)
today_weekday = weekdays[today.weekday()]
is_workday = today_weekday in workdays
is_holiday = today_day in holidays
if is_holiday or not is_workday:
start_at = getdate(add_to_date(start_at, days=1, as_datetime=True))
start_time = get_datetime(start_time)
end_time = get_datetime(end_time)
holiday_list = []
working_day_list = self.get_working_days()
working_hours = self.get_working_hours()

Check warning on line 309 in helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py

View check run for this annotation

Codecov / codecov/patch

helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py#L305-L309

Added lines #L305 - L309 were not covered by tests

total_seconds = 0
current_time = start_time

Check warning on line 312 in helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py

View check run for this annotation

Codecov / codecov/patch

helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py#L311-L312

Added lines #L311 - L312 were not covered by tests

while current_time < end_time:
in_holiday_list = current_time.date() in holiday_list
not_in_working_day_list = get_weekdays()[current_time.weekday()] not in working_day_list

Check failure on line 316 in helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py

View workflow job for this annotation

GitHub Actions / Black - Reviewdog

[black-format] reported by reviewdog 🐶 Raw Output: helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py:316:- not_in_working_day_list = get_weekdays()[current_time.weekday()] not in working_day_list helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py:317:- if in_holiday_list or not_in_working_day_list or not self.is_working_time(current_time, working_hours): helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py:318:+ not_in_working_day_list = ( helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py:319:+ get_weekdays()[current_time.weekday()] not in working_day_list helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py:320:+ ) helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py:321:+ if ( helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py:322:+ in_holiday_list helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py:323:+ or not_in_working_day_list helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py:324:+ or not self.is_working_time(current_time, working_hours) helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py:325:+ ):
if in_holiday_list or not_in_working_day_list or not self.is_working_time(current_time, working_hours):
current_time += timedelta(seconds=1)

Check warning on line 318 in helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py

View check run for this annotation

Codecov / codecov/patch

helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py#L314-L318

Added lines #L314 - L318 were not covered by tests
continue
today_workday = workdays[today_weekday]
is_today = getdate(start_at) == getdate(end_at)
if not is_today:
working_start = today_workday.start_time
working_end = today_workday.end_time
working_time = time_diff_in_seconds(working_start, working_end)
time_took += working_time
start_at = getdate(add_to_date(start_at, days=1, as_datetime=True))
continue
now_in_seconds = time_diff_in_seconds(today, today_day)
start_time = max(today_workday.start_time.total_seconds(), now_in_seconds)
end_at_seconds = time_diff_in_seconds(getdate(end_at), end_at)
end_time = max(today_workday.end_time.total_seconds(), end_at_seconds)
time_taken = end_time - start_time
time_took += time_taken
start_at = getdate(add_to_date(start_at, days=1, as_datetime=True))
return time_took
total_seconds += 1
current_time += timedelta(seconds=1)

Check warning on line 321 in helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py

View check run for this annotation

Codecov / codecov/patch

helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py#L320-L321

Added lines #L320 - L321 were not covered by tests

return total_seconds

Check warning on line 323 in helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py

View check run for this annotation

Codecov / codecov/patch

helpdesk/helpdesk/doctype/hd_service_level_agreement/hd_service_level_agreement.py#L323

Added line #L323 was not covered by tests

def get_holidays(self):
res = []
Expand Down

0 comments on commit e2dcc7f

Please sign in to comment.