summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorПавел Жуков <33721692+LeaveMyYard@users.noreply.github.com>2023-05-29 16:13:57 +0300
committerПавел Жуков <33721692+LeaveMyYard@users.noreply.github.com>2023-05-29 16:13:57 +0300
commit8009b86180cff0f0b10bc9beda9bbaaf55f08319 (patch)
tree8830af745e153dd3333bcea6ff2082718c7f9be5 /examples
parent77bda6bd5d1b49ca65610b04226052b147cc83d6 (diff)
parentcbf7036bbe47da88dd29c59febf687d0b80bd15d (diff)
Merge branch 'main' into improve-loading-for-old-pods
Diffstat (limited to 'examples')
-rw-r--r--examples/custom_formatter.py20
-rw-r--r--examples/custom_severity_calculator.py46
2 files changed, 54 insertions, 12 deletions
diff --git a/examples/custom_formatter.py b/examples/custom_formatter.py
index d3cb98d..f246404 100644
--- a/examples/custom_formatter.py
+++ b/examples/custom_formatter.py
@@ -3,21 +3,17 @@
from __future__ import annotations
import robusta_krr
-from robusta_krr.api.formatters import BaseFormatter
+from robusta_krr.api import formatters
from robusta_krr.api.models import Result
-class CustomFormatter(BaseFormatter):
- # This is the name that will be used to reference the formatter in the CLI
- __display_name__ = "my_formatter"
-
- # This will pass the result to Rich Console for formatting.
- # By default, the result is passed to `print` function.
- # See https://rich.readthedocs.io/en/latest/ for more info
- __rich_console__ = True
-
- def format(self, result: Result) -> str:
- return "Custom formatter"
+# This is a custom formatter
+# It will be available to the CLI as `my_formatter`
+# Rich console will be enabled in this case, so the output will be colored and formatted
+@formatters.register(rich_console=True)
+def my_formatter(result: Result) -> str:
+ # Return custom formatter
+ return "Custom formatter"
# Running this file will register the formatter and make it available to the CLI
diff --git a/examples/custom_severity_calculator.py b/examples/custom_severity_calculator.py
new file mode 100644
index 0000000..5978bfc
--- /dev/null
+++ b/examples/custom_severity_calculator.py
@@ -0,0 +1,46 @@
+# This is an example on how to create your own custom formatter
+
+from __future__ import annotations
+
+from typing import Optional
+
+import robusta_krr
+from robusta_krr.api.models import Severity, ResourceType, register_severity_calculator
+
+
+@register_severity_calculator(ResourceType.CPU)
+def percentage_severity_calculator(
+ current: Optional[float], recommended: Optional[float], resource_type: ResourceType
+) -> Severity:
+ """
+ This is an example on how to create your own custom severity calculator
+ You can use this decorator to bind a severity calculator function to a resource type.
+ The function will be called with the current value, the recommended value and the resource type.
+ The function should return a Severity enum value.
+
+ If you have the same calculation for multiple resource types, you can use the `bind_calculator` decorator multiple times.
+ Then, the function will be called for each resource type and you can use the resource type to distinguish between them.
+
+ Keep in mind that you can not choose the strategy for the resource type using CLI - the last one created for the resource type will be used.
+ """
+
+ if current is None and recommended is None:
+ return Severity.GOOD
+ if current is None or recommended is None:
+ return Severity.WARNING
+
+ diff = abs(current - recommended) / current
+ if diff >= 0.5:
+ return Severity.CRITICAL
+ elif diff >= 0.25:
+ return Severity.WARNING
+ elif diff >= 0.1:
+ return Severity.OK
+ else:
+ return Severity.GOOD
+
+
+# Running this file will register the formatter and make it available to the CLI
+# Run it as `python ./custom_formatter.py simple --formater my_formatter`
+if __name__ == "__main__":
+ robusta_krr.run()