From b25e2ec0a32097729baaea52c754b7995b929ee5 Mon Sep 17 00:00:00 2001 From: Saravanan Palanisamy Date: Mon, 28 Oct 2024 13:01:14 +0400 Subject: [PATCH] add html formatter (#353) **Problem Statement** Default option `table` provides pretty output in console using styles (colors, etc..) but when we save it into a file using `--fileoutput` styles will not be passed. If we share the output file to others via s3 or something, styles will be missed and looks plain. It would be nice to keep the styles when rendering from the file. **Solution** Default option `table` using `Rich` library and same library has function `export_html` to generate html from table output. It will keep the same styles used in default table formatter. --- README.md | 13 +++++++------ robusta_krr/core/runner.py | 4 ++-- robusta_krr/formatters/__init__.py | 1 + robusta_krr/formatters/html.py | 12 ++++++++++++ 4 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 robusta_krr/formatters/html.py diff --git a/README.md b/README.md index 6df91ca9..93cd35ec 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Request Feature · Support -
Like KRR? Please ⭐ this repository to show your support! +
Like KRR? Please ⭐ this repository to show your support!

@@ -119,7 +119,7 @@ Read more about [how KRR works](#how-krr-works) -## Installation +## Installation ### Requirements @@ -130,7 +130,7 @@ KRR requires Prometheus 2.26+, [kube-state-metrics](https://github.com/kubernete No setup is required if you use kube-prometheus-stack or Robusta's Embedded Prometheus. If you have a different setup, make sure the following metrics exist: - + - `container_cpu_usage_seconds_total` - `container_memory_working_set_bytes` - `kube_replicaset_owner` @@ -179,7 +179,7 @@ You can install using brew (see above) on [WSL2](https://docs.brew.sh/Homebrew-o
Airgapped Installation (Offline Environments) - + You can download pre-built binaries from Releases or use the prebuilt Docker container. For example, the container for version 1.8.3 is: ``` @@ -258,7 +258,7 @@ We highly recommend using the [free Robusta SaaS platform](https://platform.robu
Basic usage - + ```sh krr simple ``` @@ -266,7 +266,7 @@ krr simple
Tweak the recommendation algorithm (strategy) - + Most helpful flags: - `--cpu-min` Sets the minimum recommended cpu value in millicores @@ -347,6 +347,7 @@ Currently KRR ships with a few formatters to represent the scan data: - `yaml` - `pprint` - data representation from python's pprint library - `csv` - export data to a csv file in the current directory +- `html` To run a strategy with a selected formatter, add a `-f` flag. Usually this should be combined with `--fileoutput ` to write clean output to file without logs: diff --git a/robusta_krr/core/runner.py b/robusta_krr/core/runner.py index 76e1474a..9a8b1a81 100644 --- a/robusta_krr/core/runner.py +++ b/robusta_krr/core/runner.py @@ -120,8 +120,8 @@ def _process_result(self, result: Result) -> None: file_name = settings.slack_output with open(file_name, "w") as target_file: - # don't use rich when writing a csv to avoid line wrapping etc - if settings.format == "csv": + # don't use rich when writing a csv or html to avoid line wrapping etc + if settings.format == "csv" or settings.format == "html": target_file.write(formatted) else: console = Console(file=target_file, width=settings.width) diff --git a/robusta_krr/formatters/__init__.py b/robusta_krr/formatters/__init__.py index e34a25f1..7e1d1641 100644 --- a/robusta_krr/formatters/__init__.py +++ b/robusta_krr/formatters/__init__.py @@ -3,3 +3,4 @@ from .table import table from .yaml import yaml from .csv import csv +from .html import html diff --git a/robusta_krr/formatters/html.py b/robusta_krr/formatters/html.py new file mode 100644 index 00000000..a028d969 --- /dev/null +++ b/robusta_krr/formatters/html.py @@ -0,0 +1,12 @@ +from rich.console import Console + +from robusta_krr.core.abstract import formatters +from robusta_krr.core.models.result import Result +from .table import table + +@formatters.register("html") +def html(result: Result) -> str: + console = Console(record=True) + table_output = table(result) + console.print(table_output) + return console.export_html(inline_styles=True)