Skip to content

Commit

Permalink
chore: refactor listeners to optimise the speed
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Dec 17, 2024
1 parent c3a1bd3 commit 5f311da
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 26 deletions.
2 changes: 2 additions & 0 deletions ckanext/event_audit/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion ckanext/event_audit/listeners/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions ckanext/event_audit/listeners/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 3 additions & 21 deletions ckanext/event_audit/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()

Expand Down Expand Up @@ -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

Expand Down
30 changes: 27 additions & 3 deletions ckanext/event_audit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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()

Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5f311da

Please sign in to comment.