From f3e89588731ff6deb3827d1be19a4295f1992602 Mon Sep 17 00:00:00 2001 From: fivetran-catfritz <111930712+fivetran-catfritz@users.noreply.github.com> Date: Fri, 23 Aug 2024 11:10:26 -0500 Subject: [PATCH] bug/missing-sla-policies --- ...int_zendesk__agent_work_time_business_hours.sql | 12 ++++++++---- ...zendesk__requester_wait_time_business_hours.sql | 14 +++++++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/models/sla_policy/agent_work_time/int_zendesk__agent_work_time_business_hours.sql b/models/sla_policy/agent_work_time/int_zendesk__agent_work_time_business_hours.sql index 5abad813..84489d0a 100644 --- a/models/sla_policy/agent_work_time/int_zendesk__agent_work_time_business_hours.sql +++ b/models/sla_policy/agent_work_time/int_zendesk__agent_work_time_business_hours.sql @@ -120,11 +120,15 @@ with agent_work_time_filtered_statuses as ( weekly_period_agent_work_time.week_number, weekly_period_agent_work_time.ticket_week_start_time_minute, weekly_period_agent_work_time.ticket_week_end_time_minute, - schedule.start_time_utc as schedule_start_time, + coalesce(schedule.start_time_utc, 0) as schedule_start_time, schedule.end_time_utc as schedule_end_time, - least(ticket_week_end_time_minute, schedule.end_time_utc) - greatest(weekly_period_agent_work_time.ticket_week_start_time_minute, schedule.start_time_utc) as scheduled_minutes + coalesce( + least(ticket_week_end_time_minute, schedule.end_time_utc) + - greatest(weekly_period_agent_work_time.ticket_week_start_time_minute, schedule.start_time_utc), + 0) as scheduled_minutes from weekly_period_agent_work_time - join schedule on ticket_week_start_time_minute <= schedule.end_time_utc + left join schedule + on ticket_week_start_time_minute <= schedule.end_time_utc and ticket_week_end_time_minute >= schedule.start_time_utc and weekly_period_agent_work_time.schedule_id = schedule.schedule_id -- this chooses the Daylight Savings Time or Standard Time version of the schedule @@ -152,7 +156,7 @@ with agent_work_time_filtered_statuses as ( lag(target - running_total_scheduled_minutes) over (partition by ticket_id, sla_applied_at order by valid_starting_at, week_number, schedule_end_time) as lag_check, case when (target - running_total_scheduled_minutes) = 0 then true - when (target - running_total_scheduled_minutes) < 0 + when (target - running_total_scheduled_minutes) < 0 and (lag(target - running_total_scheduled_minutes) over (partition by ticket_id, sla_applied_at order by valid_starting_at, week_number, schedule_end_time) > 0 diff --git a/models/sla_policy/requester_wait_time/int_zendesk__requester_wait_time_business_hours.sql b/models/sla_policy/requester_wait_time/int_zendesk__requester_wait_time_business_hours.sql index 53b09dca..01a24139 100644 --- a/models/sla_policy/requester_wait_time/int_zendesk__requester_wait_time_business_hours.sql +++ b/models/sla_policy/requester_wait_time/int_zendesk__requester_wait_time_business_hours.sql @@ -120,18 +120,22 @@ with requester_wait_time_filtered_statuses as ( weekly_period_requester_wait_time.week_number, weekly_period_requester_wait_time.ticket_week_start_time_minute, weekly_period_requester_wait_time.ticket_week_end_time_minute, - schedule.start_time_utc as schedule_start_time, + coalesce(schedule.start_time_utc, 0) as schedule_start_time, schedule.end_time_utc as schedule_end_time, - least(ticket_week_end_time_minute, schedule.end_time_utc) - greatest(weekly_period_requester_wait_time.ticket_week_start_time_minute, schedule.start_time_utc) as scheduled_minutes + coalesce( + least(ticket_week_end_time_minute, schedule.end_time_utc) + - greatest(weekly_period_requester_wait_time.ticket_week_start_time_minute, schedule.start_time_utc), + 0) as scheduled_minutes from weekly_period_requester_wait_time - join schedule on ticket_week_start_time_minute <= schedule.end_time_utc + left join schedule + on ticket_week_start_time_minute <= schedule.end_time_utc and ticket_week_end_time_minute >= schedule.start_time_utc and weekly_period_requester_wait_time.schedule_id = schedule.schedule_id -- this chooses the Daylight Savings Time or Standard Time version of the schedule -- We have everything calculated within a week, so take us to the appropriate week first by adding the week_number * minutes-in-a-week to the minute-mark where we start and stop counting for the week and cast( {{ dbt.dateadd(datepart='minute', interval='week_number * (7*24*60) + ticket_week_end_time_minute', from_date_or_timestamp='start_week_date') }} as {{ dbt.type_timestamp() }}) > cast(schedule.valid_from as {{ dbt.type_timestamp() }}) and cast( {{ dbt.dateadd(datepart='minute', interval='week_number * (7*24*60) + ticket_week_start_time_minute', from_date_or_timestamp='start_week_date') }} as {{ dbt.type_timestamp() }}) < cast(schedule.valid_until as {{ dbt.type_timestamp() }}) - + ), intercepted_periods_with_running_total as ( select @@ -152,7 +156,7 @@ with requester_wait_time_filtered_statuses as ( lag(target - running_total_scheduled_minutes) over (partition by ticket_id, sla_applied_at order by valid_starting_at, week_number, schedule_end_time) as lag_check, case when (target - running_total_scheduled_minutes) = 0 then true - when (target - running_total_scheduled_minutes) < 0 + when (target - running_total_scheduled_minutes) < 0 and (lag(target - running_total_scheduled_minutes) over (partition by ticket_id, sla_applied_at order by valid_starting_at, week_number, schedule_end_time) > 0