Skip to content

Commit

Permalink
Merge pull request #60 from NathanPB/feat/analytics-hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
wpavan authored Mar 4, 2024
2 parents f1225dd + 85a7a0e commit a1a2f07
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
26 changes: 22 additions & 4 deletions pythia/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pythia.analytic_functions
import pythia.io
import pythia.util
import pythia.plugin
from contextlib import _GeneratorContextManager


Expand Down Expand Up @@ -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
Expand All @@ -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,
)
6 changes: 2 additions & 4 deletions pythia/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 5 additions & 5 deletions pythia/tests/plugin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ 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 == {}


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 == {}

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

Expand Down

0 comments on commit a1a2f07

Please sign in to comment.