adre-cli 0.1.0__py3-none-any.whl
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.
- adre/__init__.py +5 -0
- adre/analyzers/__init__.py +1 -0
- adre/analyzers/architecture.py +253 -0
- adre/analyzers/findings.py +457 -0
- adre/analyzers/inference.py +163 -0
- adre/cli.py +923 -0
- adre/core/__init__.py +1 -0
- adre/core/accepted.py +225 -0
- adre/core/adaptive.py +221 -0
- adre/core/baseline.py +1165 -0
- adre/core/config.py +60 -0
- adre/core/decision_engine.py +361 -0
- adre/core/declared.py +313 -0
- adre/core/evolution_intelligence.py +390 -0
- adre/core/explainer.py +413 -0
- adre/core/git_intelligence.py +392 -0
- adre/core/github_check.py +212 -0
- adre/core/incremental.py +288 -0
- adre/core/layers.py +306 -0
- adre/core/pr_intelligence.py +261 -0
- adre/core/pr_review.py +454 -0
- adre/core/prioritization.py +196 -0
- adre/core/project.py +41 -0
- adre/core/refactor_intelligence.py +201 -0
- adre/core/repositories.py +312 -0
- adre/core/review.py +96 -0
- adre/core/rules_shared.py +75 -0
- adre/core/sarif.py +141 -0
- adre/core/scope.py +141 -0
- adre/core/snapshots.py +242 -0
- adre/core/stability_intelligence.py +361 -0
- adre/graph/__init__.py +0 -0
- adre/graph/dependencies.py +379 -0
- adre/parsers/__init__.py +87 -0
- adre/parsers/go.py +140 -0
- adre/parsers/javascript.py +364 -0
- adre/parsers/jvm.py +127 -0
- adre/parsers/python.py +153 -0
- adre/reports/__init__.py +1 -0
- adre/reports/architecture.py +349 -0
- adre/rules.py +12 -0
- adre/web.py +3731 -0
- adre_cli-0.1.0.dist-info/METADATA +343 -0
- adre_cli-0.1.0.dist-info/RECORD +48 -0
- adre_cli-0.1.0.dist-info/WHEEL +5 -0
- adre_cli-0.1.0.dist-info/entry_points.txt +2 -0
- adre_cli-0.1.0.dist-info/licenses/LICENSE +202 -0
- adre_cli-0.1.0.dist-info/top_level.txt +1 -0
adre/core/sarif.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"""SARIF 2.1.0 output for drift findings (GitHub code scanning, IDEs)."""
|
|
2
|
+
import hashlib
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
from adre import __version__
|
|
7
|
+
from adre.core.config import ANALYZER_VERSION
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
SARIF_SCHEMA = "https://json.schemastore.org/sarif-2.1.0.json"
|
|
11
|
+
TOOL_NAME = "ADRE"
|
|
12
|
+
|
|
13
|
+
RULES = {
|
|
14
|
+
"declared_violation": (
|
|
15
|
+
"Declared architecture violation",
|
|
16
|
+
"A new dependency breaks the layers or forbidden rules declared in .adre/architecture.json.",
|
|
17
|
+
),
|
|
18
|
+
"indirect_violation": (
|
|
19
|
+
"Indirect layer violation",
|
|
20
|
+
"A new import connects a lower declared layer to a higher one through a chain of other modules.",
|
|
21
|
+
),
|
|
22
|
+
"layer_skip": (
|
|
23
|
+
"Layer skip",
|
|
24
|
+
"A module bypasses an established intermediate layer, e.g. controller -> repository instead of controller -> service -> repository.",
|
|
25
|
+
),
|
|
26
|
+
"layer_violation": (
|
|
27
|
+
"Reversed dependency direction",
|
|
28
|
+
"A module depends against the dependency direction recorded in the architecture baseline.",
|
|
29
|
+
),
|
|
30
|
+
"unexpected_dependency": (
|
|
31
|
+
"Unexpected cross-layer dependency",
|
|
32
|
+
"A new dependency between two established layers that the baseline never recorded.",
|
|
33
|
+
),
|
|
34
|
+
"repository_bypass": (
|
|
35
|
+
"Repository bypass",
|
|
36
|
+
"Presentation or application code imports a database driver or ORM directly although a repository/persistence boundary is established.",
|
|
37
|
+
),
|
|
38
|
+
"cycle": (
|
|
39
|
+
"New import cycle",
|
|
40
|
+
"A file joined a group of files that import each other at load time, or two such groups merged.",
|
|
41
|
+
),
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
LEVELS = {"critical": "error", "high": "error", "medium": "warning", "low": "note"}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def repo_path(path, project_prefix=""):
|
|
48
|
+
path = (path or "").replace("\\", "/").lstrip("/")
|
|
49
|
+
return (project_prefix or "") + path
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def finding_fingerprint(finding):
|
|
53
|
+
"""Stable across commits: issue type, file and what it depends on."""
|
|
54
|
+
key = "|".join([
|
|
55
|
+
finding.get("issue_type") or "",
|
|
56
|
+
finding.get("file") or "",
|
|
57
|
+
finding.get("imported") if finding.get("issue_type") != "cycle" else ",".join(finding.get("cycle_group") or []),
|
|
58
|
+
])
|
|
59
|
+
return hashlib.sha256(key.encode("utf-8")).hexdigest()[:32]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _message(finding):
|
|
63
|
+
parts = []
|
|
64
|
+
reason = finding.get("reason") or finding.get("problem")
|
|
65
|
+
if reason:
|
|
66
|
+
parts.append(reason.rstrip(".") + ".")
|
|
67
|
+
if finding.get("changed"):
|
|
68
|
+
parts.append("Changed: {0}.".format(finding["changed"]))
|
|
69
|
+
if finding.get("established"):
|
|
70
|
+
parts.append("Established: {0}.".format(finding["established"]))
|
|
71
|
+
return " ".join(parts) or "Architectural drift detected."
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _result(finding, project_prefix):
|
|
75
|
+
issue_type = finding.get("issue_type") or "drift"
|
|
76
|
+
path = finding.get("file") or (finding.get("affected_files") or [""])[0]
|
|
77
|
+
location = {"physicalLocation": {"artifactLocation": {"uri": repo_path(path, project_prefix)}}}
|
|
78
|
+
line = finding.get("line")
|
|
79
|
+
if line is not None and str(line).isdigit() and int(line) > 0:
|
|
80
|
+
location["physicalLocation"]["region"] = {"startLine": int(line)}
|
|
81
|
+
result = {
|
|
82
|
+
"ruleId": issue_type,
|
|
83
|
+
"level": LEVELS.get(finding.get("severity"), "warning"),
|
|
84
|
+
"message": {"text": _message(finding)},
|
|
85
|
+
"locations": [location],
|
|
86
|
+
"partialFingerprints": {"adreFinding/v1": finding_fingerprint(finding)},
|
|
87
|
+
"properties": {
|
|
88
|
+
"confidence": finding.get("confidence"),
|
|
89
|
+
"evidence": list(finding.get("evidence") or [])[:6],
|
|
90
|
+
},
|
|
91
|
+
}
|
|
92
|
+
related = [item for item in finding.get("affected_files") or [] if item and item != path]
|
|
93
|
+
if related:
|
|
94
|
+
result["relatedLocations"] = [
|
|
95
|
+
{"id": index, "physicalLocation": {"artifactLocation": {"uri": repo_path(item, project_prefix)}}}
|
|
96
|
+
for index, item in enumerate(related[:10], start=1)
|
|
97
|
+
]
|
|
98
|
+
return result
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def build_sarif(result):
|
|
102
|
+
project_prefix = result.get("project_prefix") or ""
|
|
103
|
+
drift = result.get("drift") or []
|
|
104
|
+
used = sorted({item.get("issue_type") or "drift" for item in drift} | set(RULES))
|
|
105
|
+
rules = []
|
|
106
|
+
for rule_id in used:
|
|
107
|
+
name, description = RULES.get(rule_id, (rule_id, rule_id))
|
|
108
|
+
rules.append({
|
|
109
|
+
"id": rule_id,
|
|
110
|
+
"name": name.title().replace(" ", ""),
|
|
111
|
+
"shortDescription": {"text": name},
|
|
112
|
+
"fullDescription": {"text": description},
|
|
113
|
+
"defaultConfiguration": {"level": "error" if rule_id != "unexpected_dependency" else "warning"},
|
|
114
|
+
})
|
|
115
|
+
run = {
|
|
116
|
+
"tool": {"driver": {
|
|
117
|
+
"name": TOOL_NAME,
|
|
118
|
+
"version": __version__,
|
|
119
|
+
"semanticVersion": __version__,
|
|
120
|
+
"informationUri": "https://github.com/christiangebru/architect-reviewer",
|
|
121
|
+
"rules": rules,
|
|
122
|
+
"properties": {"analyzerVersion": ANALYZER_VERSION},
|
|
123
|
+
}},
|
|
124
|
+
"results": [_result(item, project_prefix) for item in drift],
|
|
125
|
+
"properties": {
|
|
126
|
+
"status": result.get("status"),
|
|
127
|
+
"summary": result.get("summary"),
|
|
128
|
+
"acceptedCount": len(result.get("accepted_drift") or []),
|
|
129
|
+
},
|
|
130
|
+
}
|
|
131
|
+
return {"$schema": SARIF_SCHEMA, "version": "2.1.0", "runs": [run]}
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def write_sarif(result, destination):
|
|
135
|
+
payload = build_sarif(result)
|
|
136
|
+
directory = os.path.dirname(os.path.abspath(destination))
|
|
137
|
+
os.makedirs(directory, exist_ok=True)
|
|
138
|
+
with open(destination, "w", encoding="utf-8") as handle:
|
|
139
|
+
json.dump(payload, handle, indent=2)
|
|
140
|
+
handle.write("\n")
|
|
141
|
+
return destination
|
adre/core/scope.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
from adre.parsers import collect_source_files, language_for_path
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def _is_within_root(project_root, candidate):
|
|
7
|
+
root = os.path.realpath(project_root)
|
|
8
|
+
path = os.path.realpath(candidate)
|
|
9
|
+
return path == root or path.startswith(root + os.sep)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def resolve_selected_paths(project_root, selected_paths, indexed_files):
|
|
13
|
+
if not selected_paths:
|
|
14
|
+
return [], []
|
|
15
|
+
|
|
16
|
+
indexed = set(indexed_files)
|
|
17
|
+
selected = []
|
|
18
|
+
missing = []
|
|
19
|
+
|
|
20
|
+
for raw in selected_paths:
|
|
21
|
+
if not raw or not str(raw).strip():
|
|
22
|
+
continue
|
|
23
|
+
expanded = os.path.expanduser(str(raw).strip())
|
|
24
|
+
absolute = expanded if os.path.isabs(expanded) else os.path.join(project_root, expanded)
|
|
25
|
+
if not _is_within_root(project_root, absolute):
|
|
26
|
+
missing.append(raw)
|
|
27
|
+
continue
|
|
28
|
+
if os.path.isdir(absolute):
|
|
29
|
+
prefix = os.path.relpath(absolute, project_root)
|
|
30
|
+
if prefix == ".":
|
|
31
|
+
selected.extend(sorted(indexed))
|
|
32
|
+
continue
|
|
33
|
+
prefix = prefix.rstrip(os.sep) + os.sep
|
|
34
|
+
matched = [path for path in indexed if path == prefix[:-1] or path.startswith(prefix)]
|
|
35
|
+
if matched:
|
|
36
|
+
selected.extend(matched)
|
|
37
|
+
else:
|
|
38
|
+
missing.append(raw)
|
|
39
|
+
elif os.path.isfile(absolute):
|
|
40
|
+
rel_path = os.path.relpath(absolute, project_root)
|
|
41
|
+
if rel_path in indexed:
|
|
42
|
+
selected.append(rel_path)
|
|
43
|
+
else:
|
|
44
|
+
missing.append(raw)
|
|
45
|
+
else:
|
|
46
|
+
rel_guess = os.path.normpath(expanded)
|
|
47
|
+
if rel_guess in indexed:
|
|
48
|
+
selected.append(rel_guess)
|
|
49
|
+
else:
|
|
50
|
+
missing.append(raw)
|
|
51
|
+
|
|
52
|
+
unique = []
|
|
53
|
+
for path in selected:
|
|
54
|
+
if path not in unique:
|
|
55
|
+
unique.append(path)
|
|
56
|
+
return unique, missing
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def expand_related_files(selected, resolved_graph, hops=2):
|
|
60
|
+
related = set(selected)
|
|
61
|
+
frontier = set(selected)
|
|
62
|
+
reverse_graph = {}
|
|
63
|
+
for source, targets in resolved_graph.items():
|
|
64
|
+
for target in targets:
|
|
65
|
+
reverse_graph.setdefault(target, set()).add(source)
|
|
66
|
+
|
|
67
|
+
for _ in range(max(0, hops)):
|
|
68
|
+
nxt = set()
|
|
69
|
+
for file_path in frontier:
|
|
70
|
+
for dep in resolved_graph.get(file_path, set()):
|
|
71
|
+
if dep not in related:
|
|
72
|
+
nxt.add(dep)
|
|
73
|
+
for inbound in reverse_graph.get(file_path, set()):
|
|
74
|
+
if inbound not in related:
|
|
75
|
+
nxt.add(inbound)
|
|
76
|
+
if not nxt:
|
|
77
|
+
break
|
|
78
|
+
related.update(nxt)
|
|
79
|
+
frontier = nxt
|
|
80
|
+
return related
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def build_analysis_scope(
|
|
84
|
+
project_root,
|
|
85
|
+
dependency_graph,
|
|
86
|
+
resolved_graph,
|
|
87
|
+
selected_paths=None,
|
|
88
|
+
expand_related=True,
|
|
89
|
+
truncated=False,
|
|
90
|
+
unsupported_files=0,
|
|
91
|
+
):
|
|
92
|
+
indexed_files = sorted(dependency_graph.keys())
|
|
93
|
+
selected, missing = resolve_selected_paths(project_root, selected_paths, indexed_files)
|
|
94
|
+
|
|
95
|
+
if not selected_paths:
|
|
96
|
+
analyzed = set(indexed_files)
|
|
97
|
+
expanded = set()
|
|
98
|
+
selected = []
|
|
99
|
+
else:
|
|
100
|
+
analyzed = set(selected)
|
|
101
|
+
expanded = set()
|
|
102
|
+
if expand_related and selected:
|
|
103
|
+
related = expand_related_files(selected, resolved_graph)
|
|
104
|
+
expanded = related - analyzed
|
|
105
|
+
analyzed = related
|
|
106
|
+
|
|
107
|
+
languages = {}
|
|
108
|
+
parse_errors = 0
|
|
109
|
+
for rel_path in analyzed:
|
|
110
|
+
record = dependency_graph.get(rel_path) or {}
|
|
111
|
+
language = record.get("language") or language_for_path(rel_path) or "unknown"
|
|
112
|
+
languages[language] = languages.get(language, 0) + 1
|
|
113
|
+
if record.get("parse_error"):
|
|
114
|
+
parse_errors += 1
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
"project_root": project_root,
|
|
118
|
+
"selected_paths": list(selected_paths or []),
|
|
119
|
+
"selected_files": sorted(selected),
|
|
120
|
+
"expanded_files": sorted(expanded),
|
|
121
|
+
"analyzed_files": sorted(analyzed),
|
|
122
|
+
"missing_paths": missing,
|
|
123
|
+
"indexed_file_count": len(indexed_files),
|
|
124
|
+
"analyzed_file_count": len(analyzed),
|
|
125
|
+
"expand_related": bool(expand_related),
|
|
126
|
+
"languages": languages,
|
|
127
|
+
"unsupported_files": unsupported_files,
|
|
128
|
+
"parse_errors": parse_errors,
|
|
129
|
+
"truncated": truncated,
|
|
130
|
+
"selection_mode": "subset" if selected_paths else "repository",
|
|
131
|
+
"excluded_directories": sorted(
|
|
132
|
+
{directory for directory in (
|
|
133
|
+
"node_modules", ".git", "dist", "build", "venv", ".venv", "__pycache__", ".adre"
|
|
134
|
+
)}
|
|
135
|
+
),
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def collect_indexed_files(project_root):
|
|
140
|
+
files, truncated, unsupported = collect_source_files(project_root)
|
|
141
|
+
return files, truncated, unsupported
|
adre/core/snapshots.py
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
|
|
5
|
+
from adre.core.config import SNAPSHOT_DIR
|
|
6
|
+
from adre.core.rules_shared import calculate_structural_drift_score
|
|
7
|
+
from adre.analyzers.architecture import calculate_dependency_growth_per_file
|
|
8
|
+
from adre.graph.dependencies import dependency_graph_edges
|
|
9
|
+
from adre.reports.architecture import (
|
|
10
|
+
build_architecture_narrative,
|
|
11
|
+
calculate_change_impact_scores,
|
|
12
|
+
compare_dependency_graphs,
|
|
13
|
+
generate_refactor_plan,
|
|
14
|
+
identify_primary_cause_files,
|
|
15
|
+
summarize_drift_intelligence,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def ensure_snapshot_dir(snapshot_dir=SNAPSHOT_DIR):
|
|
20
|
+
os.makedirs(snapshot_dir, exist_ok=True)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def get_snapshot_timestamp():
|
|
24
|
+
return datetime.now().isoformat(timespec="microseconds")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def get_snapshot_filename(timestamp):
|
|
28
|
+
safe_timestamp = timestamp.replace(":", "-")
|
|
29
|
+
return f"architecture_snapshot_{safe_timestamp}.json"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def create_snapshot_payload(analysis):
|
|
33
|
+
return {
|
|
34
|
+
"timestamp": get_snapshot_timestamp(),
|
|
35
|
+
"architecture_score": analysis["score"],
|
|
36
|
+
"architecture_type": analysis.get("architecture_type"),
|
|
37
|
+
"architecture_confidence": analysis.get("architecture_confidence"),
|
|
38
|
+
"low_confidence": analysis.get("low_confidence", False),
|
|
39
|
+
"issues": analysis["issues"],
|
|
40
|
+
"cycles": analysis["cycles"],
|
|
41
|
+
"indirect_dependencies": analysis["indirect_dependencies"],
|
|
42
|
+
"hotspot_files": analysis["hotspot_files"],
|
|
43
|
+
"dependency_graph": analysis.get("dependency_graph", {})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def save_architecture_snapshot(analysis, snapshot_dir=SNAPSHOT_DIR):
|
|
48
|
+
ensure_snapshot_dir(snapshot_dir)
|
|
49
|
+
snapshot = create_snapshot_payload(analysis)
|
|
50
|
+
filename = get_snapshot_filename(snapshot["timestamp"])
|
|
51
|
+
snapshot_path = os.path.join(snapshot_dir, filename)
|
|
52
|
+
with open(snapshot_path, "w", encoding="utf-8") as f:
|
|
53
|
+
json.dump(snapshot, f, indent=2)
|
|
54
|
+
return {"file": filename, "path": snapshot_path, "snapshot": snapshot}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def get_snapshot_path(filename, snapshot_dir=SNAPSHOT_DIR):
|
|
58
|
+
if os.path.isabs(filename) and os.path.exists(filename):
|
|
59
|
+
return filename
|
|
60
|
+
safe_filename = os.path.basename(filename)
|
|
61
|
+
return os.path.join(snapshot_dir, safe_filename)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def load_snapshot_file(filename, snapshot_dir=SNAPSHOT_DIR):
|
|
65
|
+
snapshot_path = get_snapshot_path(filename, snapshot_dir)
|
|
66
|
+
with open(snapshot_path, "r", encoding="utf-8") as f:
|
|
67
|
+
return json.load(f)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def load_all_snapshots(snapshot_dir=SNAPSHOT_DIR):
|
|
71
|
+
if not os.path.isdir(snapshot_dir):
|
|
72
|
+
return []
|
|
73
|
+
snapshots = []
|
|
74
|
+
for filename in os.listdir(snapshot_dir):
|
|
75
|
+
if not filename.endswith(".json"):
|
|
76
|
+
continue
|
|
77
|
+
try:
|
|
78
|
+
snapshot = load_snapshot_file(filename, snapshot_dir)
|
|
79
|
+
except (OSError, json.JSONDecodeError):
|
|
80
|
+
continue
|
|
81
|
+
snapshot["file"] = filename
|
|
82
|
+
snapshots.append(snapshot)
|
|
83
|
+
return sorted(snapshots, key=lambda item: item.get("timestamp", ""))
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def calculate_score_trend(timeline):
|
|
87
|
+
if len(timeline) < 2:
|
|
88
|
+
return "stable"
|
|
89
|
+
first_score = timeline[0]["architecture_score"]
|
|
90
|
+
last_score = timeline[-1]["architecture_score"]
|
|
91
|
+
if last_score > first_score:
|
|
92
|
+
return "improving"
|
|
93
|
+
if last_score < first_score:
|
|
94
|
+
return "degrading"
|
|
95
|
+
return "stable"
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def build_architecture_history(snapshot_dir=SNAPSHOT_DIR):
|
|
99
|
+
snapshots = load_all_snapshots(snapshot_dir)
|
|
100
|
+
timeline = [
|
|
101
|
+
{"timestamp": snapshot.get("timestamp"), "architecture_score": snapshot.get("architecture_score")}
|
|
102
|
+
for snapshot in snapshots
|
|
103
|
+
]
|
|
104
|
+
return {
|
|
105
|
+
"snapshot_count": len(snapshots),
|
|
106
|
+
"timeline": timeline,
|
|
107
|
+
"trend": calculate_score_trend(timeline),
|
|
108
|
+
"last_5_snapshots": snapshots[-5:]
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def issue_identity(issue):
|
|
113
|
+
return (
|
|
114
|
+
issue.get("file", ""),
|
|
115
|
+
issue.get("layer", ""),
|
|
116
|
+
issue.get("imported", ""),
|
|
117
|
+
issue.get("issue_type", ""),
|
|
118
|
+
issue.get("severity", "")
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def cycle_identity(cycle):
|
|
123
|
+
return tuple(cycle)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def issue_maps_by_identity(snapshot):
|
|
127
|
+
return {issue_identity(issue): issue for issue in snapshot.get("issues", [])}
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def cycle_maps_by_identity(snapshot):
|
|
131
|
+
return {cycle_identity(cycle): cycle for cycle in snapshot.get("cycles", [])}
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def format_dependency_edges(edges):
|
|
135
|
+
return [{"source": source, "target": target} for source, target in sorted(edges)]
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def compare_snapshot_data(before, after):
|
|
139
|
+
before_issues = issue_maps_by_identity(before)
|
|
140
|
+
after_issues = issue_maps_by_identity(after)
|
|
141
|
+
before_cycles = cycle_maps_by_identity(before)
|
|
142
|
+
after_cycles = cycle_maps_by_identity(after)
|
|
143
|
+
before_score = before.get("architecture_score", 0)
|
|
144
|
+
after_score = after.get("architecture_score", 0)
|
|
145
|
+
before_graph = before.get("dependency_graph", {})
|
|
146
|
+
after_graph = after.get("dependency_graph", {})
|
|
147
|
+
before_edges = dependency_graph_edges(before_graph)
|
|
148
|
+
after_edges = dependency_graph_edges(after_graph)
|
|
149
|
+
added_edges = after_edges - before_edges
|
|
150
|
+
removed_edges = before_edges - after_edges
|
|
151
|
+
new_issues = [after_issues[key] for key in sorted(after_issues.keys() - before_issues.keys())]
|
|
152
|
+
fixed_issues = [before_issues[key] for key in sorted(before_issues.keys() - after_issues.keys())]
|
|
153
|
+
new_cycles = [after_cycles[key] for key in sorted(after_cycles.keys() - before_cycles.keys())]
|
|
154
|
+
resolved_cycles = [before_cycles[key] for key in sorted(before_cycles.keys() - after_cycles.keys())]
|
|
155
|
+
dependency_growth_per_file = calculate_dependency_growth_per_file(before_graph, after_graph)
|
|
156
|
+
structural_drift_score = calculate_structural_drift_score(
|
|
157
|
+
added_edges,
|
|
158
|
+
removed_edges,
|
|
159
|
+
new_cycles,
|
|
160
|
+
resolved_cycles,
|
|
161
|
+
new_issues,
|
|
162
|
+
fixed_issues,
|
|
163
|
+
dependency_growth_per_file
|
|
164
|
+
)
|
|
165
|
+
change_impact_scores = calculate_change_impact_scores(
|
|
166
|
+
added_edges,
|
|
167
|
+
new_cycles,
|
|
168
|
+
dependency_growth_per_file,
|
|
169
|
+
new_issues,
|
|
170
|
+
fixed_issues,
|
|
171
|
+
after_graph
|
|
172
|
+
)
|
|
173
|
+
primary_cause_files = identify_primary_cause_files(change_impact_scores)
|
|
174
|
+
refactor_plan = generate_refactor_plan(
|
|
175
|
+
change_impact_scores,
|
|
176
|
+
new_cycles,
|
|
177
|
+
new_issues,
|
|
178
|
+
dependency_growth_per_file,
|
|
179
|
+
added_edges
|
|
180
|
+
)
|
|
181
|
+
score_difference = after_score - before_score
|
|
182
|
+
architecture_narrative = build_architecture_narrative(
|
|
183
|
+
structural_drift_score,
|
|
184
|
+
score_difference,
|
|
185
|
+
new_cycles,
|
|
186
|
+
resolved_cycles,
|
|
187
|
+
new_issues,
|
|
188
|
+
fixed_issues,
|
|
189
|
+
dependency_growth_per_file,
|
|
190
|
+
added_edges,
|
|
191
|
+
primary_cause_files
|
|
192
|
+
)
|
|
193
|
+
return {
|
|
194
|
+
"file1": before.get("file"),
|
|
195
|
+
"file2": after.get("file"),
|
|
196
|
+
"score_difference": score_difference,
|
|
197
|
+
"previous_score": before_score,
|
|
198
|
+
"current_score": after_score,
|
|
199
|
+
"new_issues_introduced": new_issues,
|
|
200
|
+
"issues_fixed": fixed_issues,
|
|
201
|
+
"new_cycles_introduced": new_cycles,
|
|
202
|
+
"added_dependencies": format_dependency_edges(added_edges),
|
|
203
|
+
"removed_dependencies": format_dependency_edges(removed_edges),
|
|
204
|
+
"new_cycles": new_cycles,
|
|
205
|
+
"resolved_cycles": resolved_cycles,
|
|
206
|
+
"dependency_growth_per_file": dependency_growth_per_file,
|
|
207
|
+
"structural_drift_score": structural_drift_score,
|
|
208
|
+
"root_cause_analysis": {
|
|
209
|
+
"primary_cause_files": primary_cause_files,
|
|
210
|
+
"change_impact_score_per_file": change_impact_scores
|
|
211
|
+
},
|
|
212
|
+
"refactor_plan": refactor_plan,
|
|
213
|
+
"drift_intelligence_summary": summarize_drift_intelligence(
|
|
214
|
+
added_edges,
|
|
215
|
+
removed_edges,
|
|
216
|
+
new_cycles,
|
|
217
|
+
resolved_cycles,
|
|
218
|
+
new_issues,
|
|
219
|
+
fixed_issues,
|
|
220
|
+
primary_cause_files,
|
|
221
|
+
refactor_plan,
|
|
222
|
+
score_difference
|
|
223
|
+
),
|
|
224
|
+
"architecture_narrative": architecture_narrative,
|
|
225
|
+
"dependency_graph_changes": compare_dependency_graphs(before_graph, after_graph)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def snapshot_from_analysis(analysis, name=None):
|
|
230
|
+
return {
|
|
231
|
+
"file": name,
|
|
232
|
+
"timestamp": None,
|
|
233
|
+
"architecture_score": analysis["score"],
|
|
234
|
+
"architecture_type": analysis.get("architecture_type"),
|
|
235
|
+
"architecture_confidence": analysis.get("architecture_confidence"),
|
|
236
|
+
"low_confidence": analysis.get("low_confidence", False),
|
|
237
|
+
"issues": analysis["issues"],
|
|
238
|
+
"cycles": analysis["cycles"],
|
|
239
|
+
"indirect_dependencies": analysis["indirect_dependencies"],
|
|
240
|
+
"hotspot_files": analysis["hotspot_files"],
|
|
241
|
+
"dependency_graph": analysis["dependency_graph"]
|
|
242
|
+
}
|