From 09c7f858ef438ee8c3d0ffa5044b3c0d41941609 Mon Sep 17 00:00:00 2001 From: Amir Date: Tue, 12 Aug 2025 19:46:47 +0200 Subject: [PATCH] Inherited risk matrix graph sada je predstavljen na tabeli --- backend/core/utils.py | 73 ++++++++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 18 deletions(-) diff --git a/backend/core/utils.py b/backend/core/utils.py index d5ffeed..77c3e52 100644 --- a/backend/core/utils.py +++ b/backend/core/utils.py @@ -361,21 +361,66 @@ def map_weight_to_impact_likelihood(total_weight, total_likelihood, max_weight): return impact, likelihood +def _draw_risk_matrix_background(ax): + + ax.set_xlim(0.5, 5.5) + ax.set_ylim(0.5, 5.5) + ax.set_aspect('equal') + + def score_color(score: int) -> str: + + if score <= 2: + return '#1abc9c' + if score <= 4: + return '#2ecc71' + if score <= 9: + return '#f1c40f' + if score <= 15: + return '#f39c12' + return '#e74c3c' + + for y in range(1, 6): + for x in range(1, 6): + score = x * y + rect = plt.Rectangle( + (x - 0.5, y - 0.5), 1, 1, + facecolor=score_color(score), edgecolor='#dddddd', linewidth=1.0, zorder=0 + ) + ax.add_patch(rect) + + + text_color = '#000000' if 5 <= score <= 9 else '#ffffff' + font_weight = 'bold' if score >= 15 else 'normal' + ax.text( + x, y, str(score), + ha='center', va='center', fontsize=9, + color=text_color, alpha=0.95, zorder=1, fontweight=font_weight + ) + + ax.set_xlabel('Likelihood', labelpad=10) + ax.set_ylabel('Impact', labelpad=10) + + ax.set_xticks([]) + ax.set_yticks([]) + + ax.tick_params(length=0) + ax.grid(False) + ax.spines['top'].set_visible(False) + ax.spines['right'].set_visible(False) + + def generate_risk_graph(risks_with_controls): impacts = [risk['impact'] for risk in risks_with_controls] likelihoods = [risk['likelihood'] for risk in risks_with_controls] risk_ids = [risk['risk']['id'] for risk in risks_with_controls] - bg_img_path = find('img/graph_matrix.png') - bg_img = mpimg.imread(bg_img_path) - fig, ax = plt.subplots(figsize=(10, 8)) - ax.imshow(bg_img, extent=[0.0, 5.4, 0.0, 5.4], aspect='auto') + _draw_risk_matrix_background(ax) scatter = ax.scatter( - likelihoods, impacts, - c="blue", edgecolors="white", s=500, alpha=0.9 + likelihoods, impacts, + c="#1f6feb", edgecolors="white", linewidths=1.5, s=420, alpha=0.95, zorder=3 ) for i, risk_id in enumerate(risk_ids): @@ -387,20 +432,11 @@ def generate_risk_graph(risks_with_controls): ha="center", va="center", weight="bold", + zorder=4, ) - ax.set_xticks([]) - ax.set_yticks([]) - ax.set_xticklabels([]) - ax.set_yticklabels([]) - - ax.spines['top'].set_visible(False) - ax.spines['right'].set_visible(False) - ax.spines['left'].set_visible(False) - ax.spines['bottom'].set_visible(False) - buffer = io.BytesIO() - plt.savefig(buffer, format="png", transparent=True, bbox_inches='tight', pad_inches=0) + plt.savefig(buffer, format="png", transparent=True, bbox_inches='tight', pad_inches=0.1) buffer.seek(0) image_png = buffer.getvalue() buffer.close() @@ -457,4 +493,5 @@ def generate_residual_risk_graph(risks_with_controls): def generate_demo_code(length=6): chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789' - return ''.join(random.choices(chars, k=length)) \ No newline at end of file + return ''.join(random.choices(chars, k=length)) + -- 2.47.3