Skip to content

Commit

Permalink
add application_settings
Browse files Browse the repository at this point in the history
  • Loading branch information
hsong-rh committed Sep 18, 2024
1 parent 9c4c374 commit 31330b0
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 177 deletions.
138 changes: 2 additions & 136 deletions poetry.lock

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions src/aap_eda/analytics/analytics_collectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,18 @@ def teams_table(since: datetime, full_path: str, until: datetime, **kwargs):
return _copy_table("teams", query, full_path)


def _datetime_format(timestamp: datetime) -> str:
def _datetime_format(dt: datetime) -> str:
"""Convert datetime object to string."""
iso_format = timestamp.strftime("%Y-%m-%d %H:%M:%S.%f%z")
if (
dt.hour == 0
and dt.minute == 0
and dt.second == 0
and dt.microsecond == 0
):
iso_format = dt.strftime("%Y-%m-%d %H:%M:%S%z")
else:
iso_format = dt.strftime("%Y-%m-%d %H:%M:%S.%f%z")

return iso_format[:-2] + ":" + iso_format[-2:]


Expand All @@ -246,6 +255,7 @@ def _get_query(
Q(created_at__gt=since, created_at__lte=until)
| Q(modified_at__gt=since, modified_at__lte=until)
).order_by("id")

query = (
str(qs.query)
.replace(_datetime_format(since), f"'{since.isoformat()}'")
Expand Down
44 changes: 27 additions & 17 deletions src/aap_eda/analytics/collector.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import json
from datetime import datetime

from django.conf import settings
from django.core.serializers.json import DjangoJSONEncoder
from django.db import connection
from insights_analytics_collector import Collector

from aap_eda.analytics.package import Package
from aap_eda.analytics.utils import datetime_hook
from aap_eda.conf.settings import application_settings


class AnalyticsCollector(Collector):
Expand All @@ -18,11 +19,8 @@ def db_connection():
def _package_class():
return Package

def get_last_gathering(self):
return self._last_gathering()

def _is_shipping_configured(self):
if not settings.INSIGHTS_TRACKING_STATE:
if not application_settings.INSIGHTS_TRACKING_STATE:
self.logger.warning(
"Insights for Event Driven Ansible is not enabled."
)
Expand All @@ -35,26 +33,38 @@ def _is_valid_license(self):
return True

def _last_gathering(self):
return settings.AUTOMATION_ANALYTICS_LAST_GATHER
self.logger.info(
"Last gather: "
f"{application_settings.AUTOMATION_ANALYTICS_LAST_GATHER}"
)

return (
datetime.fromisoformat(
application_settings.AUTOMATION_ANALYTICS_LAST_GATHER
)
if bool(application_settings.AUTOMATION_ANALYTICS_LAST_GATHER)
else None
)

def _load_last_gathered_entries(self):
last_entries = settings.AUTOMATION_ANALYTICS_LAST_ENTRIES
last_entries = application_settings.AUTOMATION_ANALYTICS_LAST_ENTRIES
last_entries = last_entries.replace("'", '"')
self.logger.info(f"Last collect entries: {last_entries}")

return json.loads(
last_entries.value
if last_entries and last_entries.value
else "{}", # noqa: P103
object_hook=datetime_hook,
)
return json.loads(last_entries, object_hook=datetime_hook)

def _save_last_gathered_entries(self, last_gathered_entries):
self.logger.info(f"Save last_entries: {last_gathered_entries}")

settings.AUTOMATION_ANALYTICS_LAST_ENTRIES = json.dumps(
application_settings.AUTOMATION_ANALYTICS_LAST_ENTRIES = json.dumps(
last_gathered_entries, cls=DjangoJSONEncoder
)
self.logger.info(
"Save last_entries: "
f"{application_settings.AUTOMATION_ANALYTICS_LAST_ENTRIES}"
)

def _save_last_gather(self):
self.logger.info(f"Save last_gather: {self.gather_until}")

settings.AUTOMATION_ANALYTICS_LAST_GATHER = self.gather_until
application_settings.AUTOMATION_ANALYTICS_LAST_GATHER = (
self.gather_until.isoformat()
)
23 changes: 17 additions & 6 deletions src/aap_eda/analytics/package.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import logging

# Copyright 2024 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from django.conf import settings
from insights_analytics_collector import Package as InsightsAnalyticsPackage

logger = logging.getLogger(__name__)
from aap_eda.conf import application_settings


class Package(InsightsAnalyticsPackage):
Expand All @@ -15,16 +26,16 @@ def _tarname_base(self):
return f'eda-analytics-{timestamp.strftime("%Y-%m-%d-%H%M%S%z")}'

def get_ingress_url(self):
return settings.AUTOMATION_ANALYTICS_URL
return application_settings.AUTOMATION_ANALYTICS_URL

def shipping_auth_mode(self):
return settings.AUTOMATION_AUTH_METHOD

def _get_rh_user(self):
return settings.REDHAT_USERNAME
return application_settings.REDHAT_USERNAME

def _get_rh_password(self):
return settings.REDHAT_PASSWORD
return application_settings.REDHAT_PASSWORD

def _get_http_request_headers(self):
return {
Expand Down
3 changes: 1 addition & 2 deletions src/aap_eda/conf/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ class RegistryData(object):
),
RegistryData(
name="AUTOMATION_ANALYTICS_LAST_ENTRIES",
type=dict,
default={},
default="{}", # noqa P103
),
RegistryData(
name="AUTOMATION_ANALYTICS_GATHER_INTERVAL",
Expand Down
19 changes: 16 additions & 3 deletions src/aap_eda/core/management/commands/gather_analytics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from datetime import datetime

from dateutil import parser
from django.core.management.base import BaseCommand
from django.utils import timezone

Expand Down Expand Up @@ -57,10 +57,23 @@ def handle(self, *args, **options):
opt_since = options.get("since") or None
opt_until = options.get("until") or None

since = parser.parse(opt_since) if opt_since else None
since = (
datetime.strptime(opt_since, "%Y-%m-%d").replace(
hour=0, minute=0, second=0, microsecond=0
)
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

until = (
datetime.strptime(opt_until, "%Y-%m-%d").replace(
hour=0, minute=0, second=0, microsecond=0
)
if opt_until
else None
)
if until and until.tzinfo is None:
until = until.replace(tzinfo=timezone.utc)

Expand Down
10 changes: 10 additions & 0 deletions tests/integration/analytics/test_analytics_collectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@

from aap_eda.analytics import analytics_collectors as collectors
from aap_eda.analytics.collector import AnalyticsCollector
from aap_eda.conf import settings_registry
from aap_eda.core import models


@pytest.fixture(autouse=True)
def register() -> None:
settings_registry.persist_registry_data()
return None


@pytest.mark.django_db
def test_internal_infra_files():
collector = AnalyticsCollector(
Expand Down Expand Up @@ -140,6 +147,7 @@ def test_activations_table_collector(default_activation: models.Activation):
assert lines[0][3] == default_activation.description


@pytest.mark.django_db
def assert_audit_rules(expected_audit_rules):
time_start = now() - timedelta(hours=9)

Expand Down Expand Up @@ -209,6 +217,7 @@ def test_multiple_audit_action_table_collector(
assert_audit_events([audit_event_1, audit_event_2])


@pytest.mark.django_db
def assert_audit_actions(expected_audit_actions):
time_start = now() - timedelta(hours=9)

Expand Down Expand Up @@ -238,6 +247,7 @@ def assert_audit_actions(expected_audit_actions):
)


@pytest.mark.django_db
def assert_audit_events(expected_audit_events):
time_start = now() - timedelta(hours=9)

Expand Down
12 changes: 1 addition & 11 deletions tests/unit/test_application_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import pytest

from aap_eda.conf import application_settings, settings_registry
from aap_eda.conf.registry import InvalidKeyError, InvalidValueError
from aap_eda.conf.registry import InvalidKeyError


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -43,16 +43,6 @@ def test_read_only_application_setting():
application_settings.INSIGHTS_CERT_PATH = "path"


@pytest.mark.django_db
def test_application_setting_bad_type():
assert (
settings_registry.get_setting_type("AUTOMATION_ANALYTICS_LAST_ENTRIES")
== dict
)
with pytest.raises(InvalidValueError):
application_settings.AUTOMATION_ANALYTICS_LAST_ENTRIES = 1


@pytest.mark.django_db
def test_list_keys():
assert len(settings_registry.get_registered_settings()) == 10
Expand Down

0 comments on commit 31330b0

Please sign in to comment.