Commit 62895180 authored by rpereira2's avatar rpereira2

Update callers of HistoricalData to pass in Time as parameters

Previously, `HistoricalData.date` was a `date`. When doing date range
calculations such as `HistoricalData.where(
Date.parse('2020-10-20')..Date.parse('2020-10-23'))`, any records
containing 20th or 23rd are included in the result.

However, with `HistoricalData.date` as a timestamp with time zone,
20th would be included (since the time component is assumed to be 00:00)
but 23rd would be excluded.

In order to include both days, we need to use
`.beginning_of_day` and `.end_of_day`. Both methods are time zone aware
and will use the `Time.zone` if available.
parent 57ed0178
......@@ -38,8 +38,8 @@ module Gitlab
def default_max_count(date)
HistoricalData.max_historical_user_count(
from: ::License.current.starts_at,
to: date
from: ::License.current.starts_at.beginning_of_day,
to: date.end_of_day
)
end
......
......@@ -23,16 +23,16 @@ class HistoricalData < ApplicationRecord
def max_historical_user_count(license: nil, from: nil, to: nil)
license ||= License.current
expires_at = license&.expires_at || Date.today
from ||= expires_at - 1.year
to ||= expires_at
expires_at = license&.expires_at || Time.current
from ||= (expires_at - 1.year).beginning_of_day
to ||= expires_at.end_of_day
HistoricalData.during(from..to).maximum(:active_user_count) || 0
end
def in_license_term(license)
start_date = license.starts_at
expiration_date = license.expires_at || Date.current
start_date = license.starts_at.beginning_of_day
expiration_date = license.expires_at&.end_of_day || Time.current
HistoricalData.during(start_date..expiration_date)
end
......
......@@ -550,8 +550,8 @@ class License < ApplicationRecord
def prior_historical_max
@prior_historical_max ||= begin
from = starts_at - 1.year
to = starts_at
from = (starts_at - 1.year).beginning_of_day
to = starts_at.end_of_day
historical_max(from, to)
end
......@@ -573,8 +573,8 @@ class License < ApplicationRecord
def check_trueup
trueup_qty = restrictions[:trueup_quantity]
trueup_from = Date.parse(restrictions[:trueup_from]) rescue (starts_at - 1.year)
trueup_to = Date.parse(restrictions[:trueup_to]) rescue starts_at
trueup_from = Date.parse(restrictions[:trueup_from]).beginning_of_day rescue (starts_at - 1.year).beginning_of_day
trueup_to = Date.parse(restrictions[:trueup_to]).end_of_day rescue starts_at.end_of_day
max_historical = historical_max(trueup_from, trueup_to)
expected_trueup_qty = if previous_user_count
max_historical - previous_user_count
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment