diff --git a/Dockerfile b/Dockerfile index 56ef5e7d4..bf51a53d5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/src/robusta/core/model/env_vars.py b/src/robusta/core/model/env_vars.py index 0fe836c31..2c6ab9416 100644 --- a/src/robusta/core/model/env_vars.py +++ b/src/robusta/core/model/env_vars.py @@ -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)) diff --git a/src/robusta/core/reporting/blocks.py b/src/robusta/core/reporting/blocks.py index 16b40113d..36e70d163 100644 --- a/src/robusta/core/reporting/blocks.py +++ b/src/robusta/core/reporting/blocks.py @@ -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 @@ -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]: