2020-03-18 11:23:35 +00:00
|
|
|
class V2::ReportBuilder
|
2021-06-29 15:29:41 +00:00
|
|
|
include DateRangeHelper
|
2022-03-29 05:01:52 +00:00
|
|
|
include ReportHelper
|
2020-03-18 11:23:35 +00:00
|
|
|
attr_reader :account, :params
|
|
|
|
|
2022-02-15 11:40:49 +00:00
|
|
|
DEFAULT_GROUP_BY = 'day'.freeze
|
2022-04-26 12:52:13 +00:00
|
|
|
AGENT_RESULTS_PER_PAGE = 25
|
2022-02-15 11:40:49 +00:00
|
|
|
|
2020-03-18 11:23:35 +00:00
|
|
|
def initialize(account, params)
|
|
|
|
@account = account
|
|
|
|
@params = params
|
2022-02-28 05:26:24 +00:00
|
|
|
|
|
|
|
timezone_offset = (params[:timezone_offset] || 0).to_f
|
|
|
|
@timezone = ActiveSupport::TimeZone[timezone_offset]&.name
|
2020-03-18 11:23:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def timeseries
|
|
|
|
send(params[:metric])
|
|
|
|
end
|
|
|
|
|
|
|
|
# For backward compatible with old report
|
|
|
|
def build
|
2022-03-28 07:38:23 +00:00
|
|
|
if %w[avg_first_response_time avg_resolution_time].include?(params[:metric])
|
|
|
|
timeseries.each_with_object([]) do |p, arr|
|
|
|
|
arr << { value: p[1], timestamp: p[0].in_time_zone(@timezone).to_i, count: @grouped_values.count[p[0]] }
|
|
|
|
end
|
|
|
|
else
|
|
|
|
timeseries.each_with_object([]) do |p, arr|
|
|
|
|
arr << { value: p[1], timestamp: p[0].in_time_zone(@timezone).to_i }
|
|
|
|
end
|
2020-03-18 11:23:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def summary
|
|
|
|
{
|
|
|
|
conversations_count: conversations_count.values.sum,
|
|
|
|
incoming_messages_count: incoming_messages_count.values.sum,
|
|
|
|
outgoing_messages_count: outgoing_messages_count.values.sum,
|
|
|
|
avg_first_response_time: avg_first_response_time_summary,
|
|
|
|
avg_resolution_time: avg_resolution_time_summary,
|
|
|
|
resolutions_count: resolutions_count.values.sum
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-03-29 05:01:52 +00:00
|
|
|
def conversation_metrics
|
|
|
|
if params[:type].equal?(:account)
|
|
|
|
conversations
|
|
|
|
else
|
2022-04-26 12:52:13 +00:00
|
|
|
agent_metrics.sort_by { |hash| hash[:metric][:open] }.reverse
|
2021-08-27 17:16:32 +00:00
|
|
|
end
|
2020-03-18 11:23:35 +00:00
|
|
|
end
|
|
|
|
|
2022-03-29 05:01:52 +00:00
|
|
|
private
|
|
|
|
|
2020-03-18 11:23:35 +00:00
|
|
|
def inbox
|
2021-09-27 15:42:08 +00:00
|
|
|
@inbox ||= account.inboxes.find(params[:id])
|
2020-03-18 11:23:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def user
|
2021-09-27 15:42:08 +00:00
|
|
|
@user ||= account.users.find(params[:id])
|
2020-03-18 11:23:35 +00:00
|
|
|
end
|
|
|
|
|
2021-08-27 17:16:32 +00:00
|
|
|
def label
|
2021-09-27 15:42:08 +00:00
|
|
|
@label ||= account.labels.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def team
|
|
|
|
@team ||= account.teams.find(params[:id])
|
2021-08-27 17:16:32 +00:00
|
|
|
end
|
|
|
|
|
2022-02-28 05:26:24 +00:00
|
|
|
def get_grouped_values(object_scope)
|
2022-03-28 07:38:23 +00:00
|
|
|
@grouped_values = object_scope.group_by_period(
|
2022-02-28 05:26:24 +00:00
|
|
|
params[:group_by] || DEFAULT_GROUP_BY,
|
|
|
|
:created_at,
|
|
|
|
default_value: 0,
|
|
|
|
range: range,
|
|
|
|
permit: %w[day week month year],
|
|
|
|
time_zone: @timezone
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-03-29 05:01:52 +00:00
|
|
|
def agent_metrics
|
2022-04-25 14:34:41 +00:00
|
|
|
account_users = @account.account_users.page(params[:page]).per(AGENT_RESULTS_PER_PAGE)
|
|
|
|
account_users.each_with_object([]) do |account_user, arr|
|
|
|
|
@user = account_user.user
|
2022-03-29 05:01:52 +00:00
|
|
|
arr << {
|
2022-04-25 14:34:41 +00:00
|
|
|
id: @user.id,
|
|
|
|
name: @user.name,
|
|
|
|
email: @user.email,
|
|
|
|
thumbnail: @user.avatar_url,
|
|
|
|
availability: account_user.availability_status,
|
2022-03-29 05:01:52 +00:00
|
|
|
metric: conversations
|
|
|
|
}
|
|
|
|
end
|
2020-03-18 11:23:35 +00:00
|
|
|
end
|
|
|
|
|
2022-03-29 05:01:52 +00:00
|
|
|
def conversations
|
2022-04-26 12:52:13 +00:00
|
|
|
@open_conversations = scope.conversations.where(account_id: @account.id).open
|
2022-04-27 13:26:35 +00:00
|
|
|
first_response_count = @account.reporting_events.where(name: 'first_response', conversation_id: @open_conversations.pluck('id')).count
|
2022-03-29 05:01:52 +00:00
|
|
|
metric = {
|
2022-04-26 12:52:13 +00:00
|
|
|
open: @open_conversations.count,
|
2022-03-29 05:01:52 +00:00
|
|
|
unattended: @open_conversations.count - first_response_count
|
|
|
|
}
|
|
|
|
metric[:unassigned] = @open_conversations.unassigned.count if params[:type].equal?(:account)
|
|
|
|
metric
|
2020-03-18 11:23:35 +00:00
|
|
|
end
|
|
|
|
end
|