From 8da544e4ca733a9dcf613c7341b75fa256dd824f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB=20=D0=96=D1=83=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2?= <33721692+LeaveMyYard@users.noreply.github.com> Date: Thu, 25 May 2023 21:25:47 +0300 Subject: Fix score calculation --- robusta_krr/core/models/result.py | 50 ++++++++++++++++----------------------- robusta_krr/formatters/table.py | 3 +-- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/robusta_krr/core/models/result.py b/robusta_krr/core/models/result.py index 8e02d0c..d4e042f 100644 --- a/robusta_krr/core/models/result.py +++ b/robusta_krr/core/models/result.py @@ -1,7 +1,6 @@ from __future__ import annotations import enum -import itertools from datetime import datetime from typing import Any, Optional, Union @@ -32,7 +31,9 @@ class Severity(str, enum.Enum): }[self] @classmethod - def calculate(cls, current: RecommendationValue, recommended: RecommendationValue, resource_type: ResourceType) -> Severity: + def calculate( + cls, current: RecommendationValue, recommended: RecommendationValue, resource_type: ResourceType + ) -> Severity: if isinstance(recommended, str) or isinstance(current, str): return cls.UNKNOWN @@ -143,18 +144,8 @@ class Result(pd.BaseModel): return _formatter.format(self) @staticmethod - def __percentage_difference(current: RecommendationValue, recommended: RecommendationValue) -> float: - """Get the percentage difference between two numbers. - - Args: - current: The current value. - recommended: The recommended value. - - Returns: - The percentage difference. - """ - - return 1 + def __scan_cost(scan: ResourceScan) -> float: + return 0.7 if scan.severity == Severity.WARNING else 1 if scan.severity == Severity.CRITICAL else 0 def __calculate_score(self) -> int: """Get the score of the result. @@ -163,18 +154,19 @@ class Result(pd.BaseModel): The score of the result. """ - total_diff = 0.0 - for scan, resource_type in itertools.product(self.scans, ResourceType): - total_diff += self.__percentage_difference( - scan.object.allocations.requests[resource_type], scan.recommended.requests[resource_type] - ) - total_diff += self.__percentage_difference( - scan.object.allocations.limits[resource_type], scan.recommended.limits[resource_type] - ) - - if len(self.scans) == 0: - return 0 - - return int( - max(0, round(100 - total_diff / len(self.scans) / len(ResourceType) / 50, 2)) - ) # 50 is just a constant + score = sum(self.__scan_cost(scan) for scan in self.scans) + return int((len(self.scans) - score) / len(self.scans) * 100) + + @property + def score_letter(self) -> str: + return ( + "F" + if self.score < 30 + else "D" + if self.score < 55 + else "C" + if self.score < 70 + else "B" + if self.score < 90 + else "A" + ) diff --git a/robusta_krr/formatters/table.py b/robusta_krr/formatters/table.py index 8ef1696..6faef56 100644 --- a/robusta_krr/formatters/table.py +++ b/robusta_krr/formatters/table.py @@ -55,8 +55,7 @@ class TableFormatter(BaseFormatter): title=f"\n{result.description}\n" if result.description else None, title_justify="left", title_style="", - # TODO: Fix points calculation at [MAIN-270] - # caption=f"Scan result ({result.score} points)", + caption=f"{result.score} points - {result.score_letter}", ) table.add_column("Number", justify="right", no_wrap=True) -- cgit v1.2.3