diff --git a/awx/main/analytics/core.py b/awx/main/analytics/core.py index 27d7b5312c..2bb7b5e98e 100644 --- a/awx/main/analytics/core.py +++ b/awx/main/analytics/core.py @@ -151,27 +151,45 @@ def gather(dest=None, module=None, subset=None, since=None, until=None, collecti _now = now() # Make sure that the endpoints are not in the future. - until = None if until is None else min(until, _now) - since = None if since is None else min(since, _now) + if until is not None and until > _now: + until = _now + logger.warning(f"End of the collection interval is in the future, setting to {_now}.") + if since is not None and since > _now: + since = _now + logger.warning(f"Start of the collection interval is in the future, setting to {_now}.") - if since and not until: - # If `since` is explicit but not `until`, `since` should be used to calculate the 4-week limit - until = min(since + timedelta(weeks=4), _now) - else: - until = _now if until is None else until - - horizon = until - timedelta(weeks=4) + # The value of `until` needs to be concrete, so resolve it. If it wasn't passed in, + # set it to `now`, but only if that isn't more than 4 weeks ahead of a passed-in + # `since` parameter. if since is not None: - # Make sure the start isn't more than 4 weeks prior to `until`. - since = max(since, horizon) + if until is not None: + if until > since + timedelta(weeks=4): + until = since + timedelta(weeks=4) + logger.warning(f"End of the collection interval is greater than 4 weeks from start, setting end to {until}.") + else: # until is None + until = min(since + timedelta(weeks=4), _now) + elif until is None: + until = _now if since and since >= until: logger.warning("Start of the collection interval is later than the end, ignoring request.") return None + # The ultimate beginning of the interval needs to be compared to 4 weeks prior to + # `until`, but we want to keep `since` empty if it wasn't passed in because we use that + # case to know whether to use the bookkeeping settings variables to decide the start of + # the interval. + horizon = until - timedelta(weeks=4) + if since is not None and since < horizon: + since = horizon + logger.warning(f"Start of the collection interval is more than 4 weeks prior to {until}, setting to {horizon}.") + logger.debug("Last analytics run was: {}".format(settings.AUTOMATION_ANALYTICS_LAST_GATHER)) - # LAST_GATHER time should always get truncated to less than 4 weeks back. - last_gather = max(settings.AUTOMATION_ANALYTICS_LAST_GATHER or horizon, horizon) + + last_gather = settings.AUTOMATION_ANALYTICS_LAST_GATHER or horizon + if last_gather < horizon: + last_gather = horizon + logger.warning(f"Last analytics run was more than 4 weeks prior to {until}, using {horizon} instead.") last_entries = Setting.objects.filter(key='AUTOMATION_ANALYTICS_LAST_ENTRIES').first() last_entries = json.loads((last_entries.value if last_entries is not None else '') or '{}', object_hook=datetime_hook)