## Description Adds drilldown support for report bar charts powered by `ReportContainer`. Clicking a non-zero report bar now opens a right-side drawer with the conversations or messages that contributed to that bucket, with each row linking to the underlying conversation and message rows linking with `messageId`. This includes a new `GET /api/v2/accounts/:account_id/reports/drilldown` endpoint, backend drilldown builders/serializers, generic chart click emission, local drawer state via `useReportDrilldown`, compact drilldown cards, pagination, stale-response protection, and validation for unsupported drilldown dimensions. Fixes # CW-4497 https://linear.app/chatwoot/issue/CW-4497/drill-down-on-agent-conversations-report ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality not to work as expected) - [ ] This change requires a documentation update ## How Has This Been Tested? Ran the focused backend and frontend checks for the drilldown endpoint, builder, chart click handling, drawer/card UI, API helper, and stale-response handling. Here are the screenshots on how it looks like: <img width="1792" height="1199" alt="Screenshot 2026-06-02 at 11 32 11 PM" src="https://github.com/user-attachments/assets/6bdb8832-b9df-4bf3-9a2a-beaefe203b6e" /> <img width="1791" height="1230" alt="Screenshot 2026-06-02 at 11 32 34 PM" src="https://github.com/user-attachments/assets/36e92eb7-3208-4855-87f4-0c7f316df54d" /> <img width="1784" height="1235" alt="Screenshot 2026-06-02 at 11 32 46 PM" src="https://github.com/user-attachments/assets/f7a53916-74f2-4622-9305-042e0ac9e877" /> ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [x] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Vishnu Narayanan <iamwishnu@gmail.com> Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
215 lines
6.1 KiB
Ruby
215 lines
6.1 KiB
Ruby
class Api::V2::Accounts::ReportsController < Api::V1::Accounts::BaseController
|
|
include Api::V2::Accounts::ReportsHelper
|
|
include Api::V2::Accounts::HeatmapHelper
|
|
|
|
before_action :check_authorization
|
|
|
|
def index
|
|
builder = V2::Reports::Conversations::ReportBuilder.new(Current.account, report_params)
|
|
data = builder.timeseries
|
|
render json: data
|
|
end
|
|
|
|
def summary
|
|
render json: build_summary(:summary)
|
|
end
|
|
|
|
def bot_summary
|
|
render json: build_summary(:bot_summary)
|
|
end
|
|
|
|
def agents
|
|
@report_data = generate_agents_report
|
|
generate_csv('agents_report', 'api/v2/accounts/reports/agents')
|
|
end
|
|
|
|
def inboxes
|
|
@report_data = generate_inboxes_report
|
|
generate_csv('inboxes_report', 'api/v2/accounts/reports/inboxes')
|
|
end
|
|
|
|
def labels
|
|
@report_data = generate_labels_report
|
|
generate_csv('labels_report', 'api/v2/accounts/reports/labels')
|
|
end
|
|
|
|
def teams
|
|
@report_data = generate_teams_report
|
|
generate_csv('teams_report', 'api/v2/accounts/reports/teams')
|
|
end
|
|
|
|
def conversations_summary
|
|
@report_data = generate_conversations_report
|
|
generate_csv('conversations_summary_report', 'api/v2/accounts/reports/conversations_summary')
|
|
end
|
|
|
|
def conversation_traffic
|
|
@report_data = generate_conversations_heatmap_report
|
|
timezone_offset = (params[:timezone_offset] || 0).to_f
|
|
@timezone = ActiveSupport::TimeZone[timezone_offset]
|
|
|
|
generate_csv('conversation_traffic_reports', 'api/v2/accounts/reports/conversation_traffic')
|
|
end
|
|
|
|
def drilldown
|
|
return head :unauthorized unless Current.account_user.administrator?
|
|
return head :unprocessable_entity unless valid_drilldown_params?
|
|
|
|
render json: V2::Reports::DrilldownBuilder.new(Current.account, drilldown_params).build
|
|
end
|
|
|
|
def conversations
|
|
return head :unprocessable_entity if params[:type].blank?
|
|
|
|
render json: conversation_metrics
|
|
end
|
|
|
|
def bot_metrics
|
|
bot_metrics = V2::Reports::BotMetricsBuilder.new(Current.account, params).metrics
|
|
render json: bot_metrics
|
|
end
|
|
|
|
def inbox_label_matrix
|
|
builder = V2::Reports::InboxLabelMatrixBuilder.new(
|
|
account: Current.account,
|
|
params: inbox_label_matrix_params
|
|
)
|
|
render json: builder.build
|
|
end
|
|
|
|
def first_response_time_distribution
|
|
builder = V2::Reports::FirstResponseTimeDistributionBuilder.new(
|
|
account: Current.account,
|
|
params: first_response_time_distribution_params
|
|
)
|
|
render json: builder.build
|
|
end
|
|
|
|
OUTGOING_MESSAGES_ALLOWED_GROUP_BY = %w[agent team inbox label].freeze
|
|
|
|
def outgoing_messages_count
|
|
return head :unprocessable_entity unless OUTGOING_MESSAGES_ALLOWED_GROUP_BY.include?(params[:group_by])
|
|
|
|
builder = V2::Reports::OutgoingMessagesCountBuilder.new(Current.account, outgoing_messages_count_params)
|
|
render json: builder.build
|
|
end
|
|
|
|
private
|
|
|
|
def generate_csv(filename, template)
|
|
response.headers['Content-Type'] = 'text/csv'
|
|
response.headers['Content-Disposition'] = "attachment; filename=#{filename}.csv"
|
|
render layout: false, template: template, formats: [:csv]
|
|
end
|
|
|
|
def check_authorization
|
|
authorize :report, :view?
|
|
end
|
|
|
|
def common_params
|
|
{
|
|
type: params[:type].to_sym,
|
|
id: params[:id],
|
|
group_by: params[:group_by],
|
|
business_hours: ActiveModel::Type::Boolean.new.cast(params[:business_hours])
|
|
}
|
|
end
|
|
|
|
def current_summary_params
|
|
common_params.merge({
|
|
since: range[:current][:since],
|
|
until: range[:current][:until],
|
|
timezone_offset: params[:timezone_offset]
|
|
})
|
|
end
|
|
|
|
def previous_summary_params
|
|
common_params.merge({
|
|
since: range[:previous][:since],
|
|
until: range[:previous][:until],
|
|
timezone_offset: params[:timezone_offset]
|
|
})
|
|
end
|
|
|
|
def report_params
|
|
common_params.merge({
|
|
metric: params[:metric],
|
|
since: params[:since],
|
|
until: params[:until],
|
|
timezone_offset: params[:timezone_offset]
|
|
})
|
|
end
|
|
|
|
def drilldown_params
|
|
permitted_params = params.permit(
|
|
:metric, :id, :since, :until, :group_by, :timezone_offset, :bucket_timestamp, :page, :per_page
|
|
).to_h.symbolize_keys
|
|
permitted_params.merge(
|
|
type: (params[:type].presence || 'account').to_sym,
|
|
business_hours: ActiveModel::Type::Boolean.new.cast(params[:business_hours])
|
|
)
|
|
end
|
|
|
|
def valid_drilldown_params?
|
|
%i[metric bucket_timestamp since until].all? { |param| params[param].present? } &&
|
|
Reports::ReportMetricRegistry.supported?(params[:metric]) &&
|
|
V2::Reports::DrilldownBuilder.supported_dimension_type?(params[:type]) && Reports::DrilldownTimestampValidator.valid?(params)
|
|
end
|
|
|
|
def conversation_params
|
|
{
|
|
type: params[:type].to_sym,
|
|
user_id: params[:user_id],
|
|
page: params[:page].presence || 1
|
|
}
|
|
end
|
|
|
|
def range
|
|
{
|
|
current: {
|
|
since: params[:since],
|
|
until: params[:until]
|
|
},
|
|
previous: {
|
|
since: (params[:since].to_i - (params[:until].to_i - params[:since].to_i)).to_s,
|
|
until: params[:since]
|
|
}
|
|
}
|
|
end
|
|
|
|
def build_summary(method)
|
|
builder = V2::Reports::Conversations::MetricBuilder
|
|
current_summary = builder.new(Current.account, current_summary_params).send(method)
|
|
previous_summary = builder.new(Current.account, previous_summary_params).send(method)
|
|
current_summary.merge(previous: previous_summary)
|
|
end
|
|
|
|
def conversation_metrics
|
|
V2::ReportBuilder.new(Current.account, conversation_params).conversation_metrics
|
|
end
|
|
|
|
def inbox_label_matrix_params
|
|
{
|
|
since: params[:since],
|
|
until: params[:until],
|
|
inbox_ids: params[:inbox_ids],
|
|
label_ids: params[:label_ids]
|
|
}
|
|
end
|
|
|
|
def first_response_time_distribution_params
|
|
{
|
|
since: params[:since],
|
|
until: params[:until]
|
|
}
|
|
end
|
|
|
|
def outgoing_messages_count_params
|
|
{
|
|
group_by: params[:group_by],
|
|
since: params[:since],
|
|
until: params[:until]
|
|
}
|
|
end
|
|
end
|