From 5f311da0178105d9071497121d0e13ac41d3011d Mon Sep 17 00:00:00 2001 From: mutantsan Date: Tue, 17 Dec 2024 17:30:17 +0200 Subject: [PATCH] chore: refactor listeners to optimise the speed --- ckanext/event_audit/interfaces.py | 2 ++ ckanext/event_audit/listeners/api.py | 5 +++- ckanext/event_audit/listeners/database.py | 3 +++ ckanext/event_audit/plugin.py | 24 +++--------------- ckanext/event_audit/utils.py | 30 ++++++++++++++++++++--- pyproject.toml | 2 +- 6 files changed, 40 insertions(+), 26 deletions(-) diff --git a/ckanext/event_audit/interfaces.py b/ckanext/event_audit/interfaces.py index 7fb8ae4..e3509db 100644 --- a/ckanext/event_audit/interfaces.py +++ b/ckanext/event_audit/interfaces.py @@ -41,6 +41,8 @@ def skip_event(self, event: types.Event) -> bool: ``` """ + _reverse_iteration_order = True + def register_repository(self) -> dict[str, type[repos.AbstractRepository]]: """Return the repositories provided by this plugin. diff --git a/ckanext/event_audit/listeners/api.py b/ckanext/event_audit/listeners/api.py index 1ed1636..6641313 100644 --- a/ckanext/event_audit/listeners/api.py +++ b/ckanext/event_audit/listeners/api.py @@ -42,7 +42,10 @@ def action_succeeded_subscriber( ) ) - for plugin in reversed(list(p.PluginImplementations(IEventAudit))): + if utils.skip_event(event): + return + + for plugin in p.PluginImplementations(IEventAudit): if plugin.skip_event(event): return diff --git a/ckanext/event_audit/listeners/database.py b/ckanext/event_audit/listeners/database.py index f8105f2..72df8c2 100644 --- a/ckanext/event_audit/listeners/database.py +++ b/ckanext/event_audit/listeners/database.py @@ -111,6 +111,9 @@ def after_commit(session: SQLAlchemySession): ) ) + if utils.skip_event(event): + return + for plugin in reversed(list(p.PluginImplementations(IEventAudit))): if plugin.skip_event(event): return diff --git a/ckanext/event_audit/plugin.py b/ckanext/event_audit/plugin.py index bea9bab..407249e 100644 --- a/ckanext/event_audit/plugin.py +++ b/ckanext/event_audit/plugin.py @@ -69,7 +69,6 @@ class EventAuditPlugin(p.SingletonPlugin): p.implements(p.IConfigurable) p.implements(p.IConfigurer) p.implements(p.ISignal) - p.implements(IEventAudit, inherit=True) p.implements(p.IConfigDeclaration) event_queue = queue.Queue() @@ -81,11 +80,11 @@ def update_config(self, config_: CKANConfig): # IConfigurable def configure(self, config_: CKANConfig) -> None: - repo = utils.get_active_repo() + self.repo = utils.get_active_repo(True) - if repo.get_name() == "cloudwatch" and repo._connection is None: + if self.repo.get_name() == "cloudwatch" and self.repo._connection is None: if config_.get("testing"): - repo._connection = True # type: ignore + self.repo._connection = True # type: ignore else: utils.test_active_connection() @@ -149,23 +148,6 @@ def get_collection_factories(sender: None): return {"event-audit-list": EventAuditListCollection} - # IEventAudit - - def skip_event(self, event: types.Event) -> bool: - if event.action in config.get_ignored_actions(): - return True - - if event.category in config.get_ignored_categories(): - return True - - # track specific models have priority over ignoring specific models - if ( - not config.get_tracked_models() - and event.action_object in config.get_ignored_models() - ): - return True - - return False # IConfigDeclaration diff --git a/ckanext/event_audit/utils.py b/ckanext/event_audit/utils.py index 7fa0c28..cbea609 100644 --- a/ckanext/event_audit/utils.py +++ b/ckanext/event_audit/utils.py @@ -2,7 +2,7 @@ import ckan.plugins as p -from ckanext.event_audit import config, exporters +from ckanext.event_audit import config, exporters, types from ckanext.event_audit import repositories as repos from ckanext.event_audit.interfaces import IEventAudit @@ -23,7 +23,7 @@ def get_available_repos() -> dict[str, type[repos.AbstractRepository]]: repos.CloudWatchRepository.get_name(): repos.CloudWatchRepository, } - for plugin in reversed(list(p.PluginImplementations(IEventAudit))): + for plugin in p.PluginImplementations(IEventAudit): plugin_repos.update(plugin.register_repository()) restrict_repos = config.get_list_of_available_repos() @@ -38,7 +38,7 @@ def get_available_repos() -> dict[str, type[repos.AbstractRepository]]: } -def get_active_repo() -> repos.AbstractRepository: +def get_active_repo(ignore_cache: bool = False) -> repos.AbstractRepository: """Get the active repository. The active repository is the one that is currently configured in the @@ -47,6 +47,13 @@ def get_active_repo() -> repos.AbstractRepository: Returns: The active repository. """ + from ckan.plugins import get_plugin + + plugin_instance = get_plugin("event_audit") + + if hasattr(plugin_instance, "repo") and not ignore_cache: + return plugin_instance.repo + repos = get_available_repos() active_repo_name = config.active_repo() @@ -134,3 +141,20 @@ def get_exporter(exporter_name: str) -> type[exporters.AbstractExporter]: raise ValueError(f"Exporter {exporter_name} not found") return exporters[exporter_name] + + +def skip_event(event: types.Event) -> bool: + if event.action in config.get_ignored_actions(): + return True + + if event.category in config.get_ignored_categories(): + return True + + # track specific models have priority over ignoring specific models + if ( + not config.get_tracked_models() + and event.action_object in config.get_ignored_models() + ): + return True + + return False diff --git a/pyproject.toml b/pyproject.toml index a6eae88..f88a2b9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "ckanext-event-audit" -version = "1.1.3" +version = "1.1.4" description = "" classifiers = [ "Development Status :: 4 - Beta",