From 85a7a0e32408d7f80b84f8d696a3d8c7b89d2fa7 Mon Sep 17 00:00:00 2001 From: "Nathan P. Bombana" Date: Sat, 30 Dec 2023 19:09:21 -0300 Subject: [PATCH] feat(hooks): add pre_analytics and post_analytics hooks --- pythia/analytics.py | 26 ++++++++++++++++++++++---- pythia/plugin.py | 6 ++---- pythia/tests/plugin_test.py | 10 +++++----- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/pythia/analytics.py b/pythia/analytics.py index 048c473..0efeb45 100644 --- a/pythia/analytics.py +++ b/pythia/analytics.py @@ -8,6 +8,7 @@ import pythia.analytic_functions import pythia.io import pythia.util +import pythia.plugin from contextlib import _GeneratorContextManager @@ -250,13 +251,21 @@ def collate_outputs(config, run): def execute(config, plugins): runs = config.get("runs", []) analytics_config = config.get("analytics_setup", None) + + if not analytics_config or len(runs) == 0: + logging.warning("Skipping analytics: no configuration or runs found.") + return + + pythia.plugin.run_plugin_functions( + pythia.plugin.PluginHook.pre_analytics, + plugins, + config=config, + ) + run_outputs = [] calculated = None filtered = None - if not analytics_config: - return - if len(runs) == 0: - return + for run in runs: run_outputs.append(collate_outputs(config, run)) # Apply all the filters first @@ -272,3 +281,12 @@ def execute(config, plugins): combine_outputs(config, filtered) else: final_outputs(config, filtered) + + pythia.plugin.run_plugin_functions( + pythia.plugin.PluginHook.post_analytics, + plugins, + config=config, + run_outputs=run_outputs, + calculated=calculated, + filtered=filtered, + ) diff --git a/pythia/plugin.py b/pythia/plugin.py index b508231..004bc73 100644 --- a/pythia/plugin.py +++ b/pythia/plugin.py @@ -18,10 +18,8 @@ class PluginHook(Enum): post_run_pixel_success = 650 post_run_pixel_failed = 651 post_run_all = 700 - pre_analysis = 800 - analyze_file = 900 - analyze_pixel = 1000 - post_analysis = 1100 + pre_analytics = 800 + post_analytics = 900 def register_plugin_function(hook, fun, config, plugins): diff --git a/pythia/tests/plugin_test.py b/pythia/tests/plugin_test.py index 682d8dc..fa197a5 100644 --- a/pythia/tests/plugin_test.py +++ b/pythia/tests/plugin_test.py @@ -15,7 +15,7 @@ def test_register_with_invalid_hook(): def test_register_with_invalid_fun(): plugins = {} plugins1 = register_plugin_function( - PluginHook.analyze_file, "not a function", "", plugins + PluginHook.pre_analytics, "not a function", "", plugins ) assert plugins1 == {} @@ -23,7 +23,7 @@ def test_register_with_invalid_fun(): def test_register_with_invalid_config(): plugins = {} plugins1 = register_plugin_function( - PluginHook.post_analysis, sample_function, "", plugins + PluginHook.post_analytics, sample_function, "", plugins ) assert plugins1 == {} @@ -33,13 +33,13 @@ def test_register_twice(): plugins1 = {} plugins2 = {} plugins1 = register_plugin_function( - PluginHook.analyze_file, sample_function, {}, plugins + PluginHook.pre_analytics, sample_function, {}, plugins ) plugins2 = register_plugin_function( - PluginHook.analyze_file, sample_function, {"a": 1}, plugins1 + PluginHook.pre_analytics, sample_function, {"a": 1}, plugins1 ) assert plugins1 == { - PluginHook.analyze_file: [{"fun": sample_function, "config": {}}] + PluginHook.pre_analytics: [{"fun": sample_function, "config": {}}] } assert plugins1 == plugins2