From 8324d84fbf36ef191d69ab7bd2d84a0f885e9745 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Mon, 9 Mar 2026 12:50:29 +0530 Subject: [PATCH] fix: start od week --- .../timeseries/average_report_builder.rb | 2 +- .../timeseries/count_report_builder.rb | 2 +- .../timeseries/average_report_builder_spec.rb | 36 +++++++++++++++ .../timeseries/count_report_builder_spec.rb | 45 ++++++++++++++++++- 4 files changed, 82 insertions(+), 3 deletions(-) diff --git a/app/builders/v2/reports/timeseries/average_report_builder.rb b/app/builders/v2/reports/timeseries/average_report_builder.rb index d7da54c92..87bedb5b7 100644 --- a/app/builders/v2/reports/timeseries/average_report_builder.rb +++ b/app/builders/v2/reports/timeseries/average_report_builder.rb @@ -88,7 +88,7 @@ class V2::Reports::Timeseries::AverageReportBuilder < V2::Reports::Timeseries::B def date_key_for_group(date) case group_by - when 'week' then date.beginning_of_week(:monday) + when 'week' then date.beginning_of_week(:sunday) when 'month' then date.beginning_of_month when 'year' then date.beginning_of_year else date diff --git a/app/builders/v2/reports/timeseries/count_report_builder.rb b/app/builders/v2/reports/timeseries/count_report_builder.rb index 630504f91..bd44aaa49 100644 --- a/app/builders/v2/reports/timeseries/count_report_builder.rb +++ b/app/builders/v2/reports/timeseries/count_report_builder.rb @@ -81,7 +81,7 @@ class V2::Reports::Timeseries::CountReportBuilder < V2::Reports::Timeseries::Bas def date_key_for_period(date) case group_by - when 'week' then date.beginning_of_week(:monday) + when 'week' then date.beginning_of_week(:sunday) when 'month' then date.beginning_of_month when 'year' then date.beginning_of_year else date diff --git a/spec/builders/v2/reports/timeseries/average_report_builder_spec.rb b/spec/builders/v2/reports/timeseries/average_report_builder_spec.rb index 9822e5099..f79349bde 100644 --- a/spec/builders/v2/reports/timeseries/average_report_builder_spec.rb +++ b/spec/builders/v2/reports/timeseries/average_report_builder_spec.rb @@ -133,6 +133,42 @@ describe V2::Reports::Timeseries::AverageReportBuilder do ] ) end + + context 'when rollups are enabled' do + before do + account.update!(reporting_timezone: 'UTC') + allow(subject).to receive(:use_rollup?).and_return(true) + + create(:reporting_events_rollup, + account: account, + date: current_time.to_date - 1.day, + dimension_type: 'account', + dimension_id: account.id, + metric: 'first_response', + count: 1, + sum_value: 80.0, + sum_value_business_hours: 10.0) + + create(:reporting_events_rollup, + account: account, + date: current_time.to_date, + dimension_type: 'account', + dimension_id: account.id, + metric: 'first_response', + count: 1, + sum_value: 100.0, + sum_value_business_hours: 20.0) + end + + it 'groups weeks using sunday boundaries' do + expect(subject.timeseries).to eq( + [ + { count: 0, timestamp: (current_time - 1.week).beginning_of_week(:sunday).to_i, value: 0 }, + { count: 2, timestamp: current_time.beginning_of_week(:sunday).to_i, value: 90.0 } + ] + ) + end + end end context 'when timezone offset is provided' do diff --git a/spec/builders/v2/reports/timeseries/count_report_builder_spec.rb b/spec/builders/v2/reports/timeseries/count_report_builder_spec.rb index 038bd61c2..0a4f98d80 100644 --- a/spec/builders/v2/reports/timeseries/count_report_builder_spec.rb +++ b/spec/builders/v2/reports/timeseries/count_report_builder_spec.rb @@ -14,11 +14,14 @@ describe V2::Reports::Timeseries::CountReportBuilder do { type: 'agent', metric: 'resolutions_count', - since: (current_time - 1.day).beginning_of_day.to_i.to_s, + since: since_time.beginning_of_day.to_i.to_s, until: current_time.end_of_day.to_i.to_s, + group_by: group_by, id: user.id.to_s } end + let(:group_by) { 'day' } + let(:since_time) { current_time - 1.day } before do travel_to current_time @@ -98,6 +101,46 @@ describe V2::Reports::Timeseries::CountReportBuilder do total_count = result.sum { |r| r[:value] } expect(total_count).to eq(2) end + + context 'when rollups are enabled and grouped by week' do + let(:group_by) { 'week' } + let(:current_time) { Time.zone.parse('2020-10-26 10:00:00 UTC') } + let(:since_time) { current_time - 1.week } + + before do + account.update!(reporting_timezone: 'UTC') + allow(subject).to receive(:use_rollup?).and_return(true) + + create(:reporting_events_rollup, + account: account, + date: current_time.to_date - 1.day, + dimension_type: 'agent', + dimension_id: user.id, + metric: 'resolutions_count', + count: 1, + sum_value: 0.0, + sum_value_business_hours: 0.0) + + create(:reporting_events_rollup, + account: account, + date: current_time.to_date, + dimension_type: 'agent', + dimension_id: user.id, + metric: 'resolutions_count', + count: 1, + sum_value: 0.0, + sum_value_business_hours: 0.0) + end + + it 'groups weeks using sunday boundaries' do + expect(subject.timeseries).to eq( + [ + { value: 0, timestamp: (current_time - 1.week).beginning_of_week(:sunday).to_i }, + { value: 2, timestamp: current_time.beginning_of_week(:sunday).to_i } + ] + ) + end + end end describe 'account isolation' do