refactor: return sla events from the evaluate service

This commit is contained in:
Shivam Mishra
2024-04-08 19:04:46 +05:30
parent 0e772d729b
commit e3150562c0
2 changed files with 22 additions and 12 deletions
@@ -5,9 +5,11 @@ class Sla::ProcessAccountAppliedSlasJob < ApplicationJob
missed_slas = []
account.applied_slas.where(sla_status: %w[active active_with_misses]).includes(:conversation, :sla_policy).each do |applied_sla|
Sla::EvaluateAppliedSlaService.new(applied_sla: applied_sla).perform
applied_sla.reload
missed_slas << applied_sla if applied_sla.status == :missed || applied_sla.status == :active_with_misses
sla_events, updated_applied_sla = Sla::EvaluateAppliedSlaService.new(applied_sla: applied_sla).perform
if updated_applied_sla.status == :missed || updated_applied_sla.status == :active_with_misses
missed_slas << { sla_events: sla_events,
applied_sla: updated_applied_sla }
end
end
user_sla_map = build_user_sla_map(missed_slas)
@@ -17,19 +19,22 @@ class Sla::ProcessAccountAppliedSlasJob < ApplicationJob
def build_user_sla_map(missed_slas)
user_sla_map = {}
missed_slas.each do |applied_sla|
missed_slas.each do |missed_sla|
applied_sla = missed_sla[:applied_sla]
sla_events = missed_sla[:sla_events]
# get all participants of the conversation
users_to_notify = applied_sla.conversation.conversation_participants.map(&:user)
assignee = applied_sla.conversation.assignee
users_to_notify.each do |user|
user_sla_map[user] ||= []
user_sla_map[user] << applied_sla
user_sla_map[user] << { sla_events: sla_events, applied_sla: applied_sla }
end
if assignee.present?
user_sla_map[assignee] ||= []
user_sla_map[assignee] << applied_sla
user_sla_map[assignee] << { sla_events: sla_events, applied_sla: applied_sla }
end
end
@@ -39,8 +44,8 @@ class Sla::ProcessAccountAppliedSlasJob < ApplicationJob
def notify_users(user_sla_map, account)
admins = account.administrators
user_sla_map.each do |user, slas|
puts "Notifying user #{user.id} and #{admins.count} admins about #{slas.count} missed SLAs"
user_sla_map.each do |user, sla_data|
puts "Notifying user #{user.id} and #{admins.count} admins about #{sla_data.count} missed SLAs"
end
end
end
@@ -6,22 +6,27 @@ class Sla::EvaluateAppliedSlaService
RT_THRESHOLD = 'rt'.freeze
def perform
check_sla_thresholds
sla_events = []
check_sla_thresholds(sla_events)
# We will calculate again in the next iteration
return unless applied_sla.conversation.resolved?
return [sla_events, applied_sla] unless applied_sla.conversation.resolved?
# after conversation is resolved, we will check if the SLA was hit or missed
handle_hit_sla(applied_sla)
[sla_events, applied_sla]
end
private
def check_sla_thresholds
def check_sla_thresholds(sla_events)
[:first_response_time_threshold, :next_response_time_threshold, :resolution_time_threshold].each do |threshold|
next if applied_sla.sla_policy.send(threshold).blank?
send("check_#{threshold}", applied_sla, applied_sla.conversation, applied_sla.sla_policy)
event = send("check_#{threshold}", applied_sla, applied_sla.conversation, applied_sla.sla_policy)
sla_events << event if event.present?
end
end