Skip to content

Commit

Permalink
printed table columns wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Robusta Runner committed Nov 16, 2021
1 parent 5b4e35b commit e088f20
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ RUN /root/.local/bin/poetry install --no-root --extras "all"
ADD src/ /app

RUN pip3 install --use-feature=in-tree-build .
# Install tabulate version that fixes column width wrapping. Cannot be added to pypi as a git dependency, so adding it here
RUN pip3 install git+https://github.com/astanin/python-tabulate.git@b2c26bcb70e497f674b38aa7e29de12c0123708a#egg=tabulate

COPY playbooks/ /etc/robusta/playbooks/defaults
RUN pip3 install -r /etc/robusta/playbooks/defaults/requirements.txt
Expand Down
2 changes: 2 additions & 0 deletions src/robusta/core/model/env_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@
)

GIT_MAX_RETRIES = int(os.environ.get("GIT_MAX_RETRIES", 100))

PRINTED_TABLE_MAX_WIDTH = int(os.environ.get("PRINTED_TABLE_MAX_WIDTH", 70))
31 changes: 26 additions & 5 deletions src/robusta/core/reporting/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from .custom_rendering import render_value
from .base import BaseBlock
from ..model.env_vars import PRINTED_TABLE_MAX_WIDTH

BLOCK_SIZE_LIMIT = 2997 # due to slack block size limit of 3000

Expand Down Expand Up @@ -134,12 +135,32 @@ def __init__(
):
super().__init__(rows=rows, headers=headers, column_renderers=column_renderers)

@classmethod
def __calc_max_width(cls, headers, rendered_rows) -> List[int]:
columns_max_widths = [len(header) for header in headers]
for row in rendered_rows:
for idx, val in enumerate(row):
columns_max_widths[idx] = max(len(str(val)), columns_max_widths[idx])

if (
sum(columns_max_widths) > PRINTED_TABLE_MAX_WIDTH
): # We want to limit the widest column
largest_width = max(columns_max_widths)
widest_column_idx = columns_max_widths.index(largest_width)
diff = sum(columns_max_widths) - PRINTED_TABLE_MAX_WIDTH
columns_max_widths[widest_column_idx] = largest_width - diff

return columns_max_widths

def to_markdown(self) -> MarkdownBlock:
# TODO: when the next version of tabulate is released, use maxcolwidths to wrap lines that are too long
# this is currently implemented on tabulate's git master but isn't yet in the pypi package
# unfortunately, we can't take a dependency on the tabulate git version as that breaks our package with pypi
# see https://github.com/python-poetry/poetry/issues/2828
table = tabulate(self.render_rows(), headers=self.headers, tablefmt="presto")
rendered_rows = self.render_rows()
col_max_width = self.__calc_max_width(self.headers, rendered_rows)
table = tabulate(
rendered_rows,
headers=self.headers,
tablefmt="presto",
maxcolwidths=col_max_width,
)
return MarkdownBlock(f"```\n{table}\n```")

def render_rows(self) -> List[List]:
Expand Down

0 comments on commit e088f20

Please sign in to comment.