Log adjustments made to the analytics interval due to the 4-week limit

This commit is contained in:
Jeff Bradberry
2021-04-23 10:35:51 -04:00
parent 1e7b7d1a30
commit f4d848e596

View File

@@ -151,27 +151,45 @@ def gather(dest=None, module=None, subset=None, since=None, until=None, collecti
_now = now() _now = now()
# Make sure that the endpoints are not in the future. # Make sure that the endpoints are not in the future.
until = None if until is None else min(until, _now) if until is not None and until > _now:
since = None if since is None else min(since, _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: # The value of `until` needs to be concrete, so resolve it. If it wasn't passed in,
# If `since` is explicit but not `until`, `since` should be used to calculate the 4-week limit # set it to `now`, but only if that isn't more than 4 weeks ahead of a passed-in
until = min(since + timedelta(weeks=4), _now) # `since` parameter.
else:
until = _now if until is None else until
horizon = until - timedelta(weeks=4)
if since is not None: if since is not None:
# Make sure the start isn't more than 4 weeks prior to `until`. if until is not None:
since = max(since, horizon) 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: if since and since >= until:
logger.warning("Start of the collection interval is later than the end, ignoring request.") logger.warning("Start of the collection interval is later than the end, ignoring request.")
return None 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)) 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 = 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) last_entries = json.loads((last_entries.value if last_entries is not None else '') or '{}', object_hook=datetime_hook)