Skip to content

Commit

Permalink
add html formatter (#353)
Browse files Browse the repository at this point in the history
**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.
  • Loading branch information
saravanan30erd authored Oct 28, 2024
1 parent 7cc6861 commit b25e2ec
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<a href="https://github.com/robusta-dev/krr/issues">Request Feature</a>
·
<a href="#support">Support</a>
<br /> Like KRR? Please ⭐ this repository to show your support!
<br /> Like KRR? Please ⭐ this repository to show your support!
</p>
</div>
<!-- TABLE OF CONTENTS -->
Expand Down Expand Up @@ -119,7 +119,7 @@ Read more about [how KRR works](#how-krr-works)

<!-- GETTING STARTED -->

## Installation
## Installation

### Requirements

Expand All @@ -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 <a href="https://docs.robusta.dev/master/configuration/alertmanager-integration/embedded-prometheus.html">Robusta's Embedded Prometheus</a>.

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`
Expand Down Expand Up @@ -179,7 +179,7 @@ You can install using brew (see above) on [WSL2](https://docs.brew.sh/Homebrew-o

<details>
<summary>Airgapped Installation (Offline Environments)</summary>

You can download pre-built binaries from <a href="https://github.com/robusta-dev/krr/releases">Releases</a> or use the prebuilt Docker container. For example, the container for version 1.8.3 is:

```
Expand Down Expand Up @@ -258,15 +258,15 @@ We highly recommend using the [free Robusta SaaS platform](https://platform.robu

<details>
<summary>Basic usage</summary>

```sh
krr simple
```
</details>

<details>
<summary>Tweak the recommendation algorithm (strategy)</summary>

Most helpful flags:

- `--cpu-min` Sets the minimum recommended cpu value in millicores
Expand Down Expand Up @@ -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 <filename>` to write clean output to file without logs:

Expand Down
4 changes: 2 additions & 2 deletions robusta_krr/core/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions robusta_krr/formatters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from .table import table
from .yaml import yaml
from .csv import csv
from .html import html
12 changes: 12 additions & 0 deletions robusta_krr/formatters/html.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit b25e2ec

Please sign in to comment.