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

Update schedule slack notifications #2710

Merged
merged 10 commits into from
Aug 3, 2023
Merged

Conversation

Ferril
Copy link
Member

@Ferril Ferril commented Aug 1, 2023

What this PR does

Update schedule slack notifications to use schedule final events instead of getting events from iCal

Checklist

  • Unit, integration, and e2e (if applicable) tests updated
  • Documentation added (or pr:no public docs PR label added if not required)
  • CHANGELOG.md updated (or pr:no changelog PR label added if not required)

@Ferril Ferril added the pr:no public docs Added to a PR that does not require public documentation updates label Aug 1, 2023
@Ferril Ferril requested a review from a team August 1, 2023 09:39
Copy link
Contributor

@matiasb matiasb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

A few comments added.
Great you added more tests covering different notification trigger (or not) scenarios 👍
And really nice to see the notify task simplification!

gaps = list_of_gaps_in_schedule(schedule, today, today + timezone.timedelta(days=7))
schedule.gaps_report_sent_at = today
now = timezone.now()
events = schedule.final_events(now, now + datetime.timedelta(days=7), with_empty=False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to set with_empty=False here? final_events will still return gaps if there are any, won't it? (and empty shifts should be detected by the respective task, right?)

Also, could we use the check_gaps_for_next_week method from schedule in this case? (I see it may require some refactoring to reuse it here, maybe for a next PR)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, we can filter it in the task. It makes sense since we need to filer the output anyway.

I think, if we change using of check_gaps_for_next_week we should also change using check_empty_shifts_for_next_week since they have pretty similar logic. I would propose to do it in separate PR

gaps = list_of_gaps_in_schedule(self, today, today + datetime.timedelta(days=7))
def check_gaps_for_next_week(self) -> bool:
today = timezone.now()
events = self.final_events(today, today + datetime.timedelta(days=7), False, True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar as noted below, wondering if if/why we need to pass with_empty=False here.

Also, I think it would be clearer setting these flags as kw args make it easier to understand what we are filtering at a quick glance.

@@ -396,9 +398,11 @@ def filter_events(

return events

def final_events(self, datetime_start, datetime_end):
def final_events(self, datetime_start, datetime_end, with_empty=True, with_gap=True):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How sure are that we need to enable these flags here? I would really like (to believe?) that when checking final events, we are interested in the final schedule events only, without needing to tune the options.

Copy link
Member Author

@Ferril Ferril Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's mostly needed for slack notifications about new/next shifts. We need only shifts without gaps and empties there. We already have logic to filter such kind of events in filter_events and it makes notify_ical_schedule_shift cleaner.

on_call_shift.add_rolling_users([[user1]])
schedule_no_gaps.refresh_ical_file()

assert schedule_no_gaps.check_gaps_for_next_week() is False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe these check_gaps_for_next_week checks could be added to the previous test cases? (and then we may not need all the variations below which check almost the same scenarios that above?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense 👍



@pytest.mark.django_db
def test_empty_non_empty_shifts_trigger_notification(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is the original behavior, but wondering if this should be a warning in the schedule UI but not triggering a notification (since there is still an active user for the shift). In any case, something to discuss later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, we should discuss it later. I think, the main point is make customer know that something is wrong with one of their shifts despite the presence of an active user.


prev_shifts = json.loads(schedule.current_shifts) if not schedule.empty_oncall else []
prev_shifts_updated = False
# convert prev_shifts to new events format for compatibility with the previous version of this task
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be nice to leave a task for later to drop this conversion. Optional, if you think it makes sense, I would separate this "translation" in a helper function in this module (so it is simpler to follow the task logic ignoring this compatibility thing)


datetime_end = now + datetime.timedelta(days=days_to_lookup)

next_shifts_unfiltered = schedule.final_events(now, datetime_end, False, False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not now, but maybe we could consider having only one call to final_events?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great, but in this case we need to decide how to calculate days_to_lookup

if now < next_shift["start"]:
next_shifts.append(next_shift)

next_shifts = sorted(next_shifts, key=lambda shift: shift["start"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final_events should already return events sorted by shift["start"]

schedule.empty_oncall = True
else:
schedule.empty_oncall = False
schedule.current_shifts = json.dumps(current_shifts, default=str)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, I think this could be changed to something like

schedule.empty_oncall = len(current_shifts) == 0
if not schedule.empty_oncall:
    schedule.current_shifts = json.dumps(current_shifts, default=str)

?

Ferril added 4 commits August 2, 2023 15:08
# Conflicts:
#	CHANGELOG.md
#	engine/apps/schedules/models/on_call_schedule.py
#	engine/apps/schedules/tasks/notify_about_gaps_in_schedule.py
#	engine/apps/slack/scenarios/schedules.py
@Ferril Ferril added this pull request to the merge queue Aug 3, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 3, 2023
@Ferril Ferril added this pull request to the merge queue Aug 3, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 3, 2023
@Ferril Ferril added this pull request to the merge queue Aug 3, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 3, 2023
@Ferril Ferril added this pull request to the merge queue Aug 3, 2023
Merged via the queue into dev with commit 0494afa Aug 3, 2023
@Ferril Ferril deleted the update-schedule-slack-notifications branch August 3, 2023 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:no public docs Added to a PR that does not require public documentation updates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants