rulevis 1.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
rulevis-1.0.3/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Zafer Balkan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
rulevis-1.0.3/PKG-INFO ADDED
@@ -0,0 +1,170 @@
1
+ Metadata-Version: 2.4
2
+ Name: rulevis
3
+ Version: 1.0.3
4
+ Summary: Interactive Wazuh Rule Graph Explorer
5
+ Author-email: Zafer Balkan <zafer@zaferbalkan.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/zbalkan/rulevis
8
+ Project-URL: Repository, https://github.com/zbalkan/rulevis
9
+ Project-URL: Issues, https://github.com/zbalkan/rulevis/issues
10
+ Keywords: wazuh,detection,security,monitoring,logging,siem
11
+ Classifier: Topic :: Security
12
+ Classifier: Topic :: System :: Logging
13
+ Classifier: Development Status :: 5 - Production/Stable
14
+ Classifier: Intended Audience :: Information Technology
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Programming Language :: Python :: 3.14
22
+ Requires-Python: >=3.9
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Requires-Dist: Flask>=3.1.2
26
+ Requires-Dist: networkx>=3.5
27
+ Dynamic: license-file
28
+
29
+ # RuleVis: Interactive Wazuh Rule Graph Explorer
30
+
31
+ RuleVis is a powerful analysis tool that transforms your Wazuh ruleset into a dynamic, interactive force-directed graph. It helps you visualize the complex relationships between rules, identify critical dependencies, discover structural issues, and analyze the distribution of your rule IDs.
32
+
33
+ This tool is designed for security engineers, SOC analysts, and Wazuh administrators who need to understand, maintain, and develop complex custom rulesets.
34
+
35
+ ![General View of RuleVis](https://github.com/zbalkan/rulevis/raw/master/assets/general-view.gif)
36
+
37
+ ## Features
38
+
39
+ * **Interactive Graph Visualization:** Renders your entire ruleset as a graph using D3.js and HTML Canvas for high performance.
40
+ * **Dependency Analysis:** Clearly shows parent-child relationships (`if_sid`, `if_group`, etc.) with directed edges.
41
+ * **Node Expansion:** Interactively expand nodes to reveal their parent or child dependencies on demand.
42
+ * **Detailed Rule Information:** Click on any rule to see its full description, groups, and a complete list of its parents and children.
43
+ * **Powerful Search:** Instantly find and focus on any rule by its ID.
44
+ * **Graph Statistics Panel:** Get at-a-glance insights into your ruleset with statistics like:
45
+ * Top 5 rules with the most direct children (foundational rules).
46
+ * Top 5 rules with the highest impact (most total descendants).
47
+ * Top 5 rules with the most complex dependencies.
48
+ * A list of isolated rules.
49
+ * Cycles in the rules
50
+ * **Rule ID Heatmap:** Visualize the entire rule ID space from 0 to 100,000+ to see which ID ranges are heavily used and which are available for custom rules.
51
+ * **Keyboard Shortcuts:** Pause the simulation (`Space`), close panels (`Esc`), and more for an efficient workflow.
52
+ * **Focus:** By default, when a node is highlighted, any node except for the selected node and its neighbords are dimmed. That allows better focus minimizing visual complexity.
53
+
54
+ ## The Problem It Solves
55
+
56
+ Wazuh's rule engine builds a complex, tree-like structure in memory. While powerful, this structure is invisible to the user. It can be difficult to:
57
+
58
+ * Understand the full impact of changing a single rule.
59
+ * Find redundant rules or overly complex dependency chains.
60
+ * Identify structural issues like circular dependencies, which can impact performance.
61
+ * Know which ID ranges are safe to use for new custom rules.
62
+
63
+ RuleVis makes these invisible structures visible, turning abstract XML files into a tangible, explorable map.
64
+
65
+ ## Installation
66
+
67
+ ### Using pipx (recommended)
68
+
69
+ `pipx install rulevis`
70
+
71
+ ### Using pip (for testing)
72
+
73
+ `pip install rulevis`
74
+
75
+ ### Using source
76
+
77
+ 1. **Clone the repository:**
78
+
79
+ ```shell
80
+ git clone https://github.com/zbalkan/rulevis.git
81
+ cd rulevis
82
+ ```
83
+
84
+ 2. **Create and activate a Python virtual environment:**
85
+
86
+ ```shell
87
+ python -m venv .venv
88
+ source .venv/bin/activate
89
+ ```
90
+
91
+ Or for Windows
92
+
93
+ ```shell
94
+ python -m venv .venv
95
+ source .venv\Scripts\activate.ps1
96
+ ```
97
+
98
+ 3. **Install dependencies:**
99
+
100
+ ```shell
101
+ pip install -r requirements.txt
102
+ ```
103
+
104
+ ## Usage
105
+
106
+ The tool is run from the command line. You must provide the path to the directory (or directories) containing your Wazuh rule XML files.
107
+
108
+ ```shell
109
+ python src/rulevis.py --path /var/ossec/ruleset/rules,/var/ossec/etc/rules
110
+ ```
111
+
112
+ **Arguments:**
113
+
114
+ * `--path, -p`: **(Required)** A comma-separated list of paths to your Wazuh rule directories. This should include both the default rules and your custom rules.
115
+ * `-h, --help`: Show the help message.
116
+
117
+ Once executed, the script will:
118
+
119
+ 1. Parse all `.xml` files in the specified paths.
120
+ 2. Build a graph model of the rule relationships.
121
+ 3. Pre-calculate statistics and heatmap data.
122
+ 4. Start a local web server.
123
+ 5. Automatically open the tool in your default web browser.
124
+
125
+ ## Key Features in Action
126
+
127
+ ### Graph Statistics
128
+
129
+ Quickly identify the most important and complex rules in your entire ruleset. Click on any rule in the list to instantly navigate to it in the main graph.
130
+
131
+ ![Statistics Panel](https://github.com/zbalkan/rulevis/raw/master/assets/stats-panel.gif)
132
+
133
+ ### Rule ID Heatmap
134
+
135
+ Get a bird's-eye view of your rule ID landscape. Dark gray blocks are unused and available for your custom rules, while brighter red blocks indicate heavily populated ranges. This is invaluable for planning and organizing a large custom ruleset.
136
+
137
+ ![Heatmap View](https://github.com/zbalkan/rulevis/raw/master/assets/heatmap-view.gif)
138
+
139
+ ## Technical Overview
140
+
141
+ The project is composed of three main Python modules and a JavaScript frontend:
142
+
143
+ 1. **`generator.py`:** Parses the Wazuh XML rule files and uses the `networkx` library to build a `MultiDiGraph` object representing the rule relationships. It saves this graph to a temporary file.
144
+ 2. **`analyzer.py`:** Loads the graph file and uses `networkx` to perform complex calculations (descendants, ancestors, etc.). It pre-calculates the data needed for the Statistics Panel and the Rule ID Heatmap and saves them to temporary JSON files.
145
+ 3. **`visualizer.py`:** A Flask web application that serves the frontend and provides a clean API for the visualization to fetch graph, stats, and heatmap data.
146
+ 4. **`graph.js`:** The core frontend logic. It uses **D3.js** for the force simulation and user interactions, and renders the main graph to an **HTML Canvas** for high performance. The interactive heatmap is rendered using **SVG** for its superior event handling and styling capabilities.
147
+
148
+ Here’s a **ready-to-paste README subsection** that explains `rulevis` logging clearly and professionally for your users. It assumes the per-user setup you’ve implemented.
149
+
150
+ ---
151
+
152
+ ## Logging
153
+
154
+ `rulevis` automatically writes diagnostic and operational logs to a user-specific location. Logs are plain text encoded in UTF-8 and include timestamps, module names, and severity levels. The application creates its log directory if it does not exist.
155
+
156
+ | Platform | Log file location | Example path |
157
+ | --------------- | ------------------------------------------------------------------------------------------- | -------------------------------------------------------- |
158
+ | Windows | `%LocalAppData%\rulevis\Logs\rulevis.log` | `C:\Users\<user>\AppData\Local\rulevis\Logs\rulevis.log` |
159
+ | macOS | `~/Library/Logs/rulevis/rulevis.log` | `/Users/<user>/Library/Logs/rulevis/rulevis.log` |
160
+ | Linux / BSD | `$XDG_STATE_HOME/rulevis/rulevis.log` or fallback `~/.local/share/rulevis/logs/rulevis.log` | `/home/<user>/.local/state/rulevis/rulevis.log` |
161
+
162
+ `rulevis` follows the [XDG Base Directory specification](https://specifications.freedesktop.org/basedir-spec/latest/) on Unix-like systems and Windows conventions under `%LocalAppData%`.
163
+
164
+ The log file records informational messages, warnings, and errors emitted during execution. You can safely delete it; a new one will be created automatically on the next run.
165
+
166
+ ## Notes
167
+
168
+ While the documentation defines <if_level> as another condition creating a parent-child relationship, it has not been used in any built-in rules. And as a personal choicem I decided to omit that deliberately.
169
+
170
+ There is another `if`, called `<if_fts>`, that is used for *first time seen* events, not creating a parent-child relationship. Theefore it is not mentioned.
@@ -0,0 +1,142 @@
1
+ # RuleVis: Interactive Wazuh Rule Graph Explorer
2
+
3
+ RuleVis is a powerful analysis tool that transforms your Wazuh ruleset into a dynamic, interactive force-directed graph. It helps you visualize the complex relationships between rules, identify critical dependencies, discover structural issues, and analyze the distribution of your rule IDs.
4
+
5
+ This tool is designed for security engineers, SOC analysts, and Wazuh administrators who need to understand, maintain, and develop complex custom rulesets.
6
+
7
+ ![General View of RuleVis](https://github.com/zbalkan/rulevis/raw/master/assets/general-view.gif)
8
+
9
+ ## Features
10
+
11
+ * **Interactive Graph Visualization:** Renders your entire ruleset as a graph using D3.js and HTML Canvas for high performance.
12
+ * **Dependency Analysis:** Clearly shows parent-child relationships (`if_sid`, `if_group`, etc.) with directed edges.
13
+ * **Node Expansion:** Interactively expand nodes to reveal their parent or child dependencies on demand.
14
+ * **Detailed Rule Information:** Click on any rule to see its full description, groups, and a complete list of its parents and children.
15
+ * **Powerful Search:** Instantly find and focus on any rule by its ID.
16
+ * **Graph Statistics Panel:** Get at-a-glance insights into your ruleset with statistics like:
17
+ * Top 5 rules with the most direct children (foundational rules).
18
+ * Top 5 rules with the highest impact (most total descendants).
19
+ * Top 5 rules with the most complex dependencies.
20
+ * A list of isolated rules.
21
+ * Cycles in the rules
22
+ * **Rule ID Heatmap:** Visualize the entire rule ID space from 0 to 100,000+ to see which ID ranges are heavily used and which are available for custom rules.
23
+ * **Keyboard Shortcuts:** Pause the simulation (`Space`), close panels (`Esc`), and more for an efficient workflow.
24
+ * **Focus:** By default, when a node is highlighted, any node except for the selected node and its neighbords are dimmed. That allows better focus minimizing visual complexity.
25
+
26
+ ## The Problem It Solves
27
+
28
+ Wazuh's rule engine builds a complex, tree-like structure in memory. While powerful, this structure is invisible to the user. It can be difficult to:
29
+
30
+ * Understand the full impact of changing a single rule.
31
+ * Find redundant rules or overly complex dependency chains.
32
+ * Identify structural issues like circular dependencies, which can impact performance.
33
+ * Know which ID ranges are safe to use for new custom rules.
34
+
35
+ RuleVis makes these invisible structures visible, turning abstract XML files into a tangible, explorable map.
36
+
37
+ ## Installation
38
+
39
+ ### Using pipx (recommended)
40
+
41
+ `pipx install rulevis`
42
+
43
+ ### Using pip (for testing)
44
+
45
+ `pip install rulevis`
46
+
47
+ ### Using source
48
+
49
+ 1. **Clone the repository:**
50
+
51
+ ```shell
52
+ git clone https://github.com/zbalkan/rulevis.git
53
+ cd rulevis
54
+ ```
55
+
56
+ 2. **Create and activate a Python virtual environment:**
57
+
58
+ ```shell
59
+ python -m venv .venv
60
+ source .venv/bin/activate
61
+ ```
62
+
63
+ Or for Windows
64
+
65
+ ```shell
66
+ python -m venv .venv
67
+ source .venv\Scripts\activate.ps1
68
+ ```
69
+
70
+ 3. **Install dependencies:**
71
+
72
+ ```shell
73
+ pip install -r requirements.txt
74
+ ```
75
+
76
+ ## Usage
77
+
78
+ The tool is run from the command line. You must provide the path to the directory (or directories) containing your Wazuh rule XML files.
79
+
80
+ ```shell
81
+ python src/rulevis.py --path /var/ossec/ruleset/rules,/var/ossec/etc/rules
82
+ ```
83
+
84
+ **Arguments:**
85
+
86
+ * `--path, -p`: **(Required)** A comma-separated list of paths to your Wazuh rule directories. This should include both the default rules and your custom rules.
87
+ * `-h, --help`: Show the help message.
88
+
89
+ Once executed, the script will:
90
+
91
+ 1. Parse all `.xml` files in the specified paths.
92
+ 2. Build a graph model of the rule relationships.
93
+ 3. Pre-calculate statistics and heatmap data.
94
+ 4. Start a local web server.
95
+ 5. Automatically open the tool in your default web browser.
96
+
97
+ ## Key Features in Action
98
+
99
+ ### Graph Statistics
100
+
101
+ Quickly identify the most important and complex rules in your entire ruleset. Click on any rule in the list to instantly navigate to it in the main graph.
102
+
103
+ ![Statistics Panel](https://github.com/zbalkan/rulevis/raw/master/assets/stats-panel.gif)
104
+
105
+ ### Rule ID Heatmap
106
+
107
+ Get a bird's-eye view of your rule ID landscape. Dark gray blocks are unused and available for your custom rules, while brighter red blocks indicate heavily populated ranges. This is invaluable for planning and organizing a large custom ruleset.
108
+
109
+ ![Heatmap View](https://github.com/zbalkan/rulevis/raw/master/assets/heatmap-view.gif)
110
+
111
+ ## Technical Overview
112
+
113
+ The project is composed of three main Python modules and a JavaScript frontend:
114
+
115
+ 1. **`generator.py`:** Parses the Wazuh XML rule files and uses the `networkx` library to build a `MultiDiGraph` object representing the rule relationships. It saves this graph to a temporary file.
116
+ 2. **`analyzer.py`:** Loads the graph file and uses `networkx` to perform complex calculations (descendants, ancestors, etc.). It pre-calculates the data needed for the Statistics Panel and the Rule ID Heatmap and saves them to temporary JSON files.
117
+ 3. **`visualizer.py`:** A Flask web application that serves the frontend and provides a clean API for the visualization to fetch graph, stats, and heatmap data.
118
+ 4. **`graph.js`:** The core frontend logic. It uses **D3.js** for the force simulation and user interactions, and renders the main graph to an **HTML Canvas** for high performance. The interactive heatmap is rendered using **SVG** for its superior event handling and styling capabilities.
119
+
120
+ Here’s a **ready-to-paste README subsection** that explains `rulevis` logging clearly and professionally for your users. It assumes the per-user setup you’ve implemented.
121
+
122
+ ---
123
+
124
+ ## Logging
125
+
126
+ `rulevis` automatically writes diagnostic and operational logs to a user-specific location. Logs are plain text encoded in UTF-8 and include timestamps, module names, and severity levels. The application creates its log directory if it does not exist.
127
+
128
+ | Platform | Log file location | Example path |
129
+ | --------------- | ------------------------------------------------------------------------------------------- | -------------------------------------------------------- |
130
+ | Windows | `%LocalAppData%\rulevis\Logs\rulevis.log` | `C:\Users\<user>\AppData\Local\rulevis\Logs\rulevis.log` |
131
+ | macOS | `~/Library/Logs/rulevis/rulevis.log` | `/Users/<user>/Library/Logs/rulevis/rulevis.log` |
132
+ | Linux / BSD | `$XDG_STATE_HOME/rulevis/rulevis.log` or fallback `~/.local/share/rulevis/logs/rulevis.log` | `/home/<user>/.local/state/rulevis/rulevis.log` |
133
+
134
+ `rulevis` follows the [XDG Base Directory specification](https://specifications.freedesktop.org/basedir-spec/latest/) on Unix-like systems and Windows conventions under `%LocalAppData%`.
135
+
136
+ The log file records informational messages, warnings, and errors emitted during execution. You can safely delete it; a new one will be created automatically on the next run.
137
+
138
+ ## Notes
139
+
140
+ While the documentation defines <if_level> as another condition creating a parent-child relationship, it has not been used in any built-in rules. And as a personal choicem I decided to omit that deliberately.
141
+
142
+ There is another `if`, called `<if_fts>`, that is used for *first time seen* events, not creating a parent-child relationship. Theefore it is not mentioned.
@@ -0,0 +1,52 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77.0.3", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "rulevis"
7
+ version = "1.0.3"
8
+ description = "Interactive Wazuh Rule Graph Explorer"
9
+ readme = "README.md"
10
+ requires-python = ">=3.9"
11
+
12
+ license = "MIT"
13
+ license-files = ["LICEN[CS]E*"]
14
+
15
+ authors = [{ name = "Zafer Balkan", email = "zafer@zaferbalkan.com" }]
16
+ keywords = ["wazuh", "detection", "security", "monitoring", "logging", "siem"]
17
+ classifiers = [
18
+ "Topic :: Security",
19
+ "Topic :: System :: Logging",
20
+ "Development Status :: 5 - Production/Stable",
21
+ "Intended Audience :: Information Technology",
22
+ "Programming Language :: Python :: 3",
23
+ "Programming Language :: Python :: 3.9",
24
+ "Programming Language :: Python :: 3.10",
25
+ "Programming Language :: Python :: 3.11",
26
+ "Programming Language :: Python :: 3.12",
27
+ "Programming Language :: Python :: 3.13",
28
+ "Programming Language :: Python :: 3.14"
29
+ ]
30
+
31
+ dependencies = [
32
+ "Flask>=3.1.2",
33
+ "networkx>=3.5",
34
+ ]
35
+
36
+ [project.urls]
37
+ Homepage = "https://github.com/zbalkan/rulevis"
38
+ Repository = "https://github.com/zbalkan/rulevis"
39
+ Issues = "https://github.com/zbalkan/rulevis/issues"
40
+
41
+ [project.scripts]
42
+ rulevis = "rulevis:main"
43
+
44
+ [tool.setuptools]
45
+ package-dir = {"" = "src"}
46
+ py-modules = ["rulevis"]
47
+
48
+ [tool.setuptools.packages.find]
49
+ where = ["src"]
50
+
51
+ [tool.setuptools.package-data]
52
+ "internal.static" = ["*.*"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,167 @@
1
+ import logging
2
+ import math
3
+ import pickle
4
+ import json
5
+ import os
6
+ from networkx import MultiDiGraph, descendants, ancestors, nodes_with_selfloops, simple_cycles
7
+
8
+
9
+ class Analyzer:
10
+ """
11
+ Analyzes a graph from a pickle file and calculates key statistics.
12
+ """
13
+ def __init__(self, graph_path: str) -> None:
14
+ """
15
+ Initializes the Analyzer with the path to the graph file.
16
+
17
+ Args:
18
+ graph_path (str): The path to the .gpickle file.
19
+
20
+ Raises:
21
+ FileNotFoundError: If the graph file does not exist.
22
+ """
23
+ if not os.path.isfile(graph_path):
24
+ raise FileNotFoundError(f"Graph file not found: {graph_path}")
25
+ self.graph_path = graph_path
26
+ self.G: MultiDiGraph = self._load_graph()
27
+
28
+ def _load_graph(self) -> MultiDiGraph:
29
+ """Loads the graph from the pickle file."""
30
+ with open(self.graph_path, "rb") as f:
31
+ return pickle.load(f)
32
+
33
+ def calculate_statistics(self) -> dict:
34
+ """
35
+ Calculates all the required statistics for the graph, ensuring no
36
+ duplicate cycles are reported.
37
+ """
38
+ real_nodes = [n for n in self.G.nodes() if n != '0']
39
+
40
+ # --- Calculate Top 5 Lists ---
41
+ real_nodes = [n for n in self.G.nodes() if n != '0']
42
+ out_degrees = dict(self.G.out_degree(real_nodes))
43
+ in_degrees = dict(self.G.in_degree(real_nodes))
44
+
45
+ top_5_direct_descendants = sorted(out_degrees, key=out_degrees.get, reverse=True)[:5]
46
+ indirect_descendants_counts = {n: len(descendants(self.G, n)) for n in real_nodes}
47
+ top_5_indirect_descendants = sorted(indirect_descendants_counts,
48
+ key=indirect_descendants_counts.get, reverse=True)[:5]
49
+ top_5_direct_ancestors = sorted(in_degrees,
50
+ key=in_degrees.get, reverse=True)[:5]
51
+ indirect_ancestors_counts = {n: len(ancestors(self.G, n)) for n in real_nodes}
52
+ top_5_indirect_ancestors = sorted(indirect_ancestors_counts,
53
+ key=indirect_ancestors_counts.get, reverse=True)[:5]
54
+
55
+ isolated_rules = [
56
+ n for n in real_nodes
57
+ if self.G.out_degree(n) == 0 and list(self.G.predecessors(n)) == ['0']
58
+ ]
59
+
60
+ # 1. Find all nodes with self-loops first. This is our definitive list.
61
+ logging.info("Detecting self-loops (e.g., A -> A)...")
62
+ self_loop_nodes = set(nodes_with_selfloops(self.G)) # Use a set for efficient lookup
63
+ logging.info(f"Found {len(self_loop_nodes)} nodes with self-loops: {self_loop_nodes}")
64
+
65
+ # 2. Find all other simple cycles.
66
+ logging.info("Detecting multi-node cycles...")
67
+ all_simple_cycles = list(simple_cycles(self.G))
68
+
69
+ # 3. Filter out any multi-node cycles that contain a node we've already
70
+ # identified as having a self-loop. This prevents double-reporting.
71
+ multi_node_cycles = [
72
+ cycle for cycle in all_simple_cycles
73
+ if not self_loop_nodes.intersection(cycle)
74
+ ]
75
+
76
+ logging.info(f"Found {len(multi_node_cycles)} distinct multi-node cycles.")
77
+ if multi_node_cycles:
78
+ logging.info(f"Example multi-node cycle: {multi_node_cycles[0]}")
79
+
80
+ # 4. Format the multi-node cycles for display
81
+ for cycle in multi_node_cycles:
82
+ if cycle:
83
+ cycle.append(cycle[0])
84
+
85
+ stats = {
86
+ "top_direct_descendants": [{"id": n, "count": out_degrees[n]}
87
+ for n in top_5_direct_descendants],
88
+ "top_indirect_descendants": [{"id": n, "count": indirect_descendants_counts[n]}
89
+ for n in top_5_indirect_descendants],
90
+ "top_direct_ancestors": [{"id": n, "count": in_degrees[n]} for n in top_5_direct_ancestors],
91
+ "top_indirect_ancestors": [{"id": n, "count": indirect_ancestors_counts[n]}
92
+ for n in top_5_indirect_ancestors],
93
+ "isolated_rules": [{"id": n}
94
+ for n in isolated_rules],
95
+ "self_loops": [{"id": n}
96
+ for n in self_loop_nodes],
97
+ "cycles": multi_node_cycles
98
+ }
99
+
100
+ return stats
101
+
102
+ def calculate_heatmap_data(self, block_size: int = 10) -> dict:
103
+ """
104
+ Generates data for a block-based heatmap, showing rule ID occupancy.
105
+ The heatmap range is dynamically calculated based on the highest rule ID found.
106
+
107
+ Args:
108
+ block_size (int): The size of each ID range block (e.g., 10).
109
+
110
+ Returns:
111
+ dict: A dictionary containing the list of blocks and metadata.
112
+ """
113
+ all_rule_ids = {int(n) for n in self.G.nodes() if n.isdigit() and n != '0'}
114
+
115
+ if not all_rule_ids:
116
+ # Handle case where there are no integer rules
117
+ return {"metadata": {"block_size": block_size, "max_id": 0, "total_blocks": 0}, "blocks": []}
118
+
119
+ actual_max_id = max(all_rule_ids)
120
+
121
+ # Calculate the upper bound for the heatmap range.
122
+ # We use math.ceil to round up to the next full block.
123
+ # For example, if max ID is 101234 and block size is 1000, this becomes:
124
+ # ceil(101234 / 1000) * 1000 => ceil(101.234) * 1000 => 102 * 1000 => 102000
125
+ dynamic_max_range = math.ceil(actual_max_id / block_size) * block_size
126
+
127
+ # Ensure we have at least one block even if max_id is small
128
+ dynamic_max_range = max(dynamic_max_range, block_size)
129
+
130
+ blocks = []
131
+ # Use the dynamically calculated range for the loop
132
+ for i in range(0, dynamic_max_range, block_size):
133
+ start_range = i
134
+ end_range = i + block_size - 1
135
+
136
+ count = sum(1 for rule_id in all_rule_ids if start_range <= rule_id <= end_range)
137
+
138
+ blocks.append({
139
+ "id": f"{start_range}-{end_range}",
140
+ "count": count
141
+ })
142
+
143
+ return {
144
+ "metadata": {
145
+ "block_size": block_size,
146
+ "max_id": dynamic_max_range,
147
+ "total_blocks": len(blocks)
148
+ },
149
+ "blocks": blocks
150
+ }
151
+
152
+ def write_to_json(self, stats_output_path: str, heatmap_output_path: str) -> None:
153
+ """Calculates all data and writes to respective files."""
154
+
155
+ logging.info("Calculating graph statistics...")
156
+ stats_data = self.calculate_statistics()
157
+ logging.info(f"Writing statistics to {stats_output_path}...")
158
+ with open(stats_output_path, "w") as f:
159
+ json.dump(stats_data, f, indent=4)
160
+
161
+ logging.info("Calculating heatmap data...")
162
+ heatmap_data = self.calculate_heatmap_data()
163
+ logging.info(f"Writing heatmap data to {heatmap_output_path}...")
164
+ with open(heatmap_output_path, "w") as f:
165
+ json.dump(heatmap_data, f, indent=2)
166
+
167
+ logging.info("All analysis complete.")