feat: Add metrics API

This commit is contained in:
Muhsin Keloth
2024-03-20 13:02:42 +05:30
parent b017d05ed9
commit a6548d7921
6 changed files with 57 additions and 0 deletions
+5
View File
@@ -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
@@ -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
+5
View File
@@ -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
@@ -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
@@ -0,0 +1,2 @@
json.hit_percentage @hit_percentage
json.number_of_breaches @number_of_breaches
@@ -0,0 +1 @@
json.id sla_event.id