Skip to content

Commit b25e2ec

Browse files
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.
1 parent 7cc6861 commit b25e2ec

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<a href="https://github.com/robusta-dev/krr/issues">Request Feature</a>
3131
·
3232
<a href="#support">Support</a>
33-
<br /> Like KRR? Please ⭐ this repository to show your support!
33+
<br /> Like KRR? Please ⭐ this repository to show your support!
3434
</p>
3535
</div>
3636
<!-- TABLE OF CONTENTS -->
@@ -119,7 +119,7 @@ Read more about [how KRR works](#how-krr-works)
119119

120120
<!-- GETTING STARTED -->
121121

122-
## Installation
122+
## Installation
123123

124124
### Requirements
125125

@@ -130,7 +130,7 @@ KRR requires Prometheus 2.26+, [kube-state-metrics](https://github.com/kubernete
130130
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>.
131131

132132
If you have a different setup, make sure the following metrics exist:
133-
133+
134134
- `container_cpu_usage_seconds_total`
135135
- `container_memory_working_set_bytes`
136136
- `kube_replicaset_owner`
@@ -179,7 +179,7 @@ You can install using brew (see above) on [WSL2](https://docs.brew.sh/Homebrew-o
179179

180180
<details>
181181
<summary>Airgapped Installation (Offline Environments)</summary>
182-
182+
183183
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:
184184

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

259259
<details>
260260
<summary>Basic usage</summary>
261-
261+
262262
```sh
263263
krr simple
264264
```
265265
</details>
266266

267267
<details>
268268
<summary>Tweak the recommendation algorithm (strategy)</summary>
269-
269+
270270
Most helpful flags:
271271

272272
- `--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:
347347
- `yaml`
348348
- `pprint` - data representation from python's pprint library
349349
- `csv` - export data to a csv file in the current directory
350+
- `html`
350351

351352
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:
352353

robusta_krr/core/runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ def _process_result(self, result: Result) -> None:
120120
file_name = settings.slack_output
121121

122122
with open(file_name, "w") as target_file:
123-
# don't use rich when writing a csv to avoid line wrapping etc
124-
if settings.format == "csv":
123+
# don't use rich when writing a csv or html to avoid line wrapping etc
124+
if settings.format == "csv" or settings.format == "html":
125125
target_file.write(formatted)
126126
else:
127127
console = Console(file=target_file, width=settings.width)

robusta_krr/formatters/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
from .table import table
44
from .yaml import yaml
55
from .csv import csv
6+
from .html import html

robusta_krr/formatters/html.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from rich.console import Console
2+
3+
from robusta_krr.core.abstract import formatters
4+
from robusta_krr.core.models.result import Result
5+
from .table import table
6+
7+
@formatters.register("html")
8+
def html(result: Result) -> str:
9+
console = Console(record=True)
10+
table_output = table(result)
11+
console.print(table_output)
12+
return console.export_html(inline_styles=True)

0 commit comments

Comments
 (0)