summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorПавел Жуков <33721692+LeaveMyYard@users.noreply.github.com>2023-05-25 21:25:47 +0300
committerПавел Жуков <33721692+LeaveMyYard@users.noreply.github.com>2023-05-25 21:25:47 +0300
commit8da544e4ca733a9dcf613c7341b75fa256dd824f (patch)
tree565cd9b5d2db8bd82246b45ae102030670304d6b
parent3af322e16244d446a7d1ce244cc8e877b1b0b654 (diff)
Fix score calculation
-rw-r--r--robusta_krr/core/models/result.py50
-rw-r--r--robusta_krr/formatters/table.py3
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)