2019-08-14 09:48:44 +00:00
|
|
|
class Api::V1::ReportsController < Api::BaseController
|
|
|
|
include CustomExceptions::Report
|
|
|
|
include Constants::Report
|
|
|
|
|
2019-08-21 16:22:20 +00:00
|
|
|
around_action :report_exception
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
def account
|
|
|
|
builder = ReportBuilder.new(current_account, account_report_params)
|
|
|
|
data = builder.build
|
|
|
|
render json: data
|
|
|
|
end
|
|
|
|
|
|
|
|
def agent
|
|
|
|
builder = ReportBuilder.new(current_account, agent_report_params)
|
|
|
|
data = builder.build
|
|
|
|
render json: data
|
|
|
|
end
|
|
|
|
|
|
|
|
def account_summary
|
|
|
|
render json: account_summary_metrics
|
|
|
|
end
|
|
|
|
|
|
|
|
def agent_summary
|
|
|
|
render json: agent_summary_metrics
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def report_exception
|
2019-10-20 08:47:26 +00:00
|
|
|
yield
|
|
|
|
rescue InvalidIdentity, IdentityNotFound, MetricNotFound, InvalidStartTime, InvalidEndTime => e
|
|
|
|
render_error_response(e)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_account
|
|
|
|
current_user.account
|
|
|
|
end
|
|
|
|
|
|
|
|
def agent
|
|
|
|
@agent ||= current_account.users.find(params[:agent_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def account_summary_metrics
|
2019-10-27 05:41:51 +00:00
|
|
|
summary_metrics(ACCOUNT_METRICS, :account_summary_params, AVG_ACCOUNT_METRICS)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def agent_summary_metrics
|
2019-10-27 05:41:51 +00:00
|
|
|
summary_metrics(AGENT_METRICS, :agent_summary_params, AVG_AGENT_METRICS)
|
|
|
|
end
|
|
|
|
|
|
|
|
def summary_metrics(metrics, calc_function, avg_metrics)
|
|
|
|
metrics.each_with_object({}) do |metric, result|
|
|
|
|
data = ReportBuilder.new(current_account, send(calc_function, metric)).build
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-27 05:41:51 +00:00
|
|
|
if avg_metrics.include?(metric)
|
2019-10-20 08:47:26 +00:00
|
|
|
sum = data.inject(0) { |sum, hash| sum + hash[:value].to_i }
|
|
|
|
sum /= data.length unless sum.zero?
|
2019-08-14 09:48:44 +00:00
|
|
|
else
|
2019-10-20 08:47:26 +00:00
|
|
|
sum = data.inject(0) { |sum, hash| sum + hash[:value].to_i }
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
result[metric] = sum
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def account_summary_params(metric)
|
|
|
|
{
|
|
|
|
metric: metric.to_s,
|
|
|
|
type: :account,
|
2019-10-20 08:47:26 +00:00
|
|
|
since: params[:since],
|
2019-08-14 09:48:44 +00:00
|
|
|
until: params[:until]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def agent_summary_params(metric)
|
|
|
|
{
|
|
|
|
metric: metric.to_s,
|
|
|
|
type: :agent,
|
2019-10-20 08:47:26 +00:00
|
|
|
since: params[:since],
|
2019-08-14 09:48:44 +00:00
|
|
|
until: params[:until],
|
|
|
|
id: params[:id]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def account_report_params
|
|
|
|
{
|
|
|
|
metric: params[:metric],
|
|
|
|
type: :account,
|
2019-10-20 08:47:26 +00:00
|
|
|
since: params[:since],
|
2019-08-14 09:48:44 +00:00
|
|
|
until: params[:until]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def agent_report_params
|
|
|
|
{
|
|
|
|
metric: params[:metric],
|
|
|
|
type: :agent,
|
|
|
|
id: params[:id],
|
2019-10-20 08:47:26 +00:00
|
|
|
since: params[:since],
|
2019-08-14 09:48:44 +00:00
|
|
|
until: params[:until]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|