From 62c7554ec4e2a0ae57962c94bfe4599ccfe7891e Mon Sep 17 00:00:00 2001 From: Jeff Bradberry Date: Tue, 6 Apr 2021 15:59:54 -0400 Subject: [PATCH] Adjust datetimes to be aware when using awx-manage gather_analytics Also, make sure that an explicit `since` parameter will win over default `until=now()` when calculating the 4-week data limit. --- awx/main/analytics/core.py | 17 ++++++++++++++--- .../management/commands/gather_analytics.py | 16 +++++++--------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/awx/main/analytics/core.py b/awx/main/analytics/core.py index 71008fc2a5..239a9d6622 100644 --- a/awx/main/analytics/core.py +++ b/awx/main/analytics/core.py @@ -149,11 +149,22 @@ def gather(dest=None, module=None, subset=None, since=None, until=None, collecti from awx.main.signals import disable_activity_stream _now = now() - until = _now if until is None else min(until, _now) # Make sure the end isn't in the future. + + # 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 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) if since is not None: - # Make sure the start isn't in the future or more than 4 weeks prior to `until`. - since = max(min(since, _now), horizon) + # Make sure the start isn't more than 4 weeks prior to `until`. + since = max(since, horizon) + if since and since >= until: logger.warning("Start of the collection interval is later than the end, ignoring request.") return None diff --git a/awx/main/management/commands/gather_analytics.py b/awx/main/management/commands/gather_analytics.py index 60d1524fc7..e854f021ef 100644 --- a/awx/main/management/commands/gather_analytics.py +++ b/awx/main/management/commands/gather_analytics.py @@ -3,7 +3,7 @@ import logging from awx.main import analytics from dateutil import parser from django.core.management.base import BaseCommand -from django.utils.timezone import now +from django.utils import timezone class Command(BaseCommand): @@ -36,14 +36,12 @@ class Command(BaseCommand): opt_since = options.get('since') or None opt_until = options.get('until') or None - if opt_since: - since = parser.parse(opt_since) - else: - since = None - if opt_until: - until = parser.parse(opt_until) - else: - until = now() + since = parser.parse(opt_since) if opt_since else None + if since and since.tzinfo is None: + since = since.replace(tzinfo=timezone.utc) + until = parser.parse(opt_until) if opt_until else None + if until and until.tzinfo is None: + until = until.replace(tzinfo=timezone.utc) if opt_ship and opt_dry_run: self.logger.error('Both --ship and --dry-run cannot be processed at the same time.')