perf(reports): skip computing hidden metrics in summary
Instead of filtering metrics after computation, skip the expensive database queries entirely for hidden metrics. This prevents timeouts on accounts with large datasets. - MetricBuilder now accepts hidden_metrics param and returns nil for hidden metrics (compacted from response) - Reports controller passes hidden_metrics to builder params
This commit is contained in:
@@ -1,14 +1,21 @@
|
||||
class V2::Reports::Conversations::MetricBuilder < V2::Reports::Conversations::BaseReportBuilder
|
||||
# Mapping from metric keys to canonical hidden metric names
|
||||
METRIC_TO_HIDDEN_KEY = {
|
||||
'incoming_messages_count' => 'incoming_messages_count',
|
||||
'outgoing_messages_count' => 'outgoing_messages_count',
|
||||
'reply_time' => 'reply_time'
|
||||
}.freeze
|
||||
|
||||
def summary
|
||||
{
|
||||
conversations_count: count('conversations_count'),
|
||||
incoming_messages_count: count('incoming_messages_count'),
|
||||
outgoing_messages_count: count('outgoing_messages_count'),
|
||||
incoming_messages_count: count_unless_hidden('incoming_messages_count'),
|
||||
outgoing_messages_count: count_unless_hidden('outgoing_messages_count'),
|
||||
avg_first_response_time: count('avg_first_response_time'),
|
||||
avg_resolution_time: count('avg_resolution_time'),
|
||||
resolutions_count: count('resolutions_count'),
|
||||
reply_time: count('reply_time')
|
||||
}
|
||||
reply_time: count_unless_hidden('reply_time')
|
||||
}.compact
|
||||
end
|
||||
|
||||
def bot_summary
|
||||
@@ -24,6 +31,18 @@ class V2::Reports::Conversations::MetricBuilder < V2::Reports::Conversations::Ba
|
||||
builder_class(metric).new(account, builder_params(metric)).aggregate_value
|
||||
end
|
||||
|
||||
def count_unless_hidden(metric)
|
||||
return nil if metric_hidden?(metric)
|
||||
|
||||
count(metric)
|
||||
end
|
||||
|
||||
def metric_hidden?(metric)
|
||||
hidden_metrics = params[:hidden_metrics] || []
|
||||
canonical_key = METRIC_TO_HIDDEN_KEY[metric]
|
||||
canonical_key && hidden_metrics.include?(canonical_key)
|
||||
end
|
||||
|
||||
def builder_params(metric)
|
||||
params.merge({ metric: metric })
|
||||
end
|
||||
|
||||
@@ -15,11 +15,11 @@ class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController
|
||||
end
|
||||
|
||||
def summary
|
||||
render json: filter_summary_with_previous(build_summary(:summary))
|
||||
render json: build_summary(:summary)
|
||||
end
|
||||
|
||||
def bot_summary
|
||||
render json: filter_summary_with_previous(build_summary(:bot_summary))
|
||||
render json: build_summary(:bot_summary)
|
||||
end
|
||||
|
||||
def agents
|
||||
@@ -107,7 +107,8 @@ class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController
|
||||
common_params.merge({
|
||||
since: range[:current][:since],
|
||||
until: range[:current][:until],
|
||||
timezone_offset: params[:timezone_offset]
|
||||
timezone_offset: params[:timezone_offset],
|
||||
hidden_metrics: hidden_metrics
|
||||
})
|
||||
end
|
||||
|
||||
@@ -115,7 +116,8 @@ class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController
|
||||
common_params.merge({
|
||||
since: range[:previous][:since],
|
||||
until: range[:previous][:until],
|
||||
timezone_offset: params[:timezone_offset]
|
||||
timezone_offset: params[:timezone_offset],
|
||||
hidden_metrics: hidden_metrics
|
||||
})
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user