Properly parse datetimes from AUTOMATION_ANALYTICS_LAST_ENTRIES

This commit is contained in:
Jeff Bradberry 2021-03-25 12:52:50 -04:00
parent 6030c5cf4c
commit f85e8a44de
3 changed files with 19 additions and 6 deletions

View File

@ -12,7 +12,7 @@ from django.utils.timezone import now, timedelta
from django.utils.translation import ugettext_lazy as _
from awx.conf.license import get_license
from awx.main.utils import get_awx_version, get_custom_venv_choices, camelcase_to_underscore
from awx.main.utils import get_awx_version, get_custom_venv_choices, camelcase_to_underscore, datetime_hook
from awx.main import models
from django.contrib.sessions.models import Session
from awx.main.analytics import register
@ -42,7 +42,7 @@ def trivial_slicing(key, since, until):
horizon = until - timedelta(weeks=4)
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 '{}')
last_entries = json.loads((last_entries.value if last_entries is not None else '') or '{}', object_hook=datetime_hook)
last_entry = max(last_entries.get(key) or settings.AUTOMATION_ANALYTICS_LAST_GATHER or horizon, horizon)
return [(last_entry, until)]
@ -55,7 +55,7 @@ def four_hour_slicing(key, since, until):
horizon = until - timedelta(weeks=4)
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 '{}')
last_entries = json.loads((last_entries.value if last_entries is not None else '') or '{}', object_hook=datetime_hook)
last_entry = max(last_entries.get(key) or settings.AUTOMATION_ANALYTICS_LAST_GATHER or horizon, horizon)
start, end = last_entry, None
@ -70,7 +70,7 @@ def events_slicing(key, since, until):
last_gather = settings.AUTOMATION_ANALYTICS_LAST_GATHER
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 '{}')
last_entries = json.loads((last_entries.value if last_entries is not None else '') or '{}', object_hook=datetime_hook)
horizon = until - timedelta(weeks=4)
lower = since or last_gather or horizon

View File

@ -18,7 +18,7 @@ import requests
from awx.conf.license import get_license
from awx.main.models import Job
from awx.main.access import access_registry
from awx.main.utils import get_awx_http_client_headers, set_environ
from awx.main.utils import get_awx_http_client_headers, set_environ, datetime_hook
from awx.main.utils.pglock import advisory_lock
__all__ = ['register', 'gather', 'ship']
@ -163,7 +163,7 @@ def gather(dest=None, module=None, subset=None, since=None, until=None, collecti
last_gather = max(settings.AUTOMATION_ANALYTICS_LAST_GATHER or horizon, horizon)
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 '{}')
last_entries = json.loads((last_entries.value if last_entries is not None else '') or '{}', object_hook=datetime_hook)
collector_module = module if module else collectors
collector_list = [

View File

@ -18,6 +18,8 @@ from functools import reduce, wraps
from decimal import Decimal
from dateutil import parser
# Django
from django.core.exceptions import ObjectDoesNotExist, FieldDoesNotExist
from django.utils.translation import ugettext_lazy as _
@ -54,6 +56,7 @@ __all__ = [
'copy_m2m_relationships',
'prefetch_page_capabilities',
'to_python_boolean',
'datetime_hook',
'ignore_inventory_computed_fields',
'ignore_inventory_group_removal',
'_inventory_updates',
@ -115,6 +118,16 @@ def to_python_boolean(value, allow_none=False):
raise ValueError(_(u'Unable to convert "%s" to boolean') % value)
def datetime_hook(d):
new_d = {}
for key, value in d.items():
try:
new_d[key] = parser.parse(value)
except Exception:
new_d[key] = value
return new_d
def camelcase_to_underscore(s):
"""
Convert CamelCase names to lowercase_with_underscore.