iachat/app/helpers
Shivam Mishra ef4e287f0d
fix: wrong resolution count in timeseries reports (#12261)
There was a fundamental difference in how resolution counts were
calculated between the agent summary and timeseries reports, causing
confusion for users when the numbers didn't match.

The agent summary report counted all `conversation_resolved` events
within a time period by querying the `reporting_events` table directly.
However, the timeseries report had an additional constraint that
required the conversation to currently be in resolved status
(`conversations.status = 1`). This meant that if an agent resolved a
conversation that was later reopened, the resolution action would be
counted in the summary but not in the timeseries.

This fix aligns both reports to count resolution events rather than
conversations in resolved state. When an agent resolves a conversation,
they should receive credit for that action regardless of what happens to
the conversation afterward. The same logic now applies to bot
resolutions as well.

The change removes the `conversations: { status: :resolved }` condition
from both `scope_for_resolutions_count` and
`scope_for_bot_resolutions_count` methods in CountReportBuilder, and
updates the corresponding test expectations to reflect that all
resolution events are counted.


## About timezone

When a timezone is specified via `timezone_offset` parameter, the
reporting system:

1. Converts timestamps to the target timezone before grouping
2. Groups data by local day/week/month boundaries in that timezone, but
the primary boundaries are sent by the frontend and used as-is
3. Returns timestamps representing midnight in the target timezone

This means the same events can appear in different day buckets depending
on the timezone used. For summary reports, it works fine, since the user
only needs the total count between two timestamps and the frontend sends
the timestamps adjusted for timezone.

## Testing Locally

Run the following command, this will erase all data for that account and
put in 1000 conversations over last 3 months, parameters of this can be
tweaked in `Seeders::Reports::ReportDataSeeder`

I'd suggest updating the values to generate data over 30 days, with
10000 conversations, it will take it's sweet time to run but then the
data will be really rich, great for testing.

```
ACCOUNT_ID=2 ENABLE_ACCOUNT_SEEDING=true bundle exec rake db:seed:reports_data
```

Pro Tip: Don't run the app when the seeder is active, we manually create
the reporting events anyway. So once done just use `redis-cli FLUSHALL`
to clear all sidekiq jobs. Will be easier on the system

Use the following scripts to test it

- https://gist.github.com/scmmishra/1263a922f5efd24df8e448a816a06257
- https://gist.github.com/scmmishra/ca0b861fa0139e2cccdb72526ea844b2
- https://gist.github.com/scmmishra/5fe73d1f48f35422fd1fd142ea3498f3
- https://gist.github.com/scmmishra/3b7b1f9e2ff149007170e5c329432f45
- https://gist.github.com/scmmishra/f245fa2f44cd973e5d60aac64f979162

---------

Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
Co-authored-by: Pranav <pranav@chatwoot.com>
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
2025-09-03 15:47:16 +05:30
..
api feat: label reports overview (#11194) 2025-06-11 14:35:46 +05:30
filters chore: Ability to filter conversations with priority (#10967) 2025-03-03 16:38:22 +05:30
instagram feat: Added the ability to create Instagram channel (#11182) 2025-04-03 13:57:14 +05:30
linear feat: move Linear config to installation_config (#10999) 2025-02-28 14:20:27 +05:30
shopify feat(apps): Shopify Integration (#11101) 2025-03-19 15:37:55 -07:00
super_admin chore: Add submenu for super admin settings (#11860) 2025-07-15 21:28:39 -07:00
application_helper.rb feat: Add help URLs for features in features.yml (#9134) 2024-03-21 10:30:46 -07:00
billing_helper.rb feat: Upgrade page instead of banner (#11202) 2025-03-28 02:28:17 -07:00
cache_keys_helper.rb feat: IndexedDB based caching for labels, inboxes and teams [CW-50] (#6710) 2023-03-27 12:16:25 +05:30
contact_helper.rb feat: Add contact helper (#8989) 2024-03-06 17:39:39 +05:30
date_range_helper.rb fix: inconsistency in report and summary for metric counts (#6817) 2023-04-20 12:55:04 +05:30
email_helper.rb feat: Enable template variables in channel greeting messages (#6971) 2023-04-26 20:23:46 +05:30
file_type_helper.rb fix: check the content type for the file when uploading from cloud storage (#5378) 2022-10-21 18:05:36 -07:00
frontend_urls_helper.rb 🚨Fix Rubocop lint errors 2019-10-20 14:17:26 +05:30
home_helper.rb Initial Commit 2019-08-14 15:18:44 +05:30
message_format_helper.rb fix: Handle emoji and special characters in mention notifications (#11857) 2025-07-03 11:32:13 +05:30
portal_helper.rb chore: Refactor UTM params to stay compliant with standards (#12312) 2025-08-29 11:46:52 -07:00
report_helper.rb fix: wrong resolution count in timeseries reports (#12261) 2025-09-03 15:47:16 +05:30
reporting_event_helper.rb fix: incorrect first response time for reopened conversations (#12058) 2025-08-13 16:39:43 +05:30
timezone_helper.rb feat: Improve Report API performance (#9476) 2024-05-22 17:34:24 -07:00
widget_helper.rb chore: Enhance contact merge action for identified users (#4886) 2022-06-23 15:48:56 +05:30