summaryrefslogtreecommitdiff
path: root/robusta_krr/formatters
diff options
context:
space:
mode:
authorПавел Жуков <33721692+LeaveMyYard@users.noreply.github.com>2023-03-09 13:34:20 +0200
committerПавел Жуков <33721692+LeaveMyYard@users.noreply.github.com>2023-03-09 13:34:20 +0200
commit187db259c68ffafd5d0f2f8a71b902063ff0040b (patch)
tree07742b782c2b2dd52b70c8fa6006f64d35e12e1f /robusta_krr/formatters
parentb6a919fdf349bebf31a7a5e40bd48016dd3b3861 (diff)
Reworked processing of units
Diffstat (limited to 'robusta_krr/formatters')
-rw-r--r--robusta_krr/formatters/table.py22
1 files changed, 17 insertions, 5 deletions
diff --git a/robusta_krr/formatters/table.py b/robusta_krr/formatters/table.py
index 2fcd16f..92711c4 100644
--- a/robusta_krr/formatters/table.py
+++ b/robusta_krr/formatters/table.py
@@ -1,12 +1,16 @@
from __future__ import annotations
import itertools
+from decimal import Decimal
+
+from rich.table import Table
from robusta_krr.core.abstract.formatters import BaseFormatter
-from robusta_krr.core.models.result import Result, ResourceType
+from robusta_krr.core.models.result import ResourceScan, ResourceType, Result
from robusta_krr.utils import resource_units
-from rich.table import Table
+NONE_LITERAL = "none"
+PRESCISION = 4
class TableFormatter(BaseFormatter):
@@ -14,6 +18,16 @@ class TableFormatter(BaseFormatter):
__display_name__ = "table"
+ def _format_united_decimal(self, value: Decimal | None, prescision: int | None = None) -> str:
+ return resource_units.format(value, prescision=prescision) if value is not None else NONE_LITERAL
+
+ def _format_request_str(self, item: ResourceScan, resource: ResourceType, selector: str) -> str:
+ return (
+ self._format_united_decimal(getattr(item.object.allocations, selector)[resource])
+ + " -> "
+ + self._format_united_decimal(getattr(item.recommended, selector)[resource], prescision=PRESCISION)
+ )
+
def format(self, result: Result) -> Table:
"""Format the result as text.
@@ -52,9 +66,7 @@ class TableFormatter(BaseFormatter):
item.object.kind if full_info_row else "",
item.object.container,
*[
- f"{getattr(item.object.allocations, selector)[resource]}"
- + "->"
- + f"{resource_units.format(getattr(item.recommended, selector)[resource])}"
+ self._format_request_str(item, resource, selector)
for resource in ResourceType
for selector in ["requests", "limits"]
],