Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions core/app/models/workarea/metrics/by_week.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ module ByWeek
index({ reporting_on: 1 }, { expire_after_seconds: 2.years.seconds.to_i })

scope :last_week, -> do
# Use date math to avoid DST-related boundary shifts.
# We want the previous calendar week, not "now minus 7 days".
start_of_this_week = Time.current.to_date.beginning_of_week
start_of_last_week = start_of_this_week - 1.week

where(
:reporting_on.gte => Time.current.last_week,
:reporting_on.lt => Time.current.last_week.end_of_week
:reporting_on.gte => start_of_last_week.in_time_zone,
:reporting_on.lt => start_of_this_week.in_time_zone
)
end
end
Expand Down
8 changes: 6 additions & 2 deletions core/app/models/workarea/metrics/scoring.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ def score(field)
end

def weeks_ago
difference = Time.current.beginning_of_week - reporting_on.beginning_of_week
difference / 1.week
# Use date math instead of second-based math to avoid DST boundary issues.
# (e.g. a "week" containing a DST shift is not always 7 * 24 hours)
current_week = Time.current.to_date.beginning_of_week
reporting_week = reporting_on.to_date.beginning_of_week

((current_week - reporting_week) / 7).to_i
end
end
end
Expand Down
38 changes: 28 additions & 10 deletions core/test/models/workarea/metrics/product_by_week_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
module Workarea
module Metrics
class ProductByWeekTest < TestCase
setup do
Metrics::ProductByWeek.delete_all
Metrics::ProductForLastWeek.delete_all
end
def test_last_week
two_weeks_ago = create_product_by_week(reporting_on: Time.zone.local(2018, 11, 25))
last_week = create_product_by_week(reporting_on: Time.zone.local(2018, 12, 2))
Expand Down Expand Up @@ -117,20 +121,34 @@ def test_score
def test_weeks_ago
model = create_product_by_week(reporting_on: Time.zone.local(2019, 1, 25))

travel_to Time.zone.local(2019, 1, 25)
assert_equal(0, model.weeks_ago)
travel_to Time.zone.local(2019, 1, 25) do
assert_equal(0, model.weeks_ago)
end

travel_to Time.zone.local(2019, 1, 27) do
assert_equal(0, model.weeks_ago)
end

travel_to Time.zone.local(2019, 2, 8) do
assert_equal(2, model.weeks_ago)
end

travel_to Time.zone.local(2019, 1, 27)
assert_equal(0, model.weeks_ago)
travel_to Time.zone.local(2019, 2, 9) do
assert_equal(2, model.weeks_ago)
end

travel_to Time.zone.local(2019, 2, 8)
assert_equal(2, model.weeks_ago)
travel_to Time.zone.local(2019, 2, 11) do
assert_equal(3, model.weeks_ago)
end

travel_to Time.zone.local(2019, 2, 9)
assert_equal(2, model.weeks_ago)
# Regression: DST boundaries should not affect week math.
Time.use_zone('Eastern Time (US & Canada)') do
dst_model = create_product_by_week(reporting_on: Time.zone.local(2019, 3, 4, 0, 0, 0))

travel_to Time.zone.local(2019, 2, 11)
assert_equal(3, model.weeks_ago)
travel_to Time.zone.local(2019, 3, 11, 0, 0, 0) do
assert_equal(1, dst_model.weeks_ago)
end
end
end
end
end
Expand Down
Loading