From 037cc5e0c4b57fc8a9f27e7933253f964d912e5c 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: Mon, 29 May 2023 10:01:18 +0300 Subject: Refactor severity calculators using functional approach --- examples/custom_severity_calculator.py | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 examples/custom_severity_calculator.py (limited to 'examples') diff --git a/examples/custom_severity_calculator.py b/examples/custom_severity_calculator.py new file mode 100644 index 0000000..9754dd8 --- /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 + + +@Severity.bind_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() -- cgit v1.2.3