feat: Add SLA metrics API (#9132)

This commit is contained in:
Muhsin Keloth
2024-03-25 12:24:43 +05:30
committed by GitHub
parent 6d4551bca2
commit 48452a42f4
7 changed files with 284 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
class Api::V1::Accounts::AppliedSlasController < Api::V1::Accounts::EnterpriseAccountsController
include Sift
include DateRangeHelper
RESULTS_PER_PAGE = 25
before_action :set_applied_slas, only: [:index, :metrics]
before_action :set_current_page, only: [:index]
before_action :paginate_slas, only: [:index]
before_action :check_admin_authorization?
sort_on :created_at, type: :datetime
def index; end
def metrics
@total_applied_slas = total_applied_slas
@number_of_sla_breaches = number_of_sla_breaches
@hit_rate = hit_rate
end
private
def total_applied_slas
@total_applied_slas ||= @applied_slas.count
end
def number_of_sla_breaches
@number_of_sla_breaches ||= @applied_slas.missed.count
end
def hit_rate
number_of_sla_breaches.zero? ? '100%' : "#{hit_rate_percentage}%"
end
def hit_rate_percentage
((total_applied_slas - number_of_sla_breaches) / total_applied_slas.to_f * 100).round(2)
end
def set_applied_slas
initial_query = Current.account.applied_slas.includes(:conversation)
@applied_slas = initial_query
.filter_by_date_range(range)
.filter_by_inbox_id(params[:inbox_id])
.filter_by_team_id(params[:team_id])
.filter_by_sla_policy_id(params[:sla_policy_id])
.filter_by_label_list(params[:label_list])
.filter_by_assigned_agent_id(params[:assigned_agent_id])
end
def paginate_slas
@applied_slas = @applied_slas.page(@current_page).per(RESULTS_PER_PAGE)
end
def set_current_page
@current_page = params[:page] || 1
end
end

View File

@@ -29,6 +29,17 @@ class AppliedSla < ApplicationRecord
enum sla_status: { active: 0, hit: 1, missed: 2 }
scope :filter_by_date_range, ->(range) { where(created_at: range) if range.present? }
scope :filter_by_inbox_id, ->(inbox_id) { where(inbox_id: inbox_id) if inbox_id.present? }
scope :filter_by_team_id, ->(team_id) { where(team_id: team_id) if team_id.present? }
scope :filter_by_sla_policy_id, ->(sla_policy_id) { where(sla_policy_id: sla_policy_id) if sla_policy_id.present? }
scope :filter_by_label_list, ->(label_list) { joins(:conversation).where(conversations: { cached_label_list: label_list }) if label_list.present? }
scope :filter_by_assigned_agent_id, lambda { |assigned_agent_id|
if assigned_agent_id.present?
joins(:conversation).where(conversations: { assigned_agent_id: assigned_agent_id })
end
}
scope :missed, -> { where(sla_status: :missed) }
private
def ensure_account_id

View File

@@ -0,0 +1,14 @@
json.array! @applied_slas do |applied_sla|
json.id applied_sla.id
json.sla_policy_id applied_sla.sla_policy_id
json.conversation_id applied_sla.conversation_id
json.sla_status applied_sla.sla_status
json.created_at applied_sla.created_at
json.updated_at applied_sla.updated_at
json.conversation do
json.partial! 'api/v1/models/conversation', conversation: applied_sla.conversation
end
json.sla_events applied_sla.sla_events do |sla_event|
json.partial! 'api/v1/models/sla_event', formats: [:json], sla_event: sla_event
end
end

View File

@@ -0,0 +1,3 @@
json.total_applied_slas @total_applied_slas
json.number_of_sla_breaches @number_of_sla_breaches
json.hit_rate @hit_rate

View File

@@ -0,0 +1,5 @@
json.id sla_event.id
json.event_type sla_event.event_type
json.meta sla_event.meta
json.updated_at sla_event.updated_at.to_i
json.created_at sla_event.created_at.to_i