-
Notifications
You must be signed in to change notification settings - Fork 30
/
zendesk__sla_policies.sql
140 lines (110 loc) · 3.35 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
135
136
137
138
139
140
--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
source_relation,
ticket_id,
sla_policy_name,
metric,
sla_applied_at,
target,
in_business_hours,
sla_update_at as sla_breach_at,
sla_elapsed_time,
is_sla_breached
from reply_time_sla
union all
select
source_relation,
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") }} as is_sla_breached
from agent_work_calendar_sla
{{ dbt_utils.group_by(n=7) }}
union all
select
source_relation,
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") }} as is_sla_breached
from requester_wait_calendar_sla
{{ dbt_utils.group_by(n=7) }}
{% if var('using_schedules', True) %}
union all
select
source_relation,
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") }} as is_sla_breached
from agent_work_business_sla
{{ dbt_utils.group_by(n=7) }}
union all
select
source_relation,
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") }} as is_sla_breached
from requester_wait_business_sla
{{ dbt_utils.group_by(n=7) }}
{% endif %}
)
select
{{ dbt_utils.generate_surrogate_key(['source_relation', 'ticket_id', 'metric', 'sla_applied_at']) }} as sla_event_id,
source_relation,
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(), 'second') }} / 60) --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() }})
then null
else is_sla_breached
end as is_sla_breach
from all_slas_unioned