Refactor Reports Controller

This commit is contained in:
Sojan Jose 2019-10-27 11:11:51 +05:30
parent 6c60b60240
commit 3b6d58bbdf

View file

@ -41,25 +41,18 @@ class Api::V1::ReportsController < Api::BaseController
end end
def account_summary_metrics def account_summary_metrics
ACCOUNT_METRICS.each_with_object({}) do |metric, result| summary_metrics(ACCOUNT_METRICS, :account_summary_params, AVG_ACCOUNT_METRICS)
data = ReportBuilder.new(current_account, account_summary_params(metric)).build
if AVG_ACCOUNT_METRICS.include?(metric)
sum = data.inject(0) { |sum, hash| sum + hash[:value].to_i }
sum /= data.length unless sum.zero?
else
sum = data.inject(0) { |sum, hash| sum + hash[:value].to_i }
end
result[metric] = sum
end
end end
def agent_summary_metrics def agent_summary_metrics
AGENT_METRICS.each_with_object({}) do |metric, result| summary_metrics(AGENT_METRICS, :agent_summary_params, AVG_AGENT_METRICS)
data = ReportBuilder.new(current_account, agent_summary_params(metric)).build end
if AVG_AGENT_METRICS.include?(metric) 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
if avg_metrics.include?(metric)
sum = data.inject(0) { |sum, hash| sum + hash[:value].to_i } sum = data.inject(0) { |sum, hash| sum + hash[:value].to_i }
sum /= data.length unless sum.zero? sum /= data.length unless sum.zero?
else else