-
Notifications
You must be signed in to change notification settings - Fork 29
/
zendesk__sla_policies.sql
134 lines (104 loc) · 3.12 KB
/
zendesk__sla_policies.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
--final step where we union together all of the reply time, agent work time, and requester wait time sla's
with reply_time_sla as (
select *
from {{ ref('int_zendesk__reply_time_combined') }}
), agent_work_calendar_sla as (
select *
from {{ ref('int_zendesk__agent_work_time_calendar_hours') }}
), requester_wait_calendar_sla as (
select *
from {{ ref('int_zendesk__requester_wait_time_calendar_hours') }}
{% if var('using_schedules', True) %}
), agent_work_business_sla as (
select *
from {{ ref('int_zendesk__agent_work_time_business_hours') }}
), requester_wait_business_sla as (
select *
from {{ ref('int_zendesk__requester_wait_time_business_hours') }}
{% endif %}
), all_slas_unioned as (
select
ticket_id,
sla_policy_name,
metric,
sla_applied_at,
target,
in_business_hours,
sla_breach_at,
sla_elapsed_time,
is_sla_breached
from reply_time_sla
union all
select
ticket_id,
sla_policy_name,
'agent_work_time' as metric,
sla_applied_at,
target,
false as in_business_hours,
max(sla_breach_at) as sla_breach_at,
max(running_total_calendar_minutes) as sla_elapsed_time,
{{ fivetran_utils.max_bool("is_breached_during_schedule") }}
from agent_work_calendar_sla
group by 1, 2, 3, 4, 5, 6
union all
select
ticket_id,
sla_policy_name,
'requester_wait_time' as metric,
sla_applied_at,
target,
false as in_business_hours,
max(sla_breach_at) as sla_breach_at,
max(running_total_calendar_minutes) as sla_elapsed_time,
{{ fivetran_utils.max_bool("is_breached_during_schedule") }}
from requester_wait_calendar_sla
group by 1, 2, 3, 4, 5, 6
{% if var('using_schedules', True) %}
union all
select
ticket_id,
sla_policy_name,
'agent_work_time' as metric,
sla_applied_at,
target,
true as in_business_hours,
max(sla_breach_at) as sla_breach_at,
max(running_total_scheduled_minutes) as sla_elapsed_time,
{{ fivetran_utils.max_bool("is_breached_during_schedule") }}
from agent_work_business_sla
group by 1, 2, 3, 4, 5, 6
union all
select
ticket_id,
sla_policy_name,
'requester_wait_time' as metric,
sla_applied_at,
target,
true as in_business_hours,
max(sla_breach_at) as sla_breach_at,
max(running_total_scheduled_minutes) as sla_elapsed_time,
{{ fivetran_utils.max_bool("is_breached_during_schedule") }}
from requester_wait_business_sla
group by 1, 2, 3, 4, 5, 6
{% endif %}
)
select
{{ dbt_utils.generate_surrogate_key(['ticket_id', 'metric', 'sla_applied_at']) }} as sla_event_id,
ticket_id,
sla_policy_name,
metric,
sla_applied_at,
target,
in_business_hours,
sla_breach_at,
case when sla_elapsed_time is null
then {{ dbt.datediff("sla_applied_at", dbt.current_timestamp_backcompat(), 'minute') }} --This will create an entry for active sla's
else sla_elapsed_time
end as sla_elapsed_time,
sla_breach_at > current_timestamp as is_active_sla,
case when (sla_breach_at > {{ dbt.current_timestamp_backcompat() }})
then null
else is_sla_breached
end as is_sla_breach
from all_slas_unioned