From ef5bf65f07a661ae895542a14f95abbd39c06fca 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, 6 Apr 2023 13:02:58 +0300 Subject: Add the custom api interface for custom objects, add custom formatter example --- examples/custom_formatter.py | 20 ++++++++++++++++++++ examples/custom_strategy.py | 19 +++++++------------ 2 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 examples/custom_formatter.py (limited to 'examples') diff --git a/examples/custom_formatter.py b/examples/custom_formatter.py new file mode 100644 index 0000000..a091e22 --- /dev/null +++ b/examples/custom_formatter.py @@ -0,0 +1,20 @@ +# This is an example on how to create your own custom formatter + +from __future__ import annotations + +import robusta_krr +from robusta_krr.api.formatters import BaseFormatter +from robusta_krr.api.models import Result + + +class CustomFormatter(BaseFormatter): + __display_name__ = "my_formatter" + + def format(self, result: Result) -> str: + return "Custom formatter" + + +# 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() diff --git a/examples/custom_strategy.py b/examples/custom_strategy.py index f88edd3..f5f2e6c 100644 --- a/examples/custom_strategy.py +++ b/examples/custom_strategy.py @@ -4,25 +4,19 @@ from decimal import Decimal import pydantic as pd -from robusta_krr import run -from robusta_krr.core.abstract.strategies import ( - BaseStrategy, - HistoryData, - K8sObjectData, - ResourceRecommendation, - ResourceType, - RunResult, - StrategySettings, -) +import robusta_krr +from robusta_krr.api.models import HistoryData, K8sObjectData, ResourceRecommendation, ResourceType, RunResult +from robusta_krr.api.strategies import BaseStrategy, StrategySettings +# Providing description to the settings will make it available in the CLI help class CustomStrategySettings(StrategySettings): param_1: Decimal = pd.Field(99, gt=0, description="First example parameter") param_2: Decimal = pd.Field(105_000, gt=0, description="Second example parameter") class CustomStrategy(BaseStrategy[CustomStrategySettings]): - __display_name__ = "custom" + __display_name__ = "my_strategy" def run(self, history_data: HistoryData, object_data: K8sObjectData) -> RunResult: return { @@ -32,5 +26,6 @@ class CustomStrategy(BaseStrategy[CustomStrategySettings]): # Running this file will register the strategy and make it available to the CLI +# Run it as `python ./custom_strategy.py my_strategy` if __name__ == "__main__": - run() + robusta_krr.run() -- cgit v1.2.3