diff --git a/config/routes.rb b/config/routes.rb index cfa14c854..0ecd27181 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -144,6 +144,11 @@ Rails.application.routes.draw do get :download end end + resources :applied_slas do + collection do + get :metrics + end + end resources :custom_attribute_definitions, only: [:index, :show, :create, :update, :destroy] resources :custom_filters, only: [:index, :show, :create, :update, :destroy] resources :inboxes, only: [:index, :show, :create, :update, :destroy] do diff --git a/enterprise/app/controllers/api/v1/accounts/applied_slas_controller.rb b/enterprise/app/controllers/api/v1/accounts/applied_slas_controller.rb new file mode 100644 index 000000000..f5242b0da --- /dev/null +++ b/enterprise/app/controllers/api/v1/accounts/applied_slas_controller.rb @@ -0,0 +1,30 @@ +class Api::V1::Accounts::AppliedSlasController < Api::V1::Accounts::EnterpriseAccountsController + include Sift + include DateRangeHelper + + RESULTS_PER_PAGE = 25 + + before_action :set_sla_responses, only: [:metrics] + before_action :check_admin_authorization? + + sort_on :created_at, type: :datetime + + def index; end + + def metrics + total_applied_slas = @sla_responses.count + missed_applied_slas = @sla_responses.where(sla_status: :missed).count + @hit_percentage = total_applied_slas.zero? ? 0 : ((total_applied_slas - missed_applied_slas) / total_applied_slas.to_f) * 100 + @number_of_breaches = missed_applied_slas + end + + private + + def set_sla_responses + base_query = Current.account.applied_slas.includes([:conversation, :assigned_agent, :contact]) + @sla_responses = filtrate(base_query).filter_by_created_at(range) + .filter_by_assigned_agent_id(params[:user_ids]) + .filter_by_inbox_id(params[:inbox_id]) + .filter_by_team_id(params[:team_id]) + end +end diff --git a/enterprise/app/models/applied_sla.rb b/enterprise/app/models/applied_sla.rb index c2dde8377..5010d1a85 100644 --- a/enterprise/app/models/applied_sla.rb +++ b/enterprise/app/models/applied_sla.rb @@ -29,6 +29,11 @@ class AppliedSla < ApplicationRecord enum sla_status: { active: 0, hit: 1, missed: 2 } + scope :filter_by_created_at, ->(range) { where(created_at: range) if range.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_assigned_agent_id, ->(user_ids) { joins(:conversation).where(conversations: { assigned_agent_id: user_ids }) if user_ids.present? } + scope :filter_by_inbox_id, ->(inbox_id) { joins(:conversation).where(conversations: { inbox_id: inbox_id }) if inbox_id.present? } + scope :filter_by_team_id, ->(team_id) { joins(:conversation).where(conversations: { team_id: team_id }) if team_id.present? } private def ensure_account_id diff --git a/enterprise/app/views/api/v1/accounts/applied_slas/index.json.jbuilder b/enterprise/app/views/api/v1/accounts/applied_slas/index.json.jbuilder new file mode 100644 index 000000000..96d79d8a5 --- /dev/null +++ b/enterprise/app/views/api/v1/accounts/applied_slas/index.json.jbuilder @@ -0,0 +1,14 @@ +json.array! @sla_responses do |sla_response| + json.id sla_response.id + json.sla_policy_id sla_response.sla_policy_id + json.conversation_id sla_response.conversation_id + json.sla_status sla_response.sla_status + json.created_at sla_response.created_at + json.updated_at sla_response.updated_at + json.conversation do + json.partial! 'api/v1/models/conversation', conversation: sla_response.conversation + end + json.sla_events sla_response.sla_events do |sla_event| + json.partial! 'api/v1/models/sla_event', formats: [:json], sla_policy: sla_event + end +end diff --git a/enterprise/app/views/api/v1/accounts/applied_slas/metrics.json.jbuilder b/enterprise/app/views/api/v1/accounts/applied_slas/metrics.json.jbuilder new file mode 100644 index 000000000..f792875fa --- /dev/null +++ b/enterprise/app/views/api/v1/accounts/applied_slas/metrics.json.jbuilder @@ -0,0 +1,2 @@ +json.hit_percentage @hit_percentage +json.number_of_breaches @number_of_breaches diff --git a/enterprise/app/views/api/v1/models/_sla_event.json.jbuilder b/enterprise/app/views/api/v1/models/_sla_event.json.jbuilder new file mode 100644 index 000000000..68b26f647 --- /dev/null +++ b/enterprise/app/views/api/v1/models/_sla_event.json.jbuilder @@ -0,0 +1 @@ +json.id sla_event.id