feat: Add report bar drilldown drawer (#14626)

## 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>
This commit is contained in:
Sony Mathew
2026-07-02 16:07:26 +05:30
committed by GitHub
co-authored by Vishnu Narayanan Shivam Mishra
parent 6c9efc4e92
commit 6a7ca9dd3b
25 changed files with 2788 additions and 6 deletions
@@ -233,6 +233,107 @@ RSpec.describe 'Reports API', type: :request do
end
end
describe 'GET /api/v2/accounts/:account_id/reports/drilldown' do
let(:params) do
super().merge(
metric: 'conversations_count',
type: :account,
since: start_of_today.to_s,
until: end_of_today.to_s,
bucket_timestamp: start_of_today.to_s,
group_by: 'day'
)
end
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v2/accounts/#{account.id}/reports/drilldown"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
it 'returns unauthorized for agents' do
get "/api/v2/accounts/#{account.id}/reports/drilldown",
params: params,
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unauthorized)
end
it 'returns drilldown records for the selected bucket' do
get "/api/v2/accounts/#{account.id}/reports/drilldown",
params: params,
headers: admin.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
json_response = response.parsed_body
expect(json_response['meta']['metric']).to eq('conversations_count')
expect(json_response['meta']['record_type']).to eq('conversation')
expect(json_response['meta']['total_count']).to eq(10)
expect(json_response['payload'].first['conversation']).to include('display_id', 'contact_name', 'inbox_name')
end
it 'returns unprocessable entity for missing bucket timestamp' do
get "/api/v2/accounts/#{account.id}/reports/drilldown",
params: params.except(:bucket_timestamp),
headers: admin.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unprocessable_entity)
end
it 'returns unprocessable entity for invalid bucket timestamp' do
get "/api/v2/accounts/#{account.id}/reports/drilldown",
params: params.merge(bucket_timestamp: 'abc'),
headers: admin.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unprocessable_entity)
end
it 'returns unprocessable entity for bucket timestamp outside the requested range' do
get "/api/v2/accounts/#{account.id}/reports/drilldown",
params: params.merge(bucket_timestamp: end_of_today.to_s),
headers: admin.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unprocessable_entity)
end
it 'returns drilldown records for a partial first weekly bucket' do
range_start = Time.zone.local(2026, 5, 20, 12)
range_end = Time.zone.local(2026, 5, 27, 12)
week_start = range_start.beginning_of_week(:sunday)
get "/api/v2/accounts/#{account.id}/reports/drilldown",
params: params.merge(
since: range_start.to_i.to_s,
until: range_end.to_i.to_s,
bucket_timestamp: week_start.to_i.to_s,
group_by: 'week'
),
headers: admin.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
end
it 'returns unprocessable entity for unsupported drilldown type' do
get "/api/v2/accounts/#{account.id}/reports/drilldown",
params: params.merge(type: :unsupported),
headers: admin.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unprocessable_entity)
end
end
end
describe 'GET /api/v2/accounts/:account_id/reports/agents' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do