Skip to content

Commit 916485e

Browse files
committed
feat(graphics): shorten file path in figures
1 parent a328012 commit 916485e

3 files changed

Lines changed: 22 additions & 14 deletions

File tree

codesectools/sasts/all/graphics.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import shutil
44
import tempfile
5-
from pathlib import Path
65

76
import matplotlib
87
import matplotlib.pyplot as plt
@@ -11,6 +10,7 @@
1110
from rich import print
1211

1312
from codesectools.sasts.all.sast import AllSAST
13+
from codesectools.utils import shorten_path
1414

1515
## Matplotlib config
1616
matplotlib.rcParams.update(
@@ -107,7 +107,7 @@ def __init__(self, project_name: str) -> None:
107107
def plot_overview(self) -> Figure:
108108
"""Generate an overview plot with stats by files, SAST tools, and categories."""
109109
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, layout="constrained")
110-
by_files = {Path(k).name: v for k, v in self.result.stats_by_files().items()}
110+
by_files = self.result.stats_by_files()
111111
by_sasts = self.result.stats_by_sasts()
112112
by_categories = self.result.stats_by_categories()
113113

@@ -117,7 +117,7 @@ def plot_overview(self) -> Figure:
117117
list(by_files.items()), key=lambda e: e[1]["count"], reverse=True
118118
)
119119
for k, v in sorted_files[: self.limit]:
120-
X_files.append(k)
120+
X_files.append(shorten_path(k))
121121
Y_files.append(v["count"])
122122

123123
COLORS_COUNT = {v: 0 for k, v in self.color_mapping.items()}
@@ -130,11 +130,11 @@ def plot_overview(self) -> Figure:
130130
current_height = 0
131131
for color, height in COLORS_COUNT.items():
132132
if height > 0:
133-
bars.append((k, current_height + height, color))
133+
bars.append((shorten_path(k), current_height + height, color))
134134
current_height += height
135135

136-
for k, height, color in bars[::-1]:
137-
ax1.bar(k, height, color=color)
136+
for k_short, height, color in bars[::-1]:
137+
ax1.bar(k_short, height, color=color)
138138

139139
ax1.set_xticks(X_files, X_files, rotation=45, ha="right")
140140
ax1.set_title(f"Stats by files (limit to {self.limit})")
@@ -231,7 +231,7 @@ def plot_top_cwes(self) -> Figure:
231231
def plot_top_scores(self) -> Figure:
232232
"""Generate a stacked bar plot for files with the highest scores."""
233233
fig, ax = plt.subplots(1, 1, layout="constrained")
234-
by_scores = {Path(k).name: v for k, v in self.result.stats_by_scores().items()}
234+
by_scores = self.result.stats_by_scores()
235235

236236
for file, data in by_scores.items():
237237
by_scores[file]["total_score"] = sum(data["score"].values())
@@ -244,7 +244,7 @@ def plot_top_scores(self) -> Figure:
244244

245245
X_files, score_data = [], []
246246
for file, data in sorted_files[: self.limit]:
247-
X_files.append(file)
247+
X_files.append(shorten_path(file))
248248
score_data.append(data["score"])
249249

250250
score_keys = score_data[0].keys()

codesectools/sasts/core/graphics.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import shutil
99
import tempfile
10-
from pathlib import Path
1110

1211
import matplotlib
1312
import matplotlib.pyplot as plt
@@ -19,6 +18,7 @@
1918
from codesectools.datasets.core.dataset import FileDataset, GitRepoDataset
2019
from codesectools.sasts.core.sast import SAST
2120
from codesectools.shared.cwe import CWE
21+
from codesectools.utils import shorten_path
2222

2323
## Matplotlib config
2424
matplotlib.rcParams.update(
@@ -153,7 +153,7 @@ def plot_overview(self) -> Figure:
153153
project_name = self.result.name
154154

155155
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, layout="constrained")
156-
by_files = {Path(k).name: v for k, v in self.result.stats_by_files().items()}
156+
by_files = self.result.stats_by_files()
157157
by_checkers = self.result.stats_by_checkers()
158158
by_categories = self.result.stats_by_categories()
159159

@@ -163,7 +163,7 @@ def plot_overview(self) -> Figure:
163163
list(by_files.items()), key=lambda e: e[1]["count"], reverse=True
164164
)
165165
for k, v in sorted_files[: self.limit]:
166-
X_files.append(k)
166+
X_files.append(shorten_path(k))
167167
Y_files.append(v["count"])
168168

169169
COLORS_COUNT = {v: 0 for k, v in self.color_mapping.items()}
@@ -177,11 +177,11 @@ def plot_overview(self) -> Figure:
177177
current_height = 0
178178
for color, height in COLORS_COUNT.items():
179179
if height > 0:
180-
bars.append((k, current_height + height, color))
180+
bars.append((shorten_path(k), current_height + height, color))
181181
current_height += height
182182

183-
for k, height, color in bars[::-1]:
184-
ax1.bar(k, height, color=color)
183+
for k_short, height, color in bars[::-1]:
184+
ax1.bar(k_short, height, color=color)
185185

186186
ax1.set_xticks(X_files, X_files, rotation=45, ha="right")
187187
ax1.set_title(f"Stats by files (limit to {self.limit})")

codesectools/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,11 @@ def group_successive(numbers_list: list[int]) -> list[list[int]]:
151151
groups.append(current_group)
152152

153153
return groups
154+
155+
156+
def shorten_path(p: str) -> str:
157+
"""Shorten a file path for display if it's too long."""
158+
path = Path(p)
159+
if len(path.parts) > 3:
160+
return str(Path("...") / path.parts[-2] / path.parts[-1])
161+
return p

0 commit comments

Comments
 (0)