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 19, 2024
1 parent 2c4dfc3 commit a717996
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 176 deletions.
138 changes: 2 additions & 136 deletions poetry.lock

Large diffs are not rendered by default.

27 changes: 23 additions & 4 deletions src/aap_eda/analytics/analytics_collectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ def audit_actions_table(
):
audit_actions = _get_audit_action_qs(since, until)

if not bool(audit_actions):
return

audit_action_query = (
f"COPY ({audit_actions.query}) TO STDOUT WITH CSV HEADER"
)
Expand All @@ -82,7 +85,12 @@ def audit_events_table(
since: datetime, full_path: str, until: datetime, **kwargs
):
audit_actions = _get_audit_action_qs(since, until)
if not bool(audit_actions):
return

audit_event_query = _get_audit_event_query(audit_actions)
if not bool(audit_event_query):
return

return _copy_table("audit_events", audit_event_query, full_path)

Expand All @@ -97,6 +105,9 @@ def audit_rules_table(
since: datetime, full_path: str, until: datetime, **kwargs
):
audit_rules = _get_audit_rule_qs(since, until)
if not bool(audit_rules):
return

audit_rule_query = f"COPY ({audit_rules.query}) TO STDOUT WITH CSV HEADER"

return _copy_table("audit_rules", audit_rule_query, full_path)
Expand Down Expand Up @@ -221,9 +232,13 @@ 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.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 +261,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 All @@ -260,6 +276,9 @@ def _get_audit_event_query(actions: list[models.AuditAction]):
for action in actions:
events |= action.audit_events.all()

if not bool(events):
return

query = str(events.distinct().query)

for action in actions:
Expand All @@ -285,7 +304,7 @@ def _get_audit_rule_qs(since: datetime, until: datetime):
)

if len(activation_instance_ids) == 0:
return []
return models.RulebookProcess.objects.none()

if len(activation_instance_ids) == 1:
audit_rules = models.AuditRule.objects.filter(
Expand All @@ -304,7 +323,7 @@ def _get_audit_action_qs(since: datetime, until: datetime):
audit_rule_ids = audit_rules.values_list("id").distinct()

if len(audit_rule_ids) == 0:
return []
return models.AuditRule.objects.none()

if len(audit_rule_ids) == 1:
audit_actions = models.AuditAction.objects.filter(
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
1 change: 1 addition & 0 deletions src/aap_eda/core/management/commands/gather_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def handle(self, *args, **options):
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)
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 a717996

Please sign in to comment.