fix: start od week

This commit is contained in:
Shivam Mishra
2026-03-09 12:50:29 +05:30
parent 0bc80bf95f
commit 8324d84fbf
4 changed files with 82 additions and 3 deletions
@@ -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
@@ -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
@@ -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
@@ -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